]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/xfs/xfs_fsops.c
xfs: rework secondary superblock updates in growfs
[linux.git] / fs / xfs / xfs_fsops.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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
7  * published by the Free Software Foundation.
8  *
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.
13  *
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
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_sb.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_inode.h"
30 #include "xfs_trans.h"
31 #include "xfs_inode_item.h"
32 #include "xfs_error.h"
33 #include "xfs_btree.h"
34 #include "xfs_alloc_btree.h"
35 #include "xfs_alloc.h"
36 #include "xfs_rmap_btree.h"
37 #include "xfs_ialloc.h"
38 #include "xfs_fsops.h"
39 #include "xfs_itable.h"
40 #include "xfs_trans_space.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_trace.h"
43 #include "xfs_log.h"
44 #include "xfs_filestream.h"
45 #include "xfs_rmap.h"
46 #include "xfs_ag_resv.h"
47
48 /*
49  * File system operations
50  */
51
52 static struct xfs_buf *
53 xfs_growfs_get_hdr_buf(
54         struct xfs_mount        *mp,
55         xfs_daddr_t             blkno,
56         size_t                  numblks,
57         int                     flags,
58         const struct xfs_buf_ops *ops)
59 {
60         struct xfs_buf          *bp;
61
62         bp = xfs_buf_get_uncached(mp->m_ddev_targp, numblks, flags);
63         if (!bp)
64                 return NULL;
65
66         xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
67         bp->b_bn = blkno;
68         bp->b_maps[0].bm_bn = blkno;
69         bp->b_ops = ops;
70
71         return bp;
72 }
73
74 struct aghdr_init_data {
75         /* per ag data */
76         xfs_agnumber_t          agno;           /* ag to init */
77         xfs_extlen_t            agsize;         /* new AG size */
78         struct list_head        buffer_list;    /* buffer writeback list */
79         xfs_rfsblock_t          nfree;          /* cumulative new free space */
80
81         /* per header data */
82         xfs_daddr_t             daddr;          /* header location */
83         size_t                  numblks;        /* size of header */
84         xfs_btnum_t             type;           /* type of btree root block */
85 };
86
87 /*
88  * Generic btree root block init function
89  */
90 static void
91 xfs_btroot_init(
92         struct xfs_mount        *mp,
93         struct xfs_buf          *bp,
94         struct aghdr_init_data  *id)
95 {
96         xfs_btree_init_block(mp, bp, id->type, 0, 0, id->agno, 0);
97 }
98
99 /*
100  * Alloc btree root block init functions
101  */
102 static void
103 xfs_bnoroot_init(
104         struct xfs_mount        *mp,
105         struct xfs_buf          *bp,
106         struct aghdr_init_data  *id)
107 {
108         struct xfs_alloc_rec    *arec;
109
110         xfs_btree_init_block(mp, bp, XFS_BTNUM_BNO, 0, 1, id->agno, 0);
111         arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
112         arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
113         arec->ar_blockcount = cpu_to_be32(id->agsize -
114                                           be32_to_cpu(arec->ar_startblock));
115 }
116
117 static void
118 xfs_cntroot_init(
119         struct xfs_mount        *mp,
120         struct xfs_buf          *bp,
121         struct aghdr_init_data  *id)
122 {
123         struct xfs_alloc_rec    *arec;
124
125         xfs_btree_init_block(mp, bp, XFS_BTNUM_CNT, 0, 1, id->agno, 0);
126         arec = XFS_ALLOC_REC_ADDR(mp, XFS_BUF_TO_BLOCK(bp), 1);
127         arec->ar_startblock = cpu_to_be32(mp->m_ag_prealloc_blocks);
128         arec->ar_blockcount = cpu_to_be32(id->agsize -
129                                           be32_to_cpu(arec->ar_startblock));
130 }
131
132 /*
133  * Reverse map root block init
134  */
135 static void
136 xfs_rmaproot_init(
137         struct xfs_mount        *mp,
138         struct xfs_buf          *bp,
139         struct aghdr_init_data  *id)
140 {
141         struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
142         struct xfs_rmap_rec     *rrec;
143
144         xfs_btree_init_block(mp, bp, XFS_BTNUM_RMAP, 0, 4, id->agno, 0);
145
146         /*
147          * mark the AG header regions as static metadata The BNO
148          * btree block is the first block after the headers, so
149          * it's location defines the size of region the static
150          * metadata consumes.
151          *
152          * Note: unlike mkfs, we never have to account for log
153          * space when growing the data regions
154          */
155         rrec = XFS_RMAP_REC_ADDR(block, 1);
156         rrec->rm_startblock = 0;
157         rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
158         rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
159         rrec->rm_offset = 0;
160
161         /* account freespace btree root blocks */
162         rrec = XFS_RMAP_REC_ADDR(block, 2);
163         rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
164         rrec->rm_blockcount = cpu_to_be32(2);
165         rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
166         rrec->rm_offset = 0;
167
168         /* account inode btree root blocks */
169         rrec = XFS_RMAP_REC_ADDR(block, 3);
170         rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
171         rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
172                                           XFS_IBT_BLOCK(mp));
173         rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
174         rrec->rm_offset = 0;
175
176         /* account for rmap btree root */
177         rrec = XFS_RMAP_REC_ADDR(block, 4);
178         rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
179         rrec->rm_blockcount = cpu_to_be32(1);
180         rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
181         rrec->rm_offset = 0;
182
183         /* account for refc btree root */
184         if (xfs_sb_version_hasreflink(&mp->m_sb)) {
185                 rrec = XFS_RMAP_REC_ADDR(block, 5);
186                 rrec->rm_startblock = cpu_to_be32(xfs_refc_block(mp));
187                 rrec->rm_blockcount = cpu_to_be32(1);
188                 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
189                 rrec->rm_offset = 0;
190                 be16_add_cpu(&block->bb_numrecs, 1);
191         }
192 }
193
194 /*
195  * Initialise new secondary superblocks with the pre-grow geometry, but mark
196  * them as "in progress" so we know they haven't yet been activated. This will
197  * get cleared when the update with the new geometry information is done after
198  * changes to the primary are committed. This isn't strictly necessary, but we
199  * get it for free with the delayed buffer write lists and it means we can tell
200  * if a grow operation didn't complete properly after the fact.
201  */
202 static void
203 xfs_sbblock_init(
204         struct xfs_mount        *mp,
205         struct xfs_buf          *bp,
206         struct aghdr_init_data  *id)
207 {
208         struct xfs_dsb          *dsb = XFS_BUF_TO_SBP(bp);
209
210         xfs_sb_to_disk(dsb, &mp->m_sb);
211         dsb->sb_inprogress = 1;
212 }
213
214 static void
215 xfs_agfblock_init(
216         struct xfs_mount        *mp,
217         struct xfs_buf          *bp,
218         struct aghdr_init_data  *id)
219 {
220         struct xfs_agf          *agf = XFS_BUF_TO_AGF(bp);
221         xfs_extlen_t            tmpsize;
222
223         agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
224         agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
225         agf->agf_seqno = cpu_to_be32(id->agno);
226         agf->agf_length = cpu_to_be32(id->agsize);
227         agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
228         agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
229         agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
230         agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
231         if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
232                 agf->agf_roots[XFS_BTNUM_RMAPi] =
233                                         cpu_to_be32(XFS_RMAP_BLOCK(mp));
234                 agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
235                 agf->agf_rmap_blocks = cpu_to_be32(1);
236         }
237
238         agf->agf_flfirst = cpu_to_be32(1);
239         agf->agf_fllast = 0;
240         agf->agf_flcount = 0;
241         tmpsize = id->agsize - mp->m_ag_prealloc_blocks;
242         agf->agf_freeblks = cpu_to_be32(tmpsize);
243         agf->agf_longest = cpu_to_be32(tmpsize);
244         if (xfs_sb_version_hascrc(&mp->m_sb))
245                 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
246         if (xfs_sb_version_hasreflink(&mp->m_sb)) {
247                 agf->agf_refcount_root = cpu_to_be32(
248                                 xfs_refc_block(mp));
249                 agf->agf_refcount_level = cpu_to_be32(1);
250                 agf->agf_refcount_blocks = cpu_to_be32(1);
251         }
252 }
253
254 static void
255 xfs_agflblock_init(
256         struct xfs_mount        *mp,
257         struct xfs_buf          *bp,
258         struct aghdr_init_data  *id)
259 {
260         struct xfs_agfl         *agfl = XFS_BUF_TO_AGFL(bp);
261         __be32                  *agfl_bno;
262         int                     bucket;
263
264         if (xfs_sb_version_hascrc(&mp->m_sb)) {
265                 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
266                 agfl->agfl_seqno = cpu_to_be32(id->agno);
267                 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
268         }
269
270         agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, bp);
271         for (bucket = 0; bucket < xfs_agfl_size(mp); bucket++)
272                 agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
273 }
274
275 static void
276 xfs_agiblock_init(
277         struct xfs_mount        *mp,
278         struct xfs_buf          *bp,
279         struct aghdr_init_data  *id)
280 {
281         struct xfs_agi          *agi = XFS_BUF_TO_AGI(bp);
282         int                     bucket;
283
284         agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
285         agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
286         agi->agi_seqno = cpu_to_be32(id->agno);
287         agi->agi_length = cpu_to_be32(id->agsize);
288         agi->agi_count = 0;
289         agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
290         agi->agi_level = cpu_to_be32(1);
291         agi->agi_freecount = 0;
292         agi->agi_newino = cpu_to_be32(NULLAGINO);
293         agi->agi_dirino = cpu_to_be32(NULLAGINO);
294         if (xfs_sb_version_hascrc(&mp->m_sb))
295                 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
296         if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
297                 agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
298                 agi->agi_free_level = cpu_to_be32(1);
299         }
300         for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
301                 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
302 }
303
304 typedef void (*aghdr_init_work_f)(struct xfs_mount *mp, struct xfs_buf *bp,
305                                   struct aghdr_init_data *id);
306 static int
307 xfs_growfs_init_aghdr(
308         struct xfs_mount        *mp,
309         struct aghdr_init_data  *id,
310         aghdr_init_work_f       work,
311         const struct xfs_buf_ops *ops)
312
313 {
314         struct xfs_buf          *bp;
315
316         bp = xfs_growfs_get_hdr_buf(mp, id->daddr, id->numblks, 0, ops);
317         if (!bp)
318                 return -ENOMEM;
319
320         (*work)(mp, bp, id);
321
322         xfs_buf_delwri_queue(bp, &id->buffer_list);
323         xfs_buf_relse(bp);
324         return 0;
325 }
326
327 struct xfs_aghdr_grow_data {
328         xfs_daddr_t             daddr;
329         size_t                  numblks;
330         const struct xfs_buf_ops *ops;
331         aghdr_init_work_f       work;
332         xfs_btnum_t             type;
333         bool                    need_init;
334 };
335
336 /*
337  * Write new AG headers to disk. Non-transactional, but written
338  * synchronously so they are completed prior to the growfs transaction
339  * being logged.
340  */
341 static int
342 xfs_grow_ag_headers(
343         struct xfs_mount        *mp,
344         struct aghdr_init_data  *id)
345
346 {
347         struct xfs_aghdr_grow_data aghdr_data[] = {
348         { /* SB */
349                 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_SB_DADDR),
350                 .numblks = XFS_FSS_TO_BB(mp, 1),
351                 .ops = &xfs_sb_buf_ops,
352                 .work = &xfs_sbblock_init,
353                 .need_init = true
354         },
355         { /* AGF */
356                 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGF_DADDR(mp)),
357                 .numblks = XFS_FSS_TO_BB(mp, 1),
358                 .ops = &xfs_agf_buf_ops,
359                 .work = &xfs_agfblock_init,
360                 .need_init = true
361         },
362         { /* AGFL */
363                 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGFL_DADDR(mp)),
364                 .numblks = XFS_FSS_TO_BB(mp, 1),
365                 .ops = &xfs_agfl_buf_ops,
366                 .work = &xfs_agflblock_init,
367                 .need_init = true
368         },
369         { /* AGI */
370                 .daddr = XFS_AG_DADDR(mp, id->agno, XFS_AGI_DADDR(mp)),
371                 .numblks = XFS_FSS_TO_BB(mp, 1),
372                 .ops = &xfs_agi_buf_ops,
373                 .work = &xfs_agiblock_init,
374                 .need_init = true
375         },
376         { /* BNO root block */
377                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_BNO_BLOCK(mp)),
378                 .numblks = BTOBB(mp->m_sb.sb_blocksize),
379                 .ops = &xfs_allocbt_buf_ops,
380                 .work = &xfs_bnoroot_init,
381                 .need_init = true
382         },
383         { /* CNT root block */
384                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_CNT_BLOCK(mp)),
385                 .numblks = BTOBB(mp->m_sb.sb_blocksize),
386                 .ops = &xfs_allocbt_buf_ops,
387                 .work = &xfs_cntroot_init,
388                 .need_init = true
389         },
390         { /* INO root block */
391                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_IBT_BLOCK(mp)),
392                 .numblks = BTOBB(mp->m_sb.sb_blocksize),
393                 .ops = &xfs_inobt_buf_ops,
394                 .work = &xfs_btroot_init,
395                 .type = XFS_BTNUM_INO,
396                 .need_init = true
397         },
398         { /* FINO root block */
399                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_FIBT_BLOCK(mp)),
400                 .numblks = BTOBB(mp->m_sb.sb_blocksize),
401                 .ops = &xfs_inobt_buf_ops,
402                 .work = &xfs_btroot_init,
403                 .type = XFS_BTNUM_FINO,
404                 .need_init =  xfs_sb_version_hasfinobt(&mp->m_sb)
405         },
406         { /* RMAP root block */
407                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, XFS_RMAP_BLOCK(mp)),
408                 .numblks = BTOBB(mp->m_sb.sb_blocksize),
409                 .ops = &xfs_rmapbt_buf_ops,
410                 .work = &xfs_rmaproot_init,
411                 .need_init = xfs_sb_version_hasrmapbt(&mp->m_sb)
412         },
413         { /* REFC root block */
414                 .daddr = XFS_AGB_TO_DADDR(mp, id->agno, xfs_refc_block(mp)),
415                 .numblks = BTOBB(mp->m_sb.sb_blocksize),
416                 .ops = &xfs_refcountbt_buf_ops,
417                 .work = &xfs_btroot_init,
418                 .type = XFS_BTNUM_REFC,
419                 .need_init = xfs_sb_version_hasreflink(&mp->m_sb)
420         },
421         { /* NULL terminating block */
422                 .daddr = XFS_BUF_DADDR_NULL,
423         }
424         };
425         struct  xfs_aghdr_grow_data *dp;
426         int                     error = 0;
427
428         /* Account for AG free space in new AG */
429         id->nfree += id->agsize - mp->m_ag_prealloc_blocks;
430         for (dp = &aghdr_data[0]; dp->daddr != XFS_BUF_DADDR_NULL; dp++) {
431                 if (!dp->need_init)
432                         continue;
433
434                 id->daddr = dp->daddr;
435                 id->numblks = dp->numblks;
436                 id->type = dp->type;
437                 error = xfs_growfs_init_aghdr(mp, id, dp->work, dp->ops);
438                 if (error)
439                         break;
440         }
441         return error;
442 }
443
444 static int
445 xfs_growfs_data_private(
446         xfs_mount_t             *mp,            /* mount point for filesystem */
447         xfs_growfs_data_t       *in)            /* growfs data input struct */
448 {
449         xfs_agf_t               *agf;
450         xfs_agi_t               *agi;
451         xfs_buf_t               *bp;
452         int                     error;
453         xfs_agnumber_t          nagcount;
454         xfs_agnumber_t          nagimax = 0;
455         xfs_rfsblock_t          nb, nb_mod;
456         xfs_rfsblock_t          new;
457         xfs_agnumber_t          oagcount;
458         xfs_trans_t             *tp;
459         LIST_HEAD               (buffer_list);
460         struct aghdr_init_data  id = {};
461
462         nb = in->newblocks;
463         if (nb < mp->m_sb.sb_dblocks)
464                 return -EINVAL;
465         if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb)))
466                 return error;
467         error = xfs_buf_read_uncached(mp->m_ddev_targp,
468                                 XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
469                                 XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
470         if (error)
471                 return error;
472         xfs_buf_relse(bp);
473
474         new = nb;       /* use new as a temporary here */
475         nb_mod = do_div(new, mp->m_sb.sb_agblocks);
476         nagcount = new + (nb_mod != 0);
477         if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
478                 nagcount--;
479                 nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
480                 if (nb < mp->m_sb.sb_dblocks)
481                         return -EINVAL;
482         }
483         new = nb - mp->m_sb.sb_dblocks;
484         oagcount = mp->m_sb.sb_agcount;
485
486         /* allocate the new per-ag structures */
487         if (nagcount > oagcount) {
488                 error = xfs_initialize_perag(mp, nagcount, &nagimax);
489                 if (error)
490                         return error;
491         }
492
493         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
494                         XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
495         if (error)
496                 return error;
497
498         /*
499          * Write new AG headers to disk. Non-transactional, but need to be
500          * written and completed prior to the growfs transaction being logged.
501          * To do this, we use a delayed write buffer list and wait for
502          * submission and IO completion of the list as a whole. This allows the
503          * IO subsystem to merge all the AG headers in a single AG into a single
504          * IO and hide most of the latency of the IO from us.
505          *
506          * This also means that if we get an error whilst building the buffer
507          * list to write, we can cancel the entire list without having written
508          * anything.
509          */
510         INIT_LIST_HEAD(&id.buffer_list);
511         for (id.agno = nagcount - 1;
512              id.agno >= oagcount;
513              id.agno--, new -= id.agsize) {
514
515                 if (id.agno == nagcount - 1)
516                         id.agsize = nb -
517                                 (id.agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
518                 else
519                         id.agsize = mp->m_sb.sb_agblocks;
520
521                 error = xfs_grow_ag_headers(mp, &id);
522                 if (error) {
523                         xfs_buf_delwri_cancel(&id.buffer_list);
524                         goto out_trans_cancel;
525                 }
526         }
527         error = xfs_buf_delwri_submit(&id.buffer_list);
528         if (error)
529                 goto out_trans_cancel;
530
531         xfs_trans_agblocks_delta(tp, id.nfree);
532
533         /*
534          * There are new blocks in the old last a.g.
535          */
536         if (new) {
537                 struct xfs_owner_info   oinfo;
538
539                 /*
540                  * Change the agi length.
541                  */
542                 error = xfs_ialloc_read_agi(mp, tp, id.agno, &bp);
543                 if (error)
544                         goto out_trans_cancel;
545
546                 ASSERT(bp);
547                 agi = XFS_BUF_TO_AGI(bp);
548                 be32_add_cpu(&agi->agi_length, new);
549                 ASSERT(nagcount == oagcount ||
550                        be32_to_cpu(agi->agi_length) == mp->m_sb.sb_agblocks);
551                 xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
552
553                 /*
554                  * Change agf length.
555                  */
556                 error = xfs_alloc_read_agf(mp, tp, id.agno, 0, &bp);
557                 if (error)
558                         goto out_trans_cancel;
559
560                 ASSERT(bp);
561                 agf = XFS_BUF_TO_AGF(bp);
562                 be32_add_cpu(&agf->agf_length, new);
563                 ASSERT(be32_to_cpu(agf->agf_length) ==
564                        be32_to_cpu(agi->agi_length));
565
566                 xfs_alloc_log_agf(tp, bp, XFS_AGF_LENGTH);
567
568                 /*
569                  * Free the new space.
570                  *
571                  * XFS_RMAP_OWN_NULL is used here to tell the rmap btree that
572                  * this doesn't actually exist in the rmap btree.
573                  */
574                 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_NULL);
575                 error = xfs_rmap_free(tp, bp, id.agno,
576                                 be32_to_cpu(agf->agf_length) - new,
577                                 new, &oinfo);
578                 if (error)
579                         goto out_trans_cancel;
580                 error = xfs_free_extent(tp,
581                                 XFS_AGB_TO_FSB(mp, id.agno,
582                                         be32_to_cpu(agf->agf_length) - new),
583                                 new, &oinfo, XFS_AG_RESV_NONE);
584                 if (error)
585                         goto out_trans_cancel;
586         }
587
588         /*
589          * Update changed superblock fields transactionally. These are not
590          * seen by the rest of the world until the transaction commit applies
591          * them atomically to the superblock.
592          */
593         if (nagcount > oagcount)
594                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
595         if (nb > mp->m_sb.sb_dblocks)
596                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
597                                  nb - mp->m_sb.sb_dblocks);
598         if (id.nfree)
599                 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree);
600         xfs_trans_set_sync(tp);
601         error = xfs_trans_commit(tp);
602         if (error)
603                 return error;
604
605         /* New allocation groups fully initialized, so update mount struct */
606         if (nagimax)
607                 mp->m_maxagi = nagimax;
608         xfs_set_low_space_thresholds(mp);
609         mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
610
611         /*
612          * If we expanded the last AG, free the per-AG reservation
613          * so we can reinitialize it with the new size.
614          */
615         if (new) {
616                 struct xfs_perag        *pag;
617
618                 pag = xfs_perag_get(mp, id.agno);
619                 error = xfs_ag_resv_free(pag);
620                 xfs_perag_put(pag);
621                 if (error)
622                         return error;
623         }
624
625         /*
626          * Reserve AG metadata blocks. ENOSPC here does not mean there was a
627          * growfs failure, just that there still isn't space for new user data
628          * after the grow has been run.
629          */
630         error = xfs_fs_reserve_ag_blocks(mp);
631         if (error == -ENOSPC)
632                 error = 0;
633         return error;
634
635 out_trans_cancel:
636         xfs_trans_cancel(tp);
637         return error;
638 }
639
640 static int
641 xfs_growfs_log_private(
642         xfs_mount_t             *mp,    /* mount point for filesystem */
643         xfs_growfs_log_t        *in)    /* growfs log input struct */
644 {
645         xfs_extlen_t            nb;
646
647         nb = in->newblocks;
648         if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
649                 return -EINVAL;
650         if (nb == mp->m_sb.sb_logblocks &&
651             in->isint == (mp->m_sb.sb_logstart != 0))
652                 return -EINVAL;
653         /*
654          * Moving the log is hard, need new interfaces to sync
655          * the log first, hold off all activity while moving it.
656          * Can have shorter or longer log in the same space,
657          * or transform internal to external log or vice versa.
658          */
659         return -ENOSYS;
660 }
661
662 static int
663 xfs_growfs_imaxpct(
664         struct xfs_mount        *mp,
665         __u32                   imaxpct)
666 {
667         struct xfs_trans        *tp;
668         int                     dpct;
669         int                     error;
670
671         if (imaxpct > 100)
672                 return -EINVAL;
673
674         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
675                         XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
676         if (error)
677                 return error;
678
679         dpct = imaxpct - mp->m_sb.sb_imax_pct;
680         xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
681         xfs_trans_set_sync(tp);
682         return xfs_trans_commit(tp);
683 }
684
685 /*
686  * After a grow operation, we need to update all the secondary superblocks
687  * to match the new state of the primary. Because we are completely overwriting
688  * all the existing fields in the secondary superblock buffers, there is no need
689  * to read them in from disk. Just get a new buffer, stamp it and write it.
690  *
691  * The sb buffers need to be cached here so that we serialise against scrub
692  * scanning secondary superblocks, but we don't want to keep it in memory once
693  * it is written so we mark it as a one-shot buffer.
694  */
695 static int
696 xfs_growfs_update_superblocks(
697         struct xfs_mount        *mp)
698 {
699         xfs_agnumber_t          agno;
700         int                     saved_error = 0;
701         int                     error = 0;
702         LIST_HEAD               (buffer_list);
703
704         /* update secondary superblocks. */
705         for (agno = 1; agno < mp->m_sb.sb_agcount; agno++) {
706                 struct xfs_buf          *bp;
707
708                 bp = xfs_buf_get(mp->m_ddev_targp,
709                                  XFS_AG_DADDR(mp, agno, XFS_SB_DADDR),
710                                  XFS_FSS_TO_BB(mp, 1), 0);
711                 /*
712                  * If we get an error reading or writing alternate superblocks,
713                  * continue.  xfs_repair chooses the "best" superblock based
714                  * on most matches; if we break early, we'll leave more
715                  * superblocks un-updated than updated, and xfs_repair may
716                  * pick them over the properly-updated primary.
717                  */
718                 if (!bp) {
719                         xfs_warn(mp,
720                 "error allocating secondary superblock for ag %d",
721                                 agno);
722                         if (!saved_error)
723                                 saved_error = -ENOMEM;
724                         continue;
725                 }
726
727                 bp->b_ops = &xfs_sb_buf_ops;
728                 xfs_buf_oneshot(bp);
729                 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
730                 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
731                 xfs_buf_delwri_queue(bp, &buffer_list);
732                 xfs_buf_relse(bp);
733
734                 /* don't hold too many buffers at once */
735                 if (agno % 16)
736                         continue;
737
738                 error = xfs_buf_delwri_submit(&buffer_list);
739                 if (error) {
740                         xfs_warn(mp,
741                 "write error %d updating a secondary superblock near ag %d",
742                                 error, agno);
743                         if (!saved_error)
744                                 saved_error = error;
745                         continue;
746                 }
747         }
748         error = xfs_buf_delwri_submit(&buffer_list);
749         if (error) {
750                 xfs_warn(mp,
751                 "write error %d updating a secondary superblock near ag %d",
752                         error, agno);
753         }
754
755         return saved_error ? saved_error : error;
756 }
757
758 /*
759  * protected versions of growfs function acquire and release locks on the mount
760  * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
761  * XFS_IOC_FSGROWFSRT
762  */
763 int
764 xfs_growfs_data(
765         struct xfs_mount        *mp,
766         struct xfs_growfs_data  *in)
767 {
768         int                     error = 0;
769
770         if (!capable(CAP_SYS_ADMIN))
771                 return -EPERM;
772         if (!mutex_trylock(&mp->m_growlock))
773                 return -EWOULDBLOCK;
774
775         /* update imaxpct separately to the physical grow of the filesystem */
776         if (in->imaxpct != mp->m_sb.sb_imax_pct) {
777                 error = xfs_growfs_imaxpct(mp, in->imaxpct);
778                 if (error)
779                         goto out_error;
780         }
781
782         if (in->newblocks != mp->m_sb.sb_dblocks) {
783                 error = xfs_growfs_data_private(mp, in);
784                 if (error)
785                         goto out_error;
786         }
787
788         /* Post growfs calculations needed to reflect new state in operations */
789         if (mp->m_sb.sb_imax_pct) {
790                 uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
791                 do_div(icount, 100);
792                 mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
793         } else
794                 mp->m_maxicount = 0;
795
796         /* Update secondary superblocks now the physical grow has completed */
797         error = xfs_growfs_update_superblocks(mp);
798
799 out_error:
800         /*
801          * Increment the generation unconditionally, the error could be from
802          * updating the secondary superblocks, in which case the new size
803          * is live already.
804          */
805         mp->m_generation++;
806         mutex_unlock(&mp->m_growlock);
807         return error;
808 }
809
810 int
811 xfs_growfs_log(
812         xfs_mount_t             *mp,
813         xfs_growfs_log_t        *in)
814 {
815         int error;
816
817         if (!capable(CAP_SYS_ADMIN))
818                 return -EPERM;
819         if (!mutex_trylock(&mp->m_growlock))
820                 return -EWOULDBLOCK;
821         error = xfs_growfs_log_private(mp, in);
822         mutex_unlock(&mp->m_growlock);
823         return error;
824 }
825
826 /*
827  * exported through ioctl XFS_IOC_FSCOUNTS
828  */
829
830 int
831 xfs_fs_counts(
832         xfs_mount_t             *mp,
833         xfs_fsop_counts_t       *cnt)
834 {
835         cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
836         cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
837         cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
838                                                 mp->m_alloc_set_aside;
839
840         spin_lock(&mp->m_sb_lock);
841         cnt->freertx = mp->m_sb.sb_frextents;
842         spin_unlock(&mp->m_sb_lock);
843         return 0;
844 }
845
846 /*
847  * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
848  *
849  * xfs_reserve_blocks is called to set m_resblks
850  * in the in-core mount table. The number of unused reserved blocks
851  * is kept in m_resblks_avail.
852  *
853  * Reserve the requested number of blocks if available. Otherwise return
854  * as many as possible to satisfy the request. The actual number
855  * reserved are returned in outval
856  *
857  * A null inval pointer indicates that only the current reserved blocks
858  * available  should  be returned no settings are changed.
859  */
860
861 int
862 xfs_reserve_blocks(
863         xfs_mount_t             *mp,
864         uint64_t              *inval,
865         xfs_fsop_resblks_t      *outval)
866 {
867         int64_t                 lcounter, delta;
868         int64_t                 fdblks_delta = 0;
869         uint64_t                request;
870         int64_t                 free;
871         int                     error = 0;
872
873         /* If inval is null, report current values and return */
874         if (inval == (uint64_t *)NULL) {
875                 if (!outval)
876                         return -EINVAL;
877                 outval->resblks = mp->m_resblks;
878                 outval->resblks_avail = mp->m_resblks_avail;
879                 return 0;
880         }
881
882         request = *inval;
883
884         /*
885          * With per-cpu counters, this becomes an interesting problem. we need
886          * to work out if we are freeing or allocation blocks first, then we can
887          * do the modification as necessary.
888          *
889          * We do this under the m_sb_lock so that if we are near ENOSPC, we will
890          * hold out any changes while we work out what to do. This means that
891          * the amount of free space can change while we do this, so we need to
892          * retry if we end up trying to reserve more space than is available.
893          */
894         spin_lock(&mp->m_sb_lock);
895
896         /*
897          * If our previous reservation was larger than the current value,
898          * then move any unused blocks back to the free pool. Modify the resblks
899          * counters directly since we shouldn't have any problems unreserving
900          * space.
901          */
902         if (mp->m_resblks > request) {
903                 lcounter = mp->m_resblks_avail - request;
904                 if (lcounter  > 0) {            /* release unused blocks */
905                         fdblks_delta = lcounter;
906                         mp->m_resblks_avail -= lcounter;
907                 }
908                 mp->m_resblks = request;
909                 if (fdblks_delta) {
910                         spin_unlock(&mp->m_sb_lock);
911                         error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
912                         spin_lock(&mp->m_sb_lock);
913                 }
914
915                 goto out;
916         }
917
918         /*
919          * If the request is larger than the current reservation, reserve the
920          * blocks before we update the reserve counters. Sample m_fdblocks and
921          * perform a partial reservation if the request exceeds free space.
922          */
923         error = -ENOSPC;
924         do {
925                 free = percpu_counter_sum(&mp->m_fdblocks) -
926                                                 mp->m_alloc_set_aside;
927                 if (!free)
928                         break;
929
930                 delta = request - mp->m_resblks;
931                 lcounter = free - delta;
932                 if (lcounter < 0)
933                         /* We can't satisfy the request, just get what we can */
934                         fdblks_delta = free;
935                 else
936                         fdblks_delta = delta;
937
938                 /*
939                  * We'll either succeed in getting space from the free block
940                  * count or we'll get an ENOSPC. If we get a ENOSPC, it means
941                  * things changed while we were calculating fdblks_delta and so
942                  * we should try again to see if there is anything left to
943                  * reserve.
944                  *
945                  * Don't set the reserved flag here - we don't want to reserve
946                  * the extra reserve blocks from the reserve.....
947                  */
948                 spin_unlock(&mp->m_sb_lock);
949                 error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
950                 spin_lock(&mp->m_sb_lock);
951         } while (error == -ENOSPC);
952
953         /*
954          * Update the reserve counters if blocks have been successfully
955          * allocated.
956          */
957         if (!error && fdblks_delta) {
958                 mp->m_resblks += fdblks_delta;
959                 mp->m_resblks_avail += fdblks_delta;
960         }
961
962 out:
963         if (outval) {
964                 outval->resblks = mp->m_resblks;
965                 outval->resblks_avail = mp->m_resblks_avail;
966         }
967
968         spin_unlock(&mp->m_sb_lock);
969         return error;
970 }
971
972 int
973 xfs_fs_goingdown(
974         xfs_mount_t     *mp,
975         uint32_t        inflags)
976 {
977         switch (inflags) {
978         case XFS_FSOP_GOING_FLAGS_DEFAULT: {
979                 struct super_block *sb = freeze_bdev(mp->m_super->s_bdev);
980
981                 if (sb && !IS_ERR(sb)) {
982                         xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
983                         thaw_bdev(sb->s_bdev, sb);
984                 }
985
986                 break;
987         }
988         case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
989                 xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
990                 break;
991         case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
992                 xfs_force_shutdown(mp,
993                                 SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
994                 break;
995         default:
996                 return -EINVAL;
997         }
998
999         return 0;
1000 }
1001
1002 /*
1003  * Force a shutdown of the filesystem instantly while keeping the filesystem
1004  * consistent. We don't do an unmount here; just shutdown the shop, make sure
1005  * that absolutely nothing persistent happens to this filesystem after this
1006  * point.
1007  */
1008 void
1009 xfs_do_force_shutdown(
1010         xfs_mount_t     *mp,
1011         int             flags,
1012         char            *fname,
1013         int             lnnum)
1014 {
1015         int             logerror;
1016
1017         logerror = flags & SHUTDOWN_LOG_IO_ERROR;
1018
1019         if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
1020                 xfs_notice(mp,
1021         "%s(0x%x) called from line %d of file %s.  Return address = "PTR_FMT,
1022                         __func__, flags, lnnum, fname, __return_address);
1023         }
1024         /*
1025          * No need to duplicate efforts.
1026          */
1027         if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
1028                 return;
1029
1030         /*
1031          * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
1032          * queue up anybody new on the log reservations, and wakes up
1033          * everybody who's sleeping on log reservations to tell them
1034          * the bad news.
1035          */
1036         if (xfs_log_force_umount(mp, logerror))
1037                 return;
1038
1039         if (flags & SHUTDOWN_CORRUPT_INCORE) {
1040                 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT,
1041     "Corruption of in-memory data detected.  Shutting down filesystem");
1042                 if (XFS_ERRLEVEL_HIGH <= xfs_error_level)
1043                         xfs_stack_trace();
1044         } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
1045                 if (logerror) {
1046                         xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR,
1047                 "Log I/O Error Detected.  Shutting down filesystem");
1048                 } else if (flags & SHUTDOWN_DEVICE_REQ) {
1049                         xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
1050                 "All device paths lost.  Shutting down filesystem");
1051                 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
1052                         xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
1053                 "I/O Error Detected. Shutting down filesystem");
1054                 }
1055         }
1056         if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
1057                 xfs_alert(mp,
1058         "Please umount the filesystem and rectify the problem(s)");
1059         }
1060 }
1061
1062 /*
1063  * Reserve free space for per-AG metadata.
1064  */
1065 int
1066 xfs_fs_reserve_ag_blocks(
1067         struct xfs_mount        *mp)
1068 {
1069         xfs_agnumber_t          agno;
1070         struct xfs_perag        *pag;
1071         int                     error = 0;
1072         int                     err2;
1073
1074         for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
1075                 pag = xfs_perag_get(mp, agno);
1076                 err2 = xfs_ag_resv_init(pag);
1077                 xfs_perag_put(pag);
1078                 if (err2 && !error)
1079                         error = err2;
1080         }
1081
1082         if (error && error != -ENOSPC) {
1083                 xfs_warn(mp,
1084         "Error %d reserving per-AG metadata reserve pool.", error);
1085                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1086         }
1087
1088         return error;
1089 }
1090
1091 /*
1092  * Free space reserved for per-AG metadata.
1093  */
1094 int
1095 xfs_fs_unreserve_ag_blocks(
1096         struct xfs_mount        *mp)
1097 {
1098         xfs_agnumber_t          agno;
1099         struct xfs_perag        *pag;
1100         int                     error = 0;
1101         int                     err2;
1102
1103         for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
1104                 pag = xfs_perag_get(mp, agno);
1105                 err2 = xfs_ag_resv_free(pag);
1106                 xfs_perag_put(pag);
1107                 if (err2 && !error)
1108                         error = err2;
1109         }
1110
1111         if (error)
1112                 xfs_warn(mp,
1113         "Error %d freeing per-AG metadata reserve pool.", error);
1114
1115         return error;
1116 }