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-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_inum.h" |
| 23 | #include "xfs_log.h" |
| 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 26 | #include "xfs_ag.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 27 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_mount.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_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_dir2_sf.h" |
| 34 | #include "xfs_attr_sf.h" |
| 35 | #include "xfs_dinode.h" |
| 36 | #include "xfs_inode.h" |
| 37 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "xfs_btree.h" |
| 39 | #include "xfs_error.h" |
| 40 | #include "xfs_alloc.h" |
| 41 | #include "xfs_ialloc.h" |
| 42 | #include "xfs_fsops.h" |
| 43 | #include "xfs_itable.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "xfs_trans_space.h" |
| 45 | #include "xfs_rtalloc.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 46 | #include "xfs_rw.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * File system operations |
| 50 | */ |
| 51 | |
| 52 | int |
| 53 | xfs_fs_geometry( |
| 54 | xfs_mount_t *mp, |
| 55 | xfs_fsop_geom_t *geo, |
| 56 | int new_version) |
| 57 | { |
| 58 | geo->blocksize = mp->m_sb.sb_blocksize; |
| 59 | geo->rtextsize = mp->m_sb.sb_rextsize; |
| 60 | geo->agblocks = mp->m_sb.sb_agblocks; |
| 61 | geo->agcount = mp->m_sb.sb_agcount; |
| 62 | geo->logblocks = mp->m_sb.sb_logblocks; |
| 63 | geo->sectsize = mp->m_sb.sb_sectsize; |
| 64 | geo->inodesize = mp->m_sb.sb_inodesize; |
| 65 | geo->imaxpct = mp->m_sb.sb_imax_pct; |
| 66 | geo->datablocks = mp->m_sb.sb_dblocks; |
| 67 | geo->rtblocks = mp->m_sb.sb_rblocks; |
| 68 | geo->rtextents = mp->m_sb.sb_rextents; |
| 69 | geo->logstart = mp->m_sb.sb_logstart; |
| 70 | ASSERT(sizeof(geo->uuid)==sizeof(mp->m_sb.sb_uuid)); |
| 71 | memcpy(geo->uuid, &mp->m_sb.sb_uuid, sizeof(mp->m_sb.sb_uuid)); |
| 72 | if (new_version >= 2) { |
| 73 | geo->sunit = mp->m_sb.sb_unit; |
| 74 | geo->swidth = mp->m_sb.sb_width; |
| 75 | } |
| 76 | if (new_version >= 3) { |
| 77 | geo->version = XFS_FSOP_GEOM_VERSION; |
| 78 | geo->flags = |
| 79 | (XFS_SB_VERSION_HASATTR(&mp->m_sb) ? |
| 80 | XFS_FSOP_GEOM_FLAGS_ATTR : 0) | |
| 81 | (XFS_SB_VERSION_HASNLINK(&mp->m_sb) ? |
| 82 | XFS_FSOP_GEOM_FLAGS_NLINK : 0) | |
| 83 | (XFS_SB_VERSION_HASQUOTA(&mp->m_sb) ? |
| 84 | XFS_FSOP_GEOM_FLAGS_QUOTA : 0) | |
| 85 | (XFS_SB_VERSION_HASALIGN(&mp->m_sb) ? |
| 86 | XFS_FSOP_GEOM_FLAGS_IALIGN : 0) | |
| 87 | (XFS_SB_VERSION_HASDALIGN(&mp->m_sb) ? |
| 88 | XFS_FSOP_GEOM_FLAGS_DALIGN : 0) | |
| 89 | (XFS_SB_VERSION_HASSHARED(&mp->m_sb) ? |
| 90 | XFS_FSOP_GEOM_FLAGS_SHARED : 0) | |
| 91 | (XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) ? |
| 92 | XFS_FSOP_GEOM_FLAGS_EXTFLG : 0) | |
| 93 | (XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ? |
| 94 | XFS_FSOP_GEOM_FLAGS_DIRV2 : 0) | |
| 95 | (XFS_SB_VERSION_HASSECTOR(&mp->m_sb) ? |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 96 | XFS_FSOP_GEOM_FLAGS_SECTOR : 0) | |
| 97 | (XFS_SB_VERSION_HASATTR2(&mp->m_sb) ? |
| 98 | XFS_FSOP_GEOM_FLAGS_ATTR2 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | geo->logsectsize = XFS_SB_VERSION_HASSECTOR(&mp->m_sb) ? |
| 100 | mp->m_sb.sb_logsectsize : BBSIZE; |
| 101 | geo->rtsectsize = mp->m_sb.sb_blocksize; |
| 102 | geo->dirblocksize = mp->m_dirblksize; |
| 103 | } |
| 104 | if (new_version >= 4) { |
| 105 | geo->flags |= |
| 106 | (XFS_SB_VERSION_HASLOGV2(&mp->m_sb) ? |
| 107 | XFS_FSOP_GEOM_FLAGS_LOGV2 : 0); |
| 108 | geo->logsunit = mp->m_sb.sb_logsunit; |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int |
| 114 | xfs_growfs_data_private( |
| 115 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 116 | xfs_growfs_data_t *in) /* growfs data input struct */ |
| 117 | { |
| 118 | xfs_agf_t *agf; |
| 119 | xfs_agi_t *agi; |
| 120 | xfs_agnumber_t agno; |
| 121 | xfs_extlen_t agsize; |
| 122 | xfs_extlen_t tmpsize; |
| 123 | xfs_alloc_rec_t *arec; |
| 124 | xfs_btree_sblock_t *block; |
| 125 | xfs_buf_t *bp; |
| 126 | int bucket; |
| 127 | int dpct; |
| 128 | int error; |
| 129 | xfs_agnumber_t nagcount; |
| 130 | xfs_agnumber_t nagimax = 0; |
| 131 | xfs_rfsblock_t nb, nb_mod; |
| 132 | xfs_rfsblock_t new; |
| 133 | xfs_rfsblock_t nfree; |
| 134 | xfs_agnumber_t oagcount; |
| 135 | int pct; |
| 136 | xfs_sb_t *sbp; |
| 137 | xfs_trans_t *tp; |
| 138 | |
| 139 | nb = in->newblocks; |
| 140 | pct = in->imaxpct; |
| 141 | if (nb < mp->m_sb.sb_dblocks || pct < 0 || pct > 100) |
| 142 | return XFS_ERROR(EINVAL); |
Nathan Scott | 4cc929e | 2007-05-14 18:24:02 +1000 | [diff] [blame^] | 143 | if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb))) |
| 144 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | dpct = pct - mp->m_sb.sb_imax_pct; |
| 146 | error = xfs_read_buf(mp, mp->m_ddev_targp, |
| 147 | XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1), |
| 148 | XFS_FSS_TO_BB(mp, 1), 0, &bp); |
| 149 | if (error) |
| 150 | return error; |
| 151 | ASSERT(bp); |
| 152 | xfs_buf_relse(bp); |
| 153 | |
| 154 | new = nb; /* use new as a temporary here */ |
| 155 | nb_mod = do_div(new, mp->m_sb.sb_agblocks); |
| 156 | nagcount = new + (nb_mod != 0); |
| 157 | if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) { |
| 158 | nagcount--; |
| 159 | nb = nagcount * mp->m_sb.sb_agblocks; |
| 160 | if (nb < mp->m_sb.sb_dblocks) |
| 161 | return XFS_ERROR(EINVAL); |
| 162 | } |
| 163 | new = nb - mp->m_sb.sb_dblocks; |
| 164 | oagcount = mp->m_sb.sb_agcount; |
| 165 | if (nagcount > oagcount) { |
| 166 | down_write(&mp->m_peraglock); |
| 167 | mp->m_perag = kmem_realloc(mp->m_perag, |
| 168 | sizeof(xfs_perag_t) * nagcount, |
| 169 | sizeof(xfs_perag_t) * oagcount, |
| 170 | KM_SLEEP); |
| 171 | memset(&mp->m_perag[oagcount], 0, |
| 172 | (nagcount - oagcount) * sizeof(xfs_perag_t)); |
| 173 | mp->m_flags |= XFS_MOUNT_32BITINODES; |
Nathan Scott | c11e2c3 | 2005-11-02 15:11:45 +1100 | [diff] [blame] | 174 | nagimax = xfs_initialize_perag(XFS_MTOVFS(mp), mp, nagcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | up_write(&mp->m_peraglock); |
| 176 | } |
| 177 | tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFS); |
| 178 | if ((error = xfs_trans_reserve(tp, XFS_GROWFS_SPACE_RES(mp), |
| 179 | XFS_GROWDATA_LOG_RES(mp), 0, 0, 0))) { |
| 180 | xfs_trans_cancel(tp, 0); |
| 181 | return error; |
| 182 | } |
| 183 | |
| 184 | nfree = 0; |
| 185 | for (agno = nagcount - 1; agno >= oagcount; agno--, new -= agsize) { |
| 186 | /* |
| 187 | * AG freelist header block |
| 188 | */ |
| 189 | bp = xfs_buf_get(mp->m_ddev_targp, |
| 190 | XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)), |
| 191 | XFS_FSS_TO_BB(mp, 1), 0); |
| 192 | agf = XFS_BUF_TO_AGF(bp); |
| 193 | memset(agf, 0, mp->m_sb.sb_sectsize); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 194 | agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC); |
| 195 | agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION); |
| 196 | agf->agf_seqno = cpu_to_be32(agno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | if (agno == nagcount - 1) |
| 198 | agsize = |
| 199 | nb - |
| 200 | (agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks); |
| 201 | else |
| 202 | agsize = mp->m_sb.sb_agblocks; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 203 | agf->agf_length = cpu_to_be32(agsize); |
| 204 | agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp)); |
| 205 | agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp)); |
| 206 | agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1); |
| 207 | agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | agf->agf_flfirst = 0; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 209 | agf->agf_fllast = cpu_to_be32(XFS_AGFL_SIZE(mp) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | agf->agf_flcount = 0; |
| 211 | tmpsize = agsize - XFS_PREALLOC_BLOCKS(mp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 212 | agf->agf_freeblks = cpu_to_be32(tmpsize); |
| 213 | agf->agf_longest = cpu_to_be32(tmpsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | error = xfs_bwrite(mp, bp); |
| 215 | if (error) { |
| 216 | goto error0; |
| 217 | } |
| 218 | /* |
| 219 | * AG inode header block |
| 220 | */ |
| 221 | bp = xfs_buf_get(mp->m_ddev_targp, |
| 222 | XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), |
| 223 | XFS_FSS_TO_BB(mp, 1), 0); |
| 224 | agi = XFS_BUF_TO_AGI(bp); |
| 225 | memset(agi, 0, mp->m_sb.sb_sectsize); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 226 | agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC); |
| 227 | agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION); |
| 228 | agi->agi_seqno = cpu_to_be32(agno); |
| 229 | agi->agi_length = cpu_to_be32(agsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | agi->agi_count = 0; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 231 | agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp)); |
| 232 | agi->agi_level = cpu_to_be32(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | agi->agi_freecount = 0; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 234 | agi->agi_newino = cpu_to_be32(NULLAGINO); |
| 235 | agi->agi_dirino = cpu_to_be32(NULLAGINO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 237 | agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | error = xfs_bwrite(mp, bp); |
| 239 | if (error) { |
| 240 | goto error0; |
| 241 | } |
| 242 | /* |
| 243 | * BNO btree root block |
| 244 | */ |
| 245 | bp = xfs_buf_get(mp->m_ddev_targp, |
| 246 | XFS_AGB_TO_DADDR(mp, agno, XFS_BNO_BLOCK(mp)), |
| 247 | BTOBB(mp->m_sb.sb_blocksize), 0); |
| 248 | block = XFS_BUF_TO_SBLOCK(bp); |
| 249 | memset(block, 0, mp->m_sb.sb_blocksize); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 250 | block->bb_magic = cpu_to_be32(XFS_ABTB_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | block->bb_level = 0; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 252 | block->bb_numrecs = cpu_to_be16(1); |
| 253 | block->bb_leftsib = cpu_to_be32(NULLAGBLOCK); |
| 254 | block->bb_rightsib = cpu_to_be32(NULLAGBLOCK); |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 255 | arec = XFS_BTREE_REC_ADDR(xfs_alloc, block, 1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 256 | arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp)); |
| 257 | arec->ar_blockcount = cpu_to_be32( |
| 258 | agsize - be32_to_cpu(arec->ar_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | error = xfs_bwrite(mp, bp); |
| 260 | if (error) { |
| 261 | goto error0; |
| 262 | } |
| 263 | /* |
| 264 | * CNT btree root block |
| 265 | */ |
| 266 | bp = xfs_buf_get(mp->m_ddev_targp, |
| 267 | XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)), |
| 268 | BTOBB(mp->m_sb.sb_blocksize), 0); |
| 269 | block = XFS_BUF_TO_SBLOCK(bp); |
| 270 | memset(block, 0, mp->m_sb.sb_blocksize); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 271 | block->bb_magic = cpu_to_be32(XFS_ABTC_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | block->bb_level = 0; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 273 | block->bb_numrecs = cpu_to_be16(1); |
| 274 | block->bb_leftsib = cpu_to_be32(NULLAGBLOCK); |
| 275 | block->bb_rightsib = cpu_to_be32(NULLAGBLOCK); |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 276 | arec = XFS_BTREE_REC_ADDR(xfs_alloc, block, 1); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 277 | arec->ar_startblock = cpu_to_be32(XFS_PREALLOC_BLOCKS(mp)); |
| 278 | arec->ar_blockcount = cpu_to_be32( |
| 279 | agsize - be32_to_cpu(arec->ar_startblock)); |
| 280 | nfree += be32_to_cpu(arec->ar_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | error = xfs_bwrite(mp, bp); |
| 282 | if (error) { |
| 283 | goto error0; |
| 284 | } |
| 285 | /* |
| 286 | * INO btree root block |
| 287 | */ |
| 288 | bp = xfs_buf_get(mp->m_ddev_targp, |
| 289 | XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)), |
| 290 | BTOBB(mp->m_sb.sb_blocksize), 0); |
| 291 | block = XFS_BUF_TO_SBLOCK(bp); |
| 292 | memset(block, 0, mp->m_sb.sb_blocksize); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 293 | block->bb_magic = cpu_to_be32(XFS_IBT_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | block->bb_level = 0; |
| 295 | block->bb_numrecs = 0; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 296 | block->bb_leftsib = cpu_to_be32(NULLAGBLOCK); |
| 297 | block->bb_rightsib = cpu_to_be32(NULLAGBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | error = xfs_bwrite(mp, bp); |
| 299 | if (error) { |
| 300 | goto error0; |
| 301 | } |
| 302 | } |
| 303 | xfs_trans_agblocks_delta(tp, nfree); |
| 304 | /* |
| 305 | * There are new blocks in the old last a.g. |
| 306 | */ |
| 307 | if (new) { |
| 308 | /* |
| 309 | * Change the agi length. |
| 310 | */ |
| 311 | error = xfs_ialloc_read_agi(mp, tp, agno, &bp); |
| 312 | if (error) { |
| 313 | goto error0; |
| 314 | } |
| 315 | ASSERT(bp); |
| 316 | agi = XFS_BUF_TO_AGI(bp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 317 | be32_add(&agi->agi_length, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | ASSERT(nagcount == oagcount || |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 319 | be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH); |
| 321 | /* |
| 322 | * Change agf length. |
| 323 | */ |
| 324 | error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp); |
| 325 | if (error) { |
| 326 | goto error0; |
| 327 | } |
| 328 | ASSERT(bp); |
| 329 | agf = XFS_BUF_TO_AGF(bp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 330 | be32_add(&agf->agf_length, new); |
| 331 | ASSERT(be32_to_cpu(agf->agf_length) == |
| 332 | be32_to_cpu(agi->agi_length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | /* |
| 334 | * Free the new space. |
| 335 | */ |
| 336 | error = xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, agno, |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 337 | be32_to_cpu(agf->agf_length) - new), new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (error) { |
| 339 | goto error0; |
| 340 | } |
| 341 | } |
| 342 | if (nagcount > oagcount) |
| 343 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount); |
| 344 | if (nb > mp->m_sb.sb_dblocks) |
| 345 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS, |
| 346 | nb - mp->m_sb.sb_dblocks); |
| 347 | if (nfree) |
| 348 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree); |
| 349 | if (dpct) |
| 350 | xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 351 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | if (error) { |
| 353 | return error; |
| 354 | } |
| 355 | /* New allocation groups fully initialized, so update mount struct */ |
| 356 | if (nagimax) |
| 357 | mp->m_maxagi = nagimax; |
| 358 | if (mp->m_sb.sb_imax_pct) { |
| 359 | __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct; |
| 360 | do_div(icount, 100); |
| 361 | mp->m_maxicount = icount << mp->m_sb.sb_inopblog; |
| 362 | } else |
| 363 | mp->m_maxicount = 0; |
| 364 | for (agno = 1; agno < nagcount; agno++) { |
| 365 | error = xfs_read_buf(mp, mp->m_ddev_targp, |
| 366 | XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)), |
| 367 | XFS_FSS_TO_BB(mp, 1), 0, &bp); |
| 368 | if (error) { |
| 369 | xfs_fs_cmn_err(CE_WARN, mp, |
| 370 | "error %d reading secondary superblock for ag %d", |
| 371 | error, agno); |
| 372 | break; |
| 373 | } |
| 374 | sbp = XFS_BUF_TO_SBP(bp); |
| 375 | xfs_xlatesb(sbp, &mp->m_sb, -1, XFS_SB_ALL_BITS); |
| 376 | /* |
| 377 | * If we get an error writing out the alternate superblocks, |
| 378 | * just issue a warning and continue. The real work is |
| 379 | * already done and committed. |
| 380 | */ |
| 381 | if (!(error = xfs_bwrite(mp, bp))) { |
| 382 | continue; |
| 383 | } else { |
| 384 | xfs_fs_cmn_err(CE_WARN, mp, |
| 385 | "write error %d updating secondary superblock for ag %d", |
| 386 | error, agno); |
| 387 | break; /* no point in continuing */ |
| 388 | } |
| 389 | } |
| 390 | return 0; |
| 391 | |
| 392 | error0: |
| 393 | xfs_trans_cancel(tp, XFS_TRANS_ABORT); |
| 394 | return error; |
| 395 | } |
| 396 | |
| 397 | static int |
| 398 | xfs_growfs_log_private( |
| 399 | xfs_mount_t *mp, /* mount point for filesystem */ |
| 400 | xfs_growfs_log_t *in) /* growfs log input struct */ |
| 401 | { |
| 402 | xfs_extlen_t nb; |
| 403 | |
| 404 | nb = in->newblocks; |
| 405 | if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES)) |
| 406 | return XFS_ERROR(EINVAL); |
| 407 | if (nb == mp->m_sb.sb_logblocks && |
| 408 | in->isint == (mp->m_sb.sb_logstart != 0)) |
| 409 | return XFS_ERROR(EINVAL); |
| 410 | /* |
| 411 | * Moving the log is hard, need new interfaces to sync |
| 412 | * the log first, hold off all activity while moving it. |
| 413 | * Can have shorter or longer log in the same space, |
| 414 | * or transform internal to external log or vice versa. |
| 415 | */ |
| 416 | return XFS_ERROR(ENOSYS); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * protected versions of growfs function acquire and release locks on the mount |
| 421 | * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG, |
| 422 | * XFS_IOC_FSGROWFSRT |
| 423 | */ |
| 424 | |
| 425 | |
| 426 | int |
| 427 | xfs_growfs_data( |
| 428 | xfs_mount_t *mp, |
| 429 | xfs_growfs_data_t *in) |
| 430 | { |
| 431 | int error; |
| 432 | if (!cpsema(&mp->m_growlock)) |
| 433 | return XFS_ERROR(EWOULDBLOCK); |
| 434 | error = xfs_growfs_data_private(mp, in); |
| 435 | vsema(&mp->m_growlock); |
| 436 | return error; |
| 437 | } |
| 438 | |
| 439 | int |
| 440 | xfs_growfs_log( |
| 441 | xfs_mount_t *mp, |
| 442 | xfs_growfs_log_t *in) |
| 443 | { |
| 444 | int error; |
| 445 | if (!cpsema(&mp->m_growlock)) |
| 446 | return XFS_ERROR(EWOULDBLOCK); |
| 447 | error = xfs_growfs_log_private(mp, in); |
| 448 | vsema(&mp->m_growlock); |
| 449 | return error; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * exported through ioctl XFS_IOC_FSCOUNTS |
| 454 | */ |
| 455 | |
| 456 | int |
| 457 | xfs_fs_counts( |
| 458 | xfs_mount_t *mp, |
| 459 | xfs_fsop_counts_t *cnt) |
| 460 | { |
| 461 | unsigned long s; |
| 462 | |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 463 | xfs_icsb_sync_counters_flags(mp, XFS_ICSB_LAZY_COUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | s = XFS_SB_LOCK(mp); |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 465 | cnt->freedata = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | cnt->freertx = mp->m_sb.sb_frextents; |
| 467 | cnt->freeino = mp->m_sb.sb_ifree; |
| 468 | cnt->allocino = mp->m_sb.sb_icount; |
| 469 | XFS_SB_UNLOCK(mp, s); |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS |
| 475 | * |
| 476 | * xfs_reserve_blocks is called to set m_resblks |
| 477 | * in the in-core mount table. The number of unused reserved blocks |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 478 | * is kept in m_resblks_avail. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | * |
| 480 | * Reserve the requested number of blocks if available. Otherwise return |
| 481 | * as many as possible to satisfy the request. The actual number |
| 482 | * reserved are returned in outval |
| 483 | * |
| 484 | * A null inval pointer indicates that only the current reserved blocks |
| 485 | * available should be returned no settings are changed. |
| 486 | */ |
| 487 | |
| 488 | int |
| 489 | xfs_reserve_blocks( |
| 490 | xfs_mount_t *mp, |
| 491 | __uint64_t *inval, |
| 492 | xfs_fsop_resblks_t *outval) |
| 493 | { |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 494 | __int64_t lcounter, delta, fdblks_delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | __uint64_t request; |
| 496 | unsigned long s; |
| 497 | |
| 498 | /* If inval is null, report current values and return */ |
| 499 | |
| 500 | if (inval == (__uint64_t *)NULL) { |
| 501 | outval->resblks = mp->m_resblks; |
| 502 | outval->resblks_avail = mp->m_resblks_avail; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 503 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | request = *inval; |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 507 | |
| 508 | /* |
| 509 | * With per-cpu counters, this becomes an interesting |
| 510 | * problem. we needto work out if we are freeing or allocation |
| 511 | * blocks first, then we can do the modification as necessary. |
| 512 | * |
| 513 | * We do this under the XFS_SB_LOCK so that if we are near |
| 514 | * ENOSPC, we will hold out any changes while we work out |
| 515 | * what to do. This means that the amount of free space can |
| 516 | * change while we do this, so we need to retry if we end up |
| 517 | * trying to reserve more space than is available. |
| 518 | * |
| 519 | * We also use the xfs_mod_incore_sb() interface so that we |
| 520 | * don't have to care about whether per cpu counter are |
| 521 | * enabled, disabled or even compiled in.... |
| 522 | */ |
| 523 | retry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | s = XFS_SB_LOCK(mp); |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 525 | xfs_icsb_sync_counters_flags(mp, XFS_ICSB_SB_LOCKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | /* |
| 528 | * If our previous reservation was larger than the current value, |
| 529 | * then move any unused blocks back to the free pool. |
| 530 | */ |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 531 | fdblks_delta = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (mp->m_resblks > request) { |
| 533 | lcounter = mp->m_resblks_avail - request; |
| 534 | if (lcounter > 0) { /* release unused blocks */ |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 535 | fdblks_delta = lcounter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | mp->m_resblks_avail -= lcounter; |
| 537 | } |
| 538 | mp->m_resblks = request; |
| 539 | } else { |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 540 | __int64_t free; |
| 541 | |
| 542 | free = mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp); |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 543 | if (!free) |
| 544 | goto out; /* ENOSPC and fdblks_delta = 0 */ |
| 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | delta = request - mp->m_resblks; |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 547 | lcounter = free - delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | if (lcounter < 0) { |
| 549 | /* We can't satisfy the request, just get what we can */ |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 550 | mp->m_resblks += free; |
| 551 | mp->m_resblks_avail += free; |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 552 | fdblks_delta = -free; |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 553 | mp->m_sb.sb_fdblocks = XFS_ALLOC_SET_ASIDE(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } else { |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 555 | fdblks_delta = -delta; |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 556 | mp->m_sb.sb_fdblocks = |
| 557 | lcounter + XFS_ALLOC_SET_ASIDE(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | mp->m_resblks = request; |
| 559 | mp->m_resblks_avail += delta; |
| 560 | } |
| 561 | } |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 562 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | outval->resblks = mp->m_resblks; |
| 564 | outval->resblks_avail = mp->m_resblks_avail; |
| 565 | XFS_SB_UNLOCK(mp, s); |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 566 | |
| 567 | if (fdblks_delta) { |
| 568 | /* |
| 569 | * If we are putting blocks back here, m_resblks_avail is |
| 570 | * already at it's max so this will put it in the free pool. |
| 571 | * |
| 572 | * If we need space, we'll either succeed in getting it |
| 573 | * from the free block count or we'll get an enospc. If |
| 574 | * we get a ENOSPC, it means things changed while we were |
| 575 | * calculating fdblks_delta and so we should try again to |
| 576 | * see if there is anything left to reserve. |
| 577 | * |
| 578 | * Don't set the reserved flag here - we don't want to reserve |
| 579 | * the extra reserve blocks from the reserve..... |
| 580 | */ |
| 581 | int error; |
| 582 | error = xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, fdblks_delta, 0); |
| 583 | if (error == ENOSPC) |
| 584 | goto retry; |
| 585 | } |
| 586 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 587 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Christoph Hellwig | e13a73f | 2006-01-11 15:30:08 +1100 | [diff] [blame] | 590 | void |
Nathan Scott | 421ad13 | 2006-06-09 17:12:46 +1000 | [diff] [blame] | 591 | xfs_fs_log_dummy( |
| 592 | xfs_mount_t *mp) |
Christoph Hellwig | e13a73f | 2006-01-11 15:30:08 +1100 | [diff] [blame] | 593 | { |
Nathan Scott | 421ad13 | 2006-06-09 17:12:46 +1000 | [diff] [blame] | 594 | xfs_trans_t *tp; |
| 595 | xfs_inode_t *ip; |
Christoph Hellwig | e13a73f | 2006-01-11 15:30:08 +1100 | [diff] [blame] | 596 | |
| 597 | tp = _xfs_trans_alloc(mp, XFS_TRANS_DUMMY1); |
Christoph Hellwig | e13a73f | 2006-01-11 15:30:08 +1100 | [diff] [blame] | 598 | if (xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0)) { |
| 599 | xfs_trans_cancel(tp, 0); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | ip = mp->m_rootip; |
| 604 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 605 | |
| 606 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 607 | xfs_trans_ihold(tp, ip); |
| 608 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 609 | xfs_trans_set_sync(tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 610 | xfs_trans_commit(tp, 0); |
Christoph Hellwig | e13a73f | 2006-01-11 15:30:08 +1100 | [diff] [blame] | 611 | |
| 612 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 613 | } |
| 614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | int |
| 616 | xfs_fs_goingdown( |
| 617 | xfs_mount_t *mp, |
| 618 | __uint32_t inflags) |
| 619 | { |
| 620 | switch (inflags) { |
| 621 | case XFS_FSOP_GOING_FLAGS_DEFAULT: { |
Nathan Scott | b83bd13 | 2006-06-09 16:48:30 +1000 | [diff] [blame] | 622 | struct bhv_vfs *vfsp = XFS_MTOVFS(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | struct super_block *sb = freeze_bdev(vfsp->vfs_super->s_bdev); |
| 624 | |
Christoph Hellwig | f33c679 | 2005-11-25 16:41:47 +1100 | [diff] [blame] | 625 | if (sb && !IS_ERR(sb)) { |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 626 | xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | thaw_bdev(sb->s_bdev, sb); |
| 628 | } |
| 629 | |
| 630 | break; |
| 631 | } |
| 632 | case XFS_FSOP_GOING_FLAGS_LOGFLUSH: |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 633 | xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | break; |
| 635 | case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH: |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 636 | xfs_force_shutdown(mp, |
| 637 | SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | break; |
| 639 | default: |
| 640 | return XFS_ERROR(EINVAL); |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |