blob: ad53aa5e4ea777cffc55514ead3bfd9d7f1bf50d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,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 Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_btree.h"
30#include "xfs_ialloc.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_rtalloc.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070034#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_error.h"
36#include "xfs_bmap.h"
Dave Chinner983d09f2013-04-03 16:11:15 +110037#include "xfs_cksum.h"
Dave Chinner239880e2013-10-23 10:50:10 +110038#include "xfs_trans.h"
Dave Chinner983d09f2013-04-03 16:11:15 +110039#include "xfs_buf_item.h"
Dave Chinnerddf6ad02013-06-27 16:04:56 +100040#include "xfs_icreate_item.h"
Dave Chinner7bb85ef2013-08-12 20:49:34 +100041#include "xfs_icache.h"
Dave Chinnerd1230312013-11-01 15:27:19 +110042#include "xfs_trace.h"
Brian Fostera45086e2015-10-12 15:59:25 +110043#include "xfs_log.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100044#include "xfs_rmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * Allocation group level functions.
49 */
Darrick J. Wonge9369452017-06-16 11:00:08 -070050int
David Chinner75de2a92008-03-27 18:00:38 +110051xfs_ialloc_cluster_alignment(
Dave Chinner7a1df152014-12-04 09:42:21 +110052 struct xfs_mount *mp)
David Chinner75de2a92008-03-27 18:00:38 +110053{
Dave Chinner7a1df152014-12-04 09:42:21 +110054 if (xfs_sb_version_hasalign(&mp->m_sb) &&
Chandan Rajendra8ee9fdb2017-02-16 17:12:16 -080055 mp->m_sb.sb_inoalignmt >= xfs_icluster_size_fsb(mp))
Dave Chinner7a1df152014-12-04 09:42:21 +110056 return mp->m_sb.sb_inoalignmt;
David Chinner75de2a92008-03-27 18:00:38 +110057 return 1;
58}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
Christoph Hellwig21875502009-08-31 20:58:21 -030061 * Lookup a record by ino in the btree given by cur.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110062 */
Christoph Hellwig81e25172009-09-01 19:56:55 -040063int /* error */
Christoph Hellwig21875502009-08-31 20:58:21 -030064xfs_inobt_lookup(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110065 struct xfs_btree_cur *cur, /* btree cursor */
66 xfs_agino_t ino, /* starting inode of chunk */
Christoph Hellwig21875502009-08-31 20:58:21 -030067 xfs_lookup_t dir, /* <=, >=, == */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110068 int *stat) /* success/failure */
69{
70 cur->bc_rec.i.ir_startino = ino;
Brian Foster54190402015-05-29 09:03:04 +100071 cur->bc_rec.i.ir_holemask = 0;
72 cur->bc_rec.i.ir_count = 0;
Christoph Hellwig21875502009-08-31 20:58:21 -030073 cur->bc_rec.i.ir_freecount = 0;
74 cur->bc_rec.i.ir_free = 0;
75 return xfs_btree_lookup(cur, dir, stat);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110076}
77
78/*
Christoph Hellwigafabc242009-08-31 20:57:03 -030079 * Update the record referred to by cur to the value given.
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110080 * This either works (return 0) or gets an EFSCORRUPTED error.
81 */
82STATIC int /* error */
83xfs_inobt_update(
84 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigafabc242009-08-31 20:57:03 -030085 xfs_inobt_rec_incore_t *irec) /* btree record */
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110086{
87 union xfs_btree_rec rec;
88
Christoph Hellwigafabc242009-08-31 20:57:03 -030089 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
Brian Foster54190402015-05-29 09:03:04 +100090 if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
91 rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
92 rec.inobt.ir_u.sp.ir_count = irec->ir_count;
93 rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
94 } else {
95 /* ir_holemask/ir_count not supported on-disk */
96 rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
97 }
Christoph Hellwigafabc242009-08-31 20:57:03 -030098 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110099 return xfs_btree_update(cur, &rec);
100}
101
Darrick J. Wonge9369452017-06-16 11:00:08 -0700102/* Convert on-disk btree record to incore inobt record. */
103void
104xfs_inobt_btrec_to_irec(
105 struct xfs_mount *mp,
106 union xfs_btree_rec *rec,
107 struct xfs_inobt_rec_incore *irec)
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100108{
Brian Foster54190402015-05-29 09:03:04 +1000109 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
Darrick J. Wonge9369452017-06-16 11:00:08 -0700110 if (xfs_sb_version_hassparseinodes(&mp->m_sb)) {
Brian Foster54190402015-05-29 09:03:04 +1000111 irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
112 irec->ir_count = rec->inobt.ir_u.sp.ir_count;
113 irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
114 } else {
115 /*
116 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
117 * values for full inode chunks.
118 */
119 irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
120 irec->ir_count = XFS_INODES_PER_CHUNK;
121 irec->ir_freecount =
122 be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100123 }
Brian Foster54190402015-05-29 09:03:04 +1000124 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
Darrick J. Wonge9369452017-06-16 11:00:08 -0700125}
126
127/*
128 * Get the data from the pointed-to record.
129 */
130int
131xfs_inobt_get_rec(
132 struct xfs_btree_cur *cur,
133 struct xfs_inobt_rec_incore *irec,
134 int *stat)
135{
Dave Chinner9e6c08d2018-06-05 19:42:13 -0700136 struct xfs_mount *mp = cur->bc_mp;
137 xfs_agnumber_t agno = cur->bc_private.a.agno;
Darrick J. Wonge9369452017-06-16 11:00:08 -0700138 union xfs_btree_rec *rec;
139 int error;
Dave Chinner9e6c08d2018-06-05 19:42:13 -0700140 uint64_t realfree;
Darrick J. Wonge9369452017-06-16 11:00:08 -0700141
142 error = xfs_btree_get_rec(cur, &rec, stat);
143 if (error || *stat == 0)
144 return error;
145
Dave Chinner9e6c08d2018-06-05 19:42:13 -0700146 xfs_inobt_btrec_to_irec(mp, rec, irec);
147
148 if (!xfs_verify_agino(mp, agno, irec->ir_startino))
149 goto out_bad_rec;
150 if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT ||
151 irec->ir_count > XFS_INODES_PER_CHUNK)
152 goto out_bad_rec;
153 if (irec->ir_freecount > XFS_INODES_PER_CHUNK)
154 goto out_bad_rec;
155
156 /* if there are no holes, return the first available offset */
157 if (!xfs_inobt_issparse(irec->ir_holemask))
158 realfree = irec->ir_free;
159 else
160 realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec);
161 if (hweight64(realfree) != irec->ir_freecount)
162 goto out_bad_rec;
Brian Foster54190402015-05-29 09:03:04 +1000163
164 return 0;
Dave Chinner9e6c08d2018-06-05 19:42:13 -0700165
166out_bad_rec:
167 xfs_warn(mp,
168 "%s Inode BTree record corruption in AG %d detected!",
169 cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free", agno);
170 xfs_warn(mp,
171"start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
172 irec->ir_startino, irec->ir_count, irec->ir_freecount,
173 irec->ir_free, irec->ir_holemask);
174 return -EFSCORRUPTED;
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100175}
176
177/*
Brian Foster0aa0a752014-04-24 16:00:53 +1000178 * Insert a single inobt record. Cursor must already point to desired location.
179 */
Darrick J. Wong7f8f1312018-05-09 10:02:02 -0700180int
Brian Foster0aa0a752014-04-24 16:00:53 +1000181xfs_inobt_insert_rec(
182 struct xfs_btree_cur *cur,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700183 uint16_t holemask,
184 uint8_t count,
185 int32_t freecount,
Brian Foster0aa0a752014-04-24 16:00:53 +1000186 xfs_inofree_t free,
187 int *stat)
188{
Brian Foster54190402015-05-29 09:03:04 +1000189 cur->bc_rec.i.ir_holemask = holemask;
190 cur->bc_rec.i.ir_count = count;
Brian Foster0aa0a752014-04-24 16:00:53 +1000191 cur->bc_rec.i.ir_freecount = freecount;
192 cur->bc_rec.i.ir_free = free;
193 return xfs_btree_insert(cur, stat);
194}
195
196/*
197 * Insert records describing a newly allocated inode chunk into the inobt.
198 */
199STATIC int
200xfs_inobt_insert(
201 struct xfs_mount *mp,
202 struct xfs_trans *tp,
203 struct xfs_buf *agbp,
204 xfs_agino_t newino,
205 xfs_agino_t newlen,
206 xfs_btnum_t btnum)
207{
208 struct xfs_btree_cur *cur;
209 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
210 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
211 xfs_agino_t thisino;
212 int i;
213 int error;
214
215 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
216
217 for (thisino = newino;
218 thisino < newino + newlen;
219 thisino += XFS_INODES_PER_CHUNK) {
220 error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
221 if (error) {
222 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
223 return error;
224 }
225 ASSERT(i == 0);
226
Brian Foster54190402015-05-29 09:03:04 +1000227 error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
228 XFS_INODES_PER_CHUNK,
229 XFS_INODES_PER_CHUNK,
Brian Foster0aa0a752014-04-24 16:00:53 +1000230 XFS_INOBT_ALL_FREE, &i);
231 if (error) {
232 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
233 return error;
234 }
235 ASSERT(i == 1);
236 }
237
238 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
239
240 return 0;
241}
242
243/*
Dave Chinner0b48db82009-08-31 20:57:09 -0300244 * Verify that the number of free inodes in the AGI is correct.
245 */
246#ifdef DEBUG
247STATIC int
248xfs_check_agi_freecount(
249 struct xfs_btree_cur *cur,
250 struct xfs_agi *agi)
251{
252 if (cur->bc_nlevels == 1) {
253 xfs_inobt_rec_incore_t rec;
254 int freecount = 0;
255 int error;
256 int i;
257
Christoph Hellwig21875502009-08-31 20:58:21 -0300258 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
Dave Chinner0b48db82009-08-31 20:57:09 -0300259 if (error)
260 return error;
261
262 do {
263 error = xfs_inobt_get_rec(cur, &rec, &i);
264 if (error)
265 return error;
266
267 if (i) {
268 freecount += rec.ir_freecount;
269 error = xfs_btree_increment(cur, 0, &i);
270 if (error)
271 return error;
272 }
273 } while (i == 1);
274
275 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
276 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
277 }
278 return 0;
279}
280#else
281#define xfs_check_agi_freecount(cur, agi) 0
282#endif
283
284/*
Dave Chinner28c8e412013-06-27 16:04:55 +1000285 * Initialise a new set of inodes. When called without a transaction context
286 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
287 * than logging them (which in a transaction context puts them into the AIL
288 * for writeback rather than the xfsbufd queue).
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300289 */
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000290int
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300291xfs_ialloc_inode_init(
292 struct xfs_mount *mp,
293 struct xfs_trans *tp,
Dave Chinner28c8e412013-06-27 16:04:55 +1000294 struct list_head *buffer_list,
Brian Foster463958a2015-05-29 09:05:49 +1000295 int icount,
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300296 xfs_agnumber_t agno,
297 xfs_agblock_t agbno,
298 xfs_agblock_t length,
299 unsigned int gen)
300{
301 struct xfs_buf *fbuf;
302 struct xfs_dinode *free;
Jie Liu6e0c7b82013-12-13 15:51:49 +1100303 int nbufs, blks_per_cluster, inodes_per_cluster;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300304 int version;
305 int i, j;
306 xfs_daddr_t d;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100307 xfs_ino_t ino = 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300308
309 /*
Jie Liu6e0c7b82013-12-13 15:51:49 +1100310 * Loop over the new block(s), filling in the inodes. For small block
311 * sizes, manipulate the inodes in buffers which are multiples of the
312 * blocks size.
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300313 */
Jie Liu6e0c7b82013-12-13 15:51:49 +1100314 blks_per_cluster = xfs_icluster_size_fsb(mp);
315 inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
316 nbufs = length / blks_per_cluster;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300317
318 /*
Christoph Hellwig93848a92013-04-03 16:11:17 +1100319 * Figure out what version number to use in the inodes we create. If
320 * the superblock version has caught up to the one that supports the new
321 * inode format, then use the new inode version. Otherwise use the old
322 * version so that old kernels will continue to be able to use the file
323 * system.
324 *
325 * For v3 inodes, we also need to write the inode number into the inode,
326 * so calculate the first inode number of the chunk here as
327 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
328 * across multiple filesystem blocks (such as a cluster) and so cannot
329 * be used in the cluster buffer loop below.
330 *
331 * Further, because we are writing the inode directly into the buffer
332 * and calculating a CRC on the entire inode, we have ot log the entire
333 * inode so that the entire range the CRC covers is present in the log.
334 * That means for v3 inode we log the entire buffer rather than just the
335 * inode cores.
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300336 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100337 if (xfs_sb_version_hascrc(&mp->m_sb)) {
338 version = 3;
339 ino = XFS_AGINO_TO_INO(mp, agno,
340 XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000341
342 /*
343 * log the initialisation that is about to take place as an
344 * logical operation. This means the transaction does not
345 * need to log the physical changes to the inode buffers as log
346 * recovery will know what initialisation is actually needed.
347 * Hence we only need to log the buffers as "ordered" buffers so
348 * they track in the AIL as if they were physically logged.
349 */
350 if (tp)
Brian Foster463958a2015-05-29 09:05:49 +1000351 xfs_icreate_log(tp, agno, agbno, icount,
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000352 mp->m_sb.sb_inodesize, length, gen);
Dave Chinner263997a2014-05-20 07:46:40 +1000353 } else
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300354 version = 2;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300355
356 for (j = 0; j < nbufs; j++) {
357 /*
358 * Get the block.
359 */
360 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
361 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
Dave Chinner7c4cebe2012-11-23 14:24:23 +1100362 mp->m_bsize * blks_per_cluster,
363 XBF_UNMAPPED);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000364 if (!fbuf)
Dave Chinner24513372014-06-25 14:58:08 +1000365 return -ENOMEM;
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000366
367 /* Initialize the inode buffers and log them appropriately. */
Dave Chinner1813dd62012-11-14 17:54:40 +1100368 fbuf->b_ops = &xfs_inode_buf_ops;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100369 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
Jie Liu6e0c7b82013-12-13 15:51:49 +1100370 for (i = 0; i < inodes_per_cluster; i++) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300371 int ioffset = i << mp->m_sb.sb_inodelog;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100372 uint isize = xfs_dinode_size(version);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300373
374 free = xfs_make_iptr(mp, fbuf, i);
375 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
376 free->di_version = version;
377 free->di_gen = cpu_to_be32(gen);
378 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100379
380 if (version == 3) {
381 free->di_ino = cpu_to_be64(ino);
382 ino++;
Eric Sandeence748ea2015-07-29 11:53:31 +1000383 uuid_copy(&free->di_uuid,
384 &mp->m_sb.sb_meta_uuid);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100385 xfs_dinode_calc_crc(mp, free);
Dave Chinner28c8e412013-06-27 16:04:55 +1000386 } else if (tp) {
Christoph Hellwig93848a92013-04-03 16:11:17 +1100387 /* just log the inode core */
388 xfs_trans_log_buf(tp, fbuf, ioffset,
389 ioffset + isize - 1);
390 }
391 }
Dave Chinner28c8e412013-06-27 16:04:55 +1000392
393 if (tp) {
394 /*
395 * Mark the buffer as an inode allocation buffer so it
396 * sticks in AIL at the point of this allocation
397 * transaction. This ensures the they are on disk before
398 * the tail of the log can be moved past this
399 * transaction (i.e. by preventing relogging from moving
400 * it forward in the log).
401 */
402 xfs_trans_inode_alloc_buf(tp, fbuf);
403 if (version == 3) {
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000404 /*
405 * Mark the buffer as ordered so that they are
406 * not physically logged in the transaction but
407 * still tracked in the AIL as part of the
408 * transaction and pin the log appropriately.
409 */
410 xfs_trans_ordered_buf(tp, fbuf);
Dave Chinner28c8e412013-06-27 16:04:55 +1000411 }
412 } else {
413 fbuf->b_flags |= XBF_DONE;
414 xfs_buf_delwri_queue(fbuf, buffer_list);
415 xfs_buf_relse(fbuf);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300416 }
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300417 }
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000418 return 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300419}
420
421/*
Brian Foster56d11152015-05-29 09:18:32 +1000422 * Align startino and allocmask for a recently allocated sparse chunk such that
423 * they are fit for insertion (or merge) into the on-disk inode btrees.
424 *
425 * Background:
426 *
427 * When enabled, sparse inode support increases the inode alignment from cluster
428 * size to inode chunk size. This means that the minimum range between two
429 * non-adjacent inode records in the inobt is large enough for a full inode
430 * record. This allows for cluster sized, cluster aligned block allocation
431 * without need to worry about whether the resulting inode record overlaps with
432 * another record in the tree. Without this basic rule, we would have to deal
433 * with the consequences of overlap by potentially undoing recent allocations in
434 * the inode allocation codepath.
435 *
436 * Because of this alignment rule (which is enforced on mount), there are two
437 * inobt possibilities for newly allocated sparse chunks. One is that the
438 * aligned inode record for the chunk covers a range of inodes not already
439 * covered in the inobt (i.e., it is safe to insert a new sparse record). The
440 * other is that a record already exists at the aligned startino that considers
441 * the newly allocated range as sparse. In the latter case, record content is
442 * merged in hope that sparse inode chunks fill to full chunks over time.
443 */
444STATIC void
445xfs_align_sparse_ino(
446 struct xfs_mount *mp,
447 xfs_agino_t *startino,
448 uint16_t *allocmask)
449{
450 xfs_agblock_t agbno;
451 xfs_agblock_t mod;
452 int offset;
453
454 agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
455 mod = agbno % mp->m_sb.sb_inoalignmt;
456 if (!mod)
457 return;
458
459 /* calculate the inode offset and align startino */
460 offset = mod << mp->m_sb.sb_inopblog;
461 *startino -= offset;
462
463 /*
464 * Since startino has been aligned down, left shift allocmask such that
465 * it continues to represent the same physical inodes relative to the
466 * new startino.
467 */
468 *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
469}
470
471/*
472 * Determine whether the source inode record can merge into the target. Both
473 * records must be sparse, the inode ranges must match and there must be no
474 * allocation overlap between the records.
475 */
476STATIC bool
477__xfs_inobt_can_merge(
478 struct xfs_inobt_rec_incore *trec, /* tgt record */
479 struct xfs_inobt_rec_incore *srec) /* src record */
480{
481 uint64_t talloc;
482 uint64_t salloc;
483
484 /* records must cover the same inode range */
485 if (trec->ir_startino != srec->ir_startino)
486 return false;
487
488 /* both records must be sparse */
489 if (!xfs_inobt_issparse(trec->ir_holemask) ||
490 !xfs_inobt_issparse(srec->ir_holemask))
491 return false;
492
493 /* both records must track some inodes */
494 if (!trec->ir_count || !srec->ir_count)
495 return false;
496
497 /* can't exceed capacity of a full record */
498 if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
499 return false;
500
501 /* verify there is no allocation overlap */
502 talloc = xfs_inobt_irec_to_allocmask(trec);
503 salloc = xfs_inobt_irec_to_allocmask(srec);
504 if (talloc & salloc)
505 return false;
506
507 return true;
508}
509
510/*
511 * Merge the source inode record into the target. The caller must call
512 * __xfs_inobt_can_merge() to ensure the merge is valid.
513 */
514STATIC void
515__xfs_inobt_rec_merge(
516 struct xfs_inobt_rec_incore *trec, /* target */
517 struct xfs_inobt_rec_incore *srec) /* src */
518{
519 ASSERT(trec->ir_startino == srec->ir_startino);
520
521 /* combine the counts */
522 trec->ir_count += srec->ir_count;
523 trec->ir_freecount += srec->ir_freecount;
524
525 /*
526 * Merge the holemask and free mask. For both fields, 0 bits refer to
527 * allocated inodes. We combine the allocated ranges with bitwise AND.
528 */
529 trec->ir_holemask &= srec->ir_holemask;
530 trec->ir_free &= srec->ir_free;
531}
532
533/*
534 * Insert a new sparse inode chunk into the associated inode btree. The inode
535 * record for the sparse chunk is pre-aligned to a startino that should match
536 * any pre-existing sparse inode record in the tree. This allows sparse chunks
537 * to fill over time.
538 *
539 * This function supports two modes of handling preexisting records depending on
540 * the merge flag. If merge is true, the provided record is merged with the
541 * existing record and updated in place. The merged record is returned in nrec.
542 * If merge is false, an existing record is replaced with the provided record.
543 * If no preexisting record exists, the provided record is always inserted.
544 *
545 * It is considered corruption if a merge is requested and not possible. Given
546 * the sparse inode alignment constraints, this should never happen.
547 */
548STATIC int
549xfs_inobt_insert_sprec(
550 struct xfs_mount *mp,
551 struct xfs_trans *tp,
552 struct xfs_buf *agbp,
553 int btnum,
554 struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */
555 bool merge) /* merge or replace */
556{
557 struct xfs_btree_cur *cur;
558 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
559 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
560 int error;
561 int i;
562 struct xfs_inobt_rec_incore rec;
563
564 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
565
566 /* the new record is pre-aligned so we know where to look */
567 error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
568 if (error)
569 goto error;
570 /* if nothing there, insert a new record and return */
571 if (i == 0) {
572 error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
573 nrec->ir_count, nrec->ir_freecount,
574 nrec->ir_free, &i);
575 if (error)
576 goto error;
577 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
578
579 goto out;
580 }
581
582 /*
583 * A record exists at this startino. Merge or replace the record
584 * depending on what we've been asked to do.
585 */
586 if (merge) {
587 error = xfs_inobt_get_rec(cur, &rec, &i);
588 if (error)
589 goto error;
590 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
591 XFS_WANT_CORRUPTED_GOTO(mp,
592 rec.ir_startino == nrec->ir_startino,
593 error);
594
595 /*
596 * This should never fail. If we have coexisting records that
597 * cannot merge, something is seriously wrong.
598 */
599 XFS_WANT_CORRUPTED_GOTO(mp, __xfs_inobt_can_merge(nrec, &rec),
600 error);
601
602 trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino,
603 rec.ir_holemask, nrec->ir_startino,
604 nrec->ir_holemask);
605
606 /* merge to nrec to output the updated record */
607 __xfs_inobt_rec_merge(nrec, &rec);
608
609 trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino,
610 nrec->ir_holemask);
611
612 error = xfs_inobt_rec_check_count(mp, nrec);
613 if (error)
614 goto error;
615 }
616
617 error = xfs_inobt_update(cur, nrec);
618 if (error)
619 goto error;
620
621out:
622 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
623 return 0;
624error:
625 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
626 return error;
627}
628
629/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * Allocate new inodes in the allocation group specified by agbp.
631 * Return 0 for success, else error code.
632 */
633STATIC int /* error code or 0 */
634xfs_ialloc_ag_alloc(
635 xfs_trans_t *tp, /* transaction pointer */
636 xfs_buf_t *agbp, /* alloc group buffer */
637 int *alloc)
638{
639 xfs_agi_t *agi; /* allocation group header */
640 xfs_alloc_arg_t args; /* allocation argument structure */
David Chinner92821e22007-05-24 15:26:31 +1000641 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 xfs_agino_t newino; /* new first inode's number */
644 xfs_agino_t newlen; /* new number of inodes */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000645 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* boundary */
Brian Foster56d11152015-05-29 09:18:32 +1000647 uint16_t allocmask = (uint16_t) -1; /* init. to full chunk */
648 struct xfs_inobt_rec_incore rec;
Dave Chinner44b56e02010-01-11 11:47:43 +0000649 struct xfs_perag *pag;
Brian Foster1cdadee2015-05-29 09:19:29 +1000650 int do_sparse = 0;
651
Mark Tinguelya0041682012-09-20 13:16:45 -0500652 memset(&args, 0, sizeof(args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 args.tp = tp;
654 args.mp = tp->t_mountp;
Brian Foster1cdadee2015-05-29 09:19:29 +1000655 args.fsbno = NULLFSBLOCK;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000656 xfs_rmap_ag_owner(&args.oinfo, XFS_RMAP_OWN_INODES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Brian Foster46fc58d2015-06-04 13:03:34 +1000658#ifdef DEBUG
659 /* randomly do sparse inode allocations */
660 if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) &&
661 args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks)
662 do_sparse = prandom_u32() & 1;
663#endif
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /*
666 * Locking will ensure that we don't have two callers in here
667 * at one time.
668 */
Jie Liu71783432013-12-13 15:51:46 +1100669 newlen = args.mp->m_ialloc_inos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (args.mp->m_maxicount &&
George Wang74f9ce12015-05-29 07:39:34 +1000671 percpu_counter_read_positive(&args.mp->m_icount) + newlen >
Dave Chinner501ab322015-02-23 21:19:28 +1100672 args.mp->m_maxicount)
Dave Chinner24513372014-06-25 14:58:08 +1000673 return -ENOSPC;
Jie Liu126cd102013-12-13 15:51:48 +1100674 args.minlen = args.maxlen = args.mp->m_ialloc_blks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000676 * First try to allocate inodes contiguous with the last-allocated
677 * chunk of inodes. If the filesystem is striped, this will fill
678 * an entire stripe unit with inodes.
Dave Chinner28c8e412013-06-27 16:04:55 +1000679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000681 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300682 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000683 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
Jie Liu126cd102013-12-13 15:51:48 +1100684 args.mp->m_ialloc_blks;
Brian Foster1cdadee2015-05-29 09:19:29 +1000685 if (do_sparse)
686 goto sparse_alloc;
Nathan Scott019ff2d2006-04-11 15:45:05 +1000687 if (likely(newino != NULLAGINO &&
688 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300689 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000690 args.type = XFS_ALLOCTYPE_THIS_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000691 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100692
Glen Overby3ccb8b52006-03-29 09:52:28 +1000693 /*
David Chinner75de2a92008-03-27 18:00:38 +1100694 * We need to take into account alignment here to ensure that
695 * we don't modify the free list if we fail to have an exact
696 * block. If we don't have an exact match, and every oher
697 * attempt allocation attempt fails, we'll end up cancelling
698 * a dirty transaction and shutting down.
699 *
700 * For an exact allocation, alignment must be 1,
701 * however we need to take cluster alignment into account when
702 * fixing up the freelist. Use the minalignslop field to
703 * indicate that extra blocks might be required for alignment,
704 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000705 */
David Chinner75de2a92008-03-27 18:00:38 +1100706 args.alignment = 1;
Dave Chinner7a1df152014-12-04 09:42:21 +1100707 args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1;
David Chinner75de2a92008-03-27 18:00:38 +1100708
709 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100710 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000711 if ((error = xfs_alloc_vextent(&args)))
712 return error;
Brian Fostere480a722014-03-07 16:19:14 +1100713
714 /*
715 * This request might have dirtied the transaction if the AG can
716 * satisfy the request, but the exact block was not available.
717 * If the allocation did fail, subsequent requests will relax
718 * the exact agbno requirement and increase the alignment
719 * instead. It is critical that the total size of the request
720 * (len + alignment + slop) does not increase from this point
721 * on, so reset minalignslop to ensure it is not included in
722 * subsequent requests.
723 */
724 args.minalignslop = 0;
Brian Foster1cdadee2015-05-29 09:19:29 +1000725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Glen Overby3ccb8b52006-03-29 09:52:28 +1000727 if (unlikely(args.fsbno == NULLFSBLOCK)) {
728 /*
729 * Set the alignment for the allocation.
730 * If stripe alignment is turned on then align at stripe unit
731 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000732 * If the cluster size is smaller than a filesystem block
733 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000734 * pieces, so don't need alignment anyway.
735 */
736 isaligned = 0;
737 if (args.mp->m_sinoalign) {
738 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
739 args.alignment = args.mp->m_dalign;
740 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100741 } else
Dave Chinner7a1df152014-12-04 09:42:21 +1100742 args.alignment = xfs_ialloc_cluster_alignment(args.mp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000743 /*
744 * Need to figure out where to allocate the inode blocks.
745 * Ideally they should be spaced out through the a.g.
746 * For now, just allocate blocks up front.
747 */
748 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300749 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000750 /*
751 * Allocate a fixed-size extent of inodes.
752 */
753 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000754 args.prod = 1;
755 /*
756 * Allow space for the inode btree to split.
757 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100758 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000759 if ((error = xfs_alloc_vextent(&args)))
760 return error;
761 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /*
764 * If stripe alignment is turned on, then try again with cluster
765 * alignment.
766 */
767 if (isaligned && args.fsbno == NULLFSBLOCK) {
768 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100769 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300770 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Dave Chinner7a1df152014-12-04 09:42:21 +1100771 args.alignment = xfs_ialloc_cluster_alignment(args.mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if ((error = xfs_alloc_vextent(&args)))
773 return error;
774 }
775
Brian Foster56d11152015-05-29 09:18:32 +1000776 /*
777 * Finally, try a sparse allocation if the filesystem supports it and
778 * the sparse allocation length is smaller than a full chunk.
779 */
780 if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) &&
781 args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks &&
782 args.fsbno == NULLFSBLOCK) {
Brian Foster1cdadee2015-05-29 09:19:29 +1000783sparse_alloc:
Brian Foster56d11152015-05-29 09:18:32 +1000784 args.type = XFS_ALLOCTYPE_NEAR_BNO;
785 args.agbno = be32_to_cpu(agi->agi_root);
786 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
787 args.alignment = args.mp->m_sb.sb_spino_align;
788 args.prod = 1;
789
790 args.minlen = args.mp->m_ialloc_min_blks;
791 args.maxlen = args.minlen;
792
793 /*
794 * The inode record will be aligned to full chunk size. We must
795 * prevent sparse allocation from AG boundaries that result in
796 * invalid inode records, such as records that start at agbno 0
797 * or extend beyond the AG.
798 *
799 * Set min agbno to the first aligned, non-zero agbno and max to
800 * the last aligned agbno that is at least one full chunk from
801 * the end of the AG.
802 */
803 args.min_agbno = args.mp->m_sb.sb_inoalignmt;
804 args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
805 args.mp->m_sb.sb_inoalignmt) -
806 args.mp->m_ialloc_blks;
807
808 error = xfs_alloc_vextent(&args);
809 if (error)
810 return error;
811
812 newlen = args.len << args.mp->m_sb.sb_inopblog;
Brian Foster46fc58d2015-06-04 13:03:34 +1000813 ASSERT(newlen <= XFS_INODES_PER_CHUNK);
Brian Foster56d11152015-05-29 09:18:32 +1000814 allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (args.fsbno == NULLFSBLOCK) {
818 *alloc = 0;
819 return 0;
820 }
821 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
David Chinner359346a2008-04-29 12:53:32 +1000823 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300824 * Stamp and write the inode buffers.
825 *
David Chinner359346a2008-04-29 12:53:32 +1000826 * Seed the new inode cluster with a random generation number. This
827 * prevents short-term reuse of generation numbers if a chunk is
828 * freed and then immediately reallocated. We use random numbers
829 * rather than a linear progression to prevent the next generation
830 * number from being easily guessable.
831 */
Brian Foster463958a2015-05-29 09:05:49 +1000832 error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno,
833 args.agbno, args.len, prandom_u32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100834
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000835 if (error)
836 return error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300837 /*
838 * Convert the results.
839 */
840 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Brian Foster56d11152015-05-29 09:18:32 +1000841
842 if (xfs_inobt_issparse(~allocmask)) {
843 /*
844 * We've allocated a sparse chunk. Align the startino and mask.
845 */
846 xfs_align_sparse_ino(args.mp, &newino, &allocmask);
847
848 rec.ir_startino = newino;
849 rec.ir_holemask = ~allocmask;
850 rec.ir_count = newlen;
851 rec.ir_freecount = newlen;
852 rec.ir_free = XFS_INOBT_ALL_FREE;
853
854 /*
855 * Insert the sparse record into the inobt and allow for a merge
856 * if necessary. If a merge does occur, rec is updated to the
857 * merged record.
858 */
859 error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO,
860 &rec, true);
861 if (error == -EFSCORRUPTED) {
862 xfs_alert(args.mp,
863 "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
864 XFS_AGINO_TO_INO(args.mp, agno,
865 rec.ir_startino),
866 rec.ir_holemask, rec.ir_count);
867 xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
868 }
869 if (error)
870 return error;
871
872 /*
873 * We can't merge the part we've just allocated as for the inobt
874 * due to finobt semantics. The original record may or may not
875 * exist independent of whether physical inodes exist in this
876 * sparse chunk.
877 *
878 * We must update the finobt record based on the inobt record.
879 * rec contains the fully merged and up to date inobt record
880 * from the previous call. Set merge false to replace any
881 * existing record with this one.
882 */
883 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
884 error = xfs_inobt_insert_sprec(args.mp, tp, agbp,
885 XFS_BTNUM_FINO, &rec,
886 false);
887 if (error)
888 return error;
889 }
890 } else {
891 /* full chunk - insert new records to both btrees */
892 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
893 XFS_BTNUM_INO);
894 if (error)
895 return error;
896
897 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
898 error = xfs_inobt_insert(args.mp, tp, agbp, newino,
899 newlen, XFS_BTNUM_FINO);
900 if (error)
901 return error;
902 }
903 }
904
905 /*
906 * Update AGI counts and newino.
907 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800908 be32_add_cpu(&agi->agi_count, newlen);
909 be32_add_cpu(&agi->agi_freecount, newlen);
Dave Chinner44b56e02010-01-11 11:47:43 +0000910 pag = xfs_perag_get(args.mp, agno);
911 pag->pagi_freecount += newlen;
912 xfs_perag_put(pag);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100913 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * Log allocation group header fields
917 */
918 xfs_ialloc_log_agi(tp, agbp,
919 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
920 /*
921 * Modify/log superblock values for inode count and inode free count.
922 */
923 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
924 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
925 *alloc = 1;
926 return 0;
927}
928
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000929STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930xfs_ialloc_next_ag(
931 xfs_mount_t *mp)
932{
933 xfs_agnumber_t agno;
934
935 spin_lock(&mp->m_agirotor_lock);
936 agno = mp->m_agirotor;
Carlos Maiolino8aea3ff2012-09-20 10:32:36 -0300937 if (++mp->m_agirotor >= mp->m_maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 mp->m_agirotor = 0;
939 spin_unlock(&mp->m_agirotor_lock);
940
941 return agno;
942}
943
944/*
945 * Select an allocation group to look for a free inode in, based on the parent
Zhi Yong Wu2f21ff12013-08-12 03:14:58 +0000946 * inode and the mode. Return the allocation group buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400948STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949xfs_ialloc_ag_select(
950 xfs_trans_t *tp, /* transaction pointer */
951 xfs_ino_t parent, /* parent directory inode number */
Christoph Hellwigf59cf5c2017-12-04 17:32:55 -0800952 umode_t mode) /* bits set to indicate file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
955 xfs_agnumber_t agno; /* current ag number */
956 int flags; /* alloc buffer locking flags */
957 xfs_extlen_t ineed; /* blocks needed for inode allocation */
958 xfs_extlen_t longest = 0; /* longest extent available */
959 xfs_mount_t *mp; /* mount point structure */
960 int needspace; /* file mode implies space allocated */
961 xfs_perag_t *pag; /* per allocation group data */
962 xfs_agnumber_t pagno; /* parent (starting) ag number */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400963 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /*
966 * Files of these types need at least one block if length > 0
967 * (and they won't fit in the inode, but that's hard to figure out).
968 */
969 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
970 mp = tp->t_mountp;
971 agcount = mp->m_maxagi;
972 if (S_ISDIR(mode))
973 pagno = xfs_ialloc_next_ag(mp);
974 else {
975 pagno = XFS_INO_TO_AGNO(mp, parent);
976 if (pagno >= agcount)
977 pagno = 0;
978 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 ASSERT(pagno < agcount);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /*
983 * Loop through allocation groups, looking for one with a little
984 * free space in it. Note we don't look for free inodes, exactly.
985 * Instead, we include whether there is a need to allocate inodes
986 * to mean that blocks must be allocated for them,
987 * if none are currently free.
988 */
989 agno = pagno;
990 flags = XFS_ALLOC_FLAG_TRYLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 for (;;) {
Dave Chinner44b56e02010-01-11 11:47:43 +0000992 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!pag->pagi_inodeok) {
994 xfs_ialloc_next_ag(mp);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400995 goto nextag;
996 }
997
998 if (!pag->pagi_init) {
999 error = xfs_ialloc_pagi_init(mp, tp, agno);
1000 if (error)
1001 goto nextag;
1002 }
1003
1004 if (pag->pagi_freecount) {
1005 xfs_perag_put(pag);
1006 return agno;
1007 }
1008
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001009 if (!pag->pagf_init) {
1010 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
1011 if (error)
1012 goto nextag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
1015 /*
Dave Chinner7a1df152014-12-04 09:42:21 +11001016 * Check that there is enough free space for the file plus a
1017 * chunk of inodes if we need to allocate some. If this is the
1018 * first pass across the AGs, take into account the potential
1019 * space needed for alignment of inode chunks when checking the
1020 * longest contiguous free space in the AG - this prevents us
1021 * from getting ENOSPC because we have free space larger than
1022 * m_ialloc_blks but alignment constraints prevent us from using
1023 * it.
1024 *
1025 * If we can't find an AG with space for full alignment slack to
1026 * be taken into account, we must be near ENOSPC in all AGs.
1027 * Hence we don't include alignment for the second pass and so
1028 * if we fail allocation due to alignment issues then it is most
1029 * likely a real ENOSPC condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
Brian Foster066a1882015-05-29 08:55:20 +10001031 ineed = mp->m_ialloc_min_blks;
Dave Chinner7a1df152014-12-04 09:42:21 +11001032 if (flags && ineed > 1)
1033 ineed += xfs_ialloc_cluster_alignment(mp);
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001034 longest = pag->pagf_longest;
1035 if (!longest)
1036 longest = pag->pagf_flcount > 0;
1037
1038 if (pag->pagf_freeblks >= needspace + ineed &&
1039 longest >= ineed) {
1040 xfs_perag_put(pag);
1041 return agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043nextag:
Dave Chinner44b56e02010-01-11 11:47:43 +00001044 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * No point in iterating over the rest, if we're shutting
1047 * down.
1048 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +00001049 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001050 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 agno++;
1052 if (agno >= agcount)
1053 agno = 0;
1054 if (agno == pagno) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +00001055 if (flags == 0)
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001056 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 flags = 0;
1058 }
1059 }
1060}
1061
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001062/*
1063 * Try to retrieve the next record to the left/right from the current one.
1064 */
1065STATIC int
1066xfs_ialloc_next_rec(
1067 struct xfs_btree_cur *cur,
1068 xfs_inobt_rec_incore_t *rec,
1069 int *done,
1070 int left)
1071{
1072 int error;
1073 int i;
1074
1075 if (left)
1076 error = xfs_btree_decrement(cur, 0, &i);
1077 else
1078 error = xfs_btree_increment(cur, 0, &i);
1079
1080 if (error)
1081 return error;
1082 *done = !i;
1083 if (i) {
1084 error = xfs_inobt_get_rec(cur, rec, &i);
1085 if (error)
1086 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001087 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001088 }
1089
1090 return 0;
1091}
1092
Dave Chinnerbd169562009-08-31 20:58:28 -03001093STATIC int
1094xfs_ialloc_get_rec(
1095 struct xfs_btree_cur *cur,
1096 xfs_agino_t agino,
1097 xfs_inobt_rec_incore_t *rec,
Jie Liu43df2ee2013-06-24 13:37:23 +08001098 int *done)
Dave Chinnerbd169562009-08-31 20:58:28 -03001099{
1100 int error;
1101 int i;
1102
1103 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
1104 if (error)
1105 return error;
1106 *done = !i;
1107 if (i) {
1108 error = xfs_inobt_get_rec(cur, rec, &i);
1109 if (error)
1110 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001111 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Dave Chinnerbd169562009-08-31 20:58:28 -03001112 }
1113
1114 return 0;
1115}
Dave Chinner0b48db82009-08-31 20:57:09 -03001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117/*
Brian Foster26dd5212015-05-29 09:20:10 +10001118 * Return the offset of the first free inode in the record. If the inode chunk
1119 * is sparsely allocated, we convert the record holemask to inode granularity
1120 * and mask off the unallocated regions from the inode free mask.
Brian Fosterd4cc5402015-05-29 08:50:21 +10001121 */
1122STATIC int
1123xfs_inobt_first_free_inode(
1124 struct xfs_inobt_rec_incore *rec)
1125{
Brian Foster26dd5212015-05-29 09:20:10 +10001126 xfs_inofree_t realfree;
1127
1128 /* if there are no holes, return the first available offset */
1129 if (!xfs_inobt_issparse(rec->ir_holemask))
1130 return xfs_lowbit64(rec->ir_free);
1131
1132 realfree = xfs_inobt_irec_to_allocmask(rec);
1133 realfree &= rec->ir_free;
1134
1135 return xfs_lowbit64(realfree);
Brian Fosterd4cc5402015-05-29 08:50:21 +10001136}
1137
1138/*
Brian Foster6dd86382014-04-24 16:00:53 +10001139 * Allocate an inode using the inobt-only algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001141STATIC int
Brian Foster6dd86382014-04-24 16:00:53 +10001142xfs_dialloc_ag_inobt(
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001143 struct xfs_trans *tp,
1144 struct xfs_buf *agbp,
1145 xfs_ino_t parent,
1146 xfs_ino_t *inop)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001148 struct xfs_mount *mp = tp->t_mountp;
1149 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1150 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1151 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1152 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1153 struct xfs_perag *pag;
1154 struct xfs_btree_cur *cur, *tcur;
1155 struct xfs_inobt_rec_incore rec, trec;
1156 xfs_ino_t ino;
1157 int error;
1158 int offset;
1159 int i, j;
Carlos Maiolino2d323112017-08-18 18:07:04 -07001160 int searchdistance = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Dave Chinner44b56e02010-01-11 11:47:43 +00001162 pag = xfs_perag_get(mp, agno);
Dave Chinnerbd169562009-08-31 20:58:28 -03001163
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001164 ASSERT(pag->pagi_init);
1165 ASSERT(pag->pagi_inodeok);
1166 ASSERT(pag->pagi_freecount > 0);
1167
Dave Chinnerbd169562009-08-31 20:58:28 -03001168 restart_pagno:
Brian Foster57bd3db2014-04-24 16:00:50 +10001169 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /*
1171 * If pagino is 0 (this is the root inode allocation) use newino.
1172 * This must work because we've just allocated some.
1173 */
1174 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +11001175 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Dave Chinner0b48db82009-08-31 20:57:09 -03001177 error = xfs_check_agi_freecount(cur, agi);
1178 if (error)
1179 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001182 * If in the same AG as the parent, try to get near the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
1184 if (pagno == agno) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001185 int doneleft; /* done, to the left */
1186 int doneright; /* done, to the right */
1187
Christoph Hellwig21875502009-08-31 20:58:21 -03001188 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001189 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001191 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001192
1193 error = xfs_inobt_get_rec(cur, &rec, &j);
1194 if (error)
1195 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001196 XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001197
1198 if (rec.ir_freecount > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /*
1200 * Found a free inode in the same chunk
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001201 * as the parent, done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001203 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001205
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001208 * In the same AG as parent, but parent's chunk is full.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001211 /* duplicate the cursor, search left & right simultaneously */
1212 error = xfs_btree_dup_cursor(cur, &tcur);
1213 if (error)
1214 goto error0;
1215
Dave Chinnerbd169562009-08-31 20:58:28 -03001216 /*
1217 * Skip to last blocks looked up if same parent inode.
1218 */
1219 if (pagino != NULLAGINO &&
1220 pag->pagl_pagino == pagino &&
1221 pag->pagl_leftrec != NULLAGINO &&
1222 pag->pagl_rightrec != NULLAGINO) {
1223 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
Jie Liu43df2ee2013-06-24 13:37:23 +08001224 &trec, &doneleft);
Dave Chinnerbd169562009-08-31 20:58:28 -03001225 if (error)
1226 goto error1;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001227
Dave Chinnerbd169562009-08-31 20:58:28 -03001228 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
Jie Liu43df2ee2013-06-24 13:37:23 +08001229 &rec, &doneright);
Dave Chinnerbd169562009-08-31 20:58:28 -03001230 if (error)
1231 goto error1;
1232 } else {
1233 /* search left with tcur, back up 1 record */
1234 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
1235 if (error)
1236 goto error1;
1237
1238 /* search right with cur, go forward 1 record. */
1239 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
1240 if (error)
1241 goto error1;
1242 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001243
1244 /*
1245 * Loop until we find an inode chunk with a free inode.
1246 */
Carlos Maiolino2d323112017-08-18 18:07:04 -07001247 while (--searchdistance > 0 && (!doneleft || !doneright)) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001248 int useleft; /* using left inode chunk this time */
1249
1250 /* figure out the closer block if both are valid. */
1251 if (!doneleft && !doneright) {
1252 useleft = pagino -
1253 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
1254 rec.ir_startino - pagino;
1255 } else {
1256 useleft = !doneleft;
1257 }
1258
1259 /* free inodes to the left? */
1260 if (useleft && trec.ir_freecount) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001261 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1262 cur = tcur;
Dave Chinnerbd169562009-08-31 20:58:28 -03001263
1264 pag->pagl_leftrec = trec.ir_startino;
1265 pag->pagl_rightrec = rec.ir_startino;
1266 pag->pagl_pagino = pagino;
Omar Sandovalc44245b2017-08-11 09:00:06 -07001267 rec = trec;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001268 goto alloc_inode;
1269 }
1270
1271 /* free inodes to the right? */
1272 if (!useleft && rec.ir_freecount) {
1273 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -03001274
1275 pag->pagl_leftrec = trec.ir_startino;
1276 pag->pagl_rightrec = rec.ir_startino;
1277 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001278 goto alloc_inode;
1279 }
1280
1281 /* get next record to check */
1282 if (useleft) {
1283 error = xfs_ialloc_next_rec(tcur, &trec,
1284 &doneleft, 1);
1285 } else {
1286 error = xfs_ialloc_next_rec(cur, &rec,
1287 &doneright, 0);
1288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Dave Chinnerbd169562009-08-31 20:58:28 -03001292
Carlos Maiolino2d323112017-08-18 18:07:04 -07001293 if (searchdistance <= 0) {
1294 /*
1295 * Not in range - save last search
1296 * location and allocate a new inode
1297 */
1298 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1299 pag->pagl_leftrec = trec.ir_startino;
1300 pag->pagl_rightrec = rec.ir_startino;
1301 pag->pagl_pagino = pagino;
1302
1303 } else {
1304 /*
1305 * We've reached the end of the btree. because
1306 * we are only searching a small chunk of the
1307 * btree each search, there is obviously free
1308 * inodes closer to the parent inode than we
1309 * are now. restart the search again.
1310 */
1311 pag->pagl_pagino = NULLAGINO;
1312 pag->pagl_leftrec = NULLAGINO;
1313 pag->pagl_rightrec = NULLAGINO;
1314 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1315 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1316 goto restart_pagno;
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001321 * In a different AG from the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * See if the most recently allocated block has any free.
1323 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001324 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
Christoph Hellwig21875502009-08-31 20:58:21 -03001325 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1326 XFS_LOOKUP_EQ, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001327 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001329
1330 if (i == 1) {
1331 error = xfs_inobt_get_rec(cur, &rec, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (error)
1333 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001334
1335 if (j == 1 && rec.ir_freecount > 0) {
1336 /*
1337 * The last chunk allocated in the group
1338 * still has a free inode.
1339 */
1340 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342 }
Dave Chinnerbd169562009-08-31 20:58:28 -03001343 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001344
Dave Chinnerbd169562009-08-31 20:58:28 -03001345 /*
1346 * None left in the last group, search the whole AG
1347 */
1348 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1349 if (error)
1350 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001351 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Dave Chinnerbd169562009-08-31 20:58:28 -03001352
1353 for (;;) {
1354 error = xfs_inobt_get_rec(cur, &rec, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001355 if (error)
1356 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001357 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Dave Chinnerbd169562009-08-31 20:58:28 -03001358 if (rec.ir_freecount > 0)
1359 break;
1360 error = xfs_btree_increment(cur, 0, &i);
1361 if (error)
1362 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001363 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001365
1366alloc_inode:
Brian Fosterd4cc5402015-05-29 08:50:21 +10001367 offset = xfs_inobt_first_free_inode(&rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 ASSERT(offset >= 0);
1369 ASSERT(offset < XFS_INODES_PER_CHUNK);
1370 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1371 XFS_INODES_PER_CHUNK) == 0);
1372 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001373 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 rec.ir_freecount--;
Christoph Hellwigafabc242009-08-31 20:57:03 -03001375 error = xfs_inobt_update(cur, &rec);
1376 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001378 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001380 pag->pagi_freecount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Dave Chinner0b48db82009-08-31 20:57:09 -03001382 error = xfs_check_agi_freecount(cur, agi);
1383 if (error)
1384 goto error0;
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1387 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
Dave Chinner44b56e02010-01-11 11:47:43 +00001388 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 *inop = ino;
1390 return 0;
1391error1:
1392 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1393error0:
1394 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
Dave Chinner44b56e02010-01-11 11:47:43 +00001395 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return error;
1397}
1398
1399/*
Brian Foster6dd86382014-04-24 16:00:53 +10001400 * Use the free inode btree to allocate an inode based on distance from the
1401 * parent. Note that the provided cursor may be deleted and replaced.
1402 */
1403STATIC int
1404xfs_dialloc_ag_finobt_near(
1405 xfs_agino_t pagino,
1406 struct xfs_btree_cur **ocur,
1407 struct xfs_inobt_rec_incore *rec)
1408{
1409 struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
1410 struct xfs_btree_cur *rcur; /* right search cursor */
1411 struct xfs_inobt_rec_incore rrec;
1412 int error;
1413 int i, j;
1414
1415 error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
1416 if (error)
1417 return error;
1418
1419 if (i == 1) {
1420 error = xfs_inobt_get_rec(lcur, rec, &i);
1421 if (error)
1422 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001423 XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001424
1425 /*
1426 * See if we've landed in the parent inode record. The finobt
1427 * only tracks chunks with at least one free inode, so record
1428 * existence is enough.
1429 */
1430 if (pagino >= rec->ir_startino &&
1431 pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
1432 return 0;
1433 }
1434
1435 error = xfs_btree_dup_cursor(lcur, &rcur);
1436 if (error)
1437 return error;
1438
1439 error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
1440 if (error)
1441 goto error_rcur;
1442 if (j == 1) {
1443 error = xfs_inobt_get_rec(rcur, &rrec, &j);
1444 if (error)
1445 goto error_rcur;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001446 XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur);
Brian Foster6dd86382014-04-24 16:00:53 +10001447 }
1448
Eric Sandeenc29aad42015-02-23 22:39:08 +11001449 XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur);
Brian Foster6dd86382014-04-24 16:00:53 +10001450 if (i == 1 && j == 1) {
1451 /*
1452 * Both the left and right records are valid. Choose the closer
1453 * inode chunk to the target.
1454 */
1455 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
1456 (rrec.ir_startino - pagino)) {
1457 *rec = rrec;
1458 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1459 *ocur = rcur;
1460 } else {
1461 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1462 }
1463 } else if (j == 1) {
1464 /* only the right record is valid */
1465 *rec = rrec;
1466 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1467 *ocur = rcur;
1468 } else if (i == 1) {
1469 /* only the left record is valid */
1470 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1471 }
1472
1473 return 0;
1474
1475error_rcur:
1476 xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
1477 return error;
1478}
1479
1480/*
1481 * Use the free inode btree to find a free inode based on a newino hint. If
1482 * the hint is NULL, find the first free inode in the AG.
1483 */
1484STATIC int
1485xfs_dialloc_ag_finobt_newino(
1486 struct xfs_agi *agi,
1487 struct xfs_btree_cur *cur,
1488 struct xfs_inobt_rec_incore *rec)
1489{
1490 int error;
1491 int i;
1492
1493 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
Dave Chinnere68ed772014-09-29 10:43:15 +10001494 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1495 XFS_LOOKUP_EQ, &i);
Brian Foster6dd86382014-04-24 16:00:53 +10001496 if (error)
1497 return error;
1498 if (i == 1) {
1499 error = xfs_inobt_get_rec(cur, rec, &i);
1500 if (error)
1501 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001502 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001503 return 0;
1504 }
1505 }
1506
1507 /*
1508 * Find the first inode available in the AG.
1509 */
1510 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1511 if (error)
1512 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001513 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001514
1515 error = xfs_inobt_get_rec(cur, rec, &i);
1516 if (error)
1517 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001518 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001519
1520 return 0;
1521}
1522
1523/*
1524 * Update the inobt based on a modification made to the finobt. Also ensure that
1525 * the records from both trees are equivalent post-modification.
1526 */
1527STATIC int
1528xfs_dialloc_ag_update_inobt(
1529 struct xfs_btree_cur *cur, /* inobt cursor */
1530 struct xfs_inobt_rec_incore *frec, /* finobt record */
1531 int offset) /* inode offset */
1532{
1533 struct xfs_inobt_rec_incore rec;
1534 int error;
1535 int i;
1536
1537 error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
1538 if (error)
1539 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001540 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001541
1542 error = xfs_inobt_get_rec(cur, &rec, &i);
1543 if (error)
1544 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001545 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001546 ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
1547 XFS_INODES_PER_CHUNK) == 0);
1548
1549 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1550 rec.ir_freecount--;
1551
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001552 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) &&
Brian Foster6dd86382014-04-24 16:00:53 +10001553 (rec.ir_freecount == frec->ir_freecount));
1554
kbuild test robotb72091f2014-12-01 08:24:58 +11001555 return xfs_inobt_update(cur, &rec);
Brian Foster6dd86382014-04-24 16:00:53 +10001556}
1557
1558/*
1559 * Allocate an inode using the free inode btree, if available. Otherwise, fall
1560 * back to the inobt search algorithm.
1561 *
1562 * The caller selected an AG for us, and made sure that free inodes are
1563 * available.
1564 */
1565STATIC int
1566xfs_dialloc_ag(
1567 struct xfs_trans *tp,
1568 struct xfs_buf *agbp,
1569 xfs_ino_t parent,
1570 xfs_ino_t *inop)
1571{
1572 struct xfs_mount *mp = tp->t_mountp;
1573 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1574 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1575 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1576 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1577 struct xfs_perag *pag;
1578 struct xfs_btree_cur *cur; /* finobt cursor */
1579 struct xfs_btree_cur *icur; /* inobt cursor */
1580 struct xfs_inobt_rec_incore rec;
1581 xfs_ino_t ino;
1582 int error;
1583 int offset;
1584 int i;
1585
1586 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
1587 return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
1588
1589 pag = xfs_perag_get(mp, agno);
1590
1591 /*
1592 * If pagino is 0 (this is the root inode allocation) use newino.
1593 * This must work because we've just allocated some.
1594 */
1595 if (!pagino)
1596 pagino = be32_to_cpu(agi->agi_newino);
1597
1598 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1599
1600 error = xfs_check_agi_freecount(cur, agi);
1601 if (error)
1602 goto error_cur;
1603
1604 /*
1605 * The search algorithm depends on whether we're in the same AG as the
1606 * parent. If so, find the closest available inode to the parent. If
1607 * not, consider the agi hint or find the first free inode in the AG.
1608 */
1609 if (agno == pagno)
1610 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
1611 else
1612 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
1613 if (error)
1614 goto error_cur;
1615
Brian Fosterd4cc5402015-05-29 08:50:21 +10001616 offset = xfs_inobt_first_free_inode(&rec);
Brian Foster6dd86382014-04-24 16:00:53 +10001617 ASSERT(offset >= 0);
1618 ASSERT(offset < XFS_INODES_PER_CHUNK);
1619 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1620 XFS_INODES_PER_CHUNK) == 0);
1621 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1622
1623 /*
1624 * Modify or remove the finobt record.
1625 */
1626 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1627 rec.ir_freecount--;
1628 if (rec.ir_freecount)
1629 error = xfs_inobt_update(cur, &rec);
1630 else
1631 error = xfs_btree_delete(cur, &i);
1632 if (error)
1633 goto error_cur;
1634
1635 /*
1636 * The finobt has now been updated appropriately. We haven't updated the
1637 * agi and superblock yet, so we can create an inobt cursor and validate
1638 * the original freecount. If all is well, make the equivalent update to
1639 * the inobt using the finobt record and offset information.
1640 */
1641 icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1642
1643 error = xfs_check_agi_freecount(icur, agi);
1644 if (error)
1645 goto error_icur;
1646
1647 error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
1648 if (error)
1649 goto error_icur;
1650
1651 /*
1652 * Both trees have now been updated. We must update the perag and
1653 * superblock before we can check the freecount for each btree.
1654 */
1655 be32_add_cpu(&agi->agi_freecount, -1);
1656 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1657 pag->pagi_freecount--;
1658
1659 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1660
1661 error = xfs_check_agi_freecount(icur, agi);
1662 if (error)
1663 goto error_icur;
1664 error = xfs_check_agi_freecount(cur, agi);
1665 if (error)
1666 goto error_icur;
1667
1668 xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
1669 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1670 xfs_perag_put(pag);
1671 *inop = ino;
1672 return 0;
1673
1674error_icur:
1675 xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
1676error_cur:
1677 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1678 xfs_perag_put(pag);
1679 return error;
1680}
1681
1682/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001683 * Allocate an inode on disk.
1684 *
1685 * Mode is used to tell whether the new inode will need space, and whether it
1686 * is a directory.
1687 *
1688 * This function is designed to be called twice if it has to do an allocation
1689 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
1690 * If an inode is available without having to performn an allocation, an inode
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001691 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
1692 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
1693 * The caller should then commit the current transaction, allocate a
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001694 * new transaction, and call xfs_dialloc() again, passing in the previous value
1695 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
1696 * buffer is locked across the two calls, the second call is guaranteed to have
1697 * a free inode available.
1698 *
1699 * Once we successfully pick an inode its number is returned and the on-disk
1700 * data structures are updated. The inode itself is not read in, since doing so
1701 * would break ordering constraints with xfs_reclaim.
1702 */
1703int
1704xfs_dialloc(
1705 struct xfs_trans *tp,
1706 xfs_ino_t parent,
1707 umode_t mode,
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001708 struct xfs_buf **IO_agbp,
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001709 xfs_ino_t *inop)
1710{
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001711 struct xfs_mount *mp = tp->t_mountp;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001712 struct xfs_buf *agbp;
1713 xfs_agnumber_t agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001714 int error;
1715 int ialloced;
1716 int noroom = 0;
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001717 xfs_agnumber_t start_agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001718 struct xfs_perag *pag;
Christoph Hellwigf59cf5c2017-12-04 17:32:55 -08001719 int okalloc = 1;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001720
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001721 if (*IO_agbp) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001722 /*
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001723 * If the caller passes in a pointer to the AGI buffer,
1724 * continue where we left off before. In this case, we
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001725 * know that the allocation group has free inodes.
1726 */
1727 agbp = *IO_agbp;
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001728 goto out_alloc;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001729 }
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001730
1731 /*
1732 * We do not have an agbp, so select an initial allocation
1733 * group for inode allocation.
1734 */
Christoph Hellwigf59cf5c2017-12-04 17:32:55 -08001735 start_agno = xfs_ialloc_ag_select(tp, parent, mode);
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001736 if (start_agno == NULLAGNUMBER) {
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001737 *inop = NULLFSINO;
1738 return 0;
1739 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001740
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001741 /*
1742 * If we have already hit the ceiling of inode blocks then clear
1743 * okalloc so we scan all available agi structures for a free
1744 * inode.
George Wang74f9ce12015-05-29 07:39:34 +10001745 *
1746 * Read rough value of mp->m_icount by percpu_counter_read_positive,
1747 * which will sacrifice the preciseness but improve the performance.
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001748 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001749 if (mp->m_maxicount &&
George Wang74f9ce12015-05-29 07:39:34 +10001750 percpu_counter_read_positive(&mp->m_icount) + mp->m_ialloc_inos
1751 > mp->m_maxicount) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001752 noroom = 1;
1753 okalloc = 0;
1754 }
1755
1756 /*
1757 * Loop until we find an allocation group that either has free inodes
1758 * or in which we can allocate some inodes. Iterate through the
1759 * allocation groups upward, wrapping at the end.
1760 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001761 agno = start_agno;
1762 for (;;) {
1763 pag = xfs_perag_get(mp, agno);
1764 if (!pag->pagi_inodeok) {
1765 xfs_ialloc_next_ag(mp);
1766 goto nextag;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001767 }
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001768
1769 if (!pag->pagi_init) {
1770 error = xfs_ialloc_pagi_init(mp, tp, agno);
1771 if (error)
1772 goto out_error;
1773 }
1774
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001775 /*
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001776 * Do a first racy fast path check if this AG is usable.
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001777 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001778 if (!pag->pagi_freecount && !okalloc)
1779 goto nextag;
1780
Christoph Hellwigc4982112012-08-07 02:02:02 -04001781 /*
1782 * Then read in the AGI buffer and recheck with the AGI buffer
1783 * lock held.
1784 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001785 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1786 if (error)
1787 goto out_error;
1788
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001789 if (pag->pagi_freecount) {
1790 xfs_perag_put(pag);
1791 goto out_alloc;
1792 }
1793
Christoph Hellwigc4982112012-08-07 02:02:02 -04001794 if (!okalloc)
1795 goto nextag_relse_buffer;
1796
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001797
1798 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1799 if (error) {
1800 xfs_trans_brelse(tp, agbp);
1801
Dave Chinner24513372014-06-25 14:58:08 +10001802 if (error != -ENOSPC)
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001803 goto out_error;
1804
1805 xfs_perag_put(pag);
1806 *inop = NULLFSINO;
1807 return 0;
1808 }
1809
1810 if (ialloced) {
1811 /*
1812 * We successfully allocated some inodes, return
1813 * the current context to the caller so that it
1814 * can commit the current transaction and call
1815 * us again where we left off.
1816 */
1817 ASSERT(pag->pagi_freecount > 0);
1818 xfs_perag_put(pag);
1819
1820 *IO_agbp = agbp;
1821 *inop = NULLFSINO;
1822 return 0;
1823 }
1824
Christoph Hellwigc4982112012-08-07 02:02:02 -04001825nextag_relse_buffer:
1826 xfs_trans_brelse(tp, agbp);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001827nextag:
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001828 xfs_perag_put(pag);
1829 if (++agno == mp->m_sb.sb_agcount)
1830 agno = 0;
1831 if (agno == start_agno) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001832 *inop = NULLFSINO;
Dave Chinner24513372014-06-25 14:58:08 +10001833 return noroom ? -ENOSPC : 0;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001834 }
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001835 }
1836
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001837out_alloc:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001838 *IO_agbp = NULL;
1839 return xfs_dialloc_ag(tp, agbp, parent, inop);
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001840out_error:
1841 xfs_perag_put(pag);
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001842 return error;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001843}
1844
Brian Foster10ae3dc72015-05-29 09:22:52 +10001845/*
1846 * Free the blocks of an inode chunk. We must consider that the inode chunk
1847 * might be sparse and only free the regions that are allocated as part of the
1848 * chunk.
1849 */
1850STATIC void
1851xfs_difree_inode_chunk(
1852 struct xfs_mount *mp,
1853 xfs_agnumber_t agno,
1854 struct xfs_inobt_rec_incore *rec,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001855 struct xfs_defer_ops *dfops)
Brian Foster10ae3dc72015-05-29 09:22:52 +10001856{
1857 xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp, rec->ir_startino);
1858 int startidx, endidx;
1859 int nextbit;
1860 xfs_agblock_t agbno;
1861 int contigblk;
Darrick J. Wong340785c2016-08-03 11:33:42 +10001862 struct xfs_owner_info oinfo;
Brian Foster10ae3dc72015-05-29 09:22:52 +10001863 DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
Darrick J. Wong340785c2016-08-03 11:33:42 +10001864 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
Brian Foster10ae3dc72015-05-29 09:22:52 +10001865
1866 if (!xfs_inobt_issparse(rec->ir_holemask)) {
1867 /* not sparse, calculate extent info directly */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001868 xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, sagbno),
Darrick J. Wong340785c2016-08-03 11:33:42 +10001869 mp->m_ialloc_blks, &oinfo);
Brian Foster10ae3dc72015-05-29 09:22:52 +10001870 return;
1871 }
1872
1873 /* holemask is only 16-bits (fits in an unsigned long) */
1874 ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
1875 holemask[0] = rec->ir_holemask;
1876
1877 /*
1878 * Find contiguous ranges of zeroes (i.e., allocated regions) in the
1879 * holemask and convert the start/end index of each range to an extent.
1880 * We start with the start and end index both pointing at the first 0 in
1881 * the mask.
1882 */
1883 startidx = endidx = find_first_zero_bit(holemask,
1884 XFS_INOBT_HOLEMASK_BITS);
1885 nextbit = startidx + 1;
1886 while (startidx < XFS_INOBT_HOLEMASK_BITS) {
1887 nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
1888 nextbit);
1889 /*
1890 * If the next zero bit is contiguous, update the end index of
1891 * the current range and continue.
1892 */
1893 if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
1894 nextbit == endidx + 1) {
1895 endidx = nextbit;
1896 goto next;
1897 }
1898
1899 /*
1900 * nextbit is not contiguous with the current end index. Convert
1901 * the current start/end to an extent and add it to the free
1902 * list.
1903 */
1904 agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
1905 mp->m_sb.sb_inopblock;
1906 contigblk = ((endidx - startidx + 1) *
1907 XFS_INODES_PER_HOLEMASK_BIT) /
1908 mp->m_sb.sb_inopblock;
1909
1910 ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
1911 ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001912 xfs_bmap_add_free(mp, dfops, XFS_AGB_TO_FSB(mp, agno, agbno),
Darrick J. Wong340785c2016-08-03 11:33:42 +10001913 contigblk, &oinfo);
Brian Foster10ae3dc72015-05-29 09:22:52 +10001914
1915 /* reset range to current bit and carry on... */
1916 startidx = endidx = nextbit;
1917
1918next:
1919 nextbit++;
1920 }
1921}
1922
Brian Foster2b64ee52014-04-24 16:00:53 +10001923STATIC int
1924xfs_difree_inobt(
1925 struct xfs_mount *mp,
1926 struct xfs_trans *tp,
1927 struct xfs_buf *agbp,
1928 xfs_agino_t agino,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001929 struct xfs_defer_ops *dfops,
Brian Foster09b56602015-05-29 09:26:03 +10001930 struct xfs_icluster *xic,
Brian Foster2b64ee52014-04-24 16:00:53 +10001931 struct xfs_inobt_rec_incore *orec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Brian Foster2b64ee52014-04-24 16:00:53 +10001933 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1934 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1935 struct xfs_perag *pag;
1936 struct xfs_btree_cur *cur;
1937 struct xfs_inobt_rec_incore rec;
1938 int ilen;
1939 int error;
1940 int i;
1941 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001943 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Brian Foster2b64ee52014-04-24 16:00:53 +10001944 ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /*
1947 * Initialize the cursor.
1948 */
Brian Foster57bd3db2014-04-24 16:00:50 +10001949 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Dave Chinner0b48db82009-08-31 20:57:09 -03001951 error = xfs_check_agi_freecount(cur, agi);
1952 if (error)
1953 goto error0;
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /*
1956 * Look for the entry describing this inode.
1957 */
Christoph Hellwig21875502009-08-31 20:58:21 -03001958 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001959 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1960 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 goto error0;
1962 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11001963 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001964 error = xfs_inobt_get_rec(cur, &rec, &i);
1965 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001966 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1967 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 goto error0;
1969 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11001970 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /*
1972 * Get the offset in the inode chunk.
1973 */
1974 off = agino - rec.ir_startino;
1975 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001976 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /*
1978 * Mark the inode free & increment the count.
1979 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001980 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 rec.ir_freecount++;
1982
1983 /*
Brian Foster999633d2015-05-29 08:51:37 +10001984 * When an inode chunk is free, it becomes eligible for removal. Don't
1985 * remove the chunk if the block size is large enough for multiple inode
1986 * chunks (that might not be free).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001988 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Brian Foster999633d2015-05-29 08:51:37 +10001989 rec.ir_free == XFS_INOBT_ALL_FREE &&
1990 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001991 xic->deleted = true;
Brian Foster09b56602015-05-29 09:26:03 +10001992 xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1993 xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 /*
1996 * Remove the inode cluster from the AGI B+Tree, adjust the
1997 * AGI and Superblock inode counts, and mark the disk space
1998 * to be freed when the transaction is committed.
1999 */
Brian Foster999633d2015-05-29 08:51:37 +10002000 ilen = rec.ir_freecount;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002001 be32_add_cpu(&agi->agi_count, -ilen);
2002 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00002004 pag = xfs_perag_get(mp, agno);
2005 pag->pagi_freecount -= ilen - 1;
2006 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
2008 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
2009
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002010 if ((error = xfs_btree_delete(cur, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002011 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
2012 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto error0;
2014 }
2015
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002016 xfs_difree_inode_chunk(mp, agno, &rec, dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 } else {
Thomas Meyer749f24f2017-10-09 11:38:54 -07002018 xic->deleted = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Christoph Hellwigafabc242009-08-31 20:57:03 -03002020 error = xfs_inobt_update(cur, &rec);
2021 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002022 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
2023 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto error0;
2025 }
Christoph Hellwigafabc242009-08-31 20:57:03 -03002026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /*
2028 * Change the inode free counts and log the ag/sb changes.
2029 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002030 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00002032 pag = xfs_perag_get(mp, agno);
2033 pag->pagi_freecount++;
2034 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
2036 }
2037
Dave Chinner0b48db82009-08-31 20:57:09 -03002038 error = xfs_check_agi_freecount(cur, agi);
2039 if (error)
2040 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Brian Foster2b64ee52014-04-24 16:00:53 +10002042 *orec = rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2044 return 0;
2045
2046error0:
2047 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2048 return error;
2049}
2050
Brian Foster2b64ee52014-04-24 16:00:53 +10002051/*
Brian Foster3efa4ff2014-04-24 16:00:53 +10002052 * Free an inode in the free inode btree.
2053 */
2054STATIC int
2055xfs_difree_finobt(
2056 struct xfs_mount *mp,
2057 struct xfs_trans *tp,
2058 struct xfs_buf *agbp,
2059 xfs_agino_t agino,
2060 struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
2061{
2062 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
2063 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
2064 struct xfs_btree_cur *cur;
2065 struct xfs_inobt_rec_incore rec;
2066 int offset = agino - ibtrec->ir_startino;
2067 int error;
2068 int i;
2069
2070 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
2071
2072 error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
2073 if (error)
2074 goto error;
2075 if (i == 0) {
2076 /*
2077 * If the record does not exist in the finobt, we must have just
2078 * freed an inode in a previously fully allocated chunk. If not,
2079 * something is out of sync.
2080 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002081 XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error);
Brian Foster3efa4ff2014-04-24 16:00:53 +10002082
Brian Foster54190402015-05-29 09:03:04 +10002083 error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
2084 ibtrec->ir_count,
2085 ibtrec->ir_freecount,
Brian Foster3efa4ff2014-04-24 16:00:53 +10002086 ibtrec->ir_free, &i);
2087 if (error)
2088 goto error;
2089 ASSERT(i == 1);
2090
2091 goto out;
2092 }
2093
2094 /*
2095 * Read and update the existing record. We could just copy the ibtrec
2096 * across here, but that would defeat the purpose of having redundant
2097 * metadata. By making the modifications independently, we can catch
2098 * corruptions that we wouldn't see if we just copied from one record
2099 * to another.
2100 */
2101 error = xfs_inobt_get_rec(cur, &rec, &i);
2102 if (error)
2103 goto error;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002104 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
Brian Foster3efa4ff2014-04-24 16:00:53 +10002105
2106 rec.ir_free |= XFS_INOBT_MASK(offset);
2107 rec.ir_freecount++;
2108
Eric Sandeenc29aad42015-02-23 22:39:08 +11002109 XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) &&
Brian Foster3efa4ff2014-04-24 16:00:53 +10002110 (rec.ir_freecount == ibtrec->ir_freecount),
2111 error);
2112
2113 /*
2114 * The content of inobt records should always match between the inobt
2115 * and finobt. The lifecycle of records in the finobt is different from
2116 * the inobt in that the finobt only tracks records with at least one
2117 * free inode. Hence, if all of the inodes are free and we aren't
2118 * keeping inode chunks permanently on disk, remove the record.
2119 * Otherwise, update the record with the new information.
Brian Foster999633d2015-05-29 08:51:37 +10002120 *
2121 * Note that we currently can't free chunks when the block size is large
2122 * enough for multiple chunks. Leave the finobt record to remain in sync
2123 * with the inobt.
Brian Foster3efa4ff2014-04-24 16:00:53 +10002124 */
Brian Foster999633d2015-05-29 08:51:37 +10002125 if (rec.ir_free == XFS_INOBT_ALL_FREE &&
2126 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
Brian Foster3efa4ff2014-04-24 16:00:53 +10002127 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
2128 error = xfs_btree_delete(cur, &i);
2129 if (error)
2130 goto error;
2131 ASSERT(i == 1);
2132 } else {
2133 error = xfs_inobt_update(cur, &rec);
2134 if (error)
2135 goto error;
2136 }
2137
2138out:
2139 error = xfs_check_agi_freecount(cur, agi);
2140 if (error)
2141 goto error;
2142
2143 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2144 return 0;
2145
2146error:
2147 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2148 return error;
2149}
2150
2151/*
Brian Foster2b64ee52014-04-24 16:00:53 +10002152 * Free disk inode. Carefully avoids touching the incore inode, all
2153 * manipulations incore are the caller's responsibility.
2154 * The on-disk inode is not changed by this operation, only the
2155 * btree (free inode mask) is changed.
2156 */
2157int
2158xfs_difree(
2159 struct xfs_trans *tp, /* transaction pointer */
2160 xfs_ino_t inode, /* inode to be freed */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002161 struct xfs_defer_ops *dfops, /* extents to free */
Brian Foster09b56602015-05-29 09:26:03 +10002162 struct xfs_icluster *xic) /* cluster info if deleted */
Brian Foster2b64ee52014-04-24 16:00:53 +10002163{
2164 /* REFERENCED */
2165 xfs_agblock_t agbno; /* block number containing inode */
2166 struct xfs_buf *agbp; /* buffer for allocation group header */
2167 xfs_agino_t agino; /* allocation group inode number */
2168 xfs_agnumber_t agno; /* allocation group number */
2169 int error; /* error return value */
2170 struct xfs_mount *mp; /* mount structure for filesystem */
2171 struct xfs_inobt_rec_incore rec;/* btree record */
2172
2173 mp = tp->t_mountp;
2174
2175 /*
2176 * Break up inode number into its components.
2177 */
2178 agno = XFS_INO_TO_AGNO(mp, inode);
2179 if (agno >= mp->m_sb.sb_agcount) {
2180 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
2181 __func__, agno, mp->m_sb.sb_agcount);
2182 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10002183 return -EINVAL;
Brian Foster2b64ee52014-04-24 16:00:53 +10002184 }
2185 agino = XFS_INO_TO_AGINO(mp, inode);
2186 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
2187 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
2188 __func__, (unsigned long long)inode,
2189 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
2190 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10002191 return -EINVAL;
Brian Foster2b64ee52014-04-24 16:00:53 +10002192 }
2193 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2194 if (agbno >= mp->m_sb.sb_agblocks) {
2195 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
2196 __func__, agbno, mp->m_sb.sb_agblocks);
2197 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10002198 return -EINVAL;
Brian Foster2b64ee52014-04-24 16:00:53 +10002199 }
2200 /*
2201 * Get the allocation group header.
2202 */
2203 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
2204 if (error) {
2205 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
2206 __func__, error);
2207 return error;
2208 }
2209
2210 /*
2211 * Fix up the inode allocation btree.
2212 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002213 error = xfs_difree_inobt(mp, tp, agbp, agino, dfops, xic, &rec);
Brian Foster2b64ee52014-04-24 16:00:53 +10002214 if (error)
2215 goto error0;
2216
Brian Foster3efa4ff2014-04-24 16:00:53 +10002217 /*
2218 * Fix up the free inode btree.
2219 */
2220 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
2221 error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
2222 if (error)
2223 goto error0;
2224 }
2225
Brian Foster2b64ee52014-04-24 16:00:53 +10002226 return 0;
2227
2228error0:
2229 return error;
2230}
2231
Dave Chinner7124fe02010-06-24 11:15:33 +10002232STATIC int
2233xfs_imap_lookup(
2234 struct xfs_mount *mp,
2235 struct xfs_trans *tp,
2236 xfs_agnumber_t agno,
2237 xfs_agino_t agino,
2238 xfs_agblock_t agbno,
2239 xfs_agblock_t *chunk_agbno,
2240 xfs_agblock_t *offset_agbno,
2241 int flags)
2242{
2243 struct xfs_inobt_rec_incore rec;
2244 struct xfs_btree_cur *cur;
2245 struct xfs_buf *agbp;
Dave Chinner7124fe02010-06-24 11:15:33 +10002246 int error;
2247 int i;
2248
2249 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
2250 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +11002251 xfs_alert(mp,
2252 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
2253 __func__, error, agno);
Dave Chinner7124fe02010-06-24 11:15:33 +10002254 return error;
2255 }
2256
2257 /*
Dave Chinner4536f2a2010-08-24 11:42:30 +10002258 * Lookup the inode record for the given agino. If the record cannot be
2259 * found, then it's an invalid inode number and we should abort. Once
2260 * we have a record, we need to ensure it contains the inode number
2261 * we are looking up.
Dave Chinner7124fe02010-06-24 11:15:33 +10002262 */
Brian Foster57bd3db2014-04-24 16:00:50 +10002263 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Dave Chinner4536f2a2010-08-24 11:42:30 +10002264 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
Dave Chinner7124fe02010-06-24 11:15:33 +10002265 if (!error) {
2266 if (i)
2267 error = xfs_inobt_get_rec(cur, &rec, &i);
2268 if (!error && i == 0)
Dave Chinner24513372014-06-25 14:58:08 +10002269 error = -EINVAL;
Dave Chinner7124fe02010-06-24 11:15:33 +10002270 }
2271
2272 xfs_trans_brelse(tp, agbp);
Brian Fosterf3070802015-08-19 10:00:53 +10002273 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Dave Chinner7124fe02010-06-24 11:15:33 +10002274 if (error)
2275 return error;
2276
Dave Chinner4536f2a2010-08-24 11:42:30 +10002277 /* check that the returned record contains the required inode */
2278 if (rec.ir_startino > agino ||
Jie Liu71783432013-12-13 15:51:46 +11002279 rec.ir_startino + mp->m_ialloc_inos <= agino)
Dave Chinner24513372014-06-25 14:58:08 +10002280 return -EINVAL;
Dave Chinner4536f2a2010-08-24 11:42:30 +10002281
Dave Chinner7124fe02010-06-24 11:15:33 +10002282 /* for untrusted inodes check it is allocated first */
Dave Chinner19207792010-06-24 11:15:47 +10002283 if ((flags & XFS_IGET_UNTRUSTED) &&
Dave Chinner7124fe02010-06-24 11:15:33 +10002284 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
Dave Chinner24513372014-06-25 14:58:08 +10002285 return -EINVAL;
Dave Chinner7124fe02010-06-24 11:15:33 +10002286
2287 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
2288 *offset_agbno = agbno - *chunk_agbno;
2289 return 0;
2290}
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002293 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002296xfs_imap(
2297 xfs_mount_t *mp, /* file system mount structure */
2298 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002300 struct xfs_imap *imap, /* location map structure */
2301 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 xfs_agino_t agino; /* inode number within alloc group */
2305 xfs_agnumber_t agno; /* allocation group number */
2306 int blks_per_cluster; /* num blocks per inode cluster */
2307 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 int offset; /* index of inode in its buffer */
Dave Chinner836a94a2013-08-12 20:49:44 +10002311 xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 /*
2316 * Split up the inode number into its parts.
2317 */
2318 agno = XFS_INO_TO_AGNO(mp, ino);
2319 agino = XFS_INO_TO_AGINO(mp, ino);
2320 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2321 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
2322 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
2323#ifdef DEBUG
Dave Chinner19207792010-06-24 11:15:47 +10002324 /*
2325 * Don't output diagnostic information for untrusted inodes
2326 * as they can be invalid without implying corruption.
2327 */
2328 if (flags & XFS_IGET_UNTRUSTED)
Dave Chinner24513372014-06-25 14:58:08 +10002329 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner53487782011-03-07 10:05:35 +11002331 xfs_alert(mp,
2332 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
2333 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner53487782011-03-07 10:05:35 +11002336 xfs_alert(mp,
2337 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
2338 __func__, (unsigned long long)agbno,
2339 (unsigned long)mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner53487782011-03-07 10:05:35 +11002342 xfs_alert(mp,
2343 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
2344 __func__, ino,
2345 XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
Nathan Scott745b1f472006-09-28 11:02:23 +10002347 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348#endif /* DEBUG */
Dave Chinner24513372014-06-25 14:58:08 +10002349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002351
Jie Liuf9e5abc2013-12-13 15:51:49 +11002352 blks_per_cluster = xfs_icluster_size_fsb(mp);
Dave Chinner7124fe02010-06-24 11:15:33 +10002353
2354 /*
2355 * For bulkstat and handle lookups, we have an untrusted inode number
2356 * that we have to verify is valid. We cannot do this just by reading
2357 * the inode buffer as it may have been unlinked and removed leaving
2358 * inodes in stale state on disk. Hence we have to do a btree lookup
2359 * in all cases where an untrusted inode number is passed.
2360 */
Dave Chinner19207792010-06-24 11:15:47 +10002361 if (flags & XFS_IGET_UNTRUSTED) {
Dave Chinner7124fe02010-06-24 11:15:33 +10002362 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
2363 &chunk_agbno, &offset_agbno, flags);
2364 if (error)
2365 return error;
2366 goto out_map;
2367 }
2368
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002369 /*
2370 * If the inode cluster size is the same as the blocksize or
2371 * smaller we get to the buffer by simple arithmetics.
2372 */
Jie Liuf9e5abc2013-12-13 15:51:49 +11002373 if (blks_per_cluster == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 offset = XFS_INO_TO_OFFSET(mp, ino);
2375 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002376
2377 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
2378 imap->im_len = XFS_FSB_TO_BB(mp, 1);
Darrick J. Wong755c7bf2016-11-08 11:55:48 +11002379 imap->im_boffset = (unsigned short)(offset <<
2380 mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 0;
2382 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002383
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002384 /*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002385 * If the inode chunks are aligned then use simple maths to
2386 * find the location. Otherwise we have to do a btree
2387 * lookup to find the location.
2388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (mp->m_inoalign_mask) {
2390 offset_agbno = agbno & mp->m_inoalign_mask;
2391 chunk_agbno = agbno - offset_agbno;
2392 } else {
Dave Chinner7124fe02010-06-24 11:15:33 +10002393 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
2394 &chunk_agbno, &offset_agbno, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 if (error)
2396 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002398
Dave Chinner7124fe02010-06-24 11:15:33 +10002399out_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 ASSERT(agbno >= chunk_agbno);
2401 cluster_agbno = chunk_agbno +
2402 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
2403 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
2404 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002405
2406 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
2407 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
Darrick J. Wong755c7bf2016-11-08 11:55:48 +11002408 imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002409
2410 /*
2411 * If the inode number maps to a block outside the bounds
2412 * of the file system then return NULL rather than calling
2413 * read_buf and panicing when we get an error from the
2414 * driver.
2415 */
2416 if ((imap->im_blkno + imap->im_len) >
2417 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
Dave Chinner53487782011-03-07 10:05:35 +11002418 xfs_alert(mp,
2419 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
2420 __func__, (unsigned long long) imap->im_blkno,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002421 (unsigned long long) imap->im_len,
2422 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
Dave Chinner24513372014-06-25 14:58:08 +10002423 return -EINVAL;
Christoph Hellwig94e1b692008-11-28 14:23:41 +11002424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
2428/*
2429 * Compute and fill in value of m_in_maxlevels.
2430 */
2431void
2432xfs_ialloc_compute_maxlevels(
2433 xfs_mount_t *mp) /* file system mount structure */
2434{
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10002435 uint inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10002437 inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
Eric Sandeena1f69412018-04-06 10:09:42 -07002438 mp->m_in_maxlevels = xfs_btree_compute_maxlevels(mp->m_inobt_mnr,
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10002439 inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
2442/*
Brian Fosteraafc3c22014-04-24 16:00:52 +10002443 * Log specified fields for the ag hdr (inode section). The growth of the agi
2444 * structure over time requires that we interpret the buffer as two logical
2445 * regions delineated by the end of the unlinked list. This is due to the size
2446 * of the hash table and its location in the middle of the agi.
2447 *
2448 * For example, a request to log a field before agi_unlinked and a field after
2449 * agi_unlinked could cause us to log the entire hash table and use an excessive
2450 * amount of log space. To avoid this behavior, log the region up through
2451 * agi_unlinked in one call and the region after agi_unlinked through the end of
2452 * the structure in another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 */
2454void
2455xfs_ialloc_log_agi(
2456 xfs_trans_t *tp, /* transaction pointer */
2457 xfs_buf_t *bp, /* allocation group header buffer */
2458 int fields) /* bitmask of fields to log */
2459{
2460 int first; /* first byte number */
2461 int last; /* last byte number */
2462 static const short offsets[] = { /* field starting offsets */
2463 /* keep in sync with bit definitions */
2464 offsetof(xfs_agi_t, agi_magicnum),
2465 offsetof(xfs_agi_t, agi_versionnum),
2466 offsetof(xfs_agi_t, agi_seqno),
2467 offsetof(xfs_agi_t, agi_length),
2468 offsetof(xfs_agi_t, agi_count),
2469 offsetof(xfs_agi_t, agi_root),
2470 offsetof(xfs_agi_t, agi_level),
2471 offsetof(xfs_agi_t, agi_freecount),
2472 offsetof(xfs_agi_t, agi_newino),
2473 offsetof(xfs_agi_t, agi_dirino),
2474 offsetof(xfs_agi_t, agi_unlinked),
Brian Fosteraafc3c22014-04-24 16:00:52 +10002475 offsetof(xfs_agi_t, agi_free_root),
2476 offsetof(xfs_agi_t, agi_free_level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 sizeof(xfs_agi_t)
2478 };
2479#ifdef DEBUG
2480 xfs_agi_t *agi; /* allocation group header */
2481
2482 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002483 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484#endif
Brian Fosteraafc3c22014-04-24 16:00:52 +10002485
Brian Fosteraafc3c22014-04-24 16:00:52 +10002486 /*
2487 * Compute byte offsets for the first and last fields in the first
2488 * region and log the agi buffer. This only logs up through
2489 * agi_unlinked.
2490 */
2491 if (fields & XFS_AGI_ALL_BITS_R1) {
2492 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
2493 &first, &last);
2494 xfs_trans_log_buf(tp, bp, first, last);
2495 }
2496
2497 /*
2498 * Mask off the bits in the first region and calculate the first and
2499 * last field offsets for any bits in the second region.
2500 */
2501 fields &= ~XFS_AGI_ALL_BITS_R1;
2502 if (fields) {
2503 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
2504 &first, &last);
2505 xfs_trans_log_buf(tp, bp, first, last);
2506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002509static xfs_failaddr_t
Dave Chinner612cfbf2012-11-14 17:52:32 +11002510xfs_agi_verify(
Dave Chinner3702ce62012-11-12 22:54:05 +11002511 struct xfs_buf *bp)
2512{
2513 struct xfs_mount *mp = bp->b_target->bt_mount;
2514 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
Dave Chinner9f96cc92018-06-03 16:12:16 -07002515 int i;
Dave Chinner3702ce62012-11-12 22:54:05 +11002516
Brian Fostera45086e2015-10-12 15:59:25 +11002517 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2518 if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002519 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +11002520 if (!xfs_log_check_lsn(mp,
2521 be64_to_cpu(XFS_BUF_TO_AGI(bp)->agi_lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002522 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +11002523 }
2524
Dave Chinner3702ce62012-11-12 22:54:05 +11002525 /*
2526 * Validate the magic number of the agi block.
2527 */
Dave Chinner983d09f2013-04-03 16:11:15 +11002528 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002529 return __this_address;
Dave Chinner983d09f2013-04-03 16:11:15 +11002530 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002531 return __this_address;
Dave Chinner3702ce62012-11-12 22:54:05 +11002532
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002533 if (be32_to_cpu(agi->agi_level) < 1 ||
2534 be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002535 return __this_address;
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002536
2537 if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
2538 (be32_to_cpu(agi->agi_free_level) < 1 ||
2539 be32_to_cpu(agi->agi_free_level) > XFS_BTREE_MAXLEVELS))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002540 return __this_address;
Darrick J. Wongd2a047f2016-12-05 12:32:50 +11002541
Dave Chinner3702ce62012-11-12 22:54:05 +11002542 /*
2543 * during growfs operations, the perag is not fully initialised,
2544 * so we can't use it for any useful checking. growfs ensures we can't
2545 * use it by using uncached buffers that don't have the perag attached
2546 * so we can detect and avoid this problem.
2547 */
Dave Chinner983d09f2013-04-03 16:11:15 +11002548 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002549 return __this_address;
Dave Chinner3702ce62012-11-12 22:54:05 +11002550
Dave Chinner9f96cc92018-06-03 16:12:16 -07002551 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
2552 if (agi->agi_unlinked[i] == NULLAGINO)
2553 continue;
2554 if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i])))
2555 return __this_address;
2556 }
2557
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08002558 return NULL;
Dave Chinner612cfbf2012-11-14 17:52:32 +11002559}
2560
Dave Chinner1813dd62012-11-14 17:54:40 +11002561static void
2562xfs_agi_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002563 struct xfs_buf *bp)
2564{
Dave Chinner983d09f2013-04-03 16:11:15 +11002565 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -08002566 xfs_failaddr_t fa;
Dave Chinner983d09f2013-04-03 16:11:15 +11002567
Eric Sandeence5028c2014-02-27 15:23:10 +11002568 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2569 !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -08002570 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
2571 else {
2572 fa = xfs_agi_verify(bp);
2573 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
2574 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2575 }
Dave Chinner612cfbf2012-11-14 17:52:32 +11002576}
2577
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002578static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002579xfs_agi_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002580 struct xfs_buf *bp)
2581{
Carlos Maiolinofb1755a2018-01-24 13:38:48 -08002582 struct xfs_mount *mp = bp->b_target->bt_mount;
2583 struct xfs_buf_log_item *bip = bp->b_log_item;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -08002584 xfs_failaddr_t fa;
Dave Chinner983d09f2013-04-03 16:11:15 +11002585
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -08002586 fa = xfs_agi_verify(bp);
2587 if (fa) {
2588 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinner983d09f2013-04-03 16:11:15 +11002589 return;
2590 }
2591
2592 if (!xfs_sb_version_hascrc(&mp->m_sb))
2593 return;
2594
2595 if (bip)
2596 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002597 xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
Dave Chinner3702ce62012-11-12 22:54:05 +11002598}
2599
Dave Chinner1813dd62012-11-14 17:54:40 +11002600const struct xfs_buf_ops xfs_agi_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +11002601 .name = "xfs_agi",
Dave Chinner1813dd62012-11-14 17:54:40 +11002602 .verify_read = xfs_agi_read_verify,
2603 .verify_write = xfs_agi_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -08002604 .verify_struct = xfs_agi_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +11002605};
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607/*
2608 * Read in the allocation group header (inode allocation section)
2609 */
2610int
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002611xfs_read_agi(
2612 struct xfs_mount *mp, /* file system mount structure */
2613 struct xfs_trans *tp, /* transaction pointer */
2614 xfs_agnumber_t agno, /* allocation group number */
2615 struct xfs_buf **bpp) /* allocation group hdr buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002617 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Dave Chinnerd1230312013-11-01 15:27:19 +11002619 trace_xfs_read_agi(mp, agno);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002620
Dave Chinnerd1230312013-11-01 15:27:19 +11002621 ASSERT(agno != NULLAGNUMBER);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002622 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002624 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (error)
2626 return error;
Eric Sandeen200237d2016-12-05 12:31:31 +11002627 if (tp)
2628 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002629
Christoph Hellwig38f23232011-10-10 16:52:45 +00002630 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002631 return 0;
2632}
2633
2634int
2635xfs_ialloc_read_agi(
2636 struct xfs_mount *mp, /* file system mount structure */
2637 struct xfs_trans *tp, /* transaction pointer */
2638 xfs_agnumber_t agno, /* allocation group number */
2639 struct xfs_buf **bpp) /* allocation group hdr buf */
2640{
2641 struct xfs_agi *agi; /* allocation group header */
2642 struct xfs_perag *pag; /* per allocation group data */
2643 int error;
2644
Dave Chinnerd1230312013-11-01 15:27:19 +11002645 trace_xfs_ialloc_read_agi(mp, agno);
2646
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002647 error = xfs_read_agi(mp, tp, agno, bpp);
2648 if (error)
2649 return error;
2650
2651 agi = XFS_BUF_TO_AGI(*bpp);
Dave Chinner44b56e02010-01-11 11:47:43 +00002652 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002654 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10002655 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002659 /*
2660 * It's possible for these to be out of sync if
2661 * we are in the middle of a forced shutdown.
2662 */
2663 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
2664 XFS_FORCED_SHUTDOWN(mp));
Dave Chinner44b56e02010-01-11 11:47:43 +00002665 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return 0;
2667}
David Chinner92821e22007-05-24 15:26:31 +10002668
2669/*
2670 * Read in the agi to initialise the per-ag data in the mount structure
2671 */
2672int
2673xfs_ialloc_pagi_init(
2674 xfs_mount_t *mp, /* file system mount structure */
2675 xfs_trans_t *tp, /* transaction pointer */
2676 xfs_agnumber_t agno) /* allocation group number */
2677{
2678 xfs_buf_t *bp = NULL;
2679 int error;
2680
2681 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
2682 if (error)
2683 return error;
2684 if (bp)
2685 xfs_trans_brelse(tp, bp);
2686 return 0;
2687}
Darrick J. Wong91fb9af2017-10-17 21:37:34 -07002688
2689/* Calculate the first and last possible inode number in an AG. */
2690void
2691xfs_ialloc_agino_range(
2692 struct xfs_mount *mp,
2693 xfs_agnumber_t agno,
2694 xfs_agino_t *first,
2695 xfs_agino_t *last)
2696{
2697 xfs_agblock_t bno;
2698 xfs_agblock_t eoag;
2699
2700 eoag = xfs_ag_block_count(mp, agno);
2701
2702 /*
2703 * Calculate the first inode, which will be in the first
2704 * cluster-aligned block after the AGFL.
2705 */
2706 bno = round_up(XFS_AGFL_BLOCK(mp) + 1,
2707 xfs_ialloc_cluster_alignment(mp));
2708 *first = XFS_OFFBNO_TO_AGINO(mp, bno, 0);
2709
2710 /*
2711 * Calculate the last inode, which will be at the end of the
2712 * last (aligned) cluster that can be allocated in the AG.
2713 */
2714 bno = round_down(eoag, xfs_ialloc_cluster_alignment(mp));
2715 *last = XFS_OFFBNO_TO_AGINO(mp, bno, 0) - 1;
2716}
2717
2718/*
2719 * Verify that an AG inode number pointer neither points outside the AG
2720 * nor points at static metadata.
2721 */
2722bool
2723xfs_verify_agino(
2724 struct xfs_mount *mp,
2725 xfs_agnumber_t agno,
2726 xfs_agino_t agino)
2727{
2728 xfs_agino_t first;
2729 xfs_agino_t last;
2730
2731 xfs_ialloc_agino_range(mp, agno, &first, &last);
2732 return agino >= first && agino <= last;
2733}
2734
2735/*
2736 * Verify that an FS inode number pointer neither points outside the
2737 * filesystem nor points at static AG metadata.
2738 */
2739bool
2740xfs_verify_ino(
2741 struct xfs_mount *mp,
2742 xfs_ino_t ino)
2743{
2744 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ino);
2745 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
2746
2747 if (agno >= mp->m_sb.sb_agcount)
2748 return false;
2749 if (XFS_AGINO_TO_INO(mp, agno, agino) != ino)
2750 return false;
2751 return xfs_verify_agino(mp, agno, agino);
2752}
2753
2754/* Is this an internal inode number? */
2755bool
2756xfs_internal_inum(
2757 struct xfs_mount *mp,
2758 xfs_ino_t ino)
2759{
2760 return ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
2761 (xfs_sb_version_hasquota(&mp->m_sb) &&
2762 xfs_is_quota_inode(&mp->m_sb, ino));
2763}
2764
2765/*
2766 * Verify that a directory entry's inode number doesn't point at an internal
2767 * inode, empty space, or static AG metadata.
2768 */
2769bool
2770xfs_verify_dir_ino(
2771 struct xfs_mount *mp,
2772 xfs_ino_t ino)
2773{
2774 if (xfs_internal_inum(mp, ino))
2775 return false;
2776 return xfs_verify_ino(mp, ino);
2777}
Darrick J. Wong2e001262018-01-16 18:52:12 -08002778
2779/* Is there an inode record covering a given range of inode numbers? */
2780int
2781xfs_ialloc_has_inode_record(
2782 struct xfs_btree_cur *cur,
2783 xfs_agino_t low,
2784 xfs_agino_t high,
2785 bool *exists)
2786{
2787 struct xfs_inobt_rec_incore irec;
2788 xfs_agino_t agino;
2789 uint16_t holemask;
2790 int has_record;
2791 int i;
2792 int error;
2793
2794 *exists = false;
2795 error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record);
2796 while (error == 0 && has_record) {
2797 error = xfs_inobt_get_rec(cur, &irec, &has_record);
2798 if (error || irec.ir_startino > high)
2799 break;
2800
2801 agino = irec.ir_startino;
2802 holemask = irec.ir_holemask;
2803 for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; holemask >>= 1,
2804 i++, agino += XFS_INODES_PER_HOLEMASK_BIT) {
2805 if (holemask & 1)
2806 continue;
2807 if (agino + XFS_INODES_PER_HOLEMASK_BIT > low &&
2808 agino <= high) {
2809 *exists = true;
2810 return 0;
2811 }
2812 }
2813
2814 error = xfs_btree_increment(cur, 0, &has_record);
2815 }
2816 return error;
2817}
2818
2819/* Is there an inode record covering a given extent? */
2820int
2821xfs_ialloc_has_inodes_at_extent(
2822 struct xfs_btree_cur *cur,
2823 xfs_agblock_t bno,
2824 xfs_extlen_t len,
2825 bool *exists)
2826{
2827 xfs_agino_t low;
2828 xfs_agino_t high;
2829
2830 low = XFS_OFFBNO_TO_AGINO(cur->bc_mp, bno, 0);
2831 high = XFS_OFFBNO_TO_AGINO(cur->bc_mp, bno + len, 0) - 1;
2832
2833 return xfs_ialloc_has_inode_record(cur, low, high, exists);
2834}
2835
2836struct xfs_ialloc_count_inodes {
2837 xfs_agino_t count;
2838 xfs_agino_t freecount;
2839};
2840
2841/* Record inode counts across all inobt records. */
2842STATIC int
2843xfs_ialloc_count_inodes_rec(
2844 struct xfs_btree_cur *cur,
2845 union xfs_btree_rec *rec,
2846 void *priv)
2847{
2848 struct xfs_inobt_rec_incore irec;
2849 struct xfs_ialloc_count_inodes *ci = priv;
2850
2851 xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec);
2852 ci->count += irec.ir_count;
2853 ci->freecount += irec.ir_freecount;
2854
2855 return 0;
2856}
2857
2858/* Count allocated and free inodes under an inobt. */
2859int
2860xfs_ialloc_count_inodes(
2861 struct xfs_btree_cur *cur,
2862 xfs_agino_t *count,
2863 xfs_agino_t *freecount)
2864{
2865 struct xfs_ialloc_count_inodes ci = {0};
2866 int error;
2867
2868 ASSERT(cur->bc_btnum == XFS_BTNUM_INO);
2869 error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci);
2870 if (error)
2871 return error;
2872
2873 *count = ci.count;
2874 *freecount = ci.freecount;
2875 return 0;
2876}