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