| 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-2001,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" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" | 
|  | 25 | #include "xfs_sb.h" | 
|  | 26 | #include "xfs_ag.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_dir2.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_mount.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_dir2_sf.h" | 
|  | 33 | #include "xfs_dinode.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_inode.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_inode_item.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_bmap.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_dir2_data.h" | 
|  | 38 | #include "xfs_dir2_leaf.h" | 
|  | 39 | #include "xfs_dir2_block.h" | 
|  | 40 | #include "xfs_dir2_node.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_error.h" | 
| David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 42 | #include "xfs_vnodeops.h" | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 43 | #include "xfs_trace.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 |  | 
| Dave Chinner | 4a24cb7 | 2010-01-20 10:48:05 +1100 | [diff] [blame] | 45 | struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 |  | 
| Barry Naujok | 189f4bf | 2008-05-21 16:58:55 +1000 | [diff] [blame] | 47 | /* | 
|  | 48 | * ASCII case-insensitive (ie. A-Z) support for directories that was | 
|  | 49 | * used in IRIX. | 
|  | 50 | */ | 
|  | 51 | STATIC xfs_dahash_t | 
|  | 52 | xfs_ascii_ci_hashname( | 
|  | 53 | struct xfs_name	*name) | 
|  | 54 | { | 
|  | 55 | xfs_dahash_t	hash; | 
|  | 56 | int		i; | 
|  | 57 |  | 
|  | 58 | for (i = 0, hash = 0; i < name->len; i++) | 
|  | 59 | hash = tolower(name->name[i]) ^ rol32(hash, 7); | 
|  | 60 |  | 
|  | 61 | return hash; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | STATIC enum xfs_dacmp | 
|  | 65 | xfs_ascii_ci_compname( | 
|  | 66 | struct xfs_da_args *args, | 
| Dave Chinner | 2bc7542 | 2010-01-20 10:47:17 +1100 | [diff] [blame] | 67 | const unsigned char *name, | 
|  | 68 | int		len) | 
| Barry Naujok | 189f4bf | 2008-05-21 16:58:55 +1000 | [diff] [blame] | 69 | { | 
|  | 70 | enum xfs_dacmp	result; | 
|  | 71 | int		i; | 
|  | 72 |  | 
|  | 73 | if (args->namelen != len) | 
|  | 74 | return XFS_CMP_DIFFERENT; | 
|  | 75 |  | 
|  | 76 | result = XFS_CMP_EXACT; | 
|  | 77 | for (i = 0; i < len; i++) { | 
|  | 78 | if (args->name[i] == name[i]) | 
|  | 79 | continue; | 
|  | 80 | if (tolower(args->name[i]) != tolower(name[i])) | 
|  | 81 | return XFS_CMP_DIFFERENT; | 
|  | 82 | result = XFS_CMP_CASE; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | return result; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | static struct xfs_nameops xfs_ascii_ci_nameops = { | 
|  | 89 | .hashname	= xfs_ascii_ci_hashname, | 
|  | 90 | .compname	= xfs_ascii_ci_compname, | 
|  | 91 | }; | 
|  | 92 |  | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 93 | void | 
|  | 94 | xfs_dir_mount( | 
|  | 95 | xfs_mount_t	*mp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 97 | ASSERT(xfs_sb_version_hasdirv2(&mp->m_sb)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | ASSERT((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) <= | 
|  | 99 | XFS_MAX_BLOCKSIZE); | 
|  | 100 | mp->m_dirblksize = 1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog); | 
|  | 101 | mp->m_dirblkfsbs = 1 << mp->m_sb.sb_dirblklog; | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 102 | mp->m_dirdatablk = xfs_dir2_db_to_da(mp, XFS_DIR2_DATA_FIRSTDB(mp)); | 
|  | 103 | mp->m_dirleafblk = xfs_dir2_db_to_da(mp, XFS_DIR2_LEAF_FIRSTDB(mp)); | 
|  | 104 | mp->m_dirfreeblk = xfs_dir2_db_to_da(mp, XFS_DIR2_FREE_FIRSTDB(mp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | mp->m_attr_node_ents = | 
|  | 106 | (mp->m_sb.sb_blocksize - (uint)sizeof(xfs_da_node_hdr_t)) / | 
|  | 107 | (uint)sizeof(xfs_da_node_entry_t); | 
|  | 108 | mp->m_dir_node_ents = | 
|  | 109 | (mp->m_dirblksize - (uint)sizeof(xfs_da_node_hdr_t)) / | 
|  | 110 | (uint)sizeof(xfs_da_node_entry_t); | 
|  | 111 | mp->m_dir_magicpct = (mp->m_dirblksize * 37) / 100; | 
| Barry Naujok | 189f4bf | 2008-05-21 16:58:55 +1000 | [diff] [blame] | 112 | if (xfs_sb_version_hasasciici(&mp->m_sb)) | 
|  | 113 | mp->m_dirnameops = &xfs_ascii_ci_nameops; | 
|  | 114 | else | 
|  | 115 | mp->m_dirnameops = &xfs_default_nameops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
|  | 118 | /* | 
|  | 119 | * Return 1 if directory contains only "." and "..". | 
|  | 120 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 121 | int | 
|  | 122 | xfs_dir_isempty( | 
|  | 123 | xfs_inode_t	*dp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 125 | xfs_dir2_sf_t	*sfp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 |  | 
|  | 127 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 128 | if (dp->i_d.di_size == 0)	/* might happen during shutdown. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp)) | 
|  | 131 | return 0; | 
|  | 132 | sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data; | 
|  | 133 | return !sfp->hdr.count; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | /* | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 137 | * Validate a given inode number. | 
|  | 138 | */ | 
|  | 139 | int | 
|  | 140 | xfs_dir_ino_validate( | 
|  | 141 | xfs_mount_t	*mp, | 
|  | 142 | xfs_ino_t	ino) | 
|  | 143 | { | 
|  | 144 | xfs_agblock_t	agblkno; | 
|  | 145 | xfs_agino_t	agino; | 
|  | 146 | xfs_agnumber_t	agno; | 
|  | 147 | int		ino_ok; | 
|  | 148 | int		ioff; | 
|  | 149 |  | 
|  | 150 | agno = XFS_INO_TO_AGNO(mp, ino); | 
|  | 151 | agblkno = XFS_INO_TO_AGBNO(mp, ino); | 
|  | 152 | ioff = XFS_INO_TO_OFFSET(mp, ino); | 
|  | 153 | agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff); | 
|  | 154 | ino_ok = | 
|  | 155 | agno < mp->m_sb.sb_agcount && | 
|  | 156 | agblkno < mp->m_sb.sb_agblocks && | 
|  | 157 | agblkno != 0 && | 
|  | 158 | ioff < (1 << mp->m_sb.sb_inopblog) && | 
|  | 159 | XFS_AGINO_TO_INO(mp, agno, agino) == ino; | 
|  | 160 | if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE, | 
|  | 161 | XFS_RANDOM_DIR_INO_VALIDATE))) { | 
|  | 162 | xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx", | 
|  | 163 | (unsigned long long) ino); | 
|  | 164 | XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp); | 
|  | 165 | return XFS_ERROR(EFSCORRUPTED); | 
|  | 166 | } | 
|  | 167 | return 0; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | * Initialize a directory with its "." and ".." entries. | 
|  | 172 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 173 | int | 
|  | 174 | xfs_dir_init( | 
|  | 175 | xfs_trans_t	*tp, | 
|  | 176 | xfs_inode_t	*dp, | 
|  | 177 | xfs_inode_t	*pdp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 179 | xfs_da_args_t	args; | 
|  | 180 | int		error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 |  | 
|  | 182 | memset((char *)&args, 0, sizeof(args)); | 
|  | 183 | args.dp = dp; | 
|  | 184 | args.trans = tp; | 
|  | 185 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 186 | if ((error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | return xfs_dir2_sf_create(&args, pdp->i_ino); | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | /* | 
|  | 192 | Enter a name in a directory. | 
|  | 193 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 194 | int | 
|  | 195 | xfs_dir_createname( | 
|  | 196 | xfs_trans_t		*tp, | 
|  | 197 | xfs_inode_t		*dp, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 198 | struct xfs_name		*name, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | xfs_ino_t		inum,		/* new entry inode number */ | 
|  | 200 | xfs_fsblock_t		*first,		/* bmap's firstblock */ | 
|  | 201 | xfs_bmap_free_t		*flist,		/* bmap's freeblock list */ | 
|  | 202 | xfs_extlen_t		total)		/* bmap's total block count */ | 
|  | 203 | { | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 204 | xfs_da_args_t		args; | 
|  | 205 | int			rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | int			v;		/* type-checking value */ | 
|  | 207 |  | 
|  | 208 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 209 | if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | return rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | XFS_STATS_INC(xs_dir_create); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 212 |  | 
| Barry Naujok | 87affd08 | 2008-06-03 11:59:18 +1000 | [diff] [blame] | 213 | memset(&args, 0, sizeof(xfs_da_args_t)); | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 214 | args.name = name->name; | 
|  | 215 | args.namelen = name->len; | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 216 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | args.inumber = inum; | 
|  | 218 | args.dp = dp; | 
|  | 219 | args.firstblock = first; | 
|  | 220 | args.flist = flist; | 
|  | 221 | args.total = total; | 
|  | 222 | args.whichfork = XFS_DATA_FORK; | 
|  | 223 | args.trans = tp; | 
| Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 224 | args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 225 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) | 
|  | 227 | rval = xfs_dir2_sf_addname(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 228 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 230 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | rval = xfs_dir2_block_addname(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 232 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 234 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | rval = xfs_dir2_leaf_addname(&args); | 
|  | 236 | else | 
|  | 237 | rval = xfs_dir2_node_addname(&args); | 
|  | 238 | return rval; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | /* | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 242 | * If doing a CI lookup and case-insensitive match, dup actual name into | 
|  | 243 | * args.value. Return EEXIST for success (ie. name found) or an error. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 245 | int | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 246 | xfs_dir_cilookup_result( | 
|  | 247 | struct xfs_da_args *args, | 
| Dave Chinner | a3380ae | 2010-01-20 10:47:25 +1100 | [diff] [blame] | 248 | const unsigned char *name, | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 249 | int		len) | 
|  | 250 | { | 
|  | 251 | if (args->cmpresult == XFS_CMP_DIFFERENT) | 
|  | 252 | return ENOENT; | 
|  | 253 | if (args->cmpresult != XFS_CMP_CASE || | 
|  | 254 | !(args->op_flags & XFS_DA_OP_CILOOKUP)) | 
|  | 255 | return EEXIST; | 
|  | 256 |  | 
| Christoph Hellwig | 3f52c2f | 2009-07-18 18:14:57 -0400 | [diff] [blame] | 257 | args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL); | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 258 | if (!args->value) | 
|  | 259 | return ENOMEM; | 
|  | 260 |  | 
|  | 261 | memcpy(args->value, name, len); | 
|  | 262 | args->valuelen = len; | 
|  | 263 | return EEXIST; | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | /* | 
|  | 267 | * Lookup a name in a directory, give back the inode number. | 
|  | 268 | * If ci_name is not NULL, returns the actual name in ci_name if it differs | 
|  | 269 | * to name, or ci_name->name is set to NULL for an exact match. | 
|  | 270 | */ | 
|  | 271 |  | 
|  | 272 | int | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 273 | xfs_dir_lookup( | 
|  | 274 | xfs_trans_t	*tp, | 
|  | 275 | xfs_inode_t	*dp, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 276 | struct xfs_name	*name, | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 277 | xfs_ino_t	*inum,		/* out: inode number */ | 
|  | 278 | struct xfs_name *ci_name)	/* out: actual name if CI match */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 280 | xfs_da_args_t	args; | 
|  | 281 | int		rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | int		v;		/* type-checking value */ | 
|  | 283 |  | 
|  | 284 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | 
|  | 285 | XFS_STATS_INC(xs_dir_lookup); | 
|  | 286 |  | 
| Barry Naujok | 87affd08 | 2008-06-03 11:59:18 +1000 | [diff] [blame] | 287 | memset(&args, 0, sizeof(xfs_da_args_t)); | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 288 | args.name = name->name; | 
|  | 289 | args.namelen = name->len; | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 290 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | args.dp = dp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | args.whichfork = XFS_DATA_FORK; | 
|  | 293 | args.trans = tp; | 
| Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 294 | args.op_flags = XFS_DA_OP_OKNOENT; | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 295 | if (ci_name) | 
|  | 296 | args.op_flags |= XFS_DA_OP_CILOOKUP; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 297 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) | 
|  | 299 | rval = xfs_dir2_sf_lookup(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 300 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 302 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | rval = xfs_dir2_block_lookup(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 304 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 306 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | rval = xfs_dir2_leaf_lookup(&args); | 
|  | 308 | else | 
|  | 309 | rval = xfs_dir2_node_lookup(&args); | 
|  | 310 | if (rval == EEXIST) | 
|  | 311 | rval = 0; | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 312 | if (!rval) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | *inum = args.inumber; | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 314 | if (ci_name) { | 
|  | 315 | ci_name->name = args.value; | 
|  | 316 | ci_name->len = args.valuelen; | 
|  | 317 | } | 
|  | 318 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | return rval; | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | /* | 
|  | 323 | * Remove an entry from a directory. | 
|  | 324 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 325 | int | 
|  | 326 | xfs_dir_removename( | 
|  | 327 | xfs_trans_t	*tp, | 
|  | 328 | xfs_inode_t	*dp, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 329 | struct xfs_name	*name, | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 330 | xfs_ino_t	ino, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | xfs_fsblock_t	*first,		/* bmap's firstblock */ | 
|  | 332 | xfs_bmap_free_t	*flist,		/* bmap's freeblock list */ | 
|  | 333 | xfs_extlen_t	total)		/* bmap's total block count */ | 
|  | 334 | { | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 335 | xfs_da_args_t	args; | 
|  | 336 | int		rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | int		v;		/* type-checking value */ | 
|  | 338 |  | 
|  | 339 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | 
|  | 340 | XFS_STATS_INC(xs_dir_remove); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 341 |  | 
| Barry Naujok | 87affd08 | 2008-06-03 11:59:18 +1000 | [diff] [blame] | 342 | memset(&args, 0, sizeof(xfs_da_args_t)); | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 343 | args.name = name->name; | 
|  | 344 | args.namelen = name->len; | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 345 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | args.inumber = ino; | 
|  | 347 | args.dp = dp; | 
|  | 348 | args.firstblock = first; | 
|  | 349 | args.flist = flist; | 
|  | 350 | args.total = total; | 
|  | 351 | args.whichfork = XFS_DATA_FORK; | 
|  | 352 | args.trans = tp; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 353 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) | 
|  | 355 | rval = xfs_dir2_sf_removename(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 356 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 358 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | rval = xfs_dir2_block_removename(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 360 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 362 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | rval = xfs_dir2_leaf_removename(&args); | 
|  | 364 | else | 
|  | 365 | rval = xfs_dir2_node_removename(&args); | 
|  | 366 | return rval; | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | /* | 
|  | 370 | * Read a directory. | 
|  | 371 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 372 | int | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 373 | xfs_readdir( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 374 | xfs_inode_t	*dp, | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 375 | void		*dirent, | 
|  | 376 | size_t		bufsize, | 
|  | 377 | xfs_off_t	*offset, | 
|  | 378 | filldir_t	filldir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | int		rval;		/* return value */ | 
|  | 381 | int		v;		/* type-checking value */ | 
|  | 382 |  | 
| Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 383 | trace_xfs_readdir(dp); | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 384 |  | 
|  | 385 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) | 
|  | 386 | return XFS_ERROR(EIO); | 
|  | 387 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | 
|  | 389 | XFS_STATS_INC(xs_dir_getdents); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 392 | rval = xfs_dir2_sf_getdents(dp, dirent, offset, filldir); | 
|  | 393 | else if ((rval = xfs_dir2_isblock(NULL, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | ; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 395 | else if (v) | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 396 | rval = xfs_dir2_block_getdents(dp, dirent, offset, filldir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | else | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 398 | rval = xfs_dir2_leaf_getdents(dp, dirent, bufsize, offset, | 
|  | 399 | filldir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | return rval; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | /* | 
|  | 404 | * Replace the inode number of a directory entry. | 
|  | 405 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 406 | int | 
|  | 407 | xfs_dir_replace( | 
|  | 408 | xfs_trans_t	*tp, | 
|  | 409 | xfs_inode_t	*dp, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 410 | struct xfs_name	*name,		/* name of entry to replace */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | xfs_ino_t	inum,		/* new inode number */ | 
|  | 412 | xfs_fsblock_t	*first,		/* bmap's firstblock */ | 
|  | 413 | xfs_bmap_free_t	*flist,		/* bmap's freeblock list */ | 
|  | 414 | xfs_extlen_t	total)		/* bmap's total block count */ | 
|  | 415 | { | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 416 | xfs_da_args_t	args; | 
|  | 417 | int		rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | int		v;		/* type-checking value */ | 
|  | 419 |  | 
|  | 420 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | 
|  | 421 |  | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 422 | if ((rval = xfs_dir_ino_validate(tp->t_mountp, inum))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 424 |  | 
| Barry Naujok | 87affd08 | 2008-06-03 11:59:18 +1000 | [diff] [blame] | 425 | memset(&args, 0, sizeof(xfs_da_args_t)); | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 426 | args.name = name->name; | 
|  | 427 | args.namelen = name->len; | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 428 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | args.inumber = inum; | 
|  | 430 | args.dp = dp; | 
|  | 431 | args.firstblock = first; | 
|  | 432 | args.flist = flist; | 
|  | 433 | args.total = total; | 
|  | 434 | args.whichfork = XFS_DATA_FORK; | 
|  | 435 | args.trans = tp; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 436 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) | 
|  | 438 | rval = xfs_dir2_sf_replace(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 439 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 441 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | rval = xfs_dir2_block_replace(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 443 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 445 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | rval = xfs_dir2_leaf_replace(&args); | 
|  | 447 | else | 
|  | 448 | rval = xfs_dir2_node_replace(&args); | 
|  | 449 | return rval; | 
|  | 450 | } | 
|  | 451 |  | 
|  | 452 | /* | 
|  | 453 | * See if this entry can be added to the directory without allocating space. | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 454 | * First checks that the caller couldn't reserve enough space (resblks = 0). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 456 | int | 
|  | 457 | xfs_dir_canenter( | 
|  | 458 | xfs_trans_t	*tp, | 
|  | 459 | xfs_inode_t	*dp, | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 460 | struct xfs_name	*name,		/* name of entry to add */ | 
|  | 461 | uint		resblks) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 463 | xfs_da_args_t	args; | 
|  | 464 | int		rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | int		v;		/* type-checking value */ | 
|  | 466 |  | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 467 | if (resblks) | 
|  | 468 | return 0; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 469 |  | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 470 | ASSERT((dp->i_d.di_mode & S_IFMT) == S_IFDIR); | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 471 |  | 
| Barry Naujok | 87affd08 | 2008-06-03 11:59:18 +1000 | [diff] [blame] | 472 | memset(&args, 0, sizeof(xfs_da_args_t)); | 
| Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 473 | args.name = name->name; | 
|  | 474 | args.namelen = name->len; | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 475 | args.hashval = dp->i_mount->m_dirnameops->hashname(name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | args.dp = dp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | args.whichfork = XFS_DATA_FORK; | 
|  | 478 | args.trans = tp; | 
| Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 479 | args.op_flags = XFS_DA_OP_JUSTCHECK | XFS_DA_OP_ADDNAME | | 
|  | 480 | XFS_DA_OP_OKNOENT; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 481 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) | 
|  | 483 | rval = xfs_dir2_sf_addname(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 484 | else if ((rval = xfs_dir2_isblock(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 486 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | rval = xfs_dir2_block_addname(&args); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 488 | else if ((rval = xfs_dir2_isleaf(tp, dp, &v))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | return rval; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 490 | else if (v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | rval = xfs_dir2_leaf_addname(&args); | 
|  | 492 | else | 
|  | 493 | rval = xfs_dir2_node_addname(&args); | 
|  | 494 | return rval; | 
|  | 495 | } | 
|  | 496 |  | 
|  | 497 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | * Utility routines. | 
|  | 499 | */ | 
|  | 500 |  | 
|  | 501 | /* | 
|  | 502 | * Add a block to the directory. | 
|  | 503 | * This routine is for data and free blocks, not leaf/node blocks | 
|  | 504 | * which are handled by xfs_da_grow_inode. | 
|  | 505 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 506 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | xfs_dir2_grow_inode( | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 508 | xfs_da_args_t	*args, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | int		space,		/* v2 dir's space XFS_DIR2_xxx_SPACE */ | 
|  | 510 | xfs_dir2_db_t	*dbp)		/* out: block number added */ | 
|  | 511 | { | 
|  | 512 | xfs_fileoff_t	bno;		/* directory offset of new block */ | 
|  | 513 | int		count;		/* count of filesystem blocks */ | 
|  | 514 | xfs_inode_t	*dp;		/* incore directory inode */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 515 | int		error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | int		got;		/* blocks actually mapped */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 517 | int		i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | xfs_bmbt_irec_t	map;		/* single structure for bmap */ | 
|  | 519 | int		mapi;		/* mapping index */ | 
|  | 520 | xfs_bmbt_irec_t	*mapp;		/* bmap mapping structure(s) */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 521 | xfs_mount_t	*mp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | int		nmap;		/* number of bmap entries */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 523 | xfs_trans_t	*tp; | 
| David Chinner | a744405 | 2008-10-30 17:38:12 +1100 | [diff] [blame] | 524 | xfs_drfsbno_t	nblks; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 526 | trace_xfs_dir2_grow_inode(args, space); | 
|  | 527 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | dp = args->dp; | 
|  | 529 | tp = args->trans; | 
|  | 530 | mp = dp->i_mount; | 
| David Chinner | a744405 | 2008-10-30 17:38:12 +1100 | [diff] [blame] | 531 | nblks = dp->i_d.di_nblocks; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | /* | 
|  | 533 | * Set lowest possible block in the space requested. | 
|  | 534 | */ | 
|  | 535 | bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE); | 
|  | 536 | count = mp->m_dirblkfsbs; | 
|  | 537 | /* | 
|  | 538 | * Find the first hole for our block. | 
|  | 539 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 540 | if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, XFS_DATA_FORK))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | nmap = 1; | 
|  | 543 | ASSERT(args->firstblock != NULL); | 
|  | 544 | /* | 
|  | 545 | * Try mapping the new block contiguously (one extent). | 
|  | 546 | */ | 
|  | 547 | if ((error = xfs_bmapi(tp, dp, bno, count, | 
|  | 548 | XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG, | 
|  | 549 | args->firstblock, args->total, &map, &nmap, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 550 | args->flist))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | ASSERT(nmap <= 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | if (nmap == 1) { | 
|  | 554 | mapp = ↦ | 
|  | 555 | mapi = 1; | 
|  | 556 | } | 
|  | 557 | /* | 
|  | 558 | * Didn't work and this is a multiple-fsb directory block. | 
|  | 559 | * Try again with contiguous flag turned on. | 
|  | 560 | */ | 
|  | 561 | else if (nmap == 0 && count > 1) { | 
|  | 562 | xfs_fileoff_t	b;	/* current file offset */ | 
|  | 563 |  | 
|  | 564 | /* | 
|  | 565 | * Space for maximum number of mappings. | 
|  | 566 | */ | 
|  | 567 | mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP); | 
|  | 568 | /* | 
|  | 569 | * Iterate until we get to the end of our block. | 
|  | 570 | */ | 
|  | 571 | for (b = bno, mapi = 0; b < bno + count; ) { | 
|  | 572 | int	c;	/* current fsb count */ | 
|  | 573 |  | 
|  | 574 | /* | 
|  | 575 | * Can't map more than MAX_NMAP at once. | 
|  | 576 | */ | 
|  | 577 | nmap = MIN(XFS_BMAP_MAX_NMAP, count); | 
|  | 578 | c = (int)(bno + count - b); | 
|  | 579 | if ((error = xfs_bmapi(tp, dp, b, c, | 
|  | 580 | XFS_BMAPI_WRITE|XFS_BMAPI_METADATA, | 
|  | 581 | args->firstblock, args->total, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 582 | &mapp[mapi], &nmap, args->flist))) { | 
| Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 583 | kmem_free(mapp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | return error; | 
|  | 585 | } | 
|  | 586 | if (nmap < 1) | 
|  | 587 | break; | 
|  | 588 | /* | 
|  | 589 | * Add this bunch into our table, go to the next offset. | 
|  | 590 | */ | 
|  | 591 | mapi += nmap; | 
|  | 592 | b = mapp[mapi - 1].br_startoff + | 
|  | 593 | mapp[mapi - 1].br_blockcount; | 
|  | 594 | } | 
|  | 595 | } | 
|  | 596 | /* | 
|  | 597 | * Didn't work. | 
|  | 598 | */ | 
|  | 599 | else { | 
|  | 600 | mapi = 0; | 
|  | 601 | mapp = NULL; | 
|  | 602 | } | 
|  | 603 | /* | 
|  | 604 | * See how many fsb's we got. | 
|  | 605 | */ | 
|  | 606 | for (i = 0, got = 0; i < mapi; i++) | 
|  | 607 | got += mapp[i].br_blockcount; | 
|  | 608 | /* | 
|  | 609 | * Didn't get enough fsb's, or the first/last block's are wrong. | 
|  | 610 | */ | 
|  | 611 | if (got != count || mapp[0].br_startoff != bno || | 
|  | 612 | mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount != | 
|  | 613 | bno + count) { | 
|  | 614 | if (mapp != &map) | 
| Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 615 | kmem_free(mapp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | return XFS_ERROR(ENOSPC); | 
|  | 617 | } | 
|  | 618 | /* | 
|  | 619 | * Done with the temporary mapping table. | 
|  | 620 | */ | 
|  | 621 | if (mapp != &map) | 
| Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 622 | kmem_free(mapp); | 
| David Chinner | a744405 | 2008-10-30 17:38:12 +1100 | [diff] [blame] | 623 |  | 
|  | 624 | /* account for newly allocated blocks in reserved blocks total */ | 
|  | 625 | args->total -= dp->i_d.di_nblocks - nblks; | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 626 | *dbp = xfs_dir2_da_to_db(mp, (xfs_dablk_t)bno); | 
| David Chinner | a744405 | 2008-10-30 17:38:12 +1100 | [diff] [blame] | 627 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | /* | 
|  | 629 | * Update file's size if this is the data space and it grew. | 
|  | 630 | */ | 
|  | 631 | if (space == XFS_DIR2_DATA_SPACE) { | 
|  | 632 | xfs_fsize_t	size;		/* directory file (data) size */ | 
|  | 633 |  | 
|  | 634 | size = XFS_FSB_TO_B(mp, bno + count); | 
|  | 635 | if (size > dp->i_d.di_size) { | 
|  | 636 | dp->i_d.di_size = size; | 
|  | 637 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | 
|  | 638 | } | 
|  | 639 | } | 
|  | 640 | return 0; | 
|  | 641 | } | 
|  | 642 |  | 
|  | 643 | /* | 
|  | 644 | * See if the directory is a single-block form directory. | 
|  | 645 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 646 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | xfs_dir2_isblock( | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 648 | xfs_trans_t	*tp, | 
|  | 649 | xfs_inode_t	*dp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | int		*vp)		/* out: 1 is block, 0 is not block */ | 
|  | 651 | { | 
|  | 652 | xfs_fileoff_t	last;		/* last file offset */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 653 | xfs_mount_t	*mp; | 
|  | 654 | int		rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 |  | 
|  | 656 | mp = dp->i_mount; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 657 | if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | rval = XFS_FSB_TO_B(mp, last) == mp->m_dirblksize; | 
|  | 660 | ASSERT(rval == 0 || dp->i_d.di_size == mp->m_dirblksize); | 
|  | 661 | *vp = rval; | 
|  | 662 | return 0; | 
|  | 663 | } | 
|  | 664 |  | 
|  | 665 | /* | 
|  | 666 | * See if the directory is a single-leaf form directory. | 
|  | 667 | */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 668 | int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | xfs_dir2_isleaf( | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 670 | xfs_trans_t	*tp, | 
|  | 671 | xfs_inode_t	*dp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | int		*vp)		/* out: 1 is leaf, 0 is not leaf */ | 
|  | 673 | { | 
|  | 674 | xfs_fileoff_t	last;		/* last file offset */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 675 | xfs_mount_t	*mp; | 
|  | 676 | int		rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 |  | 
|  | 678 | mp = dp->i_mount; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 679 | if ((rval = xfs_bmap_last_offset(tp, dp, &last, XFS_DATA_FORK))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | return rval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | *vp = last == mp->m_dirleafblk + (1 << mp->m_sb.sb_dirblklog); | 
|  | 682 | return 0; | 
|  | 683 | } | 
|  | 684 |  | 
|  | 685 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | * Remove the given block from the directory. | 
|  | 687 | * This routine is used for data and free blocks, leaf/node are done | 
|  | 688 | * by xfs_da_shrink_inode. | 
|  | 689 | */ | 
|  | 690 | int | 
|  | 691 | xfs_dir2_shrink_inode( | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 692 | xfs_da_args_t	*args, | 
|  | 693 | xfs_dir2_db_t	db, | 
|  | 694 | xfs_dabuf_t	*bp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { | 
|  | 696 | xfs_fileoff_t	bno;		/* directory file offset */ | 
|  | 697 | xfs_dablk_t	da;		/* directory file offset */ | 
|  | 698 | int		done;		/* bunmap is finished */ | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 699 | xfs_inode_t	*dp; | 
|  | 700 | int		error; | 
|  | 701 | xfs_mount_t	*mp; | 
|  | 702 | xfs_trans_t	*tp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 704 | trace_xfs_dir2_shrink_inode(args, db); | 
|  | 705 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | dp = args->dp; | 
|  | 707 | mp = dp->i_mount; | 
|  | 708 | tp = args->trans; | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 709 | da = xfs_dir2_db_to_da(mp, db); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | /* | 
|  | 711 | * Unmap the fsblock(s). | 
|  | 712 | */ | 
|  | 713 | if ((error = xfs_bunmapi(tp, dp, da, mp->m_dirblkfsbs, | 
|  | 714 | XFS_BMAPI_METADATA, 0, args->firstblock, args->flist, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 715 | &done))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | /* | 
|  | 717 | * ENOSPC actually can happen if we're in a removename with | 
|  | 718 | * no space reservation, and the resulting block removal | 
|  | 719 | * would cause a bmap btree split or conversion from extents | 
|  | 720 | * to btree.  This can only happen for un-fragmented | 
|  | 721 | * directory blocks, since you need to be punching out | 
|  | 722 | * the middle of an extent. | 
|  | 723 | * In this case we need to leave the block in the file, | 
|  | 724 | * and not binval it. | 
|  | 725 | * So the block has to be in a consistent empty state | 
|  | 726 | * and appropriately logged. | 
|  | 727 | * We don't free up the buffer, the caller can tell it | 
|  | 728 | * hasn't happened since it got an error back. | 
|  | 729 | */ | 
|  | 730 | return error; | 
|  | 731 | } | 
|  | 732 | ASSERT(done); | 
|  | 733 | /* | 
|  | 734 | * Invalidate the buffer from the transaction. | 
|  | 735 | */ | 
|  | 736 | xfs_da_binval(tp, bp); | 
|  | 737 | /* | 
|  | 738 | * If it's not a data block, we're done. | 
|  | 739 | */ | 
|  | 740 | if (db >= XFS_DIR2_LEAF_FIRSTDB(mp)) | 
|  | 741 | return 0; | 
|  | 742 | /* | 
|  | 743 | * If the block isn't the last one in the directory, we're done. | 
|  | 744 | */ | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 745 | if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(mp, db + 1, 0)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | return 0; | 
|  | 747 | bno = da; | 
|  | 748 | if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) { | 
|  | 749 | /* | 
|  | 750 | * This can't really happen unless there's kernel corruption. | 
|  | 751 | */ | 
|  | 752 | return error; | 
|  | 753 | } | 
|  | 754 | if (db == mp->m_dirdatablk) | 
|  | 755 | ASSERT(bno == 0); | 
|  | 756 | else | 
|  | 757 | ASSERT(bno > 0); | 
|  | 758 | /* | 
|  | 759 | * Set the size to the new last block. | 
|  | 760 | */ | 
|  | 761 | dp->i_d.di_size = XFS_FSB_TO_B(mp, bno); | 
|  | 762 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | 
|  | 763 | return 0; | 
|  | 764 | } |