blob: 7a0c17d7ec0974354cfd645695e9f4f7778704fa [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dinode.h"
32#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_btree.h"
34#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_rtalloc.h"
37#include "xfs_error.h"
38#include "xfs_bmap.h"
Dave Chinner983d09f2013-04-03 16:11:15 +110039#include "xfs_cksum.h"
40#include "xfs_buf_item.h"
Dave Chinnerddf6ad02013-06-27 16:04:56 +100041#include "xfs_icreate_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * Allocation group level functions.
46 */
David Chinner75de2a92008-03-27 18:00:38 +110047static inline int
48xfs_ialloc_cluster_alignment(
49 xfs_alloc_arg_t *args)
50{
51 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
52 args->mp->m_sb.sb_inoalignmt >=
53 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
54 return args->mp->m_sb.sb_inoalignmt;
55 return 1;
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
Christoph Hellwig21875502009-08-31 20:58:21 -030059 * Lookup a record by ino in the btree given by cur.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110060 */
Christoph Hellwig81e25172009-09-01 19:56:55 -040061int /* error */
Christoph Hellwig21875502009-08-31 20:58:21 -030062xfs_inobt_lookup(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110063 struct xfs_btree_cur *cur, /* btree cursor */
64 xfs_agino_t ino, /* starting inode of chunk */
Christoph Hellwig21875502009-08-31 20:58:21 -030065 xfs_lookup_t dir, /* <=, >=, == */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110066 int *stat) /* success/failure */
67{
68 cur->bc_rec.i.ir_startino = ino;
Christoph Hellwig21875502009-08-31 20:58:21 -030069 cur->bc_rec.i.ir_freecount = 0;
70 cur->bc_rec.i.ir_free = 0;
71 return xfs_btree_lookup(cur, dir, stat);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110072}
73
74/*
Christoph Hellwigafabc242009-08-31 20:57:03 -030075 * Update the record referred to by cur to the value given.
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110076 * This either works (return 0) or gets an EFSCORRUPTED error.
77 */
78STATIC int /* error */
79xfs_inobt_update(
80 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigafabc242009-08-31 20:57:03 -030081 xfs_inobt_rec_incore_t *irec) /* btree record */
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110082{
83 union xfs_btree_rec rec;
84
Christoph Hellwigafabc242009-08-31 20:57:03 -030085 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
86 rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
87 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110088 return xfs_btree_update(cur, &rec);
89}
90
91/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +110092 * Get the data from the pointed-to record.
93 */
94int /* error */
95xfs_inobt_get_rec(
96 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig2e287a72009-08-31 20:56:58 -030097 xfs_inobt_rec_incore_t *irec, /* btree record */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +110098 int *stat) /* output: success/failure */
99{
100 union xfs_btree_rec *rec;
101 int error;
102
103 error = xfs_btree_get_rec(cur, &rec, stat);
104 if (!error && *stat == 1) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300105 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
106 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
107 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100108 }
109 return error;
110}
111
112/*
Dave Chinner0b48db82009-08-31 20:57:09 -0300113 * Verify that the number of free inodes in the AGI is correct.
114 */
115#ifdef DEBUG
116STATIC int
117xfs_check_agi_freecount(
118 struct xfs_btree_cur *cur,
119 struct xfs_agi *agi)
120{
121 if (cur->bc_nlevels == 1) {
122 xfs_inobt_rec_incore_t rec;
123 int freecount = 0;
124 int error;
125 int i;
126
Christoph Hellwig21875502009-08-31 20:58:21 -0300127 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
Dave Chinner0b48db82009-08-31 20:57:09 -0300128 if (error)
129 return error;
130
131 do {
132 error = xfs_inobt_get_rec(cur, &rec, &i);
133 if (error)
134 return error;
135
136 if (i) {
137 freecount += rec.ir_freecount;
138 error = xfs_btree_increment(cur, 0, &i);
139 if (error)
140 return error;
141 }
142 } while (i == 1);
143
144 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
145 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
146 }
147 return 0;
148}
149#else
150#define xfs_check_agi_freecount(cur, agi) 0
151#endif
152
153/*
Dave Chinner28c8e412013-06-27 16:04:55 +1000154 * Initialise a new set of inodes. When called without a transaction context
155 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
156 * than logging them (which in a transaction context puts them into the AIL
157 * for writeback rather than the xfsbufd queue).
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300158 */
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000159int
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300160xfs_ialloc_inode_init(
161 struct xfs_mount *mp,
162 struct xfs_trans *tp,
Dave Chinner28c8e412013-06-27 16:04:55 +1000163 struct list_head *buffer_list,
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300164 xfs_agnumber_t agno,
165 xfs_agblock_t agbno,
166 xfs_agblock_t length,
167 unsigned int gen)
168{
169 struct xfs_buf *fbuf;
170 struct xfs_dinode *free;
171 int blks_per_cluster, nbufs, ninodes;
172 int version;
173 int i, j;
174 xfs_daddr_t d;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100175 xfs_ino_t ino = 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300176
177 /*
178 * Loop over the new block(s), filling in the inodes.
179 * For small block sizes, manipulate the inodes in buffers
180 * which are multiples of the blocks size.
181 */
182 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
183 blks_per_cluster = 1;
184 nbufs = length;
185 ninodes = mp->m_sb.sb_inopblock;
186 } else {
187 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
188 mp->m_sb.sb_blocksize;
189 nbufs = length / blks_per_cluster;
190 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
191 }
192
193 /*
Christoph Hellwig93848a92013-04-03 16:11:17 +1100194 * Figure out what version number to use in the inodes we create. If
195 * the superblock version has caught up to the one that supports the new
196 * inode format, then use the new inode version. Otherwise use the old
197 * version so that old kernels will continue to be able to use the file
198 * system.
199 *
200 * For v3 inodes, we also need to write the inode number into the inode,
201 * so calculate the first inode number of the chunk here as
202 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
203 * across multiple filesystem blocks (such as a cluster) and so cannot
204 * be used in the cluster buffer loop below.
205 *
206 * Further, because we are writing the inode directly into the buffer
207 * and calculating a CRC on the entire inode, we have ot log the entire
208 * inode so that the entire range the CRC covers is present in the log.
209 * That means for v3 inode we log the entire buffer rather than just the
210 * inode cores.
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300211 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100212 if (xfs_sb_version_hascrc(&mp->m_sb)) {
213 version = 3;
214 ino = XFS_AGINO_TO_INO(mp, agno,
215 XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000216
217 /*
218 * log the initialisation that is about to take place as an
219 * logical operation. This means the transaction does not
220 * need to log the physical changes to the inode buffers as log
221 * recovery will know what initialisation is actually needed.
222 * Hence we only need to log the buffers as "ordered" buffers so
223 * they track in the AIL as if they were physically logged.
224 */
225 if (tp)
226 xfs_icreate_log(tp, agno, agbno, XFS_IALLOC_INODES(mp),
227 mp->m_sb.sb_inodesize, length, gen);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100228 } else if (xfs_sb_version_hasnlink(&mp->m_sb))
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300229 version = 2;
230 else
231 version = 1;
232
233 for (j = 0; j < nbufs; j++) {
234 /*
235 * Get the block.
236 */
237 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
238 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
Dave Chinner7c4cebe2012-11-23 14:24:23 +1100239 mp->m_bsize * blks_per_cluster,
240 XBF_UNMAPPED);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000241 if (!fbuf)
242 return ENOMEM;
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000243
244 /* Initialize the inode buffers and log them appropriately. */
Dave Chinner1813dd62012-11-14 17:54:40 +1100245 fbuf->b_ops = &xfs_inode_buf_ops;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100246 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300247 for (i = 0; i < ninodes; i++) {
248 int ioffset = i << mp->m_sb.sb_inodelog;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100249 uint isize = xfs_dinode_size(version);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300250
251 free = xfs_make_iptr(mp, fbuf, i);
252 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
253 free->di_version = version;
254 free->di_gen = cpu_to_be32(gen);
255 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100256
257 if (version == 3) {
258 free->di_ino = cpu_to_be64(ino);
259 ino++;
260 uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
261 xfs_dinode_calc_crc(mp, free);
Dave Chinner28c8e412013-06-27 16:04:55 +1000262 } else if (tp) {
Christoph Hellwig93848a92013-04-03 16:11:17 +1100263 /* just log the inode core */
264 xfs_trans_log_buf(tp, fbuf, ioffset,
265 ioffset + isize - 1);
266 }
267 }
Dave Chinner28c8e412013-06-27 16:04:55 +1000268
269 if (tp) {
270 /*
271 * Mark the buffer as an inode allocation buffer so it
272 * sticks in AIL at the point of this allocation
273 * transaction. This ensures the they are on disk before
274 * the tail of the log can be moved past this
275 * transaction (i.e. by preventing relogging from moving
276 * it forward in the log).
277 */
278 xfs_trans_inode_alloc_buf(tp, fbuf);
279 if (version == 3) {
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000280 /*
281 * Mark the buffer as ordered so that they are
282 * not physically logged in the transaction but
283 * still tracked in the AIL as part of the
284 * transaction and pin the log appropriately.
285 */
286 xfs_trans_ordered_buf(tp, fbuf);
Dave Chinner28c8e412013-06-27 16:04:55 +1000287 xfs_trans_log_buf(tp, fbuf, 0,
288 BBTOB(fbuf->b_length) - 1);
289 }
290 } else {
291 fbuf->b_flags |= XBF_DONE;
292 xfs_buf_delwri_queue(fbuf, buffer_list);
293 xfs_buf_relse(fbuf);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300294 }
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300295 }
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000296 return 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300297}
298
299/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * Allocate new inodes in the allocation group specified by agbp.
301 * Return 0 for success, else error code.
302 */
303STATIC int /* error code or 0 */
304xfs_ialloc_ag_alloc(
305 xfs_trans_t *tp, /* transaction pointer */
306 xfs_buf_t *agbp, /* alloc group buffer */
307 int *alloc)
308{
309 xfs_agi_t *agi; /* allocation group header */
310 xfs_alloc_arg_t args; /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 xfs_btree_cur_t *cur; /* inode btree cursor */
David Chinner92821e22007-05-24 15:26:31 +1000312 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300314 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 xfs_agino_t newino; /* new first inode's number */
316 xfs_agino_t newlen; /* new number of inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 xfs_agino_t thisino; /* current inode number, for loop */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000318 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* boundary */
Dave Chinner44b56e02010-01-11 11:47:43 +0000320 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Mark Tinguelya0041682012-09-20 13:16:45 -0500322 memset(&args, 0, sizeof(args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 args.tp = tp;
324 args.mp = tp->t_mountp;
325
326 /*
327 * Locking will ensure that we don't have two callers in here
328 * at one time.
329 */
330 newlen = XFS_IALLOC_INODES(args.mp);
331 if (args.mp->m_maxicount &&
332 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
333 return XFS_ERROR(ENOSPC);
334 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
335 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000336 * First try to allocate inodes contiguous with the last-allocated
337 * chunk of inodes. If the filesystem is striped, this will fill
338 * an entire stripe unit with inodes.
Dave Chinner28c8e412013-06-27 16:04:55 +1000339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000341 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300342 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000343 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
344 XFS_IALLOC_BLOCKS(args.mp);
345 if (likely(newino != NULLAGINO &&
346 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300347 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000348 args.type = XFS_ALLOCTYPE_THIS_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000349 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100350
Glen Overby3ccb8b52006-03-29 09:52:28 +1000351 /*
David Chinner75de2a92008-03-27 18:00:38 +1100352 * We need to take into account alignment here to ensure that
353 * we don't modify the free list if we fail to have an exact
354 * block. If we don't have an exact match, and every oher
355 * attempt allocation attempt fails, we'll end up cancelling
356 * a dirty transaction and shutting down.
357 *
358 * For an exact allocation, alignment must be 1,
359 * however we need to take cluster alignment into account when
360 * fixing up the freelist. Use the minalignslop field to
361 * indicate that extra blocks might be required for alignment,
362 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000363 */
David Chinner75de2a92008-03-27 18:00:38 +1100364 args.alignment = 1;
365 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
366
367 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100368 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000369 if ((error = xfs_alloc_vextent(&args)))
370 return error;
371 } else
372 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Glen Overby3ccb8b52006-03-29 09:52:28 +1000374 if (unlikely(args.fsbno == NULLFSBLOCK)) {
375 /*
376 * Set the alignment for the allocation.
377 * If stripe alignment is turned on then align at stripe unit
378 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000379 * If the cluster size is smaller than a filesystem block
380 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000381 * pieces, so don't need alignment anyway.
382 */
383 isaligned = 0;
384 if (args.mp->m_sinoalign) {
385 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
386 args.alignment = args.mp->m_dalign;
387 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100388 } else
389 args.alignment = xfs_ialloc_cluster_alignment(&args);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000390 /*
391 * Need to figure out where to allocate the inode blocks.
392 * Ideally they should be spaced out through the a.g.
393 * For now, just allocate blocks up front.
394 */
395 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300396 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000397 /*
398 * Allocate a fixed-size extent of inodes.
399 */
400 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000401 args.prod = 1;
402 /*
403 * Allow space for the inode btree to split.
404 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100405 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000406 if ((error = xfs_alloc_vextent(&args)))
407 return error;
408 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * If stripe alignment is turned on, then try again with cluster
412 * alignment.
413 */
414 if (isaligned && args.fsbno == NULLFSBLOCK) {
415 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100416 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300417 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
David Chinner75de2a92008-03-27 18:00:38 +1100418 args.alignment = xfs_ialloc_cluster_alignment(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if ((error = xfs_alloc_vextent(&args)))
420 return error;
421 }
422
423 if (args.fsbno == NULLFSBLOCK) {
424 *alloc = 0;
425 return 0;
426 }
427 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David Chinner359346a2008-04-29 12:53:32 +1000429 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300430 * Stamp and write the inode buffers.
431 *
David Chinner359346a2008-04-29 12:53:32 +1000432 * Seed the new inode cluster with a random generation number. This
433 * prevents short-term reuse of generation numbers if a chunk is
434 * freed and then immediately reallocated. We use random numbers
435 * rather than a linear progression to prevent the next generation
436 * number from being easily guessable.
437 */
Dave Chinner28c8e412013-06-27 16:04:55 +1000438 error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
Akinobu Mitaecb34032013-03-04 21:58:20 +0900439 args.len, prandom_u32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100440
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000441 if (error)
442 return error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300443 /*
444 * Convert the results.
445 */
446 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800447 be32_add_cpu(&agi->agi_count, newlen);
448 be32_add_cpu(&agi->agi_freecount, newlen);
Dave Chinner44b56e02010-01-11 11:47:43 +0000449 pag = xfs_perag_get(args.mp, agno);
450 pag->pagi_freecount += newlen;
451 xfs_perag_put(pag);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100452 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 * Insert records describing the new inode chunk into the btree.
456 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100457 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 for (thisino = newino;
459 thisino < newino + newlen;
460 thisino += XFS_INODES_PER_CHUNK) {
Christoph Hellwig21875502009-08-31 20:58:21 -0300461 cur->bc_rec.i.ir_startino = thisino;
462 cur->bc_rec.i.ir_freecount = XFS_INODES_PER_CHUNK;
463 cur->bc_rec.i.ir_free = XFS_INOBT_ALL_FREE;
464 error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, &i);
465 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
467 return error;
468 }
469 ASSERT(i == 0);
Christoph Hellwig21875502009-08-31 20:58:21 -0300470 error = xfs_btree_insert(cur, &i);
471 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
473 return error;
474 }
475 ASSERT(i == 1);
476 }
477 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
478 /*
479 * Log allocation group header fields
480 */
481 xfs_ialloc_log_agi(tp, agbp,
482 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
483 /*
484 * Modify/log superblock values for inode count and inode free count.
485 */
486 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
487 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
488 *alloc = 1;
489 return 0;
490}
491
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000492STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493xfs_ialloc_next_ag(
494 xfs_mount_t *mp)
495{
496 xfs_agnumber_t agno;
497
498 spin_lock(&mp->m_agirotor_lock);
499 agno = mp->m_agirotor;
Carlos Maiolino8aea3ff2012-09-20 10:32:36 -0300500 if (++mp->m_agirotor >= mp->m_maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 mp->m_agirotor = 0;
502 spin_unlock(&mp->m_agirotor_lock);
503
504 return agno;
505}
506
507/*
508 * Select an allocation group to look for a free inode in, based on the parent
509 * inode and then mode. Return the allocation group buffer.
510 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400511STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512xfs_ialloc_ag_select(
513 xfs_trans_t *tp, /* transaction pointer */
514 xfs_ino_t parent, /* parent directory inode number */
Al Viro576b1d62011-07-26 02:50:15 -0400515 umode_t mode, /* bits set to indicate file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 int okalloc) /* ok to allocate more space */
517{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
519 xfs_agnumber_t agno; /* current ag number */
520 int flags; /* alloc buffer locking flags */
521 xfs_extlen_t ineed; /* blocks needed for inode allocation */
522 xfs_extlen_t longest = 0; /* longest extent available */
523 xfs_mount_t *mp; /* mount point structure */
524 int needspace; /* file mode implies space allocated */
525 xfs_perag_t *pag; /* per allocation group data */
526 xfs_agnumber_t pagno; /* parent (starting) ag number */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400527 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /*
530 * Files of these types need at least one block if length > 0
531 * (and they won't fit in the inode, but that's hard to figure out).
532 */
533 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
534 mp = tp->t_mountp;
535 agcount = mp->m_maxagi;
536 if (S_ISDIR(mode))
537 pagno = xfs_ialloc_next_ag(mp);
538 else {
539 pagno = XFS_INO_TO_AGNO(mp, parent);
540 if (pagno >= agcount)
541 pagno = 0;
542 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 ASSERT(pagno < agcount);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /*
547 * Loop through allocation groups, looking for one with a little
548 * free space in it. Note we don't look for free inodes, exactly.
549 * Instead, we include whether there is a need to allocate inodes
550 * to mean that blocks must be allocated for them,
551 * if none are currently free.
552 */
553 agno = pagno;
554 flags = XFS_ALLOC_FLAG_TRYLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 for (;;) {
Dave Chinner44b56e02010-01-11 11:47:43 +0000556 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (!pag->pagi_inodeok) {
558 xfs_ialloc_next_ag(mp);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400559 goto nextag;
560 }
561
562 if (!pag->pagi_init) {
563 error = xfs_ialloc_pagi_init(mp, tp, agno);
564 if (error)
565 goto nextag;
566 }
567
568 if (pag->pagi_freecount) {
569 xfs_perag_put(pag);
570 return agno;
571 }
572
573 if (!okalloc)
574 goto nextag;
575
576 if (!pag->pagf_init) {
577 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
578 if (error)
579 goto nextag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 /*
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400583 * Is there enough free space for the file plus a block of
584 * inodes? (if we need to allocate some)?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400586 ineed = XFS_IALLOC_BLOCKS(mp);
587 longest = pag->pagf_longest;
588 if (!longest)
589 longest = pag->pagf_flcount > 0;
590
591 if (pag->pagf_freeblks >= needspace + ineed &&
592 longest >= ineed) {
593 xfs_perag_put(pag);
594 return agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596nextag:
Dave Chinner44b56e02010-01-11 11:47:43 +0000597 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * No point in iterating over the rest, if we're shutting
600 * down.
601 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000602 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400603 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 agno++;
605 if (agno >= agcount)
606 agno = 0;
607 if (agno == pagno) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000608 if (flags == 0)
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400609 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 flags = 0;
611 }
612 }
613}
614
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300615/*
616 * Try to retrieve the next record to the left/right from the current one.
617 */
618STATIC int
619xfs_ialloc_next_rec(
620 struct xfs_btree_cur *cur,
621 xfs_inobt_rec_incore_t *rec,
622 int *done,
623 int left)
624{
625 int error;
626 int i;
627
628 if (left)
629 error = xfs_btree_decrement(cur, 0, &i);
630 else
631 error = xfs_btree_increment(cur, 0, &i);
632
633 if (error)
634 return error;
635 *done = !i;
636 if (i) {
637 error = xfs_inobt_get_rec(cur, rec, &i);
638 if (error)
639 return error;
640 XFS_WANT_CORRUPTED_RETURN(i == 1);
641 }
642
643 return 0;
644}
645
Dave Chinnerbd169562009-08-31 20:58:28 -0300646STATIC int
647xfs_ialloc_get_rec(
648 struct xfs_btree_cur *cur,
649 xfs_agino_t agino,
650 xfs_inobt_rec_incore_t *rec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800651 int *done)
Dave Chinnerbd169562009-08-31 20:58:28 -0300652{
653 int error;
654 int i;
655
656 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
657 if (error)
658 return error;
659 *done = !i;
660 if (i) {
661 error = xfs_inobt_get_rec(cur, rec, &i);
662 if (error)
663 return error;
664 XFS_WANT_CORRUPTED_RETURN(i == 1);
665 }
666
667 return 0;
668}
Dave Chinner0b48db82009-08-31 20:57:09 -0300669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400671 * Allocate an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 *
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400673 * The caller selected an AG for us, and made sure that free inodes are
674 * available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400676STATIC int
677xfs_dialloc_ag(
678 struct xfs_trans *tp,
679 struct xfs_buf *agbp,
680 xfs_ino_t parent,
681 xfs_ino_t *inop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400683 struct xfs_mount *mp = tp->t_mountp;
684 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
685 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
686 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
687 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
688 struct xfs_perag *pag;
689 struct xfs_btree_cur *cur, *tcur;
690 struct xfs_inobt_rec_incore rec, trec;
691 xfs_ino_t ino;
692 int error;
693 int offset;
694 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dave Chinner44b56e02010-01-11 11:47:43 +0000696 pag = xfs_perag_get(mp, agno);
Dave Chinnerbd169562009-08-31 20:58:28 -0300697
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400698 ASSERT(pag->pagi_init);
699 ASSERT(pag->pagi_inodeok);
700 ASSERT(pag->pagi_freecount > 0);
701
Dave Chinnerbd169562009-08-31 20:58:28 -0300702 restart_pagno:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400703 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /*
705 * If pagino is 0 (this is the root inode allocation) use newino.
706 * This must work because we've just allocated some.
707 */
708 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100709 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dave Chinner0b48db82009-08-31 20:57:09 -0300711 error = xfs_check_agi_freecount(cur, agi);
712 if (error)
713 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300716 * If in the same AG as the parent, try to get near the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
718 if (pagno == agno) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300719 int doneleft; /* done, to the left */
720 int doneright; /* done, to the right */
Dave Chinnerbd169562009-08-31 20:58:28 -0300721 int searchdistance = 10;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300722
Christoph Hellwig21875502009-08-31 20:58:21 -0300723 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300724 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300726 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
727
728 error = xfs_inobt_get_rec(cur, &rec, &j);
729 if (error)
730 goto error0;
731 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
732
733 if (rec.ir_freecount > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * Found a free inode in the same chunk
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300736 * as the parent, done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300738 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300740
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300743 * In the same AG as parent, but parent's chunk is full.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300746 /* duplicate the cursor, search left & right simultaneously */
747 error = xfs_btree_dup_cursor(cur, &tcur);
748 if (error)
749 goto error0;
750
Dave Chinnerbd169562009-08-31 20:58:28 -0300751 /*
752 * Skip to last blocks looked up if same parent inode.
753 */
754 if (pagino != NULLAGINO &&
755 pag->pagl_pagino == pagino &&
756 pag->pagl_leftrec != NULLAGINO &&
757 pag->pagl_rightrec != NULLAGINO) {
758 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800759 &trec, &doneleft);
Dave Chinnerbd169562009-08-31 20:58:28 -0300760 if (error)
761 goto error1;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300762
Dave Chinnerbd169562009-08-31 20:58:28 -0300763 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800764 &rec, &doneright);
Dave Chinnerbd169562009-08-31 20:58:28 -0300765 if (error)
766 goto error1;
767 } else {
768 /* search left with tcur, back up 1 record */
769 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
770 if (error)
771 goto error1;
772
773 /* search right with cur, go forward 1 record. */
774 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
775 if (error)
776 goto error1;
777 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300778
779 /*
780 * Loop until we find an inode chunk with a free inode.
781 */
782 while (!doneleft || !doneright) {
783 int useleft; /* using left inode chunk this time */
784
Dave Chinnerbd169562009-08-31 20:58:28 -0300785 if (!--searchdistance) {
786 /*
787 * Not in range - save last search
788 * location and allocate a new inode
789 */
Eric Sandeen3b826382009-10-30 09:27:07 +0100790 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300791 pag->pagl_leftrec = trec.ir_startino;
792 pag->pagl_rightrec = rec.ir_startino;
793 pag->pagl_pagino = pagino;
794 goto newino;
795 }
796
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300797 /* figure out the closer block if both are valid. */
798 if (!doneleft && !doneright) {
799 useleft = pagino -
800 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
801 rec.ir_startino - pagino;
802 } else {
803 useleft = !doneleft;
804 }
805
806 /* free inodes to the left? */
807 if (useleft && trec.ir_freecount) {
808 rec = trec;
809 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
810 cur = tcur;
Dave Chinnerbd169562009-08-31 20:58:28 -0300811
812 pag->pagl_leftrec = trec.ir_startino;
813 pag->pagl_rightrec = rec.ir_startino;
814 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300815 goto alloc_inode;
816 }
817
818 /* free inodes to the right? */
819 if (!useleft && rec.ir_freecount) {
820 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300821
822 pag->pagl_leftrec = trec.ir_startino;
823 pag->pagl_rightrec = rec.ir_startino;
824 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300825 goto alloc_inode;
826 }
827
828 /* get next record to check */
829 if (useleft) {
830 error = xfs_ialloc_next_rec(tcur, &trec,
831 &doneleft, 1);
832 } else {
833 error = xfs_ialloc_next_rec(cur, &rec,
834 &doneright, 0);
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300839
840 /*
841 * We've reached the end of the btree. because
842 * we are only searching a small chunk of the
843 * btree each search, there is obviously free
844 * inodes closer to the parent inode than we
845 * are now. restart the search again.
846 */
847 pag->pagl_pagino = NULLAGINO;
848 pag->pagl_leftrec = NULLAGINO;
849 pag->pagl_rightrec = NULLAGINO;
850 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
851 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
852 goto restart_pagno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300856 * In a different AG from the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * See if the most recently allocated block has any free.
858 */
Dave Chinnerbd169562009-08-31 20:58:28 -0300859newino:
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200860 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
Christoph Hellwig21875502009-08-31 20:58:21 -0300861 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
862 XFS_LOOKUP_EQ, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300863 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300865
866 if (i == 1) {
867 error = xfs_inobt_get_rec(cur, &rec, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (error)
869 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300870
871 if (j == 1 && rec.ir_freecount > 0) {
872 /*
873 * The last chunk allocated in the group
874 * still has a free inode.
875 */
876 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300879 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300880
Dave Chinnerbd169562009-08-31 20:58:28 -0300881 /*
882 * None left in the last group, search the whole AG
883 */
884 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
885 if (error)
886 goto error0;
887 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
888
889 for (;;) {
890 error = xfs_inobt_get_rec(cur, &rec, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300891 if (error)
892 goto error0;
893 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Dave Chinnerbd169562009-08-31 20:58:28 -0300894 if (rec.ir_freecount > 0)
895 break;
896 error = xfs_btree_increment(cur, 0, &i);
897 if (error)
898 goto error0;
899 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300901
902alloc_inode:
Christoph Hellwig824c3132012-07-04 10:54:45 -0400903 offset = xfs_lowbit64(rec.ir_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 ASSERT(offset >= 0);
905 ASSERT(offset < XFS_INODES_PER_CHUNK);
906 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
907 XFS_INODES_PER_CHUNK) == 0);
908 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100909 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 rec.ir_freecount--;
Christoph Hellwigafabc242009-08-31 20:57:03 -0300911 error = xfs_inobt_update(cur, &rec);
912 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800914 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +0000916 pag->pagi_freecount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Dave Chinner0b48db82009-08-31 20:57:09 -0300918 error = xfs_check_agi_freecount(cur, agi);
919 if (error)
920 goto error0;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
923 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
Dave Chinner44b56e02010-01-11 11:47:43 +0000924 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 *inop = ino;
926 return 0;
927error1:
928 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
929error0:
930 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
Dave Chinner44b56e02010-01-11 11:47:43 +0000931 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return error;
933}
934
935/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400936 * Allocate an inode on disk.
937 *
938 * Mode is used to tell whether the new inode will need space, and whether it
939 * is a directory.
940 *
941 * This function is designed to be called twice if it has to do an allocation
942 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
943 * If an inode is available without having to performn an allocation, an inode
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300944 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
945 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
946 * The caller should then commit the current transaction, allocate a
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400947 * new transaction, and call xfs_dialloc() again, passing in the previous value
948 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
949 * buffer is locked across the two calls, the second call is guaranteed to have
950 * a free inode available.
951 *
952 * Once we successfully pick an inode its number is returned and the on-disk
953 * data structures are updated. The inode itself is not read in, since doing so
954 * would break ordering constraints with xfs_reclaim.
955 */
956int
957xfs_dialloc(
958 struct xfs_trans *tp,
959 xfs_ino_t parent,
960 umode_t mode,
961 int okalloc,
962 struct xfs_buf **IO_agbp,
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400963 xfs_ino_t *inop)
964{
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400965 struct xfs_mount *mp = tp->t_mountp;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400966 struct xfs_buf *agbp;
967 xfs_agnumber_t agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400968 int error;
969 int ialloced;
970 int noroom = 0;
Christoph Hellwigbe60fe52012-07-04 10:54:50 -0400971 xfs_agnumber_t start_agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400972 struct xfs_perag *pag;
973
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400974 if (*IO_agbp) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400975 /*
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400976 * If the caller passes in a pointer to the AGI buffer,
977 * continue where we left off before. In this case, we
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400978 * know that the allocation group has free inodes.
979 */
980 agbp = *IO_agbp;
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400981 goto out_alloc;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400982 }
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400983
984 /*
985 * We do not have an agbp, so select an initial allocation
986 * group for inode allocation.
987 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -0400988 start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
989 if (start_agno == NULLAGNUMBER) {
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400990 *inop = NULLFSINO;
991 return 0;
992 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400993
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400994 /*
995 * If we have already hit the ceiling of inode blocks then clear
996 * okalloc so we scan all available agi structures for a free
997 * inode.
998 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400999 if (mp->m_maxicount &&
1000 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
1001 noroom = 1;
1002 okalloc = 0;
1003 }
1004
1005 /*
1006 * Loop until we find an allocation group that either has free inodes
1007 * or in which we can allocate some inodes. Iterate through the
1008 * allocation groups upward, wrapping at the end.
1009 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001010 agno = start_agno;
1011 for (;;) {
1012 pag = xfs_perag_get(mp, agno);
1013 if (!pag->pagi_inodeok) {
1014 xfs_ialloc_next_ag(mp);
1015 goto nextag;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001016 }
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001017
1018 if (!pag->pagi_init) {
1019 error = xfs_ialloc_pagi_init(mp, tp, agno);
1020 if (error)
1021 goto out_error;
1022 }
1023
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001024 /*
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001025 * Do a first racy fast path check if this AG is usable.
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001026 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001027 if (!pag->pagi_freecount && !okalloc)
1028 goto nextag;
1029
Christoph Hellwigc4982112012-08-07 02:02:02 -04001030 /*
1031 * Then read in the AGI buffer and recheck with the AGI buffer
1032 * lock held.
1033 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001034 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1035 if (error)
1036 goto out_error;
1037
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001038 if (pag->pagi_freecount) {
1039 xfs_perag_put(pag);
1040 goto out_alloc;
1041 }
1042
Christoph Hellwigc4982112012-08-07 02:02:02 -04001043 if (!okalloc)
1044 goto nextag_relse_buffer;
1045
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001046
1047 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1048 if (error) {
1049 xfs_trans_brelse(tp, agbp);
1050
1051 if (error != ENOSPC)
1052 goto out_error;
1053
1054 xfs_perag_put(pag);
1055 *inop = NULLFSINO;
1056 return 0;
1057 }
1058
1059 if (ialloced) {
1060 /*
1061 * We successfully allocated some inodes, return
1062 * the current context to the caller so that it
1063 * can commit the current transaction and call
1064 * us again where we left off.
1065 */
1066 ASSERT(pag->pagi_freecount > 0);
1067 xfs_perag_put(pag);
1068
1069 *IO_agbp = agbp;
1070 *inop = NULLFSINO;
1071 return 0;
1072 }
1073
Christoph Hellwigc4982112012-08-07 02:02:02 -04001074nextag_relse_buffer:
1075 xfs_trans_brelse(tp, agbp);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001076nextag:
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001077 xfs_perag_put(pag);
1078 if (++agno == mp->m_sb.sb_agcount)
1079 agno = 0;
1080 if (agno == start_agno) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001081 *inop = NULLFSINO;
1082 return noroom ? ENOSPC : 0;
1083 }
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001084 }
1085
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001086out_alloc:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001087 *IO_agbp = NULL;
1088 return xfs_dialloc_ag(tp, agbp, parent, inop);
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001089out_error:
1090 xfs_perag_put(pag);
1091 return XFS_ERROR(error);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001092}
1093
1094/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 * Free disk inode. Carefully avoids touching the incore inode, all
1096 * manipulations incore are the caller's responsibility.
1097 * The on-disk inode is not changed by this operation, only the
1098 * btree (free inode mask) is changed.
1099 */
1100int
1101xfs_difree(
1102 xfs_trans_t *tp, /* transaction pointer */
1103 xfs_ino_t inode, /* inode to be freed */
1104 xfs_bmap_free_t *flist, /* extents to free */
1105 int *delete, /* set if inode cluster was deleted */
1106 xfs_ino_t *first_ino) /* first inode in deleted cluster */
1107{
1108 /* REFERENCED */
1109 xfs_agblock_t agbno; /* block number containing inode */
1110 xfs_buf_t *agbp; /* buffer containing allocation group header */
1111 xfs_agino_t agino; /* inode number relative to allocation group */
1112 xfs_agnumber_t agno; /* allocation group number */
1113 xfs_agi_t *agi; /* allocation group header */
1114 xfs_btree_cur_t *cur; /* inode btree cursor */
1115 int error; /* error return value */
1116 int i; /* result code */
1117 int ilen; /* inodes in an inode cluster */
1118 xfs_mount_t *mp; /* mount structure for filesystem */
1119 int off; /* offset of inode in inode chunk */
Christoph Hellwig61a25842006-09-28 10:57:04 +10001120 xfs_inobt_rec_incore_t rec; /* btree record */
Dave Chinner44b56e02010-01-11 11:47:43 +00001121 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 mp = tp->t_mountp;
1124
1125 /*
1126 * Break up inode number into its components.
1127 */
1128 agno = XFS_INO_TO_AGNO(mp, inode);
1129 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001130 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1131 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 ASSERT(0);
1133 return XFS_ERROR(EINVAL);
1134 }
1135 agino = XFS_INO_TO_AGINO(mp, inode);
1136 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001137 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1138 __func__, (unsigned long long)inode,
1139 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 ASSERT(0);
1141 return XFS_ERROR(EINVAL);
1142 }
1143 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1144 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001145 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1146 __func__, agbno, mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ASSERT(0);
1148 return XFS_ERROR(EINVAL);
1149 }
1150 /*
1151 * Get the allocation group header.
1152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001155 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1156 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return error;
1158 }
1159 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001160 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Christoph Hellwig16259e72005-11-02 15:11:25 +11001161 ASSERT(agbno < be32_to_cpu(agi->agi_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /*
1163 * Initialize the cursor.
1164 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001165 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Dave Chinner0b48db82009-08-31 20:57:09 -03001167 error = xfs_check_agi_freecount(cur, agi);
1168 if (error)
1169 goto error0;
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /*
1172 * Look for the entry describing this inode.
1173 */
Christoph Hellwig21875502009-08-31 20:58:21 -03001174 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001175 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1176 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 goto error0;
1178 }
1179 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001180 error = xfs_inobt_get_rec(cur, &rec, &i);
1181 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001182 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1183 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto error0;
1185 }
1186 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1187 /*
1188 * Get the offset in the inode chunk.
1189 */
1190 off = agino - rec.ir_startino;
1191 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001192 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /*
1194 * Mark the inode free & increment the count.
1195 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001196 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 rec.ir_freecount++;
1198
1199 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001200 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001202 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1204
1205 *delete = 1;
1206 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1207
1208 /*
1209 * Remove the inode cluster from the AGI B+Tree, adjust the
1210 * AGI and Superblock inode counts, and mark the disk space
1211 * to be freed when the transaction is committed.
1212 */
1213 ilen = XFS_IALLOC_INODES(mp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001214 be32_add_cpu(&agi->agi_count, -ilen);
1215 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001217 pag = xfs_perag_get(mp, agno);
1218 pag->pagi_freecount -= ilen - 1;
1219 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1221 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1222
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001223 if ((error = xfs_btree_delete(cur, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001224 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1225 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 goto error0;
1227 }
1228
1229 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1230 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1231 XFS_IALLOC_BLOCKS(mp), flist, mp);
1232 } else {
1233 *delete = 0;
1234
Christoph Hellwigafabc242009-08-31 20:57:03 -03001235 error = xfs_inobt_update(cur, &rec);
1236 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001237 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1238 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto error0;
1240 }
Christoph Hellwigafabc242009-08-31 20:57:03 -03001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
1243 * Change the inode free counts and log the ag/sb changes.
1244 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001245 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001247 pag = xfs_perag_get(mp, agno);
1248 pag->pagi_freecount++;
1249 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1251 }
1252
Dave Chinner0b48db82009-08-31 20:57:09 -03001253 error = xfs_check_agi_freecount(cur, agi);
1254 if (error)
1255 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1258 return 0;
1259
1260error0:
1261 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1262 return error;
1263}
1264
Dave Chinner7124fe02010-06-24 11:15:33 +10001265STATIC int
1266xfs_imap_lookup(
1267 struct xfs_mount *mp,
1268 struct xfs_trans *tp,
1269 xfs_agnumber_t agno,
1270 xfs_agino_t agino,
1271 xfs_agblock_t agbno,
1272 xfs_agblock_t *chunk_agbno,
1273 xfs_agblock_t *offset_agbno,
1274 int flags)
1275{
1276 struct xfs_inobt_rec_incore rec;
1277 struct xfs_btree_cur *cur;
1278 struct xfs_buf *agbp;
Dave Chinner7124fe02010-06-24 11:15:33 +10001279 int error;
1280 int i;
1281
1282 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1283 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +11001284 xfs_alert(mp,
1285 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1286 __func__, error, agno);
Dave Chinner7124fe02010-06-24 11:15:33 +10001287 return error;
1288 }
1289
1290 /*
Dave Chinner4536f2a2010-08-24 11:42:30 +10001291 * Lookup the inode record for the given agino. If the record cannot be
1292 * found, then it's an invalid inode number and we should abort. Once
1293 * we have a record, we need to ensure it contains the inode number
1294 * we are looking up.
Dave Chinner7124fe02010-06-24 11:15:33 +10001295 */
1296 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Dave Chinner4536f2a2010-08-24 11:42:30 +10001297 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
Dave Chinner7124fe02010-06-24 11:15:33 +10001298 if (!error) {
1299 if (i)
1300 error = xfs_inobt_get_rec(cur, &rec, &i);
1301 if (!error && i == 0)
1302 error = EINVAL;
1303 }
1304
1305 xfs_trans_brelse(tp, agbp);
1306 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1307 if (error)
1308 return error;
1309
Dave Chinner4536f2a2010-08-24 11:42:30 +10001310 /* check that the returned record contains the required inode */
1311 if (rec.ir_startino > agino ||
1312 rec.ir_startino + XFS_IALLOC_INODES(mp) <= agino)
1313 return EINVAL;
1314
Dave Chinner7124fe02010-06-24 11:15:33 +10001315 /* for untrusted inodes check it is allocated first */
Dave Chinner19207792010-06-24 11:15:47 +10001316 if ((flags & XFS_IGET_UNTRUSTED) &&
Dave Chinner7124fe02010-06-24 11:15:33 +10001317 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
1318 return EINVAL;
1319
1320 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1321 *offset_agbno = agbno - *chunk_agbno;
1322 return 0;
1323}
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001326 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001329xfs_imap(
1330 xfs_mount_t *mp, /* file system mount structure */
1331 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001333 struct xfs_imap *imap, /* location map structure */
1334 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 xfs_agino_t agino; /* inode number within alloc group */
1338 xfs_agnumber_t agno; /* allocation group number */
1339 int blks_per_cluster; /* num blocks per inode cluster */
1340 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int offset; /* index of inode in its buffer */
1344 int offset_agbno; /* blks from chunk start to inode */
1345
1346 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /*
1349 * Split up the inode number into its parts.
1350 */
1351 agno = XFS_INO_TO_AGNO(mp, ino);
1352 agino = XFS_INO_TO_AGINO(mp, ino);
1353 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1354 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1355 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1356#ifdef DEBUG
Dave Chinner19207792010-06-24 11:15:47 +10001357 /*
1358 * Don't output diagnostic information for untrusted inodes
1359 * as they can be invalid without implying corruption.
1360 */
1361 if (flags & XFS_IGET_UNTRUSTED)
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001362 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner53487782011-03-07 10:05:35 +11001364 xfs_alert(mp,
1365 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1366 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner53487782011-03-07 10:05:35 +11001369 xfs_alert(mp,
1370 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1371 __func__, (unsigned long long)agbno,
1372 (unsigned long)mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner53487782011-03-07 10:05:35 +11001375 xfs_alert(mp,
1376 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1377 __func__, ino,
1378 XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001380 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381#endif /* DEBUG */
1382 return XFS_ERROR(EINVAL);
1383 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001384
Dave Chinner7124fe02010-06-24 11:15:33 +10001385 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1386
1387 /*
1388 * For bulkstat and handle lookups, we have an untrusted inode number
1389 * that we have to verify is valid. We cannot do this just by reading
1390 * the inode buffer as it may have been unlinked and removed leaving
1391 * inodes in stale state on disk. Hence we have to do a btree lookup
1392 * in all cases where an untrusted inode number is passed.
1393 */
Dave Chinner19207792010-06-24 11:15:47 +10001394 if (flags & XFS_IGET_UNTRUSTED) {
Dave Chinner7124fe02010-06-24 11:15:33 +10001395 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1396 &chunk_agbno, &offset_agbno, flags);
1397 if (error)
1398 return error;
1399 goto out_map;
1400 }
1401
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001402 /*
1403 * If the inode cluster size is the same as the blocksize or
1404 * smaller we get to the buffer by simple arithmetics.
1405 */
1406 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 offset = XFS_INO_TO_OFFSET(mp, ino);
1408 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001409
1410 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1411 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1412 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return 0;
1414 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001415
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001416 /*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001417 * If the inode chunks are aligned then use simple maths to
1418 * find the location. Otherwise we have to do a btree
1419 * lookup to find the location.
1420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (mp->m_inoalign_mask) {
1422 offset_agbno = agbno & mp->m_inoalign_mask;
1423 chunk_agbno = agbno - offset_agbno;
1424 } else {
Dave Chinner7124fe02010-06-24 11:15:33 +10001425 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1426 &chunk_agbno, &offset_agbno, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (error)
1428 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001430
Dave Chinner7124fe02010-06-24 11:15:33 +10001431out_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 ASSERT(agbno >= chunk_agbno);
1433 cluster_agbno = chunk_agbno +
1434 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1435 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1436 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001437
1438 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1439 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1440 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1441
1442 /*
1443 * If the inode number maps to a block outside the bounds
1444 * of the file system then return NULL rather than calling
1445 * read_buf and panicing when we get an error from the
1446 * driver.
1447 */
1448 if ((imap->im_blkno + imap->im_len) >
1449 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
Dave Chinner53487782011-03-07 10:05:35 +11001450 xfs_alert(mp,
1451 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1452 __func__, (unsigned long long) imap->im_blkno,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001453 (unsigned long long) imap->im_len,
1454 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1455 return XFS_ERROR(EINVAL);
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460/*
1461 * Compute and fill in value of m_in_maxlevels.
1462 */
1463void
1464xfs_ialloc_compute_maxlevels(
1465 xfs_mount_t *mp) /* file system mount structure */
1466{
1467 int level;
1468 uint maxblocks;
1469 uint maxleafents;
1470 int minleafrecs;
1471 int minnoderecs;
1472
1473 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1474 XFS_INODES_PER_CHUNK_LOG;
1475 minleafrecs = mp->m_alloc_mnr[0];
1476 minnoderecs = mp->m_alloc_mnr[1];
1477 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1478 for (level = 1; maxblocks > 1; level++)
1479 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1480 mp->m_in_maxlevels = level;
1481}
1482
1483/*
1484 * Log specified fields for the ag hdr (inode section)
1485 */
1486void
1487xfs_ialloc_log_agi(
1488 xfs_trans_t *tp, /* transaction pointer */
1489 xfs_buf_t *bp, /* allocation group header buffer */
1490 int fields) /* bitmask of fields to log */
1491{
1492 int first; /* first byte number */
1493 int last; /* last byte number */
1494 static const short offsets[] = { /* field starting offsets */
1495 /* keep in sync with bit definitions */
1496 offsetof(xfs_agi_t, agi_magicnum),
1497 offsetof(xfs_agi_t, agi_versionnum),
1498 offsetof(xfs_agi_t, agi_seqno),
1499 offsetof(xfs_agi_t, agi_length),
1500 offsetof(xfs_agi_t, agi_count),
1501 offsetof(xfs_agi_t, agi_root),
1502 offsetof(xfs_agi_t, agi_level),
1503 offsetof(xfs_agi_t, agi_freecount),
1504 offsetof(xfs_agi_t, agi_newino),
1505 offsetof(xfs_agi_t, agi_dirino),
1506 offsetof(xfs_agi_t, agi_unlinked),
1507 sizeof(xfs_agi_t)
1508 };
1509#ifdef DEBUG
1510 xfs_agi_t *agi; /* allocation group header */
1511
1512 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001513 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514#endif
1515 /*
1516 * Compute byte offsets for the first and last fields.
1517 */
1518 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1519 /*
1520 * Log the allocation group inode header buffer.
1521 */
Dave Chinner61fe1352013-04-03 16:11:30 +11001522 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 xfs_trans_log_buf(tp, bp, first, last);
1524}
1525
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001526#ifdef DEBUG
1527STATIC void
1528xfs_check_agi_unlinked(
1529 struct xfs_agi *agi)
1530{
1531 int i;
1532
1533 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1534 ASSERT(agi->agi_unlinked[i]);
1535}
1536#else
1537#define xfs_check_agi_unlinked(agi)
1538#endif
1539
Dave Chinner983d09f2013-04-03 16:11:15 +11001540static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11001541xfs_agi_verify(
Dave Chinner3702ce62012-11-12 22:54:05 +11001542 struct xfs_buf *bp)
1543{
1544 struct xfs_mount *mp = bp->b_target->bt_mount;
1545 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
Dave Chinner3702ce62012-11-12 22:54:05 +11001546
Dave Chinner983d09f2013-04-03 16:11:15 +11001547 if (xfs_sb_version_hascrc(&mp->m_sb) &&
1548 !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
1549 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11001550 /*
1551 * Validate the magic number of the agi block.
1552 */
Dave Chinner983d09f2013-04-03 16:11:15 +11001553 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
1554 return false;
1555 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
1556 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11001557
1558 /*
1559 * during growfs operations, the perag is not fully initialised,
1560 * so we can't use it for any useful checking. growfs ensures we can't
1561 * use it by using uncached buffers that don't have the perag attached
1562 * so we can detect and avoid this problem.
1563 */
Dave Chinner983d09f2013-04-03 16:11:15 +11001564 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
1565 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11001566
Dave Chinner3702ce62012-11-12 22:54:05 +11001567 xfs_check_agi_unlinked(agi);
Dave Chinner983d09f2013-04-03 16:11:15 +11001568 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +11001569}
1570
Dave Chinner1813dd62012-11-14 17:54:40 +11001571static void
1572xfs_agi_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11001573 struct xfs_buf *bp)
1574{
Dave Chinner983d09f2013-04-03 16:11:15 +11001575 struct xfs_mount *mp = bp->b_target->bt_mount;
1576 int agi_ok = 1;
1577
1578 if (xfs_sb_version_hascrc(&mp->m_sb))
1579 agi_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
1580 offsetof(struct xfs_agi, agi_crc));
1581 agi_ok = agi_ok && xfs_agi_verify(bp);
1582
1583 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1584 XFS_RANDOM_IALLOC_READ_AGI))) {
1585 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1586 xfs_buf_ioerror(bp, EFSCORRUPTED);
1587 }
Dave Chinner612cfbf2012-11-14 17:52:32 +11001588}
1589
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001590static void
Dave Chinner1813dd62012-11-14 17:54:40 +11001591xfs_agi_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11001592 struct xfs_buf *bp)
1593{
Dave Chinner983d09f2013-04-03 16:11:15 +11001594 struct xfs_mount *mp = bp->b_target->bt_mount;
1595 struct xfs_buf_log_item *bip = bp->b_fspriv;
1596
1597 if (!xfs_agi_verify(bp)) {
1598 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1599 xfs_buf_ioerror(bp, EFSCORRUPTED);
1600 return;
1601 }
1602
1603 if (!xfs_sb_version_hascrc(&mp->m_sb))
1604 return;
1605
1606 if (bip)
1607 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
1608 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
1609 offsetof(struct xfs_agi, agi_crc));
Dave Chinner3702ce62012-11-12 22:54:05 +11001610}
1611
Dave Chinner1813dd62012-11-14 17:54:40 +11001612const struct xfs_buf_ops xfs_agi_buf_ops = {
1613 .verify_read = xfs_agi_read_verify,
1614 .verify_write = xfs_agi_write_verify,
1615};
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617/*
1618 * Read in the allocation group header (inode allocation section)
1619 */
1620int
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001621xfs_read_agi(
1622 struct xfs_mount *mp, /* file system mount structure */
1623 struct xfs_trans *tp, /* transaction pointer */
1624 xfs_agnumber_t agno, /* allocation group number */
1625 struct xfs_buf **bpp) /* allocation group hdr buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001627 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 ASSERT(agno != NULLAGNUMBER);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001630
1631 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11001633 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (error)
1635 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001636
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001637 ASSERT(!xfs_buf_geterror(*bpp));
Christoph Hellwig38f23232011-10-10 16:52:45 +00001638 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001639 return 0;
1640}
1641
1642int
1643xfs_ialloc_read_agi(
1644 struct xfs_mount *mp, /* file system mount structure */
1645 struct xfs_trans *tp, /* transaction pointer */
1646 xfs_agnumber_t agno, /* allocation group number */
1647 struct xfs_buf **bpp) /* allocation group hdr buf */
1648{
1649 struct xfs_agi *agi; /* allocation group header */
1650 struct xfs_perag *pag; /* per allocation group data */
1651 int error;
1652
1653 error = xfs_read_agi(mp, tp, agno, bpp);
1654 if (error)
1655 return error;
1656
1657 agi = XFS_BUF_TO_AGI(*bpp);
Dave Chinner44b56e02010-01-11 11:47:43 +00001658 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11001660 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10001661 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001665 /*
1666 * It's possible for these to be out of sync if
1667 * we are in the middle of a forced shutdown.
1668 */
1669 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1670 XFS_FORCED_SHUTDOWN(mp));
Dave Chinner44b56e02010-01-11 11:47:43 +00001671 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return 0;
1673}
David Chinner92821e22007-05-24 15:26:31 +10001674
1675/*
1676 * Read in the agi to initialise the per-ag data in the mount structure
1677 */
1678int
1679xfs_ialloc_pagi_init(
1680 xfs_mount_t *mp, /* file system mount structure */
1681 xfs_trans_t *tp, /* transaction pointer */
1682 xfs_agnumber_t agno) /* allocation group number */
1683{
1684 xfs_buf_t *bp = NULL;
1685 int error;
1686
1687 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1688 if (error)
1689 return error;
1690 if (bp)
1691 xfs_trans_brelse(tp, bp);
1692 return 0;
1693}