blob: 0f96847b90e1175d2c6d0f497fe278e67f95e141 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110022#include "xfs_log_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100026#include "xfs_defer.h"
Dave Chinner8f661932014-06-06 15:15:59 +100027#include "xfs_da_format.h"
28#include "xfs_da_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110030#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110033#include "xfs_btree.h"
34#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_alloc.h"
Darrick J. Wonge70d8292016-08-03 11:36:08 +100036#include "xfs_rmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_ialloc.h"
38#include "xfs_fsops.h"
39#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_trans_space.h"
41#include "xfs_rtalloc.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110043#include "xfs_log.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110044#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100045#include "xfs_rmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * File system operations
49 */
50
51int
52xfs_fs_geometry(
53 xfs_mount_t *mp,
54 xfs_fsop_geom_t *geo,
55 int new_version)
56{
Dan Rosenbergc4d0c3b2011-02-14 13:45:28 +000057
58 memset(geo, 0, sizeof(*geo));
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 geo->blocksize = mp->m_sb.sb_blocksize;
61 geo->rtextsize = mp->m_sb.sb_rextsize;
62 geo->agblocks = mp->m_sb.sb_agblocks;
63 geo->agcount = mp->m_sb.sb_agcount;
64 geo->logblocks = mp->m_sb.sb_logblocks;
65 geo->sectsize = mp->m_sb.sb_sectsize;
66 geo->inodesize = mp->m_sb.sb_inodesize;
67 geo->imaxpct = mp->m_sb.sb_imax_pct;
68 geo->datablocks = mp->m_sb.sb_dblocks;
69 geo->rtblocks = mp->m_sb.sb_rblocks;
70 geo->rtextents = mp->m_sb.sb_rextents;
71 geo->logstart = mp->m_sb.sb_logstart;
72 ASSERT(sizeof(geo->uuid)==sizeof(mp->m_sb.sb_uuid));
73 memcpy(geo->uuid, &mp->m_sb.sb_uuid, sizeof(mp->m_sb.sb_uuid));
74 if (new_version >= 2) {
75 geo->sunit = mp->m_sb.sb_unit;
76 geo->swidth = mp->m_sb.sb_width;
77 }
78 if (new_version >= 3) {
79 geo->version = XFS_FSOP_GEOM_VERSION;
Dave Chinner263997a2014-05-20 07:46:40 +100080 geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
Dave Chinner5d074a42014-05-20 07:46:55 +100081 XFS_FSOP_GEOM_FLAGS_DIRV2 |
Eric Sandeen62118702008-03-06 13:44:28 +110082 (xfs_sb_version_hasattr(&mp->m_sb) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 XFS_FSOP_GEOM_FLAGS_ATTR : 0) |
Eric Sandeen62118702008-03-06 13:44:28 +110084 (xfs_sb_version_hasquota(&mp->m_sb) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 XFS_FSOP_GEOM_FLAGS_QUOTA : 0) |
Eric Sandeen62118702008-03-06 13:44:28 +110086 (xfs_sb_version_hasalign(&mp->m_sb) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 XFS_FSOP_GEOM_FLAGS_IALIGN : 0) |
Eric Sandeen62118702008-03-06 13:44:28 +110088 (xfs_sb_version_hasdalign(&mp->m_sb) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 XFS_FSOP_GEOM_FLAGS_DALIGN : 0) |
Eric Sandeen62118702008-03-06 13:44:28 +110090 (xfs_sb_version_hasextflgbit(&mp->m_sb) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 XFS_FSOP_GEOM_FLAGS_EXTFLG : 0) |
Eric Sandeen62118702008-03-06 13:44:28 +110092 (xfs_sb_version_hassector(&mp->m_sb) ?
Nathan Scottd8cc8902005-11-02 10:34:53 +110093 XFS_FSOP_GEOM_FLAGS_SECTOR : 0) |
Barry Naujok189f4bf2008-05-21 16:58:55 +100094 (xfs_sb_version_hasasciici(&mp->m_sb) ?
95 XFS_FSOP_GEOM_FLAGS_DIRV2CI : 0) |
David Chinner92821e22007-05-24 15:26:31 +100096 (xfs_sb_version_haslazysbcount(&mp->m_sb) ?
97 XFS_FSOP_GEOM_FLAGS_LAZYSB : 0) |
Eric Sandeen62118702008-03-06 13:44:28 +110098 (xfs_sb_version_hasattr2(&mp->m_sb) ?
Eric Sandeen69a58a42012-10-09 14:11:45 -050099 XFS_FSOP_GEOM_FLAGS_ATTR2 : 0) |
100 (xfs_sb_version_hasprojid32bit(&mp->m_sb) ?
Dave Chinner74137fff2013-05-27 16:38:26 +1000101 XFS_FSOP_GEOM_FLAGS_PROJID32 : 0) |
102 (xfs_sb_version_hascrc(&mp->m_sb) ?
Mark Tinguely2900a572013-10-02 16:00:06 -0500103 XFS_FSOP_GEOM_FLAGS_V5SB : 0) |
104 (xfs_sb_version_hasftype(&mp->m_sb) ?
Brian Foster0c153c12014-04-24 16:01:41 +1000105 XFS_FSOP_GEOM_FLAGS_FTYPE : 0) |
106 (xfs_sb_version_hasfinobt(&mp->m_sb) ?
Brian Foster502a4e72015-05-29 08:58:32 +1000107 XFS_FSOP_GEOM_FLAGS_FINOBT : 0) |
108 (xfs_sb_version_hassparseinodes(&mp->m_sb) ?
Darrick J. Wong5d650e92016-08-03 12:16:44 +1000109 XFS_FSOP_GEOM_FLAGS_SPINODES : 0) |
110 (xfs_sb_version_hasrmapbt(&mp->m_sb) ?
111 XFS_FSOP_GEOM_FLAGS_RMAPBT : 0);
Eric Sandeen62118702008-03-06 13:44:28 +1100112 geo->logsectsize = xfs_sb_version_hassector(&mp->m_sb) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 mp->m_sb.sb_logsectsize : BBSIZE;
114 geo->rtsectsize = mp->m_sb.sb_blocksize;
Dave Chinner8f661932014-06-06 15:15:59 +1000115 geo->dirblocksize = mp->m_dir_geo->blksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117 if (new_version >= 4) {
118 geo->flags |=
Eric Sandeen62118702008-03-06 13:44:28 +1100119 (xfs_sb_version_haslogv2(&mp->m_sb) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 XFS_FSOP_GEOM_FLAGS_LOGV2 : 0);
121 geo->logsunit = mp->m_sb.sb_logsunit;
122 }
123 return 0;
124}
125
Dave Chinnerfd236832012-11-12 22:53:59 +1100126static struct xfs_buf *
127xfs_growfs_get_hdr_buf(
128 struct xfs_mount *mp,
129 xfs_daddr_t blkno,
130 size_t numblks,
Dave Chinner1813dd62012-11-14 17:54:40 +1100131 int flags,
132 const struct xfs_buf_ops *ops)
Dave Chinnerfd236832012-11-12 22:53:59 +1100133{
134 struct xfs_buf *bp;
135
136 bp = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, flags);
137 if (!bp)
138 return NULL;
139
140 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
141 bp->b_bn = blkno;
142 bp->b_maps[0].bm_bn = blkno;
Dave Chinner1813dd62012-11-14 17:54:40 +1100143 bp->b_ops = ops;
Dave Chinnerfd236832012-11-12 22:53:59 +1100144
145 return bp;
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int
149xfs_growfs_data_private(
150 xfs_mount_t *mp, /* mount point for filesystem */
151 xfs_growfs_data_t *in) /* growfs data input struct */
152{
153 xfs_agf_t *agf;
Dave Chinnerde497682012-11-12 22:54:00 +1100154 struct xfs_agfl *agfl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 xfs_agi_t *agi;
156 xfs_agnumber_t agno;
157 xfs_extlen_t agsize;
158 xfs_extlen_t tmpsize;
159 xfs_alloc_rec_t *arec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 xfs_buf_t *bp;
161 int bucket;
162 int dpct;
Eric Sandeen59e5a0e2013-10-11 14:14:05 -0500163 int error, saved_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 xfs_agnumber_t nagcount;
165 xfs_agnumber_t nagimax = 0;
166 xfs_rfsblock_t nb, nb_mod;
167 xfs_rfsblock_t new;
168 xfs_rfsblock_t nfree;
169 xfs_agnumber_t oagcount;
170 int pct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 xfs_trans_t *tp;
172
173 nb = in->newblocks;
174 pct = in->imaxpct;
175 if (nb < mp->m_sb.sb_dblocks || pct < 0 || pct > 100)
Dave Chinner24513372014-06-25 14:58:08 +1000176 return -EINVAL;
Nathan Scott4cc929e2007-05-14 18:24:02 +1000177 if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb)))
178 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dpct = pct - mp->m_sb.sb_imax_pct;
Dave Chinnerba372672014-10-02 09:05:32 +1000180 error = xfs_buf_read_uncached(mp->m_ddev_targp,
Dave Chinner1922c942010-09-22 10:47:20 +1000181 XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
Dave Chinnerba372672014-10-02 09:05:32 +1000182 XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
183 if (error)
Dave Chinnereab4e632012-11-12 22:54:02 +1100184 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 xfs_buf_relse(bp);
186
187 new = nb; /* use new as a temporary here */
188 nb_mod = do_div(new, mp->m_sb.sb_agblocks);
189 nagcount = new + (nb_mod != 0);
190 if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
191 nagcount--;
Eric Sandeene6da7c92009-05-23 14:30:12 -0500192 nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (nb < mp->m_sb.sb_dblocks)
Dave Chinner24513372014-06-25 14:58:08 +1000194 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 new = nb - mp->m_sb.sb_dblocks;
197 oagcount = mp->m_sb.sb_agcount;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000198
199 /* allocate the new per-ag structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (nagcount > oagcount) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000201 error = xfs_initialize_perag(mp, nagcount, &nagimax);
202 if (error)
203 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000205
Christoph Hellwig253f4912016-04-06 09:19:55 +1000206 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
207 XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
208 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000211 /*
212 * Write new AG headers to disk. Non-transactional, but written
213 * synchronously so they are completed prior to the growfs transaction
214 * being logged.
215 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 nfree = 0;
217 for (agno = nagcount - 1; agno >= oagcount; agno--, new -= agsize) {
Dave Chinnerf94c4452013-11-21 15:41:06 +1100218 __be32 *agfl_bno;
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /*
Dave Chinnerde497682012-11-12 22:54:00 +1100221 * AG freespace header block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Dave Chinnerfd236832012-11-12 22:53:59 +1100223 bp = xfs_growfs_get_hdr_buf(mp,
224 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100225 XFS_FSS_TO_BB(mp, 1), 0,
226 &xfs_agf_buf_ops);
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000227 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000228 error = -ENOMEM;
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000229 goto error0;
230 }
Dave Chinnerfd236832012-11-12 22:53:59 +1100231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 agf = XFS_BUF_TO_AGF(bp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100233 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
234 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
235 agf->agf_seqno = cpu_to_be32(agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (agno == nagcount - 1)
237 agsize =
238 nb -
239 (agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
240 else
241 agsize = mp->m_sb.sb_agblocks;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100242 agf->agf_length = cpu_to_be32(agsize);
243 agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
244 agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
245 agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
246 agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
Darrick J. Wonge70d8292016-08-03 11:36:08 +1000247 if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
248 agf->agf_roots[XFS_BTNUM_RMAPi] =
249 cpu_to_be32(XFS_RMAP_BLOCK(mp));
250 agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
251 }
252
Dave Chinnerad747e32016-04-06 07:06:20 +1000253 agf->agf_flfirst = cpu_to_be32(1);
254 agf->agf_fllast = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 agf->agf_flcount = 0;
Darrick J. Wong80180262016-08-03 11:31:47 +1000256 tmpsize = agsize - mp->m_ag_prealloc_blocks;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100257 agf->agf_freeblks = cpu_to_be32(tmpsize);
258 agf->agf_longest = cpu_to_be32(tmpsize);
Dave Chinner4e0e6042013-04-03 16:11:13 +1100259 if (xfs_sb_version_hascrc(&mp->m_sb))
Dave Chinnerac383de2015-08-19 10:31:41 +1000260 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner4e0e6042013-04-03 16:11:13 +1100261
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000262 error = xfs_bwrite(bp);
263 xfs_buf_relse(bp);
264 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto error0;
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /*
Dave Chinnerde497682012-11-12 22:54:00 +1100268 * AG freelist header block
269 */
270 bp = xfs_growfs_get_hdr_buf(mp,
271 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100272 XFS_FSS_TO_BB(mp, 1), 0,
273 &xfs_agfl_buf_ops);
Dave Chinnerde497682012-11-12 22:54:00 +1100274 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000275 error = -ENOMEM;
Dave Chinnerde497682012-11-12 22:54:00 +1100276 goto error0;
277 }
278
279 agfl = XFS_BUF_TO_AGFL(bp);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100280 if (xfs_sb_version_hascrc(&mp->m_sb)) {
281 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
282 agfl->agfl_seqno = cpu_to_be32(agno);
Dave Chinnerac383de2015-08-19 10:31:41 +1000283 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
Christoph Hellwig77c95bb2013-04-03 16:11:14 +1100284 }
Dave Chinnerf94c4452013-11-21 15:41:06 +1100285
286 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, bp);
Dave Chinnerde497682012-11-12 22:54:00 +1100287 for (bucket = 0; bucket < XFS_AGFL_SIZE(mp); bucket++)
Dave Chinnerf94c4452013-11-21 15:41:06 +1100288 agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
Dave Chinnerde497682012-11-12 22:54:00 +1100289
290 error = xfs_bwrite(bp);
291 xfs_buf_relse(bp);
292 if (error)
293 goto error0;
294
295 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * AG inode header block
297 */
Dave Chinnerfd236832012-11-12 22:53:59 +1100298 bp = xfs_growfs_get_hdr_buf(mp,
299 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100300 XFS_FSS_TO_BB(mp, 1), 0,
301 &xfs_agi_buf_ops);
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000302 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000303 error = -ENOMEM;
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000304 goto error0;
305 }
Dave Chinnerfd236832012-11-12 22:53:59 +1100306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100308 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
309 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
310 agi->agi_seqno = cpu_to_be32(agno);
311 agi->agi_length = cpu_to_be32(agsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 agi->agi_count = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100313 agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
314 agi->agi_level = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 agi->agi_freecount = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100316 agi->agi_newino = cpu_to_be32(NULLAGINO);
317 agi->agi_dirino = cpu_to_be32(NULLAGINO);
Dave Chinner983d09f2013-04-03 16:11:15 +1100318 if (xfs_sb_version_hascrc(&mp->m_sb))
Dave Chinnerac383de2015-08-19 10:31:41 +1000319 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
Brian Fostera3fa5162014-04-24 16:01:39 +1000320 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
321 agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
322 agi->agi_free_level = cpu_to_be32(1);
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100325 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
Dave Chinner983d09f2013-04-03 16:11:15 +1100326
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000327 error = xfs_bwrite(bp);
328 xfs_buf_relse(bp);
329 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 goto error0;
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
333 * BNO btree root block
334 */
Dave Chinnerfd236832012-11-12 22:53:59 +1100335 bp = xfs_growfs_get_hdr_buf(mp,
336 XFS_AGB_TO_DADDR(mp, agno, XFS_BNO_BLOCK(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100337 BTOBB(mp->m_sb.sb_blocksize), 0,
338 &xfs_allocbt_buf_ops);
Dave Chinnerfd236832012-11-12 22:53:59 +1100339
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000340 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000341 error = -ENOMEM;
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000342 goto error0;
343 }
Dave Chinnerb64f3a32012-11-13 16:40:27 -0600344
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500345 if (xfs_sb_version_hascrc(&mp->m_sb))
346 xfs_btree_init_block(mp, bp, XFS_ABTB_CRC_MAGIC, 0, 1,
347 agno, XFS_BTREE_CRC_BLOCKS);
348 else
349 xfs_btree_init_block(mp, bp, XFS_ABTB_MAGIC, 0, 1,
350 agno, 0);
351
Dave Chinnerb64f3a32012-11-13 16:40:27 -0600352 arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
Darrick J. Wong80180262016-08-03 11:31:47 +1000353 arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100354 arec->ar_blockcount = cpu_to_be32(
355 agsize - be32_to_cpu(arec->ar_startblock));
Dave Chinnerb64f3a32012-11-13 16:40:27 -0600356
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000357 error = xfs_bwrite(bp);
358 xfs_buf_relse(bp);
359 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 goto error0;
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /*
363 * CNT btree root block
364 */
Dave Chinnerfd236832012-11-12 22:53:59 +1100365 bp = xfs_growfs_get_hdr_buf(mp,
366 XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100367 BTOBB(mp->m_sb.sb_blocksize), 0,
368 &xfs_allocbt_buf_ops);
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000369 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000370 error = -ENOMEM;
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000371 goto error0;
372 }
Dave Chinnerb64f3a32012-11-13 16:40:27 -0600373
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500374 if (xfs_sb_version_hascrc(&mp->m_sb))
375 xfs_btree_init_block(mp, bp, XFS_ABTC_CRC_MAGIC, 0, 1,
376 agno, XFS_BTREE_CRC_BLOCKS);
377 else
378 xfs_btree_init_block(mp, bp, XFS_ABTC_MAGIC, 0, 1,
379 agno, 0);
380
Dave Chinnerb64f3a32012-11-13 16:40:27 -0600381 arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
Darrick J. Wong80180262016-08-03 11:31:47 +1000382 arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100383 arec->ar_blockcount = cpu_to_be32(
384 agsize - be32_to_cpu(arec->ar_startblock));
385 nfree += be32_to_cpu(arec->ar_blockcount);
Dave Chinnerb64f3a32012-11-13 16:40:27 -0600386
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000387 error = xfs_bwrite(bp);
388 xfs_buf_relse(bp);
389 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 goto error0;
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000391
Darrick J. Wonge70d8292016-08-03 11:36:08 +1000392 /* RMAP btree root block */
393 if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
394 struct xfs_rmap_rec *rrec;
395 struct xfs_btree_block *block;
396
397 bp = xfs_growfs_get_hdr_buf(mp,
398 XFS_AGB_TO_DADDR(mp, agno, XFS_RMAP_BLOCK(mp)),
399 BTOBB(mp->m_sb.sb_blocksize), 0,
400 &xfs_rmapbt_buf_ops);
401 if (!bp) {
402 error = -ENOMEM;
403 goto error0;
404 }
405
406 xfs_btree_init_block(mp, bp, XFS_RMAP_CRC_MAGIC, 0, 0,
407 agno, XFS_BTREE_CRC_BLOCKS);
408 block = XFS_BUF_TO_BLOCK(bp);
409
410
411 /*
412 * mark the AG header regions as static metadata The BNO
413 * btree block is the first block after the headers, so
414 * it's location defines the size of region the static
415 * metadata consumes.
416 *
417 * Note: unlike mkfs, we never have to account for log
418 * space when growing the data regions
419 */
420 rrec = XFS_RMAP_REC_ADDR(block, 1);
421 rrec->rm_startblock = 0;
422 rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
423 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
424 rrec->rm_offset = 0;
425 be16_add_cpu(&block->bb_numrecs, 1);
426
427 /* account freespace btree root blocks */
428 rrec = XFS_RMAP_REC_ADDR(block, 2);
429 rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
430 rrec->rm_blockcount = cpu_to_be32(2);
431 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
432 rrec->rm_offset = 0;
433 be16_add_cpu(&block->bb_numrecs, 1);
434
435 /* account inode btree root blocks */
436 rrec = XFS_RMAP_REC_ADDR(block, 3);
437 rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
438 rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
439 XFS_IBT_BLOCK(mp));
440 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
441 rrec->rm_offset = 0;
442 be16_add_cpu(&block->bb_numrecs, 1);
443
444 /* account for rmap btree root */
445 rrec = XFS_RMAP_REC_ADDR(block, 4);
446 rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
447 rrec->rm_blockcount = cpu_to_be32(1);
448 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
449 rrec->rm_offset = 0;
450 be16_add_cpu(&block->bb_numrecs, 1);
451
452 error = xfs_bwrite(bp);
453 xfs_buf_relse(bp);
454 if (error)
455 goto error0;
456 }
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /*
459 * INO btree root block
460 */
Dave Chinnerfd236832012-11-12 22:53:59 +1100461 bp = xfs_growfs_get_hdr_buf(mp,
462 XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +1100463 BTOBB(mp->m_sb.sb_blocksize), 0,
464 &xfs_inobt_buf_ops);
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000465 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000466 error = -ENOMEM;
Chandra Seetharamanb5229502011-09-07 19:37:54 +0000467 goto error0;
468 }
Dave Chinnerfd236832012-11-12 22:53:59 +1100469
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500470 if (xfs_sb_version_hascrc(&mp->m_sb))
471 xfs_btree_init_block(mp, bp, XFS_IBT_CRC_MAGIC, 0, 0,
472 agno, XFS_BTREE_CRC_BLOCKS);
473 else
474 xfs_btree_init_block(mp, bp, XFS_IBT_MAGIC, 0, 0,
475 agno, 0);
Dave Chinnerb64f3a32012-11-13 16:40:27 -0600476
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000477 error = xfs_bwrite(bp);
478 xfs_buf_relse(bp);
479 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto error0;
Brian Fostera3fa5162014-04-24 16:01:39 +1000481
482 /*
483 * FINO btree root block
484 */
485 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
486 bp = xfs_growfs_get_hdr_buf(mp,
487 XFS_AGB_TO_DADDR(mp, agno, XFS_FIBT_BLOCK(mp)),
488 BTOBB(mp->m_sb.sb_blocksize), 0,
489 &xfs_inobt_buf_ops);
490 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +1000491 error = -ENOMEM;
Brian Fostera3fa5162014-04-24 16:01:39 +1000492 goto error0;
493 }
494
495 if (xfs_sb_version_hascrc(&mp->m_sb))
496 xfs_btree_init_block(mp, bp, XFS_FIBT_CRC_MAGIC,
497 0, 0, agno,
498 XFS_BTREE_CRC_BLOCKS);
499 else
500 xfs_btree_init_block(mp, bp, XFS_FIBT_MAGIC, 0,
501 0, agno, 0);
502
503 error = xfs_bwrite(bp);
504 xfs_buf_relse(bp);
505 if (error)
506 goto error0;
507 }
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 xfs_trans_agblocks_delta(tp, nfree);
511 /*
512 * There are new blocks in the old last a.g.
513 */
514 if (new) {
Darrick J. Wong340785c2016-08-03 11:33:42 +1000515 struct xfs_owner_info oinfo;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * Change the agi length.
519 */
520 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
521 if (error) {
522 goto error0;
523 }
524 ASSERT(bp);
525 agi = XFS_BUF_TO_AGI(bp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800526 be32_add_cpu(&agi->agi_length, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ASSERT(nagcount == oagcount ||
Christoph Hellwig16259e72005-11-02 15:11:25 +1100528 be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
530 /*
531 * Change agf length.
532 */
533 error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp);
534 if (error) {
535 goto error0;
536 }
537 ASSERT(bp);
538 agf = XFS_BUF_TO_AGF(bp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800539 be32_add_cpu(&agf->agf_length, new);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100540 ASSERT(be32_to_cpu(agf->agf_length) ==
541 be32_to_cpu(agi->agi_length));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000542
Tim Shimmin0164af52007-06-18 16:50:08 +1000543 xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /*
546 * Free the new space.
Darrick J. Wong340785c2016-08-03 11:33:42 +1000547 *
548 * XFS_RMAP_OWN_NULL is used here to tell the rmap btree that
549 * this doesn't actually exist in the rmap btree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000551 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_NULL);
552 error = xfs_free_extent(tp,
553 XFS_AGB_TO_FSB(mp, agno,
554 be32_to_cpu(agf->agf_length) - new),
555 new, &oinfo);
556 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000559
560 /*
561 * Update changed superblock fields transactionally. These are not
562 * seen by the rest of the world until the transaction commit applies
563 * them atomically to the superblock.
564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (nagcount > oagcount)
566 xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
567 if (nb > mp->m_sb.sb_dblocks)
568 xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
569 nb - mp->m_sb.sb_dblocks);
570 if (nfree)
571 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree);
572 if (dpct)
573 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
Christoph Hellwigf8079b82015-02-05 11:13:21 +1100574 xfs_trans_set_sync(tp);
Christoph Hellwig70393312015-06-04 13:48:08 +1000575 error = xfs_trans_commit(tp);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000576 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return error;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* New allocation groups fully initialized, so update mount struct */
580 if (nagimax)
581 mp->m_maxagi = nagimax;
582 if (mp->m_sb.sb_imax_pct) {
583 __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
584 do_div(icount, 100);
585 mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
586 } else
587 mp->m_maxicount = 0;
Dave Chinner055388a2011-01-04 11:35:03 +1100588 xfs_set_low_space_thresholds(mp);
Darrick J. Wong52548852016-08-03 11:38:24 +1000589 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000590
591 /* update secondary superblocks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 for (agno = 1; agno < nagcount; agno++) {
Dave Chinner1375cb62012-10-09 14:50:52 +1100593 error = 0;
594 /*
595 * new secondary superblocks need to be zeroed, not read from
596 * disk as the contents of the new area we are growing into is
597 * completely unknown.
598 */
599 if (agno < oagcount) {
600 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
Dave Chinner98021822012-11-12 22:54:03 +1100602 XFS_FSS_TO_BB(mp, 1), 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100603 &xfs_sb_buf_ops);
Dave Chinner1375cb62012-10-09 14:50:52 +1100604 } else {
605 bp = xfs_trans_get_buf(NULL, mp->m_ddev_targp,
606 XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
607 XFS_FSS_TO_BB(mp, 1), 0);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100608 if (bp) {
Dave Chinner1813dd62012-11-14 17:54:40 +1100609 bp->b_ops = &xfs_sb_buf_ops;
Dave Chinner1375cb62012-10-09 14:50:52 +1100610 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100611 } else
Dave Chinner24513372014-06-25 14:58:08 +1000612 error = -ENOMEM;
Dave Chinner1375cb62012-10-09 14:50:52 +1100613 }
614
Eric Sandeen59e5a0e2013-10-11 14:14:05 -0500615 /*
616 * If we get an error reading or writing alternate superblocks,
617 * continue. xfs_repair chooses the "best" superblock based
618 * on most matches; if we break early, we'll leave more
619 * superblocks un-updated than updated, and xfs_repair may
620 * pick them over the properly-updated primary.
621 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +1100623 xfs_warn(mp,
624 "error %d reading secondary superblock for ag %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 error, agno);
Eric Sandeen59e5a0e2013-10-11 14:14:05 -0500626 saved_error = error;
627 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Dave Chinner4d11a402015-01-22 09:10:26 +1100629 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
Dave Chinner98021822012-11-12 22:54:03 +1100630
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000631 error = xfs_bwrite(bp);
632 xfs_buf_relse(bp);
633 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +1100634 xfs_warn(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 "write error %d updating secondary superblock for ag %d",
636 error, agno);
Eric Sandeen59e5a0e2013-10-11 14:14:05 -0500637 saved_error = error;
638 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 }
Eric Sandeen59e5a0e2013-10-11 14:14:05 -0500641 return saved_error ? saved_error : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 error0:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000644 xfs_trans_cancel(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return error;
646}
647
648static int
649xfs_growfs_log_private(
650 xfs_mount_t *mp, /* mount point for filesystem */
651 xfs_growfs_log_t *in) /* growfs log input struct */
652{
653 xfs_extlen_t nb;
654
655 nb = in->newblocks;
656 if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
Dave Chinner24513372014-06-25 14:58:08 +1000657 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (nb == mp->m_sb.sb_logblocks &&
659 in->isint == (mp->m_sb.sb_logstart != 0))
Dave Chinner24513372014-06-25 14:58:08 +1000660 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * Moving the log is hard, need new interfaces to sync
663 * the log first, hold off all activity while moving it.
664 * Can have shorter or longer log in the same space,
665 * or transform internal to external log or vice versa.
666 */
Dave Chinner24513372014-06-25 14:58:08 +1000667 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670/*
671 * protected versions of growfs function acquire and release locks on the mount
672 * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
673 * XFS_IOC_FSGROWFSRT
674 */
675
676
677int
678xfs_growfs_data(
679 xfs_mount_t *mp,
680 xfs_growfs_data_t *in)
681{
682 int error;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600683
684 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000685 return -EPERM;
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000686 if (!mutex_trylock(&mp->m_growlock))
Dave Chinner24513372014-06-25 14:58:08 +1000687 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 error = xfs_growfs_data_private(mp, in);
Christoph Hellwig52785112015-02-16 11:49:23 +1100689 /*
690 * Increment the generation unconditionally, the error could be from
691 * updating the secondary superblocks, in which case the new size
692 * is live already.
693 */
694 mp->m_generation++;
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000695 mutex_unlock(&mp->m_growlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return error;
697}
698
699int
700xfs_growfs_log(
701 xfs_mount_t *mp,
702 xfs_growfs_log_t *in)
703{
704 int error;
sandeen@sandeen.net743bb4652008-11-25 21:20:06 -0600705
706 if (!capable(CAP_SYS_ADMIN))
Dave Chinner24513372014-06-25 14:58:08 +1000707 return -EPERM;
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000708 if (!mutex_trylock(&mp->m_growlock))
Dave Chinner24513372014-06-25 14:58:08 +1000709 return -EWOULDBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 error = xfs_growfs_log_private(mp, in);
Christoph Hellwigcc92e7a2007-08-30 17:21:54 +1000711 mutex_unlock(&mp->m_growlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return error;
713}
714
715/*
716 * exported through ioctl XFS_IOC_FSCOUNTS
717 */
718
719int
720xfs_fs_counts(
721 xfs_mount_t *mp,
722 xfs_fsop_counts_t *cnt)
723{
Dave Chinner501ab322015-02-23 21:19:28 +1100724 cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
Dave Chinnere88b64e2015-02-23 21:19:53 +1100725 cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
Dave Chinner0d485ad2015-02-23 21:22:03 +1100726 cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
Darrick J. Wong52548852016-08-03 11:38:24 +1000727 mp->m_alloc_set_aside;
Dave Chinner501ab322015-02-23 21:19:28 +1100728
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000729 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 cnt->freertx = mp->m_sb.sb_frextents;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000731 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 0;
733}
734
735/*
736 * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
737 *
738 * xfs_reserve_blocks is called to set m_resblks
739 * in the in-core mount table. The number of unused reserved blocks
Nathan Scottc41564b2006-03-29 08:55:14 +1000740 * is kept in m_resblks_avail.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 *
742 * Reserve the requested number of blocks if available. Otherwise return
743 * as many as possible to satisfy the request. The actual number
744 * reserved are returned in outval
745 *
746 * A null inval pointer indicates that only the current reserved blocks
747 * available should be returned no settings are changed.
748 */
749
750int
751xfs_reserve_blocks(
752 xfs_mount_t *mp,
753 __uint64_t *inval,
754 xfs_fsop_resblks_t *outval)
755{
Brian Foster408fd482016-06-21 11:53:28 +1000756 __int64_t lcounter, delta;
757 __int64_t fdblks_delta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 __uint64_t request;
Brian Foster408fd482016-06-21 11:53:28 +1000759 __int64_t free;
760 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* If inval is null, report current values and return */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (inval == (__uint64_t *)NULL) {
David Chinner84e1e992007-06-18 16:50:27 +1000764 if (!outval)
Dave Chinner24513372014-06-25 14:58:08 +1000765 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 outval->resblks = mp->m_resblks;
767 outval->resblks_avail = mp->m_resblks_avail;
Jesper Juhl014c2542006-01-15 02:37:08 +0100768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
771 request = *inval;
David Chinnerdbcabad2007-02-10 18:36:17 +1100772
773 /*
Brian Foster408fd482016-06-21 11:53:28 +1000774 * With per-cpu counters, this becomes an interesting problem. we need
775 * to work out if we are freeing or allocation blocks first, then we can
776 * do the modification as necessary.
David Chinnerdbcabad2007-02-10 18:36:17 +1100777 *
Brian Foster408fd482016-06-21 11:53:28 +1000778 * We do this under the m_sb_lock so that if we are near ENOSPC, we will
779 * hold out any changes while we work out what to do. This means that
780 * the amount of free space can change while we do this, so we need to
781 * retry if we end up trying to reserve more space than is available.
David Chinnerdbcabad2007-02-10 18:36:17 +1100782 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000783 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /*
786 * If our previous reservation was larger than the current value,
Brian Foster408fd482016-06-21 11:53:28 +1000787 * then move any unused blocks back to the free pool. Modify the resblks
788 * counters directly since we shouldn't have any problems unreserving
789 * space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (mp->m_resblks > request) {
792 lcounter = mp->m_resblks_avail - request;
793 if (lcounter > 0) { /* release unused blocks */
David Chinnerdbcabad2007-02-10 18:36:17 +1100794 fdblks_delta = lcounter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 mp->m_resblks_avail -= lcounter;
796 }
797 mp->m_resblks = request;
Brian Foster408fd482016-06-21 11:53:28 +1000798 if (fdblks_delta) {
799 spin_unlock(&mp->m_sb_lock);
800 error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
801 spin_lock(&mp->m_sb_lock);
802 }
David Chinner4be536d2006-09-07 14:26:50 +1000803
Brian Foster408fd482016-06-21 11:53:28 +1000804 goto out;
805 }
806
807 /*
808 * If the request is larger than the current reservation, reserve the
809 * blocks before we update the reserve counters. Sample m_fdblocks and
810 * perform a partial reservation if the request exceeds free space.
811 */
812 error = -ENOSPC;
813 do {
Dave Chinner0d485ad2015-02-23 21:22:03 +1100814 free = percpu_counter_sum(&mp->m_fdblocks) -
Darrick J. Wong52548852016-08-03 11:38:24 +1000815 mp->m_alloc_set_aside;
David Chinnerdbcabad2007-02-10 18:36:17 +1100816 if (!free)
Brian Foster408fd482016-06-21 11:53:28 +1000817 break;
David Chinnerdbcabad2007-02-10 18:36:17 +1100818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 delta = request - mp->m_resblks;
David Chinner4be536d2006-09-07 14:26:50 +1000820 lcounter = free - delta;
Brian Foster408fd482016-06-21 11:53:28 +1000821 if (lcounter < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* We can't satisfy the request, just get what we can */
Brian Foster408fd482016-06-21 11:53:28 +1000823 fdblks_delta = free;
824 else
825 fdblks_delta = delta;
826
827 /*
828 * We'll either succeed in getting space from the free block
829 * count or we'll get an ENOSPC. If we get a ENOSPC, it means
830 * things changed while we were calculating fdblks_delta and so
831 * we should try again to see if there is anything left to
832 * reserve.
833 *
834 * Don't set the reserved flag here - we don't want to reserve
835 * the extra reserve blocks from the reserve.....
836 */
837 spin_unlock(&mp->m_sb_lock);
838 error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
839 spin_lock(&mp->m_sb_lock);
840 } while (error == -ENOSPC);
841
842 /*
843 * Update the reserve counters if blocks have been successfully
844 * allocated.
845 */
846 if (!error && fdblks_delta) {
847 mp->m_resblks += fdblks_delta;
848 mp->m_resblks_avail += fdblks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Brian Foster408fd482016-06-21 11:53:28 +1000850
David Chinnerdbcabad2007-02-10 18:36:17 +1100851out:
David Chinner84e1e992007-06-18 16:50:27 +1000852 if (outval) {
853 outval->resblks = mp->m_resblks;
854 outval->resblks_avail = mp->m_resblks_avail;
855 }
David Chinnerdbcabad2007-02-10 18:36:17 +1100856
Brian Foster408fd482016-06-21 11:53:28 +1000857 spin_unlock(&mp->m_sb_lock);
858 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861int
862xfs_fs_goingdown(
863 xfs_mount_t *mp,
864 __uint32_t inflags)
865{
866 switch (inflags) {
867 case XFS_FSOP_GOING_FLAGS_DEFAULT: {
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000868 struct super_block *sb = freeze_bdev(mp->m_super->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Christoph Hellwigf33c6792005-11-25 16:41:47 +1100870 if (sb && !IS_ERR(sb)) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000871 xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 thaw_bdev(sb->s_bdev, sb);
873 }
Barry Naujok189f4bf2008-05-21 16:58:55 +1000874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 break;
876 }
877 case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
Nathan Scott7d04a332006-06-09 14:58:38 +1000878 xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 break;
880 case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
Nathan Scott7d04a332006-06-09 14:58:38 +1000881 xfs_force_shutdown(mp,
882 SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884 default:
Dave Chinner24513372014-06-25 14:58:08 +1000885 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
888 return 0;
889}
Dave Chinner2af51f32012-04-23 15:59:03 +1000890
891/*
892 * Force a shutdown of the filesystem instantly while keeping the filesystem
893 * consistent. We don't do an unmount here; just shutdown the shop, make sure
894 * that absolutely nothing persistent happens to this filesystem after this
895 * point.
896 */
897void
898xfs_do_force_shutdown(
899 xfs_mount_t *mp,
900 int flags,
901 char *fname,
902 int lnnum)
903{
904 int logerror;
905
906 logerror = flags & SHUTDOWN_LOG_IO_ERROR;
907
908 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
909 xfs_notice(mp,
910 "%s(0x%x) called from line %d of file %s. Return address = 0x%p",
911 __func__, flags, lnnum, fname, __return_address);
912 }
913 /*
914 * No need to duplicate efforts.
915 */
916 if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
917 return;
918
919 /*
920 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
921 * queue up anybody new on the log reservations, and wakes up
922 * everybody who's sleeping on log reservations to tell them
923 * the bad news.
924 */
925 if (xfs_log_force_umount(mp, logerror))
926 return;
927
928 if (flags & SHUTDOWN_CORRUPT_INCORE) {
929 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT,
930 "Corruption of in-memory data detected. Shutting down filesystem");
931 if (XFS_ERRLEVEL_HIGH <= xfs_error_level)
932 xfs_stack_trace();
933 } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
934 if (logerror) {
935 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR,
936 "Log I/O Error Detected. Shutting down filesystem");
937 } else if (flags & SHUTDOWN_DEVICE_REQ) {
938 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
939 "All device paths lost. Shutting down filesystem");
940 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
941 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
942 "I/O Error Detected. Shutting down filesystem");
943 }
944 }
945 if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
946 xfs_alert(mp,
947 "Please umount the filesystem and rectify the problem(s)");
948 }
949}