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