blob: 85a477a5f41cd057adea701db0b48160ea85008a [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_btree.h"
29#include "xfs_ialloc.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110030#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_rtalloc.h"
33#include "xfs_error.h"
34#include "xfs_bmap.h"
Dave Chinner983d09f2013-04-03 16:11:15 +110035#include "xfs_cksum.h"
Dave Chinner239880e2013-10-23 10:50:10 +110036#include "xfs_trans.h"
Dave Chinner983d09f2013-04-03 16:11:15 +110037#include "xfs_buf_item.h"
Dave Chinnerddf6ad02013-06-27 16:04:56 +100038#include "xfs_icreate_item.h"
Dave Chinner7bb85ef2013-08-12 20:49:34 +100039#include "xfs_icache.h"
Dave Chinnerd1230312013-11-01 15:27:19 +110040#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Allocation group level functions.
45 */
David Chinner75de2a92008-03-27 18:00:38 +110046static inline int
47xfs_ialloc_cluster_alignment(
Dave Chinner7a1df152014-12-04 09:42:21 +110048 struct xfs_mount *mp)
David Chinner75de2a92008-03-27 18:00:38 +110049{
Dave Chinner7a1df152014-12-04 09:42:21 +110050 if (xfs_sb_version_hasalign(&mp->m_sb) &&
51 mp->m_sb.sb_inoalignmt >=
52 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
53 return mp->m_sb.sb_inoalignmt;
David Chinner75de2a92008-03-27 18:00:38 +110054 return 1;
55}
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
Christoph Hellwig21875502009-08-31 20:58:21 -030058 * Lookup a record by ino in the btree given by cur.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110059 */
Christoph Hellwig81e25172009-09-01 19:56:55 -040060int /* error */
Christoph Hellwig21875502009-08-31 20:58:21 -030061xfs_inobt_lookup(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110062 struct xfs_btree_cur *cur, /* btree cursor */
63 xfs_agino_t ino, /* starting inode of chunk */
Christoph Hellwig21875502009-08-31 20:58:21 -030064 xfs_lookup_t dir, /* <=, >=, == */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110065 int *stat) /* success/failure */
66{
67 cur->bc_rec.i.ir_startino = ino;
Brian Foster54190402015-05-29 09:03:04 +100068 cur->bc_rec.i.ir_holemask = 0;
69 cur->bc_rec.i.ir_count = 0;
Christoph Hellwig21875502009-08-31 20:58:21 -030070 cur->bc_rec.i.ir_freecount = 0;
71 cur->bc_rec.i.ir_free = 0;
72 return xfs_btree_lookup(cur, dir, stat);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110073}
74
75/*
Christoph Hellwigafabc242009-08-31 20:57:03 -030076 * Update the record referred to by cur to the value given.
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110077 * This either works (return 0) or gets an EFSCORRUPTED error.
78 */
79STATIC int /* error */
80xfs_inobt_update(
81 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigafabc242009-08-31 20:57:03 -030082 xfs_inobt_rec_incore_t *irec) /* btree record */
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110083{
84 union xfs_btree_rec rec;
85
Christoph Hellwigafabc242009-08-31 20:57:03 -030086 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
Brian Foster54190402015-05-29 09:03:04 +100087 if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
88 rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
89 rec.inobt.ir_u.sp.ir_count = irec->ir_count;
90 rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
91 } else {
92 /* ir_holemask/ir_count not supported on-disk */
93 rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
94 }
Christoph Hellwigafabc242009-08-31 20:57:03 -030095 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110096 return xfs_btree_update(cur, &rec);
97}
98
99/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100100 * Get the data from the pointed-to record.
101 */
102int /* error */
103xfs_inobt_get_rec(
104 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300105 xfs_inobt_rec_incore_t *irec, /* btree record */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100106 int *stat) /* output: success/failure */
107{
108 union xfs_btree_rec *rec;
109 int error;
110
111 error = xfs_btree_get_rec(cur, &rec, stat);
Brian Foster54190402015-05-29 09:03:04 +1000112 if (error || *stat == 0)
113 return error;
114
115 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
116 if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
117 irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
118 irec->ir_count = rec->inobt.ir_u.sp.ir_count;
119 irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
120 } else {
121 /*
122 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
123 * values for full inode chunks.
124 */
125 irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
126 irec->ir_count = XFS_INODES_PER_CHUNK;
127 irec->ir_freecount =
128 be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100129 }
Brian Foster54190402015-05-29 09:03:04 +1000130 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
131
132 return 0;
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100133}
134
135/*
Brian Foster0aa0a752014-04-24 16:00:53 +1000136 * Insert a single inobt record. Cursor must already point to desired location.
137 */
138STATIC int
139xfs_inobt_insert_rec(
140 struct xfs_btree_cur *cur,
Brian Foster54190402015-05-29 09:03:04 +1000141 __uint16_t holemask,
142 __uint8_t count,
Brian Foster0aa0a752014-04-24 16:00:53 +1000143 __int32_t freecount,
144 xfs_inofree_t free,
145 int *stat)
146{
Brian Foster54190402015-05-29 09:03:04 +1000147 cur->bc_rec.i.ir_holemask = holemask;
148 cur->bc_rec.i.ir_count = count;
Brian Foster0aa0a752014-04-24 16:00:53 +1000149 cur->bc_rec.i.ir_freecount = freecount;
150 cur->bc_rec.i.ir_free = free;
151 return xfs_btree_insert(cur, stat);
152}
153
154/*
155 * Insert records describing a newly allocated inode chunk into the inobt.
156 */
157STATIC int
158xfs_inobt_insert(
159 struct xfs_mount *mp,
160 struct xfs_trans *tp,
161 struct xfs_buf *agbp,
162 xfs_agino_t newino,
163 xfs_agino_t newlen,
164 xfs_btnum_t btnum)
165{
166 struct xfs_btree_cur *cur;
167 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
168 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
169 xfs_agino_t thisino;
170 int i;
171 int error;
172
173 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
174
175 for (thisino = newino;
176 thisino < newino + newlen;
177 thisino += XFS_INODES_PER_CHUNK) {
178 error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
179 if (error) {
180 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
181 return error;
182 }
183 ASSERT(i == 0);
184
Brian Foster54190402015-05-29 09:03:04 +1000185 error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
186 XFS_INODES_PER_CHUNK,
187 XFS_INODES_PER_CHUNK,
Brian Foster0aa0a752014-04-24 16:00:53 +1000188 XFS_INOBT_ALL_FREE, &i);
189 if (error) {
190 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
191 return error;
192 }
193 ASSERT(i == 1);
194 }
195
196 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
197
198 return 0;
199}
200
201/*
Dave Chinner0b48db82009-08-31 20:57:09 -0300202 * Verify that the number of free inodes in the AGI is correct.
203 */
204#ifdef DEBUG
205STATIC int
206xfs_check_agi_freecount(
207 struct xfs_btree_cur *cur,
208 struct xfs_agi *agi)
209{
210 if (cur->bc_nlevels == 1) {
211 xfs_inobt_rec_incore_t rec;
212 int freecount = 0;
213 int error;
214 int i;
215
Christoph Hellwig21875502009-08-31 20:58:21 -0300216 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
Dave Chinner0b48db82009-08-31 20:57:09 -0300217 if (error)
218 return error;
219
220 do {
221 error = xfs_inobt_get_rec(cur, &rec, &i);
222 if (error)
223 return error;
224
225 if (i) {
226 freecount += rec.ir_freecount;
227 error = xfs_btree_increment(cur, 0, &i);
228 if (error)
229 return error;
230 }
231 } while (i == 1);
232
233 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
234 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
235 }
236 return 0;
237}
238#else
239#define xfs_check_agi_freecount(cur, agi) 0
240#endif
241
242/*
Dave Chinner28c8e412013-06-27 16:04:55 +1000243 * Initialise a new set of inodes. When called without a transaction context
244 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
245 * than logging them (which in a transaction context puts them into the AIL
246 * for writeback rather than the xfsbufd queue).
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300247 */
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000248int
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300249xfs_ialloc_inode_init(
250 struct xfs_mount *mp,
251 struct xfs_trans *tp,
Dave Chinner28c8e412013-06-27 16:04:55 +1000252 struct list_head *buffer_list,
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300253 xfs_agnumber_t agno,
254 xfs_agblock_t agbno,
255 xfs_agblock_t length,
256 unsigned int gen)
257{
258 struct xfs_buf *fbuf;
259 struct xfs_dinode *free;
Jie Liu6e0c7b82013-12-13 15:51:49 +1100260 int nbufs, blks_per_cluster, inodes_per_cluster;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300261 int version;
262 int i, j;
263 xfs_daddr_t d;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100264 xfs_ino_t ino = 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300265
266 /*
Jie Liu6e0c7b82013-12-13 15:51:49 +1100267 * Loop over the new block(s), filling in the inodes. For small block
268 * sizes, manipulate the inodes in buffers which are multiples of the
269 * blocks size.
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300270 */
Jie Liu6e0c7b82013-12-13 15:51:49 +1100271 blks_per_cluster = xfs_icluster_size_fsb(mp);
272 inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
273 nbufs = length / blks_per_cluster;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300274
275 /*
Christoph Hellwig93848a92013-04-03 16:11:17 +1100276 * Figure out what version number to use in the inodes we create. If
277 * the superblock version has caught up to the one that supports the new
278 * inode format, then use the new inode version. Otherwise use the old
279 * version so that old kernels will continue to be able to use the file
280 * system.
281 *
282 * For v3 inodes, we also need to write the inode number into the inode,
283 * so calculate the first inode number of the chunk here as
284 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
285 * across multiple filesystem blocks (such as a cluster) and so cannot
286 * be used in the cluster buffer loop below.
287 *
288 * Further, because we are writing the inode directly into the buffer
289 * and calculating a CRC on the entire inode, we have ot log the entire
290 * inode so that the entire range the CRC covers is present in the log.
291 * That means for v3 inode we log the entire buffer rather than just the
292 * inode cores.
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300293 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100294 if (xfs_sb_version_hascrc(&mp->m_sb)) {
295 version = 3;
296 ino = XFS_AGINO_TO_INO(mp, agno,
297 XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000298
299 /*
300 * log the initialisation that is about to take place as an
301 * logical operation. This means the transaction does not
302 * need to log the physical changes to the inode buffers as log
303 * recovery will know what initialisation is actually needed.
304 * Hence we only need to log the buffers as "ordered" buffers so
305 * they track in the AIL as if they were physically logged.
306 */
307 if (tp)
Jie Liu71783432013-12-13 15:51:46 +1100308 xfs_icreate_log(tp, agno, agbno, mp->m_ialloc_inos,
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000309 mp->m_sb.sb_inodesize, length, gen);
Dave Chinner263997a2014-05-20 07:46:40 +1000310 } else
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300311 version = 2;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300312
313 for (j = 0; j < nbufs; j++) {
314 /*
315 * Get the block.
316 */
317 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
318 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
Dave Chinner7c4cebe2012-11-23 14:24:23 +1100319 mp->m_bsize * blks_per_cluster,
320 XBF_UNMAPPED);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000321 if (!fbuf)
Dave Chinner24513372014-06-25 14:58:08 +1000322 return -ENOMEM;
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000323
324 /* Initialize the inode buffers and log them appropriately. */
Dave Chinner1813dd62012-11-14 17:54:40 +1100325 fbuf->b_ops = &xfs_inode_buf_ops;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100326 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
Jie Liu6e0c7b82013-12-13 15:51:49 +1100327 for (i = 0; i < inodes_per_cluster; i++) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300328 int ioffset = i << mp->m_sb.sb_inodelog;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100329 uint isize = xfs_dinode_size(version);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300330
331 free = xfs_make_iptr(mp, fbuf, i);
332 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
333 free->di_version = version;
334 free->di_gen = cpu_to_be32(gen);
335 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100336
337 if (version == 3) {
338 free->di_ino = cpu_to_be64(ino);
339 ino++;
340 uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
341 xfs_dinode_calc_crc(mp, free);
Dave Chinner28c8e412013-06-27 16:04:55 +1000342 } else if (tp) {
Christoph Hellwig93848a92013-04-03 16:11:17 +1100343 /* just log the inode core */
344 xfs_trans_log_buf(tp, fbuf, ioffset,
345 ioffset + isize - 1);
346 }
347 }
Dave Chinner28c8e412013-06-27 16:04:55 +1000348
349 if (tp) {
350 /*
351 * Mark the buffer as an inode allocation buffer so it
352 * sticks in AIL at the point of this allocation
353 * transaction. This ensures the they are on disk before
354 * the tail of the log can be moved past this
355 * transaction (i.e. by preventing relogging from moving
356 * it forward in the log).
357 */
358 xfs_trans_inode_alloc_buf(tp, fbuf);
359 if (version == 3) {
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000360 /*
361 * Mark the buffer as ordered so that they are
362 * not physically logged in the transaction but
363 * still tracked in the AIL as part of the
364 * transaction and pin the log appropriately.
365 */
366 xfs_trans_ordered_buf(tp, fbuf);
Dave Chinner28c8e412013-06-27 16:04:55 +1000367 xfs_trans_log_buf(tp, fbuf, 0,
368 BBTOB(fbuf->b_length) - 1);
369 }
370 } else {
371 fbuf->b_flags |= XBF_DONE;
372 xfs_buf_delwri_queue(fbuf, buffer_list);
373 xfs_buf_relse(fbuf);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300374 }
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300375 }
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000376 return 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300377}
378
379/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * Allocate new inodes in the allocation group specified by agbp.
381 * Return 0 for success, else error code.
382 */
383STATIC int /* error code or 0 */
384xfs_ialloc_ag_alloc(
385 xfs_trans_t *tp, /* transaction pointer */
386 xfs_buf_t *agbp, /* alloc group buffer */
387 int *alloc)
388{
389 xfs_agi_t *agi; /* allocation group header */
390 xfs_alloc_arg_t args; /* allocation argument structure */
David Chinner92821e22007-05-24 15:26:31 +1000391 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 xfs_agino_t newino; /* new first inode's number */
394 xfs_agino_t newlen; /* new number of inodes */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000395 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* boundary */
Dave Chinner44b56e02010-01-11 11:47:43 +0000397 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Mark Tinguelya0041682012-09-20 13:16:45 -0500399 memset(&args, 0, sizeof(args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 args.tp = tp;
401 args.mp = tp->t_mountp;
402
403 /*
404 * Locking will ensure that we don't have two callers in here
405 * at one time.
406 */
Jie Liu71783432013-12-13 15:51:46 +1100407 newlen = args.mp->m_ialloc_inos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (args.mp->m_maxicount &&
Dave Chinner501ab322015-02-23 21:19:28 +1100409 percpu_counter_read(&args.mp->m_icount) + newlen >
410 args.mp->m_maxicount)
Dave Chinner24513372014-06-25 14:58:08 +1000411 return -ENOSPC;
Jie Liu126cd102013-12-13 15:51:48 +1100412 args.minlen = args.maxlen = args.mp->m_ialloc_blks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000414 * First try to allocate inodes contiguous with the last-allocated
415 * chunk of inodes. If the filesystem is striped, this will fill
416 * an entire stripe unit with inodes.
Dave Chinner28c8e412013-06-27 16:04:55 +1000417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000419 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300420 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000421 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
Jie Liu126cd102013-12-13 15:51:48 +1100422 args.mp->m_ialloc_blks;
Nathan Scott019ff2d2006-04-11 15:45:05 +1000423 if (likely(newino != NULLAGINO &&
424 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300425 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000426 args.type = XFS_ALLOCTYPE_THIS_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000427 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100428
Glen Overby3ccb8b52006-03-29 09:52:28 +1000429 /*
David Chinner75de2a92008-03-27 18:00:38 +1100430 * We need to take into account alignment here to ensure that
431 * we don't modify the free list if we fail to have an exact
432 * block. If we don't have an exact match, and every oher
433 * attempt allocation attempt fails, we'll end up cancelling
434 * a dirty transaction and shutting down.
435 *
436 * For an exact allocation, alignment must be 1,
437 * however we need to take cluster alignment into account when
438 * fixing up the freelist. Use the minalignslop field to
439 * indicate that extra blocks might be required for alignment,
440 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000441 */
David Chinner75de2a92008-03-27 18:00:38 +1100442 args.alignment = 1;
Dave Chinner7a1df152014-12-04 09:42:21 +1100443 args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1;
David Chinner75de2a92008-03-27 18:00:38 +1100444
445 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100446 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000447 if ((error = xfs_alloc_vextent(&args)))
448 return error;
Brian Fostere480a722014-03-07 16:19:14 +1100449
450 /*
451 * This request might have dirtied the transaction if the AG can
452 * satisfy the request, but the exact block was not available.
453 * If the allocation did fail, subsequent requests will relax
454 * the exact agbno requirement and increase the alignment
455 * instead. It is critical that the total size of the request
456 * (len + alignment + slop) does not increase from this point
457 * on, so reset minalignslop to ensure it is not included in
458 * subsequent requests.
459 */
460 args.minalignslop = 0;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000461 } else
462 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Glen Overby3ccb8b52006-03-29 09:52:28 +1000464 if (unlikely(args.fsbno == NULLFSBLOCK)) {
465 /*
466 * Set the alignment for the allocation.
467 * If stripe alignment is turned on then align at stripe unit
468 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000469 * If the cluster size is smaller than a filesystem block
470 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000471 * pieces, so don't need alignment anyway.
472 */
473 isaligned = 0;
474 if (args.mp->m_sinoalign) {
475 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
476 args.alignment = args.mp->m_dalign;
477 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100478 } else
Dave Chinner7a1df152014-12-04 09:42:21 +1100479 args.alignment = xfs_ialloc_cluster_alignment(args.mp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000480 /*
481 * Need to figure out where to allocate the inode blocks.
482 * Ideally they should be spaced out through the a.g.
483 * For now, just allocate blocks up front.
484 */
485 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300486 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000487 /*
488 * Allocate a fixed-size extent of inodes.
489 */
490 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000491 args.prod = 1;
492 /*
493 * Allow space for the inode btree to split.
494 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100495 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000496 if ((error = xfs_alloc_vextent(&args)))
497 return error;
498 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
501 * If stripe alignment is turned on, then try again with cluster
502 * alignment.
503 */
504 if (isaligned && args.fsbno == NULLFSBLOCK) {
505 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100506 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300507 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Dave Chinner7a1df152014-12-04 09:42:21 +1100508 args.alignment = xfs_ialloc_cluster_alignment(args.mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if ((error = xfs_alloc_vextent(&args)))
510 return error;
511 }
512
513 if (args.fsbno == NULLFSBLOCK) {
514 *alloc = 0;
515 return 0;
516 }
517 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
David Chinner359346a2008-04-29 12:53:32 +1000519 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300520 * Stamp and write the inode buffers.
521 *
David Chinner359346a2008-04-29 12:53:32 +1000522 * Seed the new inode cluster with a random generation number. This
523 * prevents short-term reuse of generation numbers if a chunk is
524 * freed and then immediately reallocated. We use random numbers
525 * rather than a linear progression to prevent the next generation
526 * number from being easily guessable.
527 */
Dave Chinner28c8e412013-06-27 16:04:55 +1000528 error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
Akinobu Mitaecb34032013-03-04 21:58:20 +0900529 args.len, prandom_u32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100530
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000531 if (error)
532 return error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300533 /*
534 * Convert the results.
535 */
536 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800537 be32_add_cpu(&agi->agi_count, newlen);
538 be32_add_cpu(&agi->agi_freecount, newlen);
Dave Chinner44b56e02010-01-11 11:47:43 +0000539 pag = xfs_perag_get(args.mp, agno);
540 pag->pagi_freecount += newlen;
541 xfs_perag_put(pag);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100542 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /*
Brian Foster0aa0a752014-04-24 16:00:53 +1000545 * Insert records describing the new inode chunk into the btrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
Brian Foster0aa0a752014-04-24 16:00:53 +1000547 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
548 XFS_BTNUM_INO);
549 if (error)
550 return error;
551
552 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
553 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
554 XFS_BTNUM_FINO);
555 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * Log allocation group header fields
560 */
561 xfs_ialloc_log_agi(tp, agbp,
562 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
563 /*
564 * Modify/log superblock values for inode count and inode free count.
565 */
566 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
567 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
568 *alloc = 1;
569 return 0;
570}
571
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000572STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573xfs_ialloc_next_ag(
574 xfs_mount_t *mp)
575{
576 xfs_agnumber_t agno;
577
578 spin_lock(&mp->m_agirotor_lock);
579 agno = mp->m_agirotor;
Carlos Maiolino8aea3ff2012-09-20 10:32:36 -0300580 if (++mp->m_agirotor >= mp->m_maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 mp->m_agirotor = 0;
582 spin_unlock(&mp->m_agirotor_lock);
583
584 return agno;
585}
586
587/*
588 * Select an allocation group to look for a free inode in, based on the parent
Zhi Yong Wu2f21ff12013-08-12 03:14:58 +0000589 * inode and the mode. Return the allocation group buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400591STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592xfs_ialloc_ag_select(
593 xfs_trans_t *tp, /* transaction pointer */
594 xfs_ino_t parent, /* parent directory inode number */
Al Viro576b1d62011-07-26 02:50:15 -0400595 umode_t mode, /* bits set to indicate file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int okalloc) /* ok to allocate more space */
597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
599 xfs_agnumber_t agno; /* current ag number */
600 int flags; /* alloc buffer locking flags */
601 xfs_extlen_t ineed; /* blocks needed for inode allocation */
602 xfs_extlen_t longest = 0; /* longest extent available */
603 xfs_mount_t *mp; /* mount point structure */
604 int needspace; /* file mode implies space allocated */
605 xfs_perag_t *pag; /* per allocation group data */
606 xfs_agnumber_t pagno; /* parent (starting) ag number */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400607 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /*
610 * Files of these types need at least one block if length > 0
611 * (and they won't fit in the inode, but that's hard to figure out).
612 */
613 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
614 mp = tp->t_mountp;
615 agcount = mp->m_maxagi;
616 if (S_ISDIR(mode))
617 pagno = xfs_ialloc_next_ag(mp);
618 else {
619 pagno = XFS_INO_TO_AGNO(mp, parent);
620 if (pagno >= agcount)
621 pagno = 0;
622 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 ASSERT(pagno < agcount);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /*
627 * Loop through allocation groups, looking for one with a little
628 * free space in it. Note we don't look for free inodes, exactly.
629 * Instead, we include whether there is a need to allocate inodes
630 * to mean that blocks must be allocated for them,
631 * if none are currently free.
632 */
633 agno = pagno;
634 flags = XFS_ALLOC_FLAG_TRYLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 for (;;) {
Dave Chinner44b56e02010-01-11 11:47:43 +0000636 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!pag->pagi_inodeok) {
638 xfs_ialloc_next_ag(mp);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400639 goto nextag;
640 }
641
642 if (!pag->pagi_init) {
643 error = xfs_ialloc_pagi_init(mp, tp, agno);
644 if (error)
645 goto nextag;
646 }
647
648 if (pag->pagi_freecount) {
649 xfs_perag_put(pag);
650 return agno;
651 }
652
653 if (!okalloc)
654 goto nextag;
655
656 if (!pag->pagf_init) {
657 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
658 if (error)
659 goto nextag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 /*
Dave Chinner7a1df152014-12-04 09:42:21 +1100663 * Check that there is enough free space for the file plus a
664 * chunk of inodes if we need to allocate some. If this is the
665 * first pass across the AGs, take into account the potential
666 * space needed for alignment of inode chunks when checking the
667 * longest contiguous free space in the AG - this prevents us
668 * from getting ENOSPC because we have free space larger than
669 * m_ialloc_blks but alignment constraints prevent us from using
670 * it.
671 *
672 * If we can't find an AG with space for full alignment slack to
673 * be taken into account, we must be near ENOSPC in all AGs.
674 * Hence we don't include alignment for the second pass and so
675 * if we fail allocation due to alignment issues then it is most
676 * likely a real ENOSPC condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
Brian Foster066a1882015-05-29 08:55:20 +1000678 ineed = mp->m_ialloc_min_blks;
Dave Chinner7a1df152014-12-04 09:42:21 +1100679 if (flags && ineed > 1)
680 ineed += xfs_ialloc_cluster_alignment(mp);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400681 longest = pag->pagf_longest;
682 if (!longest)
683 longest = pag->pagf_flcount > 0;
684
685 if (pag->pagf_freeblks >= needspace + ineed &&
686 longest >= ineed) {
687 xfs_perag_put(pag);
688 return agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690nextag:
Dave Chinner44b56e02010-01-11 11:47:43 +0000691 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * No point in iterating over the rest, if we're shutting
694 * down.
695 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000696 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400697 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 agno++;
699 if (agno >= agcount)
700 agno = 0;
701 if (agno == pagno) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000702 if (flags == 0)
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400703 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 flags = 0;
705 }
706 }
707}
708
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300709/*
710 * Try to retrieve the next record to the left/right from the current one.
711 */
712STATIC int
713xfs_ialloc_next_rec(
714 struct xfs_btree_cur *cur,
715 xfs_inobt_rec_incore_t *rec,
716 int *done,
717 int left)
718{
719 int error;
720 int i;
721
722 if (left)
723 error = xfs_btree_decrement(cur, 0, &i);
724 else
725 error = xfs_btree_increment(cur, 0, &i);
726
727 if (error)
728 return error;
729 *done = !i;
730 if (i) {
731 error = xfs_inobt_get_rec(cur, rec, &i);
732 if (error)
733 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100734 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300735 }
736
737 return 0;
738}
739
Dave Chinnerbd169562009-08-31 20:58:28 -0300740STATIC int
741xfs_ialloc_get_rec(
742 struct xfs_btree_cur *cur,
743 xfs_agino_t agino,
744 xfs_inobt_rec_incore_t *rec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800745 int *done)
Dave Chinnerbd169562009-08-31 20:58:28 -0300746{
747 int error;
748 int i;
749
750 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
751 if (error)
752 return error;
753 *done = !i;
754 if (i) {
755 error = xfs_inobt_get_rec(cur, rec, &i);
756 if (error)
757 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100758 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Dave Chinnerbd169562009-08-31 20:58:28 -0300759 }
760
761 return 0;
762}
Dave Chinner0b48db82009-08-31 20:57:09 -0300763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/*
Brian Fosterd4cc5402015-05-29 08:50:21 +1000765 * Return the offset of the first free inode in the record.
766 */
767STATIC int
768xfs_inobt_first_free_inode(
769 struct xfs_inobt_rec_incore *rec)
770{
771 return xfs_lowbit64(rec->ir_free);
772}
773
774/*
Brian Foster6dd86382014-04-24 16:00:53 +1000775 * Allocate an inode using the inobt-only algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400777STATIC int
Brian Foster6dd86382014-04-24 16:00:53 +1000778xfs_dialloc_ag_inobt(
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400779 struct xfs_trans *tp,
780 struct xfs_buf *agbp,
781 xfs_ino_t parent,
782 xfs_ino_t *inop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400784 struct xfs_mount *mp = tp->t_mountp;
785 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
786 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
787 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
788 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
789 struct xfs_perag *pag;
790 struct xfs_btree_cur *cur, *tcur;
791 struct xfs_inobt_rec_incore rec, trec;
792 xfs_ino_t ino;
793 int error;
794 int offset;
795 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Dave Chinner44b56e02010-01-11 11:47:43 +0000797 pag = xfs_perag_get(mp, agno);
Dave Chinnerbd169562009-08-31 20:58:28 -0300798
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400799 ASSERT(pag->pagi_init);
800 ASSERT(pag->pagi_inodeok);
801 ASSERT(pag->pagi_freecount > 0);
802
Dave Chinnerbd169562009-08-31 20:58:28 -0300803 restart_pagno:
Brian Foster57bd3db2014-04-24 16:00:50 +1000804 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
806 * If pagino is 0 (this is the root inode allocation) use newino.
807 * This must work because we've just allocated some.
808 */
809 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100810 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Dave Chinner0b48db82009-08-31 20:57:09 -0300812 error = xfs_check_agi_freecount(cur, agi);
813 if (error)
814 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300817 * If in the same AG as the parent, try to get near the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
819 if (pagno == agno) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300820 int doneleft; /* done, to the left */
821 int doneright; /* done, to the right */
Dave Chinnerbd169562009-08-31 20:58:28 -0300822 int searchdistance = 10;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300823
Christoph Hellwig21875502009-08-31 20:58:21 -0300824 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300825 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100827 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300828
829 error = xfs_inobt_get_rec(cur, &rec, &j);
830 if (error)
831 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100832 XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300833
834 if (rec.ir_freecount > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /*
836 * Found a free inode in the same chunk
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300837 * as the parent, done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300839 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300841
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300844 * In the same AG as parent, but parent's chunk is full.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300847 /* duplicate the cursor, search left & right simultaneously */
848 error = xfs_btree_dup_cursor(cur, &tcur);
849 if (error)
850 goto error0;
851
Dave Chinnerbd169562009-08-31 20:58:28 -0300852 /*
853 * Skip to last blocks looked up if same parent inode.
854 */
855 if (pagino != NULLAGINO &&
856 pag->pagl_pagino == pagino &&
857 pag->pagl_leftrec != NULLAGINO &&
858 pag->pagl_rightrec != NULLAGINO) {
859 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800860 &trec, &doneleft);
Dave Chinnerbd169562009-08-31 20:58:28 -0300861 if (error)
862 goto error1;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300863
Dave Chinnerbd169562009-08-31 20:58:28 -0300864 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800865 &rec, &doneright);
Dave Chinnerbd169562009-08-31 20:58:28 -0300866 if (error)
867 goto error1;
868 } else {
869 /* search left with tcur, back up 1 record */
870 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
871 if (error)
872 goto error1;
873
874 /* search right with cur, go forward 1 record. */
875 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
876 if (error)
877 goto error1;
878 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300879
880 /*
881 * Loop until we find an inode chunk with a free inode.
882 */
883 while (!doneleft || !doneright) {
884 int useleft; /* using left inode chunk this time */
885
Dave Chinnerbd169562009-08-31 20:58:28 -0300886 if (!--searchdistance) {
887 /*
888 * Not in range - save last search
889 * location and allocate a new inode
890 */
Eric Sandeen3b826382009-10-30 09:27:07 +0100891 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300892 pag->pagl_leftrec = trec.ir_startino;
893 pag->pagl_rightrec = rec.ir_startino;
894 pag->pagl_pagino = pagino;
895 goto newino;
896 }
897
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300898 /* figure out the closer block if both are valid. */
899 if (!doneleft && !doneright) {
900 useleft = pagino -
901 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
902 rec.ir_startino - pagino;
903 } else {
904 useleft = !doneleft;
905 }
906
907 /* free inodes to the left? */
908 if (useleft && trec.ir_freecount) {
909 rec = trec;
910 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
911 cur = tcur;
Dave Chinnerbd169562009-08-31 20:58:28 -0300912
913 pag->pagl_leftrec = trec.ir_startino;
914 pag->pagl_rightrec = rec.ir_startino;
915 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300916 goto alloc_inode;
917 }
918
919 /* free inodes to the right? */
920 if (!useleft && rec.ir_freecount) {
921 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300922
923 pag->pagl_leftrec = trec.ir_startino;
924 pag->pagl_rightrec = rec.ir_startino;
925 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300926 goto alloc_inode;
927 }
928
929 /* get next record to check */
930 if (useleft) {
931 error = xfs_ialloc_next_rec(tcur, &trec,
932 &doneleft, 1);
933 } else {
934 error = xfs_ialloc_next_rec(cur, &rec,
935 &doneright, 0);
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300940
941 /*
942 * We've reached the end of the btree. because
943 * we are only searching a small chunk of the
944 * btree each search, there is obviously free
945 * inodes closer to the parent inode than we
946 * are now. restart the search again.
947 */
948 pag->pagl_pagino = NULLAGINO;
949 pag->pagl_leftrec = NULLAGINO;
950 pag->pagl_rightrec = NULLAGINO;
951 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
952 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
953 goto restart_pagno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300957 * In a different AG from the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * See if the most recently allocated block has any free.
959 */
Dave Chinnerbd169562009-08-31 20:58:28 -0300960newino:
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200961 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
Christoph Hellwig21875502009-08-31 20:58:21 -0300962 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
963 XFS_LOOKUP_EQ, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300964 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300966
967 if (i == 1) {
968 error = xfs_inobt_get_rec(cur, &rec, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (error)
970 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300971
972 if (j == 1 && rec.ir_freecount > 0) {
973 /*
974 * The last chunk allocated in the group
975 * still has a free inode.
976 */
977 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300980 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300981
Dave Chinnerbd169562009-08-31 20:58:28 -0300982 /*
983 * None left in the last group, search the whole AG
984 */
985 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
986 if (error)
987 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100988 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Dave Chinnerbd169562009-08-31 20:58:28 -0300989
990 for (;;) {
991 error = xfs_inobt_get_rec(cur, &rec, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300992 if (error)
993 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +1100994 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Dave Chinnerbd169562009-08-31 20:58:28 -0300995 if (rec.ir_freecount > 0)
996 break;
997 error = xfs_btree_increment(cur, 0, &i);
998 if (error)
999 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001000 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -03001002
1003alloc_inode:
Brian Fosterd4cc5402015-05-29 08:50:21 +10001004 offset = xfs_inobt_first_free_inode(&rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ASSERT(offset >= 0);
1006 ASSERT(offset < XFS_INODES_PER_CHUNK);
1007 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1008 XFS_INODES_PER_CHUNK) == 0);
1009 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001010 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 rec.ir_freecount--;
Christoph Hellwigafabc242009-08-31 20:57:03 -03001012 error = xfs_inobt_update(cur, &rec);
1013 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001015 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001017 pag->pagi_freecount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Dave Chinner0b48db82009-08-31 20:57:09 -03001019 error = xfs_check_agi_freecount(cur, agi);
1020 if (error)
1021 goto error0;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1024 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
Dave Chinner44b56e02010-01-11 11:47:43 +00001025 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 *inop = ino;
1027 return 0;
1028error1:
1029 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1030error0:
1031 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
Dave Chinner44b56e02010-01-11 11:47:43 +00001032 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return error;
1034}
1035
1036/*
Brian Foster6dd86382014-04-24 16:00:53 +10001037 * Use the free inode btree to allocate an inode based on distance from the
1038 * parent. Note that the provided cursor may be deleted and replaced.
1039 */
1040STATIC int
1041xfs_dialloc_ag_finobt_near(
1042 xfs_agino_t pagino,
1043 struct xfs_btree_cur **ocur,
1044 struct xfs_inobt_rec_incore *rec)
1045{
1046 struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
1047 struct xfs_btree_cur *rcur; /* right search cursor */
1048 struct xfs_inobt_rec_incore rrec;
1049 int error;
1050 int i, j;
1051
1052 error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
1053 if (error)
1054 return error;
1055
1056 if (i == 1) {
1057 error = xfs_inobt_get_rec(lcur, rec, &i);
1058 if (error)
1059 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001060 XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001061
1062 /*
1063 * See if we've landed in the parent inode record. The finobt
1064 * only tracks chunks with at least one free inode, so record
1065 * existence is enough.
1066 */
1067 if (pagino >= rec->ir_startino &&
1068 pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
1069 return 0;
1070 }
1071
1072 error = xfs_btree_dup_cursor(lcur, &rcur);
1073 if (error)
1074 return error;
1075
1076 error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
1077 if (error)
1078 goto error_rcur;
1079 if (j == 1) {
1080 error = xfs_inobt_get_rec(rcur, &rrec, &j);
1081 if (error)
1082 goto error_rcur;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001083 XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur);
Brian Foster6dd86382014-04-24 16:00:53 +10001084 }
1085
Eric Sandeenc29aad42015-02-23 22:39:08 +11001086 XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur);
Brian Foster6dd86382014-04-24 16:00:53 +10001087 if (i == 1 && j == 1) {
1088 /*
1089 * Both the left and right records are valid. Choose the closer
1090 * inode chunk to the target.
1091 */
1092 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
1093 (rrec.ir_startino - pagino)) {
1094 *rec = rrec;
1095 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1096 *ocur = rcur;
1097 } else {
1098 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1099 }
1100 } else if (j == 1) {
1101 /* only the right record is valid */
1102 *rec = rrec;
1103 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1104 *ocur = rcur;
1105 } else if (i == 1) {
1106 /* only the left record is valid */
1107 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1108 }
1109
1110 return 0;
1111
1112error_rcur:
1113 xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
1114 return error;
1115}
1116
1117/*
1118 * Use the free inode btree to find a free inode based on a newino hint. If
1119 * the hint is NULL, find the first free inode in the AG.
1120 */
1121STATIC int
1122xfs_dialloc_ag_finobt_newino(
1123 struct xfs_agi *agi,
1124 struct xfs_btree_cur *cur,
1125 struct xfs_inobt_rec_incore *rec)
1126{
1127 int error;
1128 int i;
1129
1130 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
Dave Chinnere68ed772014-09-29 10:43:15 +10001131 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1132 XFS_LOOKUP_EQ, &i);
Brian Foster6dd86382014-04-24 16:00:53 +10001133 if (error)
1134 return error;
1135 if (i == 1) {
1136 error = xfs_inobt_get_rec(cur, rec, &i);
1137 if (error)
1138 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001139 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001140 return 0;
1141 }
1142 }
1143
1144 /*
1145 * Find the first inode available in the AG.
1146 */
1147 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1148 if (error)
1149 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001150 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001151
1152 error = xfs_inobt_get_rec(cur, rec, &i);
1153 if (error)
1154 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001155 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001156
1157 return 0;
1158}
1159
1160/*
1161 * Update the inobt based on a modification made to the finobt. Also ensure that
1162 * the records from both trees are equivalent post-modification.
1163 */
1164STATIC int
1165xfs_dialloc_ag_update_inobt(
1166 struct xfs_btree_cur *cur, /* inobt cursor */
1167 struct xfs_inobt_rec_incore *frec, /* finobt record */
1168 int offset) /* inode offset */
1169{
1170 struct xfs_inobt_rec_incore rec;
1171 int error;
1172 int i;
1173
1174 error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
1175 if (error)
1176 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001177 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001178
1179 error = xfs_inobt_get_rec(cur, &rec, &i);
1180 if (error)
1181 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001182 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Brian Foster6dd86382014-04-24 16:00:53 +10001183 ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
1184 XFS_INODES_PER_CHUNK) == 0);
1185
1186 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1187 rec.ir_freecount--;
1188
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001189 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) &&
Brian Foster6dd86382014-04-24 16:00:53 +10001190 (rec.ir_freecount == frec->ir_freecount));
1191
kbuild test robotb72091f2014-12-01 08:24:58 +11001192 return xfs_inobt_update(cur, &rec);
Brian Foster6dd86382014-04-24 16:00:53 +10001193}
1194
1195/*
1196 * Allocate an inode using the free inode btree, if available. Otherwise, fall
1197 * back to the inobt search algorithm.
1198 *
1199 * The caller selected an AG for us, and made sure that free inodes are
1200 * available.
1201 */
1202STATIC int
1203xfs_dialloc_ag(
1204 struct xfs_trans *tp,
1205 struct xfs_buf *agbp,
1206 xfs_ino_t parent,
1207 xfs_ino_t *inop)
1208{
1209 struct xfs_mount *mp = tp->t_mountp;
1210 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1211 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1212 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1213 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1214 struct xfs_perag *pag;
1215 struct xfs_btree_cur *cur; /* finobt cursor */
1216 struct xfs_btree_cur *icur; /* inobt cursor */
1217 struct xfs_inobt_rec_incore rec;
1218 xfs_ino_t ino;
1219 int error;
1220 int offset;
1221 int i;
1222
1223 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
1224 return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
1225
1226 pag = xfs_perag_get(mp, agno);
1227
1228 /*
1229 * If pagino is 0 (this is the root inode allocation) use newino.
1230 * This must work because we've just allocated some.
1231 */
1232 if (!pagino)
1233 pagino = be32_to_cpu(agi->agi_newino);
1234
1235 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1236
1237 error = xfs_check_agi_freecount(cur, agi);
1238 if (error)
1239 goto error_cur;
1240
1241 /*
1242 * The search algorithm depends on whether we're in the same AG as the
1243 * parent. If so, find the closest available inode to the parent. If
1244 * not, consider the agi hint or find the first free inode in the AG.
1245 */
1246 if (agno == pagno)
1247 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
1248 else
1249 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
1250 if (error)
1251 goto error_cur;
1252
Brian Fosterd4cc5402015-05-29 08:50:21 +10001253 offset = xfs_inobt_first_free_inode(&rec);
Brian Foster6dd86382014-04-24 16:00:53 +10001254 ASSERT(offset >= 0);
1255 ASSERT(offset < XFS_INODES_PER_CHUNK);
1256 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1257 XFS_INODES_PER_CHUNK) == 0);
1258 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1259
1260 /*
1261 * Modify or remove the finobt record.
1262 */
1263 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1264 rec.ir_freecount--;
1265 if (rec.ir_freecount)
1266 error = xfs_inobt_update(cur, &rec);
1267 else
1268 error = xfs_btree_delete(cur, &i);
1269 if (error)
1270 goto error_cur;
1271
1272 /*
1273 * The finobt has now been updated appropriately. We haven't updated the
1274 * agi and superblock yet, so we can create an inobt cursor and validate
1275 * the original freecount. If all is well, make the equivalent update to
1276 * the inobt using the finobt record and offset information.
1277 */
1278 icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1279
1280 error = xfs_check_agi_freecount(icur, agi);
1281 if (error)
1282 goto error_icur;
1283
1284 error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
1285 if (error)
1286 goto error_icur;
1287
1288 /*
1289 * Both trees have now been updated. We must update the perag and
1290 * superblock before we can check the freecount for each btree.
1291 */
1292 be32_add_cpu(&agi->agi_freecount, -1);
1293 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1294 pag->pagi_freecount--;
1295
1296 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1297
1298 error = xfs_check_agi_freecount(icur, agi);
1299 if (error)
1300 goto error_icur;
1301 error = xfs_check_agi_freecount(cur, agi);
1302 if (error)
1303 goto error_icur;
1304
1305 xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
1306 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1307 xfs_perag_put(pag);
1308 *inop = ino;
1309 return 0;
1310
1311error_icur:
1312 xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
1313error_cur:
1314 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1315 xfs_perag_put(pag);
1316 return error;
1317}
1318
1319/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001320 * Allocate an inode on disk.
1321 *
1322 * Mode is used to tell whether the new inode will need space, and whether it
1323 * is a directory.
1324 *
1325 * This function is designed to be called twice if it has to do an allocation
1326 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
1327 * If an inode is available without having to performn an allocation, an inode
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001328 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
1329 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
1330 * The caller should then commit the current transaction, allocate a
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001331 * new transaction, and call xfs_dialloc() again, passing in the previous value
1332 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
1333 * buffer is locked across the two calls, the second call is guaranteed to have
1334 * a free inode available.
1335 *
1336 * Once we successfully pick an inode its number is returned and the on-disk
1337 * data structures are updated. The inode itself is not read in, since doing so
1338 * would break ordering constraints with xfs_reclaim.
1339 */
1340int
1341xfs_dialloc(
1342 struct xfs_trans *tp,
1343 xfs_ino_t parent,
1344 umode_t mode,
1345 int okalloc,
1346 struct xfs_buf **IO_agbp,
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001347 xfs_ino_t *inop)
1348{
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001349 struct xfs_mount *mp = tp->t_mountp;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001350 struct xfs_buf *agbp;
1351 xfs_agnumber_t agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001352 int error;
1353 int ialloced;
1354 int noroom = 0;
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001355 xfs_agnumber_t start_agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001356 struct xfs_perag *pag;
1357
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001358 if (*IO_agbp) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001359 /*
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001360 * If the caller passes in a pointer to the AGI buffer,
1361 * continue where we left off before. In this case, we
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001362 * know that the allocation group has free inodes.
1363 */
1364 agbp = *IO_agbp;
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001365 goto out_alloc;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001366 }
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001367
1368 /*
1369 * We do not have an agbp, so select an initial allocation
1370 * group for inode allocation.
1371 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001372 start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
1373 if (start_agno == NULLAGNUMBER) {
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001374 *inop = NULLFSINO;
1375 return 0;
1376 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001377
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001378 /*
1379 * If we have already hit the ceiling of inode blocks then clear
1380 * okalloc so we scan all available agi structures for a free
1381 * inode.
1382 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001383 if (mp->m_maxicount &&
Dave Chinner501ab322015-02-23 21:19:28 +11001384 percpu_counter_read(&mp->m_icount) + mp->m_ialloc_inos >
1385 mp->m_maxicount) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001386 noroom = 1;
1387 okalloc = 0;
1388 }
1389
1390 /*
1391 * Loop until we find an allocation group that either has free inodes
1392 * or in which we can allocate some inodes. Iterate through the
1393 * allocation groups upward, wrapping at the end.
1394 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001395 agno = start_agno;
1396 for (;;) {
1397 pag = xfs_perag_get(mp, agno);
1398 if (!pag->pagi_inodeok) {
1399 xfs_ialloc_next_ag(mp);
1400 goto nextag;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001401 }
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001402
1403 if (!pag->pagi_init) {
1404 error = xfs_ialloc_pagi_init(mp, tp, agno);
1405 if (error)
1406 goto out_error;
1407 }
1408
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001409 /*
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001410 * Do a first racy fast path check if this AG is usable.
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001411 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001412 if (!pag->pagi_freecount && !okalloc)
1413 goto nextag;
1414
Christoph Hellwigc4982112012-08-07 02:02:02 -04001415 /*
1416 * Then read in the AGI buffer and recheck with the AGI buffer
1417 * lock held.
1418 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001419 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1420 if (error)
1421 goto out_error;
1422
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001423 if (pag->pagi_freecount) {
1424 xfs_perag_put(pag);
1425 goto out_alloc;
1426 }
1427
Christoph Hellwigc4982112012-08-07 02:02:02 -04001428 if (!okalloc)
1429 goto nextag_relse_buffer;
1430
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001431
1432 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1433 if (error) {
1434 xfs_trans_brelse(tp, agbp);
1435
Dave Chinner24513372014-06-25 14:58:08 +10001436 if (error != -ENOSPC)
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001437 goto out_error;
1438
1439 xfs_perag_put(pag);
1440 *inop = NULLFSINO;
1441 return 0;
1442 }
1443
1444 if (ialloced) {
1445 /*
1446 * We successfully allocated some inodes, return
1447 * the current context to the caller so that it
1448 * can commit the current transaction and call
1449 * us again where we left off.
1450 */
1451 ASSERT(pag->pagi_freecount > 0);
1452 xfs_perag_put(pag);
1453
1454 *IO_agbp = agbp;
1455 *inop = NULLFSINO;
1456 return 0;
1457 }
1458
Christoph Hellwigc4982112012-08-07 02:02:02 -04001459nextag_relse_buffer:
1460 xfs_trans_brelse(tp, agbp);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001461nextag:
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001462 xfs_perag_put(pag);
1463 if (++agno == mp->m_sb.sb_agcount)
1464 agno = 0;
1465 if (agno == start_agno) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001466 *inop = NULLFSINO;
Dave Chinner24513372014-06-25 14:58:08 +10001467 return noroom ? -ENOSPC : 0;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001468 }
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001469 }
1470
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001471out_alloc:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001472 *IO_agbp = NULL;
1473 return xfs_dialloc_ag(tp, agbp, parent, inop);
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001474out_error:
1475 xfs_perag_put(pag);
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001476 return error;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001477}
1478
Brian Foster2b64ee52014-04-24 16:00:53 +10001479STATIC int
1480xfs_difree_inobt(
1481 struct xfs_mount *mp,
1482 struct xfs_trans *tp,
1483 struct xfs_buf *agbp,
1484 xfs_agino_t agino,
1485 struct xfs_bmap_free *flist,
Dave Chinner0d907a32014-05-20 08:56:00 +10001486 int *deleted,
Brian Foster2b64ee52014-04-24 16:00:53 +10001487 xfs_ino_t *first_ino,
1488 struct xfs_inobt_rec_incore *orec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Brian Foster2b64ee52014-04-24 16:00:53 +10001490 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1491 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1492 struct xfs_perag *pag;
1493 struct xfs_btree_cur *cur;
1494 struct xfs_inobt_rec_incore rec;
1495 int ilen;
1496 int error;
1497 int i;
1498 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001500 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Brian Foster2b64ee52014-04-24 16:00:53 +10001501 ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /*
1504 * Initialize the cursor.
1505 */
Brian Foster57bd3db2014-04-24 16:00:50 +10001506 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Dave Chinner0b48db82009-08-31 20:57:09 -03001508 error = xfs_check_agi_freecount(cur, agi);
1509 if (error)
1510 goto error0;
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /*
1513 * Look for the entry describing this inode.
1514 */
Christoph Hellwig21875502009-08-31 20:58:21 -03001515 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001516 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1517 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 goto error0;
1519 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11001520 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001521 error = xfs_inobt_get_rec(cur, &rec, &i);
1522 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001523 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1524 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 goto error0;
1526 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11001527 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 /*
1529 * Get the offset in the inode chunk.
1530 */
1531 off = agino - rec.ir_startino;
1532 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001533 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /*
1535 * Mark the inode free & increment the count.
1536 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001537 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 rec.ir_freecount++;
1539
1540 /*
Brian Foster999633d2015-05-29 08:51:37 +10001541 * When an inode chunk is free, it becomes eligible for removal. Don't
1542 * remove the chunk if the block size is large enough for multiple inode
1543 * chunks (that might not be free).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001545 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Brian Foster999633d2015-05-29 08:51:37 +10001546 rec.ir_free == XFS_INOBT_ALL_FREE &&
1547 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Roger Willcocks376c2f32014-05-20 08:52:21 +10001549 *deleted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1551
1552 /*
1553 * Remove the inode cluster from the AGI B+Tree, adjust the
1554 * AGI and Superblock inode counts, and mark the disk space
1555 * to be freed when the transaction is committed.
1556 */
Brian Foster999633d2015-05-29 08:51:37 +10001557 ilen = rec.ir_freecount;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001558 be32_add_cpu(&agi->agi_count, -ilen);
1559 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001561 pag = xfs_perag_get(mp, agno);
1562 pag->pagi_freecount -= ilen - 1;
1563 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1565 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1566
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001567 if ((error = xfs_btree_delete(cur, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001568 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1569 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto error0;
1571 }
1572
Jie Liu126cd102013-12-13 15:51:48 +11001573 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno,
1574 XFS_AGINO_TO_AGBNO(mp, rec.ir_startino)),
1575 mp->m_ialloc_blks, flist, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 } else {
Roger Willcocks376c2f32014-05-20 08:52:21 +10001577 *deleted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Christoph Hellwigafabc242009-08-31 20:57:03 -03001579 error = xfs_inobt_update(cur, &rec);
1580 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001581 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1582 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto error0;
1584 }
Christoph Hellwigafabc242009-08-31 20:57:03 -03001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /*
1587 * Change the inode free counts and log the ag/sb changes.
1588 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001589 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001591 pag = xfs_perag_get(mp, agno);
1592 pag->pagi_freecount++;
1593 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1595 }
1596
Dave Chinner0b48db82009-08-31 20:57:09 -03001597 error = xfs_check_agi_freecount(cur, agi);
1598 if (error)
1599 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Brian Foster2b64ee52014-04-24 16:00:53 +10001601 *orec = rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1603 return 0;
1604
1605error0:
1606 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1607 return error;
1608}
1609
Brian Foster2b64ee52014-04-24 16:00:53 +10001610/*
Brian Foster3efa4ff2014-04-24 16:00:53 +10001611 * Free an inode in the free inode btree.
1612 */
1613STATIC int
1614xfs_difree_finobt(
1615 struct xfs_mount *mp,
1616 struct xfs_trans *tp,
1617 struct xfs_buf *agbp,
1618 xfs_agino_t agino,
1619 struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
1620{
1621 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1622 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1623 struct xfs_btree_cur *cur;
1624 struct xfs_inobt_rec_incore rec;
1625 int offset = agino - ibtrec->ir_startino;
1626 int error;
1627 int i;
1628
1629 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1630
1631 error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
1632 if (error)
1633 goto error;
1634 if (i == 0) {
1635 /*
1636 * If the record does not exist in the finobt, we must have just
1637 * freed an inode in a previously fully allocated chunk. If not,
1638 * something is out of sync.
1639 */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001640 XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error);
Brian Foster3efa4ff2014-04-24 16:00:53 +10001641
Brian Foster54190402015-05-29 09:03:04 +10001642 error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
1643 ibtrec->ir_count,
1644 ibtrec->ir_freecount,
Brian Foster3efa4ff2014-04-24 16:00:53 +10001645 ibtrec->ir_free, &i);
1646 if (error)
1647 goto error;
1648 ASSERT(i == 1);
1649
1650 goto out;
1651 }
1652
1653 /*
1654 * Read and update the existing record. We could just copy the ibtrec
1655 * across here, but that would defeat the purpose of having redundant
1656 * metadata. By making the modifications independently, we can catch
1657 * corruptions that we wouldn't see if we just copied from one record
1658 * to another.
1659 */
1660 error = xfs_inobt_get_rec(cur, &rec, &i);
1661 if (error)
1662 goto error;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001663 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
Brian Foster3efa4ff2014-04-24 16:00:53 +10001664
1665 rec.ir_free |= XFS_INOBT_MASK(offset);
1666 rec.ir_freecount++;
1667
Eric Sandeenc29aad42015-02-23 22:39:08 +11001668 XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) &&
Brian Foster3efa4ff2014-04-24 16:00:53 +10001669 (rec.ir_freecount == ibtrec->ir_freecount),
1670 error);
1671
1672 /*
1673 * The content of inobt records should always match between the inobt
1674 * and finobt. The lifecycle of records in the finobt is different from
1675 * the inobt in that the finobt only tracks records with at least one
1676 * free inode. Hence, if all of the inodes are free and we aren't
1677 * keeping inode chunks permanently on disk, remove the record.
1678 * Otherwise, update the record with the new information.
Brian Foster999633d2015-05-29 08:51:37 +10001679 *
1680 * Note that we currently can't free chunks when the block size is large
1681 * enough for multiple chunks. Leave the finobt record to remain in sync
1682 * with the inobt.
Brian Foster3efa4ff2014-04-24 16:00:53 +10001683 */
Brian Foster999633d2015-05-29 08:51:37 +10001684 if (rec.ir_free == XFS_INOBT_ALL_FREE &&
1685 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
Brian Foster3efa4ff2014-04-24 16:00:53 +10001686 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
1687 error = xfs_btree_delete(cur, &i);
1688 if (error)
1689 goto error;
1690 ASSERT(i == 1);
1691 } else {
1692 error = xfs_inobt_update(cur, &rec);
1693 if (error)
1694 goto error;
1695 }
1696
1697out:
1698 error = xfs_check_agi_freecount(cur, agi);
1699 if (error)
1700 goto error;
1701
1702 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1703 return 0;
1704
1705error:
1706 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1707 return error;
1708}
1709
1710/*
Brian Foster2b64ee52014-04-24 16:00:53 +10001711 * Free disk inode. Carefully avoids touching the incore inode, all
1712 * manipulations incore are the caller's responsibility.
1713 * The on-disk inode is not changed by this operation, only the
1714 * btree (free inode mask) is changed.
1715 */
1716int
1717xfs_difree(
1718 struct xfs_trans *tp, /* transaction pointer */
1719 xfs_ino_t inode, /* inode to be freed */
1720 struct xfs_bmap_free *flist, /* extents to free */
Dave Chinner0d907a32014-05-20 08:56:00 +10001721 int *deleted,/* set if inode cluster was deleted */
Brian Foster2b64ee52014-04-24 16:00:53 +10001722 xfs_ino_t *first_ino)/* first inode in deleted cluster */
1723{
1724 /* REFERENCED */
1725 xfs_agblock_t agbno; /* block number containing inode */
1726 struct xfs_buf *agbp; /* buffer for allocation group header */
1727 xfs_agino_t agino; /* allocation group inode number */
1728 xfs_agnumber_t agno; /* allocation group number */
1729 int error; /* error return value */
1730 struct xfs_mount *mp; /* mount structure for filesystem */
1731 struct xfs_inobt_rec_incore rec;/* btree record */
1732
1733 mp = tp->t_mountp;
1734
1735 /*
1736 * Break up inode number into its components.
1737 */
1738 agno = XFS_INO_TO_AGNO(mp, inode);
1739 if (agno >= mp->m_sb.sb_agcount) {
1740 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1741 __func__, agno, mp->m_sb.sb_agcount);
1742 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001743 return -EINVAL;
Brian Foster2b64ee52014-04-24 16:00:53 +10001744 }
1745 agino = XFS_INO_TO_AGINO(mp, inode);
1746 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1747 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1748 __func__, (unsigned long long)inode,
1749 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
1750 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001751 return -EINVAL;
Brian Foster2b64ee52014-04-24 16:00:53 +10001752 }
1753 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1754 if (agbno >= mp->m_sb.sb_agblocks) {
1755 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1756 __func__, agbno, mp->m_sb.sb_agblocks);
1757 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001758 return -EINVAL;
Brian Foster2b64ee52014-04-24 16:00:53 +10001759 }
1760 /*
1761 * Get the allocation group header.
1762 */
1763 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1764 if (error) {
1765 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1766 __func__, error);
1767 return error;
1768 }
1769
1770 /*
1771 * Fix up the inode allocation btree.
1772 */
Dave Chinner0d907a32014-05-20 08:56:00 +10001773 error = xfs_difree_inobt(mp, tp, agbp, agino, flist, deleted, first_ino,
Brian Foster2b64ee52014-04-24 16:00:53 +10001774 &rec);
1775 if (error)
1776 goto error0;
1777
Brian Foster3efa4ff2014-04-24 16:00:53 +10001778 /*
1779 * Fix up the free inode btree.
1780 */
1781 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
1782 error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
1783 if (error)
1784 goto error0;
1785 }
1786
Brian Foster2b64ee52014-04-24 16:00:53 +10001787 return 0;
1788
1789error0:
1790 return error;
1791}
1792
Dave Chinner7124fe02010-06-24 11:15:33 +10001793STATIC int
1794xfs_imap_lookup(
1795 struct xfs_mount *mp,
1796 struct xfs_trans *tp,
1797 xfs_agnumber_t agno,
1798 xfs_agino_t agino,
1799 xfs_agblock_t agbno,
1800 xfs_agblock_t *chunk_agbno,
1801 xfs_agblock_t *offset_agbno,
1802 int flags)
1803{
1804 struct xfs_inobt_rec_incore rec;
1805 struct xfs_btree_cur *cur;
1806 struct xfs_buf *agbp;
Dave Chinner7124fe02010-06-24 11:15:33 +10001807 int error;
1808 int i;
1809
1810 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1811 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +11001812 xfs_alert(mp,
1813 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1814 __func__, error, agno);
Dave Chinner7124fe02010-06-24 11:15:33 +10001815 return error;
1816 }
1817
1818 /*
Dave Chinner4536f2a2010-08-24 11:42:30 +10001819 * Lookup the inode record for the given agino. If the record cannot be
1820 * found, then it's an invalid inode number and we should abort. Once
1821 * we have a record, we need to ensure it contains the inode number
1822 * we are looking up.
Dave Chinner7124fe02010-06-24 11:15:33 +10001823 */
Brian Foster57bd3db2014-04-24 16:00:50 +10001824 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Dave Chinner4536f2a2010-08-24 11:42:30 +10001825 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
Dave Chinner7124fe02010-06-24 11:15:33 +10001826 if (!error) {
1827 if (i)
1828 error = xfs_inobt_get_rec(cur, &rec, &i);
1829 if (!error && i == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001830 error = -EINVAL;
Dave Chinner7124fe02010-06-24 11:15:33 +10001831 }
1832
1833 xfs_trans_brelse(tp, agbp);
1834 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1835 if (error)
1836 return error;
1837
Dave Chinner4536f2a2010-08-24 11:42:30 +10001838 /* check that the returned record contains the required inode */
1839 if (rec.ir_startino > agino ||
Jie Liu71783432013-12-13 15:51:46 +11001840 rec.ir_startino + mp->m_ialloc_inos <= agino)
Dave Chinner24513372014-06-25 14:58:08 +10001841 return -EINVAL;
Dave Chinner4536f2a2010-08-24 11:42:30 +10001842
Dave Chinner7124fe02010-06-24 11:15:33 +10001843 /* for untrusted inodes check it is allocated first */
Dave Chinner19207792010-06-24 11:15:47 +10001844 if ((flags & XFS_IGET_UNTRUSTED) &&
Dave Chinner7124fe02010-06-24 11:15:33 +10001845 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
Dave Chinner24513372014-06-25 14:58:08 +10001846 return -EINVAL;
Dave Chinner7124fe02010-06-24 11:15:33 +10001847
1848 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1849 *offset_agbno = agbno - *chunk_agbno;
1850 return 0;
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001854 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001857xfs_imap(
1858 xfs_mount_t *mp, /* file system mount structure */
1859 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001861 struct xfs_imap *imap, /* location map structure */
1862 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 xfs_agino_t agino; /* inode number within alloc group */
1866 xfs_agnumber_t agno; /* allocation group number */
1867 int blks_per_cluster; /* num blocks per inode cluster */
1868 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 int offset; /* index of inode in its buffer */
Dave Chinner836a94a2013-08-12 20:49:44 +10001872 xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /*
1877 * Split up the inode number into its parts.
1878 */
1879 agno = XFS_INO_TO_AGNO(mp, ino);
1880 agino = XFS_INO_TO_AGINO(mp, ino);
1881 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1882 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1883 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1884#ifdef DEBUG
Dave Chinner19207792010-06-24 11:15:47 +10001885 /*
1886 * Don't output diagnostic information for untrusted inodes
1887 * as they can be invalid without implying corruption.
1888 */
1889 if (flags & XFS_IGET_UNTRUSTED)
Dave Chinner24513372014-06-25 14:58:08 +10001890 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner53487782011-03-07 10:05:35 +11001892 xfs_alert(mp,
1893 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1894 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner53487782011-03-07 10:05:35 +11001897 xfs_alert(mp,
1898 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1899 __func__, (unsigned long long)agbno,
1900 (unsigned long)mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner53487782011-03-07 10:05:35 +11001903 xfs_alert(mp,
1904 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1905 __func__, ino,
1906 XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001908 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909#endif /* DEBUG */
Dave Chinner24513372014-06-25 14:58:08 +10001910 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001912
Jie Liuf9e5abc2013-12-13 15:51:49 +11001913 blks_per_cluster = xfs_icluster_size_fsb(mp);
Dave Chinner7124fe02010-06-24 11:15:33 +10001914
1915 /*
1916 * For bulkstat and handle lookups, we have an untrusted inode number
1917 * that we have to verify is valid. We cannot do this just by reading
1918 * the inode buffer as it may have been unlinked and removed leaving
1919 * inodes in stale state on disk. Hence we have to do a btree lookup
1920 * in all cases where an untrusted inode number is passed.
1921 */
Dave Chinner19207792010-06-24 11:15:47 +10001922 if (flags & XFS_IGET_UNTRUSTED) {
Dave Chinner7124fe02010-06-24 11:15:33 +10001923 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1924 &chunk_agbno, &offset_agbno, flags);
1925 if (error)
1926 return error;
1927 goto out_map;
1928 }
1929
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001930 /*
1931 * If the inode cluster size is the same as the blocksize or
1932 * smaller we get to the buffer by simple arithmetics.
1933 */
Jie Liuf9e5abc2013-12-13 15:51:49 +11001934 if (blks_per_cluster == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 offset = XFS_INO_TO_OFFSET(mp, ino);
1936 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001937
1938 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1939 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1940 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return 0;
1942 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001943
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001944 /*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001945 * If the inode chunks are aligned then use simple maths to
1946 * find the location. Otherwise we have to do a btree
1947 * lookup to find the location.
1948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (mp->m_inoalign_mask) {
1950 offset_agbno = agbno & mp->m_inoalign_mask;
1951 chunk_agbno = agbno - offset_agbno;
1952 } else {
Dave Chinner7124fe02010-06-24 11:15:33 +10001953 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1954 &chunk_agbno, &offset_agbno, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (error)
1956 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001958
Dave Chinner7124fe02010-06-24 11:15:33 +10001959out_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 ASSERT(agbno >= chunk_agbno);
1961 cluster_agbno = chunk_agbno +
1962 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1963 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1964 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001965
1966 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1967 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1968 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1969
1970 /*
1971 * If the inode number maps to a block outside the bounds
1972 * of the file system then return NULL rather than calling
1973 * read_buf and panicing when we get an error from the
1974 * driver.
1975 */
1976 if ((imap->im_blkno + imap->im_len) >
1977 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
Dave Chinner53487782011-03-07 10:05:35 +11001978 xfs_alert(mp,
1979 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1980 __func__, (unsigned long long) imap->im_blkno,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001981 (unsigned long long) imap->im_len,
1982 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
Dave Chinner24513372014-06-25 14:58:08 +10001983 return -EINVAL;
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
1988/*
1989 * Compute and fill in value of m_in_maxlevels.
1990 */
1991void
1992xfs_ialloc_compute_maxlevels(
1993 xfs_mount_t *mp) /* file system mount structure */
1994{
1995 int level;
1996 uint maxblocks;
1997 uint maxleafents;
1998 int minleafrecs;
1999 int minnoderecs;
2000
2001 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
2002 XFS_INODES_PER_CHUNK_LOG;
2003 minleafrecs = mp->m_alloc_mnr[0];
2004 minnoderecs = mp->m_alloc_mnr[1];
2005 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
2006 for (level = 1; maxblocks > 1; level++)
2007 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
2008 mp->m_in_maxlevels = level;
2009}
2010
2011/*
Brian Fosteraafc3c22014-04-24 16:00:52 +10002012 * Log specified fields for the ag hdr (inode section). The growth of the agi
2013 * structure over time requires that we interpret the buffer as two logical
2014 * regions delineated by the end of the unlinked list. This is due to the size
2015 * of the hash table and its location in the middle of the agi.
2016 *
2017 * For example, a request to log a field before agi_unlinked and a field after
2018 * agi_unlinked could cause us to log the entire hash table and use an excessive
2019 * amount of log space. To avoid this behavior, log the region up through
2020 * agi_unlinked in one call and the region after agi_unlinked through the end of
2021 * the structure in another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 */
2023void
2024xfs_ialloc_log_agi(
2025 xfs_trans_t *tp, /* transaction pointer */
2026 xfs_buf_t *bp, /* allocation group header buffer */
2027 int fields) /* bitmask of fields to log */
2028{
2029 int first; /* first byte number */
2030 int last; /* last byte number */
2031 static const short offsets[] = { /* field starting offsets */
2032 /* keep in sync with bit definitions */
2033 offsetof(xfs_agi_t, agi_magicnum),
2034 offsetof(xfs_agi_t, agi_versionnum),
2035 offsetof(xfs_agi_t, agi_seqno),
2036 offsetof(xfs_agi_t, agi_length),
2037 offsetof(xfs_agi_t, agi_count),
2038 offsetof(xfs_agi_t, agi_root),
2039 offsetof(xfs_agi_t, agi_level),
2040 offsetof(xfs_agi_t, agi_freecount),
2041 offsetof(xfs_agi_t, agi_newino),
2042 offsetof(xfs_agi_t, agi_dirino),
2043 offsetof(xfs_agi_t, agi_unlinked),
Brian Fosteraafc3c22014-04-24 16:00:52 +10002044 offsetof(xfs_agi_t, agi_free_root),
2045 offsetof(xfs_agi_t, agi_free_level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 sizeof(xfs_agi_t)
2047 };
2048#ifdef DEBUG
2049 xfs_agi_t *agi; /* allocation group header */
2050
2051 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002052 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053#endif
Brian Fosteraafc3c22014-04-24 16:00:52 +10002054
Dave Chinner61fe1352013-04-03 16:11:30 +11002055 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
Brian Fosteraafc3c22014-04-24 16:00:52 +10002056
2057 /*
2058 * Compute byte offsets for the first and last fields in the first
2059 * region and log the agi buffer. This only logs up through
2060 * agi_unlinked.
2061 */
2062 if (fields & XFS_AGI_ALL_BITS_R1) {
2063 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
2064 &first, &last);
2065 xfs_trans_log_buf(tp, bp, first, last);
2066 }
2067
2068 /*
2069 * Mask off the bits in the first region and calculate the first and
2070 * last field offsets for any bits in the second region.
2071 */
2072 fields &= ~XFS_AGI_ALL_BITS_R1;
2073 if (fields) {
2074 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
2075 &first, &last);
2076 xfs_trans_log_buf(tp, bp, first, last);
2077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002080#ifdef DEBUG
2081STATIC void
2082xfs_check_agi_unlinked(
2083 struct xfs_agi *agi)
2084{
2085 int i;
2086
2087 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
2088 ASSERT(agi->agi_unlinked[i]);
2089}
2090#else
2091#define xfs_check_agi_unlinked(agi)
2092#endif
2093
Dave Chinner983d09f2013-04-03 16:11:15 +11002094static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002095xfs_agi_verify(
Dave Chinner3702ce62012-11-12 22:54:05 +11002096 struct xfs_buf *bp)
2097{
2098 struct xfs_mount *mp = bp->b_target->bt_mount;
2099 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
Dave Chinner3702ce62012-11-12 22:54:05 +11002100
Dave Chinner983d09f2013-04-03 16:11:15 +11002101 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2102 !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
2103 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002104 /*
2105 * Validate the magic number of the agi block.
2106 */
Dave Chinner983d09f2013-04-03 16:11:15 +11002107 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
2108 return false;
2109 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
2110 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002111
Eric Sandeene1b05722014-09-09 11:47:24 +10002112 if (be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
2113 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002114 /*
2115 * during growfs operations, the perag is not fully initialised,
2116 * so we can't use it for any useful checking. growfs ensures we can't
2117 * use it by using uncached buffers that don't have the perag attached
2118 * so we can detect and avoid this problem.
2119 */
Dave Chinner983d09f2013-04-03 16:11:15 +11002120 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
2121 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002122
Dave Chinner3702ce62012-11-12 22:54:05 +11002123 xfs_check_agi_unlinked(agi);
Dave Chinner983d09f2013-04-03 16:11:15 +11002124 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +11002125}
2126
Dave Chinner1813dd62012-11-14 17:54:40 +11002127static void
2128xfs_agi_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002129 struct xfs_buf *bp)
2130{
Dave Chinner983d09f2013-04-03 16:11:15 +11002131 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner983d09f2013-04-03 16:11:15 +11002132
Eric Sandeence5028c2014-02-27 15:23:10 +11002133 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2134 !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002135 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002136 else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp,
2137 XFS_ERRTAG_IALLOC_READ_AGI,
2138 XFS_RANDOM_IALLOC_READ_AGI))
Dave Chinner24513372014-06-25 14:58:08 +10002139 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002140
2141 if (bp->b_error)
2142 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002143}
2144
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002145static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002146xfs_agi_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002147 struct xfs_buf *bp)
2148{
Dave Chinner983d09f2013-04-03 16:11:15 +11002149 struct xfs_mount *mp = bp->b_target->bt_mount;
2150 struct xfs_buf_log_item *bip = bp->b_fspriv;
2151
2152 if (!xfs_agi_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002153 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002154 xfs_verifier_error(bp);
Dave Chinner983d09f2013-04-03 16:11:15 +11002155 return;
2156 }
2157
2158 if (!xfs_sb_version_hascrc(&mp->m_sb))
2159 return;
2160
2161 if (bip)
2162 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002163 xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
Dave Chinner3702ce62012-11-12 22:54:05 +11002164}
2165
Dave Chinner1813dd62012-11-14 17:54:40 +11002166const struct xfs_buf_ops xfs_agi_buf_ops = {
2167 .verify_read = xfs_agi_read_verify,
2168 .verify_write = xfs_agi_write_verify,
2169};
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171/*
2172 * Read in the allocation group header (inode allocation section)
2173 */
2174int
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002175xfs_read_agi(
2176 struct xfs_mount *mp, /* file system mount structure */
2177 struct xfs_trans *tp, /* transaction pointer */
2178 xfs_agnumber_t agno, /* allocation group number */
2179 struct xfs_buf **bpp) /* allocation group hdr buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002181 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Dave Chinnerd1230312013-11-01 15:27:19 +11002183 trace_xfs_read_agi(mp, agno);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002184
Dave Chinnerd1230312013-11-01 15:27:19 +11002185 ASSERT(agno != NULLAGNUMBER);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002186 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002188 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (error)
2190 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002191
Christoph Hellwig38f23232011-10-10 16:52:45 +00002192 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002193 return 0;
2194}
2195
2196int
2197xfs_ialloc_read_agi(
2198 struct xfs_mount *mp, /* file system mount structure */
2199 struct xfs_trans *tp, /* transaction pointer */
2200 xfs_agnumber_t agno, /* allocation group number */
2201 struct xfs_buf **bpp) /* allocation group hdr buf */
2202{
2203 struct xfs_agi *agi; /* allocation group header */
2204 struct xfs_perag *pag; /* per allocation group data */
2205 int error;
2206
Dave Chinnerd1230312013-11-01 15:27:19 +11002207 trace_xfs_ialloc_read_agi(mp, agno);
2208
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002209 error = xfs_read_agi(mp, tp, agno, bpp);
2210 if (error)
2211 return error;
2212
2213 agi = XFS_BUF_TO_AGI(*bpp);
Dave Chinner44b56e02010-01-11 11:47:43 +00002214 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002216 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10002217 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
2220
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002221 /*
2222 * It's possible for these to be out of sync if
2223 * we are in the middle of a forced shutdown.
2224 */
2225 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
2226 XFS_FORCED_SHUTDOWN(mp));
Dave Chinner44b56e02010-01-11 11:47:43 +00002227 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return 0;
2229}
David Chinner92821e22007-05-24 15:26:31 +10002230
2231/*
2232 * Read in the agi to initialise the per-ag data in the mount structure
2233 */
2234int
2235xfs_ialloc_pagi_init(
2236 xfs_mount_t *mp, /* file system mount structure */
2237 xfs_trans_t *tp, /* transaction pointer */
2238 xfs_agnumber_t agno) /* allocation group number */
2239{
2240 xfs_buf_t *bp = NULL;
2241 int error;
2242
2243 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
2244 if (error)
2245 return error;
2246 if (bp)
2247 xfs_trans_brelse(tp, bp);
2248 return 0;
2249}