blob: c973ff8c4a7c6a6c3890600449e4b19b52ae1785 [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 Chinner70a9883c2013-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"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
33#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_btree.h"
35#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_rtalloc.h"
38#include "xfs_error.h"
39#include "xfs_bmap.h"
Dave Chinner983d09f2013-04-03 16:11:15 +110040#include "xfs_cksum.h"
Dave Chinner239880e2013-10-23 10:50:10 +110041#include "xfs_trans.h"
Dave Chinner983d09f2013-04-03 16:11:15 +110042#include "xfs_buf_item.h"
Dave Chinnerddf6ad02013-06-27 16:04:56 +100043#include "xfs_icreate_item.h"
Dave Chinner7bb85ef2013-08-12 20:49:34 +100044#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * Allocation group level functions.
49 */
David Chinner75de2a92008-03-27 18:00:38 +110050static inline int
51xfs_ialloc_cluster_alignment(
52 xfs_alloc_arg_t *args)
53{
54 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
55 args->mp->m_sb.sb_inoalignmt >=
56 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
57 return args->mp->m_sb.sb_inoalignmt;
58 return 1;
59}
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
Christoph Hellwig21875502009-08-31 20:58:21 -030062 * Lookup a record by ino in the btree given by cur.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110063 */
Christoph Hellwig81e25172009-09-01 19:56:55 -040064int /* error */
Christoph Hellwig21875502009-08-31 20:58:21 -030065xfs_inobt_lookup(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110066 struct xfs_btree_cur *cur, /* btree cursor */
67 xfs_agino_t ino, /* starting inode of chunk */
Christoph Hellwig21875502009-08-31 20:58:21 -030068 xfs_lookup_t dir, /* <=, >=, == */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110069 int *stat) /* success/failure */
70{
71 cur->bc_rec.i.ir_startino = ino;
Christoph Hellwig21875502009-08-31 20:58:21 -030072 cur->bc_rec.i.ir_freecount = 0;
73 cur->bc_rec.i.ir_free = 0;
74 return xfs_btree_lookup(cur, dir, stat);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110075}
76
77/*
Christoph Hellwigafabc242009-08-31 20:57:03 -030078 * Update the record referred to by cur to the value given.
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110079 * This either works (return 0) or gets an EFSCORRUPTED error.
80 */
81STATIC int /* error */
82xfs_inobt_update(
83 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigafabc242009-08-31 20:57:03 -030084 xfs_inobt_rec_incore_t *irec) /* btree record */
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110085{
86 union xfs_btree_rec rec;
87
Christoph Hellwigafabc242009-08-31 20:57:03 -030088 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
89 rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
90 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110091 return xfs_btree_update(cur, &rec);
92}
93
94/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +110095 * Get the data from the pointed-to record.
96 */
97int /* error */
98xfs_inobt_get_rec(
99 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300100 xfs_inobt_rec_incore_t *irec, /* btree record */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100101 int *stat) /* output: success/failure */
102{
103 union xfs_btree_rec *rec;
104 int error;
105
106 error = xfs_btree_get_rec(cur, &rec, stat);
107 if (!error && *stat == 1) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300108 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
109 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
110 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100111 }
112 return error;
113}
114
115/*
Dave Chinner0b48db82009-08-31 20:57:09 -0300116 * Verify that the number of free inodes in the AGI is correct.
117 */
118#ifdef DEBUG
119STATIC int
120xfs_check_agi_freecount(
121 struct xfs_btree_cur *cur,
122 struct xfs_agi *agi)
123{
124 if (cur->bc_nlevels == 1) {
125 xfs_inobt_rec_incore_t rec;
126 int freecount = 0;
127 int error;
128 int i;
129
Christoph Hellwig21875502009-08-31 20:58:21 -0300130 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
Dave Chinner0b48db82009-08-31 20:57:09 -0300131 if (error)
132 return error;
133
134 do {
135 error = xfs_inobt_get_rec(cur, &rec, &i);
136 if (error)
137 return error;
138
139 if (i) {
140 freecount += rec.ir_freecount;
141 error = xfs_btree_increment(cur, 0, &i);
142 if (error)
143 return error;
144 }
145 } while (i == 1);
146
147 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
148 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
149 }
150 return 0;
151}
152#else
153#define xfs_check_agi_freecount(cur, agi) 0
154#endif
155
156/*
Dave Chinner28c8e412013-06-27 16:04:55 +1000157 * Initialise a new set of inodes. When called without a transaction context
158 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
159 * than logging them (which in a transaction context puts them into the AIL
160 * for writeback rather than the xfsbufd queue).
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300161 */
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000162int
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300163xfs_ialloc_inode_init(
164 struct xfs_mount *mp,
165 struct xfs_trans *tp,
Dave Chinner28c8e412013-06-27 16:04:55 +1000166 struct list_head *buffer_list,
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300167 xfs_agnumber_t agno,
168 xfs_agblock_t agbno,
169 xfs_agblock_t length,
170 unsigned int gen)
171{
172 struct xfs_buf *fbuf;
173 struct xfs_dinode *free;
174 int blks_per_cluster, nbufs, ninodes;
175 int version;
176 int i, j;
177 xfs_daddr_t d;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100178 xfs_ino_t ino = 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300179
180 /*
181 * Loop over the new block(s), filling in the inodes.
182 * For small block sizes, manipulate the inodes in buffers
183 * which are multiples of the blocks size.
184 */
185 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
186 blks_per_cluster = 1;
187 nbufs = length;
188 ninodes = mp->m_sb.sb_inopblock;
189 } else {
190 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
191 mp->m_sb.sb_blocksize;
192 nbufs = length / blks_per_cluster;
193 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
194 }
195
196 /*
Christoph Hellwig93848a92013-04-03 16:11:17 +1100197 * Figure out what version number to use in the inodes we create. If
198 * the superblock version has caught up to the one that supports the new
199 * inode format, then use the new inode version. Otherwise use the old
200 * version so that old kernels will continue to be able to use the file
201 * system.
202 *
203 * For v3 inodes, we also need to write the inode number into the inode,
204 * so calculate the first inode number of the chunk here as
205 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
206 * across multiple filesystem blocks (such as a cluster) and so cannot
207 * be used in the cluster buffer loop below.
208 *
209 * Further, because we are writing the inode directly into the buffer
210 * and calculating a CRC on the entire inode, we have ot log the entire
211 * inode so that the entire range the CRC covers is present in the log.
212 * That means for v3 inode we log the entire buffer rather than just the
213 * inode cores.
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300214 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100215 if (xfs_sb_version_hascrc(&mp->m_sb)) {
216 version = 3;
217 ino = XFS_AGINO_TO_INO(mp, agno,
218 XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000219
220 /*
221 * log the initialisation that is about to take place as an
222 * logical operation. This means the transaction does not
223 * need to log the physical changes to the inode buffers as log
224 * recovery will know what initialisation is actually needed.
225 * Hence we only need to log the buffers as "ordered" buffers so
226 * they track in the AIL as if they were physically logged.
227 */
228 if (tp)
229 xfs_icreate_log(tp, agno, agbno, XFS_IALLOC_INODES(mp),
230 mp->m_sb.sb_inodesize, length, gen);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100231 } else if (xfs_sb_version_hasnlink(&mp->m_sb))
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300232 version = 2;
233 else
234 version = 1;
235
236 for (j = 0; j < nbufs; j++) {
237 /*
238 * Get the block.
239 */
240 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
241 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
Dave Chinner7c4cebe2012-11-23 14:24:23 +1100242 mp->m_bsize * blks_per_cluster,
243 XBF_UNMAPPED);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000244 if (!fbuf)
245 return ENOMEM;
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000246
247 /* Initialize the inode buffers and log them appropriately. */
Dave Chinner1813dd62012-11-14 17:54:40 +1100248 fbuf->b_ops = &xfs_inode_buf_ops;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100249 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300250 for (i = 0; i < ninodes; i++) {
251 int ioffset = i << mp->m_sb.sb_inodelog;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100252 uint isize = xfs_dinode_size(version);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300253
254 free = xfs_make_iptr(mp, fbuf, i);
255 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
256 free->di_version = version;
257 free->di_gen = cpu_to_be32(gen);
258 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100259
260 if (version == 3) {
261 free->di_ino = cpu_to_be64(ino);
262 ino++;
263 uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
264 xfs_dinode_calc_crc(mp, free);
Dave Chinner28c8e412013-06-27 16:04:55 +1000265 } else if (tp) {
Christoph Hellwig93848a92013-04-03 16:11:17 +1100266 /* just log the inode core */
267 xfs_trans_log_buf(tp, fbuf, ioffset,
268 ioffset + isize - 1);
269 }
270 }
Dave Chinner28c8e412013-06-27 16:04:55 +1000271
272 if (tp) {
273 /*
274 * Mark the buffer as an inode allocation buffer so it
275 * sticks in AIL at the point of this allocation
276 * transaction. This ensures the they are on disk before
277 * the tail of the log can be moved past this
278 * transaction (i.e. by preventing relogging from moving
279 * it forward in the log).
280 */
281 xfs_trans_inode_alloc_buf(tp, fbuf);
282 if (version == 3) {
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000283 /*
284 * Mark the buffer as ordered so that they are
285 * not physically logged in the transaction but
286 * still tracked in the AIL as part of the
287 * transaction and pin the log appropriately.
288 */
289 xfs_trans_ordered_buf(tp, fbuf);
Dave Chinner28c8e412013-06-27 16:04:55 +1000290 xfs_trans_log_buf(tp, fbuf, 0,
291 BBTOB(fbuf->b_length) - 1);
292 }
293 } else {
294 fbuf->b_flags |= XBF_DONE;
295 xfs_buf_delwri_queue(fbuf, buffer_list);
296 xfs_buf_relse(fbuf);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300297 }
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300298 }
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000299 return 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300300}
301
302/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 * Allocate new inodes in the allocation group specified by agbp.
304 * Return 0 for success, else error code.
305 */
306STATIC int /* error code or 0 */
307xfs_ialloc_ag_alloc(
308 xfs_trans_t *tp, /* transaction pointer */
309 xfs_buf_t *agbp, /* alloc group buffer */
310 int *alloc)
311{
312 xfs_agi_t *agi; /* allocation group header */
313 xfs_alloc_arg_t args; /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 xfs_btree_cur_t *cur; /* inode btree cursor */
David Chinner92821e22007-05-24 15:26:31 +1000315 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300317 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 xfs_agino_t newino; /* new first inode's number */
319 xfs_agino_t newlen; /* new number of inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 xfs_agino_t thisino; /* current inode number, for loop */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000321 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* boundary */
Dave Chinner44b56e02010-01-11 11:47:43 +0000323 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Mark Tinguelya0041682012-09-20 13:16:45 -0500325 memset(&args, 0, sizeof(args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 args.tp = tp;
327 args.mp = tp->t_mountp;
328
329 /*
330 * Locking will ensure that we don't have two callers in here
331 * at one time.
332 */
333 newlen = XFS_IALLOC_INODES(args.mp);
334 if (args.mp->m_maxicount &&
335 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
336 return XFS_ERROR(ENOSPC);
337 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
338 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000339 * First try to allocate inodes contiguous with the last-allocated
340 * chunk of inodes. If the filesystem is striped, this will fill
341 * an entire stripe unit with inodes.
Dave Chinner28c8e412013-06-27 16:04:55 +1000342 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000344 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300345 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000346 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
347 XFS_IALLOC_BLOCKS(args.mp);
348 if (likely(newino != NULLAGINO &&
349 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300350 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000351 args.type = XFS_ALLOCTYPE_THIS_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000352 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100353
Glen Overby3ccb8b52006-03-29 09:52:28 +1000354 /*
David Chinner75de2a92008-03-27 18:00:38 +1100355 * We need to take into account alignment here to ensure that
356 * we don't modify the free list if we fail to have an exact
357 * block. If we don't have an exact match, and every oher
358 * attempt allocation attempt fails, we'll end up cancelling
359 * a dirty transaction and shutting down.
360 *
361 * For an exact allocation, alignment must be 1,
362 * however we need to take cluster alignment into account when
363 * fixing up the freelist. Use the minalignslop field to
364 * indicate that extra blocks might be required for alignment,
365 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000366 */
David Chinner75de2a92008-03-27 18:00:38 +1100367 args.alignment = 1;
368 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
369
370 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100371 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000372 if ((error = xfs_alloc_vextent(&args)))
373 return error;
374 } else
375 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Glen Overby3ccb8b52006-03-29 09:52:28 +1000377 if (unlikely(args.fsbno == NULLFSBLOCK)) {
378 /*
379 * Set the alignment for the allocation.
380 * If stripe alignment is turned on then align at stripe unit
381 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000382 * If the cluster size is smaller than a filesystem block
383 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000384 * pieces, so don't need alignment anyway.
385 */
386 isaligned = 0;
387 if (args.mp->m_sinoalign) {
388 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
389 args.alignment = args.mp->m_dalign;
390 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100391 } else
392 args.alignment = xfs_ialloc_cluster_alignment(&args);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000393 /*
394 * Need to figure out where to allocate the inode blocks.
395 * Ideally they should be spaced out through the a.g.
396 * For now, just allocate blocks up front.
397 */
398 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300399 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000400 /*
401 * Allocate a fixed-size extent of inodes.
402 */
403 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000404 args.prod = 1;
405 /*
406 * Allow space for the inode btree to split.
407 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100408 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000409 if ((error = xfs_alloc_vextent(&args)))
410 return error;
411 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /*
414 * If stripe alignment is turned on, then try again with cluster
415 * alignment.
416 */
417 if (isaligned && args.fsbno == NULLFSBLOCK) {
418 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100419 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300420 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
David Chinner75de2a92008-03-27 18:00:38 +1100421 args.alignment = xfs_ialloc_cluster_alignment(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if ((error = xfs_alloc_vextent(&args)))
423 return error;
424 }
425
426 if (args.fsbno == NULLFSBLOCK) {
427 *alloc = 0;
428 return 0;
429 }
430 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
David Chinner359346a2008-04-29 12:53:32 +1000432 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300433 * Stamp and write the inode buffers.
434 *
David Chinner359346a2008-04-29 12:53:32 +1000435 * Seed the new inode cluster with a random generation number. This
436 * prevents short-term reuse of generation numbers if a chunk is
437 * freed and then immediately reallocated. We use random numbers
438 * rather than a linear progression to prevent the next generation
439 * number from being easily guessable.
440 */
Dave Chinner28c8e412013-06-27 16:04:55 +1000441 error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
Akinobu Mitaecb34032013-03-04 21:58:20 +0900442 args.len, prandom_u32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100443
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000444 if (error)
445 return error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300446 /*
447 * Convert the results.
448 */
449 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800450 be32_add_cpu(&agi->agi_count, newlen);
451 be32_add_cpu(&agi->agi_freecount, newlen);
Dave Chinner44b56e02010-01-11 11:47:43 +0000452 pag = xfs_perag_get(args.mp, agno);
453 pag->pagi_freecount += newlen;
454 xfs_perag_put(pag);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100455 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /*
458 * Insert records describing the new inode chunk into the btree.
459 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100460 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 for (thisino = newino;
462 thisino < newino + newlen;
463 thisino += XFS_INODES_PER_CHUNK) {
Christoph Hellwig21875502009-08-31 20:58:21 -0300464 cur->bc_rec.i.ir_startino = thisino;
465 cur->bc_rec.i.ir_freecount = XFS_INODES_PER_CHUNK;
466 cur->bc_rec.i.ir_free = XFS_INOBT_ALL_FREE;
467 error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, &i);
468 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
470 return error;
471 }
472 ASSERT(i == 0);
Christoph Hellwig21875502009-08-31 20:58:21 -0300473 error = xfs_btree_insert(cur, &i);
474 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
476 return error;
477 }
478 ASSERT(i == 1);
479 }
480 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
481 /*
482 * Log allocation group header fields
483 */
484 xfs_ialloc_log_agi(tp, agbp,
485 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
486 /*
487 * Modify/log superblock values for inode count and inode free count.
488 */
489 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
490 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
491 *alloc = 1;
492 return 0;
493}
494
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000495STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496xfs_ialloc_next_ag(
497 xfs_mount_t *mp)
498{
499 xfs_agnumber_t agno;
500
501 spin_lock(&mp->m_agirotor_lock);
502 agno = mp->m_agirotor;
Carlos Maiolino8aea3ff2012-09-20 10:32:36 -0300503 if (++mp->m_agirotor >= mp->m_maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 mp->m_agirotor = 0;
505 spin_unlock(&mp->m_agirotor_lock);
506
507 return agno;
508}
509
510/*
511 * Select an allocation group to look for a free inode in, based on the parent
Zhi Yong Wu2f21ff12013-08-12 03:14:58 +0000512 * inode and the mode. Return the allocation group buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400514STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515xfs_ialloc_ag_select(
516 xfs_trans_t *tp, /* transaction pointer */
517 xfs_ino_t parent, /* parent directory inode number */
Al Viro576b1d62011-07-26 02:50:15 -0400518 umode_t mode, /* bits set to indicate file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int okalloc) /* ok to allocate more space */
520{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
522 xfs_agnumber_t agno; /* current ag number */
523 int flags; /* alloc buffer locking flags */
524 xfs_extlen_t ineed; /* blocks needed for inode allocation */
525 xfs_extlen_t longest = 0; /* longest extent available */
526 xfs_mount_t *mp; /* mount point structure */
527 int needspace; /* file mode implies space allocated */
528 xfs_perag_t *pag; /* per allocation group data */
529 xfs_agnumber_t pagno; /* parent (starting) ag number */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400530 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /*
533 * Files of these types need at least one block if length > 0
534 * (and they won't fit in the inode, but that's hard to figure out).
535 */
536 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
537 mp = tp->t_mountp;
538 agcount = mp->m_maxagi;
539 if (S_ISDIR(mode))
540 pagno = xfs_ialloc_next_ag(mp);
541 else {
542 pagno = XFS_INO_TO_AGNO(mp, parent);
543 if (pagno >= agcount)
544 pagno = 0;
545 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ASSERT(pagno < agcount);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /*
550 * Loop through allocation groups, looking for one with a little
551 * free space in it. Note we don't look for free inodes, exactly.
552 * Instead, we include whether there is a need to allocate inodes
553 * to mean that blocks must be allocated for them,
554 * if none are currently free.
555 */
556 agno = pagno;
557 flags = XFS_ALLOC_FLAG_TRYLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 for (;;) {
Dave Chinner44b56e02010-01-11 11:47:43 +0000559 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (!pag->pagi_inodeok) {
561 xfs_ialloc_next_ag(mp);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400562 goto nextag;
563 }
564
565 if (!pag->pagi_init) {
566 error = xfs_ialloc_pagi_init(mp, tp, agno);
567 if (error)
568 goto nextag;
569 }
570
571 if (pag->pagi_freecount) {
572 xfs_perag_put(pag);
573 return agno;
574 }
575
576 if (!okalloc)
577 goto nextag;
578
579 if (!pag->pagf_init) {
580 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
581 if (error)
582 goto nextag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585 /*
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400586 * Is there enough free space for the file plus a block of
587 * inodes? (if we need to allocate some)?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400589 ineed = XFS_IALLOC_BLOCKS(mp);
590 longest = pag->pagf_longest;
591 if (!longest)
592 longest = pag->pagf_flcount > 0;
593
594 if (pag->pagf_freeblks >= needspace + ineed &&
595 longest >= ineed) {
596 xfs_perag_put(pag);
597 return agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599nextag:
Dave Chinner44b56e02010-01-11 11:47:43 +0000600 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /*
602 * No point in iterating over the rest, if we're shutting
603 * down.
604 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000605 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400606 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 agno++;
608 if (agno >= agcount)
609 agno = 0;
610 if (agno == pagno) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000611 if (flags == 0)
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400612 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 flags = 0;
614 }
615 }
616}
617
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300618/*
619 * Try to retrieve the next record to the left/right from the current one.
620 */
621STATIC int
622xfs_ialloc_next_rec(
623 struct xfs_btree_cur *cur,
624 xfs_inobt_rec_incore_t *rec,
625 int *done,
626 int left)
627{
628 int error;
629 int i;
630
631 if (left)
632 error = xfs_btree_decrement(cur, 0, &i);
633 else
634 error = xfs_btree_increment(cur, 0, &i);
635
636 if (error)
637 return error;
638 *done = !i;
639 if (i) {
640 error = xfs_inobt_get_rec(cur, rec, &i);
641 if (error)
642 return error;
643 XFS_WANT_CORRUPTED_RETURN(i == 1);
644 }
645
646 return 0;
647}
648
Dave Chinnerbd169562009-08-31 20:58:28 -0300649STATIC int
650xfs_ialloc_get_rec(
651 struct xfs_btree_cur *cur,
652 xfs_agino_t agino,
653 xfs_inobt_rec_incore_t *rec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800654 int *done)
Dave Chinnerbd169562009-08-31 20:58:28 -0300655{
656 int error;
657 int i;
658
659 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
660 if (error)
661 return error;
662 *done = !i;
663 if (i) {
664 error = xfs_inobt_get_rec(cur, rec, &i);
665 if (error)
666 return error;
667 XFS_WANT_CORRUPTED_RETURN(i == 1);
668 }
669
670 return 0;
671}
Dave Chinner0b48db82009-08-31 20:57:09 -0300672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400674 * Allocate an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400676 * The caller selected an AG for us, and made sure that free inodes are
677 * available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400679STATIC int
680xfs_dialloc_ag(
681 struct xfs_trans *tp,
682 struct xfs_buf *agbp,
683 xfs_ino_t parent,
684 xfs_ino_t *inop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400686 struct xfs_mount *mp = tp->t_mountp;
687 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
688 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
689 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
690 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
691 struct xfs_perag *pag;
692 struct xfs_btree_cur *cur, *tcur;
693 struct xfs_inobt_rec_incore rec, trec;
694 xfs_ino_t ino;
695 int error;
696 int offset;
697 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Dave Chinner44b56e02010-01-11 11:47:43 +0000699 pag = xfs_perag_get(mp, agno);
Dave Chinnerbd169562009-08-31 20:58:28 -0300700
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400701 ASSERT(pag->pagi_init);
702 ASSERT(pag->pagi_inodeok);
703 ASSERT(pag->pagi_freecount > 0);
704
Dave Chinnerbd169562009-08-31 20:58:28 -0300705 restart_pagno:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400706 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
708 * If pagino is 0 (this is the root inode allocation) use newino.
709 * This must work because we've just allocated some.
710 */
711 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100712 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dave Chinner0b48db82009-08-31 20:57:09 -0300714 error = xfs_check_agi_freecount(cur, agi);
715 if (error)
716 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300719 * If in the same AG as the parent, try to get near the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
721 if (pagno == agno) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300722 int doneleft; /* done, to the left */
723 int doneright; /* done, to the right */
Dave Chinnerbd169562009-08-31 20:58:28 -0300724 int searchdistance = 10;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300725
Christoph Hellwig21875502009-08-31 20:58:21 -0300726 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300727 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300729 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
730
731 error = xfs_inobt_get_rec(cur, &rec, &j);
732 if (error)
733 goto error0;
Brian Fosterb1210992013-08-27 17:15:45 -0400734 XFS_WANT_CORRUPTED_GOTO(j == 1, error0);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300735
736 if (rec.ir_freecount > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /*
738 * Found a free inode in the same chunk
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300739 * as the parent, done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300741 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300743
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300746 * In the same AG as parent, but parent's chunk is full.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300749 /* duplicate the cursor, search left & right simultaneously */
750 error = xfs_btree_dup_cursor(cur, &tcur);
751 if (error)
752 goto error0;
753
Dave Chinnerbd169562009-08-31 20:58:28 -0300754 /*
755 * Skip to last blocks looked up if same parent inode.
756 */
757 if (pagino != NULLAGINO &&
758 pag->pagl_pagino == pagino &&
759 pag->pagl_leftrec != NULLAGINO &&
760 pag->pagl_rightrec != NULLAGINO) {
761 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800762 &trec, &doneleft);
Dave Chinnerbd169562009-08-31 20:58:28 -0300763 if (error)
764 goto error1;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300765
Dave Chinnerbd169562009-08-31 20:58:28 -0300766 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800767 &rec, &doneright);
Dave Chinnerbd169562009-08-31 20:58:28 -0300768 if (error)
769 goto error1;
770 } else {
771 /* search left with tcur, back up 1 record */
772 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
773 if (error)
774 goto error1;
775
776 /* search right with cur, go forward 1 record. */
777 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
778 if (error)
779 goto error1;
780 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300781
782 /*
783 * Loop until we find an inode chunk with a free inode.
784 */
785 while (!doneleft || !doneright) {
786 int useleft; /* using left inode chunk this time */
787
Dave Chinnerbd169562009-08-31 20:58:28 -0300788 if (!--searchdistance) {
789 /*
790 * Not in range - save last search
791 * location and allocate a new inode
792 */
Eric Sandeen3b826382009-10-30 09:27:07 +0100793 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300794 pag->pagl_leftrec = trec.ir_startino;
795 pag->pagl_rightrec = rec.ir_startino;
796 pag->pagl_pagino = pagino;
797 goto newino;
798 }
799
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300800 /* figure out the closer block if both are valid. */
801 if (!doneleft && !doneright) {
802 useleft = pagino -
803 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
804 rec.ir_startino - pagino;
805 } else {
806 useleft = !doneleft;
807 }
808
809 /* free inodes to the left? */
810 if (useleft && trec.ir_freecount) {
811 rec = trec;
812 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
813 cur = tcur;
Dave Chinnerbd169562009-08-31 20:58:28 -0300814
815 pag->pagl_leftrec = trec.ir_startino;
816 pag->pagl_rightrec = rec.ir_startino;
817 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300818 goto alloc_inode;
819 }
820
821 /* free inodes to the right? */
822 if (!useleft && rec.ir_freecount) {
823 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300824
825 pag->pagl_leftrec = trec.ir_startino;
826 pag->pagl_rightrec = rec.ir_startino;
827 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300828 goto alloc_inode;
829 }
830
831 /* get next record to check */
832 if (useleft) {
833 error = xfs_ialloc_next_rec(tcur, &trec,
834 &doneleft, 1);
835 } else {
836 error = xfs_ialloc_next_rec(cur, &rec,
837 &doneright, 0);
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300842
843 /*
844 * We've reached the end of the btree. because
845 * we are only searching a small chunk of the
846 * btree each search, there is obviously free
847 * inodes closer to the parent inode than we
848 * are now. restart the search again.
849 */
850 pag->pagl_pagino = NULLAGINO;
851 pag->pagl_leftrec = NULLAGINO;
852 pag->pagl_rightrec = NULLAGINO;
853 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
854 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
855 goto restart_pagno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300859 * In a different AG from the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * See if the most recently allocated block has any free.
861 */
Dave Chinnerbd169562009-08-31 20:58:28 -0300862newino:
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200863 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
Christoph Hellwig21875502009-08-31 20:58:21 -0300864 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
865 XFS_LOOKUP_EQ, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300866 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300868
869 if (i == 1) {
870 error = xfs_inobt_get_rec(cur, &rec, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (error)
872 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300873
874 if (j == 1 && rec.ir_freecount > 0) {
875 /*
876 * The last chunk allocated in the group
877 * still has a free inode.
878 */
879 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300882 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300883
Dave Chinnerbd169562009-08-31 20:58:28 -0300884 /*
885 * None left in the last group, search the whole AG
886 */
887 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
888 if (error)
889 goto error0;
890 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
891
892 for (;;) {
893 error = xfs_inobt_get_rec(cur, &rec, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300894 if (error)
895 goto error0;
896 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Dave Chinnerbd169562009-08-31 20:58:28 -0300897 if (rec.ir_freecount > 0)
898 break;
899 error = xfs_btree_increment(cur, 0, &i);
900 if (error)
901 goto error0;
902 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300904
905alloc_inode:
Christoph Hellwig824c3132012-07-04 10:54:45 -0400906 offset = xfs_lowbit64(rec.ir_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ASSERT(offset >= 0);
908 ASSERT(offset < XFS_INODES_PER_CHUNK);
909 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
910 XFS_INODES_PER_CHUNK) == 0);
911 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100912 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 rec.ir_freecount--;
Christoph Hellwigafabc242009-08-31 20:57:03 -0300914 error = xfs_inobt_update(cur, &rec);
915 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800917 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +0000919 pag->pagi_freecount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Dave Chinner0b48db82009-08-31 20:57:09 -0300921 error = xfs_check_agi_freecount(cur, agi);
922 if (error)
923 goto error0;
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
926 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
Dave Chinner44b56e02010-01-11 11:47:43 +0000927 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 *inop = ino;
929 return 0;
930error1:
931 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
932error0:
933 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
Dave Chinner44b56e02010-01-11 11:47:43 +0000934 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return error;
936}
937
938/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400939 * Allocate an inode on disk.
940 *
941 * Mode is used to tell whether the new inode will need space, and whether it
942 * is a directory.
943 *
944 * This function is designed to be called twice if it has to do an allocation
945 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
946 * If an inode is available without having to performn an allocation, an inode
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300947 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
948 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
949 * The caller should then commit the current transaction, allocate a
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400950 * new transaction, and call xfs_dialloc() again, passing in the previous value
951 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
952 * buffer is locked across the two calls, the second call is guaranteed to have
953 * a free inode available.
954 *
955 * Once we successfully pick an inode its number is returned and the on-disk
956 * data structures are updated. The inode itself is not read in, since doing so
957 * would break ordering constraints with xfs_reclaim.
958 */
959int
960xfs_dialloc(
961 struct xfs_trans *tp,
962 xfs_ino_t parent,
963 umode_t mode,
964 int okalloc,
965 struct xfs_buf **IO_agbp,
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400966 xfs_ino_t *inop)
967{
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400968 struct xfs_mount *mp = tp->t_mountp;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400969 struct xfs_buf *agbp;
970 xfs_agnumber_t agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400971 int error;
972 int ialloced;
973 int noroom = 0;
Christoph Hellwigbe60fe52012-07-04 10:54:50 -0400974 xfs_agnumber_t start_agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400975 struct xfs_perag *pag;
976
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400977 if (*IO_agbp) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400978 /*
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400979 * If the caller passes in a pointer to the AGI buffer,
980 * continue where we left off before. In this case, we
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400981 * know that the allocation group has free inodes.
982 */
983 agbp = *IO_agbp;
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400984 goto out_alloc;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400985 }
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400986
987 /*
988 * We do not have an agbp, so select an initial allocation
989 * group for inode allocation.
990 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -0400991 start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
992 if (start_agno == NULLAGNUMBER) {
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400993 *inop = NULLFSINO;
994 return 0;
995 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400996
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400997 /*
998 * If we have already hit the ceiling of inode blocks then clear
999 * okalloc so we scan all available agi structures for a free
1000 * inode.
1001 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001002 if (mp->m_maxicount &&
1003 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
1004 noroom = 1;
1005 okalloc = 0;
1006 }
1007
1008 /*
1009 * Loop until we find an allocation group that either has free inodes
1010 * or in which we can allocate some inodes. Iterate through the
1011 * allocation groups upward, wrapping at the end.
1012 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001013 agno = start_agno;
1014 for (;;) {
1015 pag = xfs_perag_get(mp, agno);
1016 if (!pag->pagi_inodeok) {
1017 xfs_ialloc_next_ag(mp);
1018 goto nextag;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001019 }
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001020
1021 if (!pag->pagi_init) {
1022 error = xfs_ialloc_pagi_init(mp, tp, agno);
1023 if (error)
1024 goto out_error;
1025 }
1026
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001027 /*
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001028 * Do a first racy fast path check if this AG is usable.
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001029 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001030 if (!pag->pagi_freecount && !okalloc)
1031 goto nextag;
1032
Christoph Hellwigc4982112012-08-07 02:02:02 -04001033 /*
1034 * Then read in the AGI buffer and recheck with the AGI buffer
1035 * lock held.
1036 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001037 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1038 if (error)
1039 goto out_error;
1040
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001041 if (pag->pagi_freecount) {
1042 xfs_perag_put(pag);
1043 goto out_alloc;
1044 }
1045
Christoph Hellwigc4982112012-08-07 02:02:02 -04001046 if (!okalloc)
1047 goto nextag_relse_buffer;
1048
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001049
1050 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1051 if (error) {
1052 xfs_trans_brelse(tp, agbp);
1053
1054 if (error != ENOSPC)
1055 goto out_error;
1056
1057 xfs_perag_put(pag);
1058 *inop = NULLFSINO;
1059 return 0;
1060 }
1061
1062 if (ialloced) {
1063 /*
1064 * We successfully allocated some inodes, return
1065 * the current context to the caller so that it
1066 * can commit the current transaction and call
1067 * us again where we left off.
1068 */
1069 ASSERT(pag->pagi_freecount > 0);
1070 xfs_perag_put(pag);
1071
1072 *IO_agbp = agbp;
1073 *inop = NULLFSINO;
1074 return 0;
1075 }
1076
Christoph Hellwigc4982112012-08-07 02:02:02 -04001077nextag_relse_buffer:
1078 xfs_trans_brelse(tp, agbp);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001079nextag:
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001080 xfs_perag_put(pag);
1081 if (++agno == mp->m_sb.sb_agcount)
1082 agno = 0;
1083 if (agno == start_agno) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001084 *inop = NULLFSINO;
1085 return noroom ? ENOSPC : 0;
1086 }
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001087 }
1088
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001089out_alloc:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001090 *IO_agbp = NULL;
1091 return xfs_dialloc_ag(tp, agbp, parent, inop);
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001092out_error:
1093 xfs_perag_put(pag);
1094 return XFS_ERROR(error);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001095}
1096
1097/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * Free disk inode. Carefully avoids touching the incore inode, all
1099 * manipulations incore are the caller's responsibility.
1100 * The on-disk inode is not changed by this operation, only the
1101 * btree (free inode mask) is changed.
1102 */
1103int
1104xfs_difree(
1105 xfs_trans_t *tp, /* transaction pointer */
1106 xfs_ino_t inode, /* inode to be freed */
1107 xfs_bmap_free_t *flist, /* extents to free */
1108 int *delete, /* set if inode cluster was deleted */
1109 xfs_ino_t *first_ino) /* first inode in deleted cluster */
1110{
1111 /* REFERENCED */
1112 xfs_agblock_t agbno; /* block number containing inode */
1113 xfs_buf_t *agbp; /* buffer containing allocation group header */
1114 xfs_agino_t agino; /* inode number relative to allocation group */
1115 xfs_agnumber_t agno; /* allocation group number */
1116 xfs_agi_t *agi; /* allocation group header */
1117 xfs_btree_cur_t *cur; /* inode btree cursor */
1118 int error; /* error return value */
1119 int i; /* result code */
1120 int ilen; /* inodes in an inode cluster */
1121 xfs_mount_t *mp; /* mount structure for filesystem */
1122 int off; /* offset of inode in inode chunk */
Christoph Hellwig61a25842006-09-28 10:57:04 +10001123 xfs_inobt_rec_incore_t rec; /* btree record */
Dave Chinner44b56e02010-01-11 11:47:43 +00001124 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 mp = tp->t_mountp;
1127
1128 /*
1129 * Break up inode number into its components.
1130 */
1131 agno = XFS_INO_TO_AGNO(mp, inode);
1132 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001133 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1134 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 ASSERT(0);
1136 return XFS_ERROR(EINVAL);
1137 }
1138 agino = XFS_INO_TO_AGINO(mp, inode);
1139 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001140 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1141 __func__, (unsigned long long)inode,
1142 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ASSERT(0);
1144 return XFS_ERROR(EINVAL);
1145 }
1146 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1147 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001148 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1149 __func__, agbno, mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 ASSERT(0);
1151 return XFS_ERROR(EINVAL);
1152 }
1153 /*
1154 * Get the allocation group header.
1155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001158 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1159 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return error;
1161 }
1162 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001163 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Christoph Hellwig16259e72005-11-02 15:11:25 +11001164 ASSERT(agbno < be32_to_cpu(agi->agi_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * Initialize the cursor.
1167 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001168 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Dave Chinner0b48db82009-08-31 20:57:09 -03001170 error = xfs_check_agi_freecount(cur, agi);
1171 if (error)
1172 goto error0;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /*
1175 * Look for the entry describing this inode.
1176 */
Christoph Hellwig21875502009-08-31 20:58:21 -03001177 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001178 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1179 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto error0;
1181 }
1182 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001183 error = xfs_inobt_get_rec(cur, &rec, &i);
1184 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001185 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1186 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto error0;
1188 }
1189 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1190 /*
1191 * Get the offset in the inode chunk.
1192 */
1193 off = agino - rec.ir_startino;
1194 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001195 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /*
1197 * Mark the inode free & increment the count.
1198 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001199 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 rec.ir_freecount++;
1201
1202 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001203 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001205 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1207
1208 *delete = 1;
1209 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1210
1211 /*
1212 * Remove the inode cluster from the AGI B+Tree, adjust the
1213 * AGI and Superblock inode counts, and mark the disk space
1214 * to be freed when the transaction is committed.
1215 */
1216 ilen = XFS_IALLOC_INODES(mp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001217 be32_add_cpu(&agi->agi_count, -ilen);
1218 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001220 pag = xfs_perag_get(mp, agno);
1221 pag->pagi_freecount -= ilen - 1;
1222 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1224 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1225
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001226 if ((error = xfs_btree_delete(cur, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001227 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1228 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto error0;
1230 }
1231
1232 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1233 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1234 XFS_IALLOC_BLOCKS(mp), flist, mp);
1235 } else {
1236 *delete = 0;
1237
Christoph Hellwigafabc242009-08-31 20:57:03 -03001238 error = xfs_inobt_update(cur, &rec);
1239 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001240 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1241 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 goto error0;
1243 }
Christoph Hellwigafabc242009-08-31 20:57:03 -03001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /*
1246 * Change the inode free counts and log the ag/sb changes.
1247 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001248 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001250 pag = xfs_perag_get(mp, agno);
1251 pag->pagi_freecount++;
1252 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1254 }
1255
Dave Chinner0b48db82009-08-31 20:57:09 -03001256 error = xfs_check_agi_freecount(cur, agi);
1257 if (error)
1258 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1261 return 0;
1262
1263error0:
1264 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1265 return error;
1266}
1267
Dave Chinner7124fe02010-06-24 11:15:33 +10001268STATIC int
1269xfs_imap_lookup(
1270 struct xfs_mount *mp,
1271 struct xfs_trans *tp,
1272 xfs_agnumber_t agno,
1273 xfs_agino_t agino,
1274 xfs_agblock_t agbno,
1275 xfs_agblock_t *chunk_agbno,
1276 xfs_agblock_t *offset_agbno,
1277 int flags)
1278{
1279 struct xfs_inobt_rec_incore rec;
1280 struct xfs_btree_cur *cur;
1281 struct xfs_buf *agbp;
Dave Chinner7124fe02010-06-24 11:15:33 +10001282 int error;
1283 int i;
1284
1285 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1286 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +11001287 xfs_alert(mp,
1288 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1289 __func__, error, agno);
Dave Chinner7124fe02010-06-24 11:15:33 +10001290 return error;
1291 }
1292
1293 /*
Dave Chinner4536f2a2010-08-24 11:42:30 +10001294 * Lookup the inode record for the given agino. If the record cannot be
1295 * found, then it's an invalid inode number and we should abort. Once
1296 * we have a record, we need to ensure it contains the inode number
1297 * we are looking up.
Dave Chinner7124fe02010-06-24 11:15:33 +10001298 */
1299 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Dave Chinner4536f2a2010-08-24 11:42:30 +10001300 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
Dave Chinner7124fe02010-06-24 11:15:33 +10001301 if (!error) {
1302 if (i)
1303 error = xfs_inobt_get_rec(cur, &rec, &i);
1304 if (!error && i == 0)
1305 error = EINVAL;
1306 }
1307
1308 xfs_trans_brelse(tp, agbp);
1309 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1310 if (error)
1311 return error;
1312
Dave Chinner4536f2a2010-08-24 11:42:30 +10001313 /* check that the returned record contains the required inode */
1314 if (rec.ir_startino > agino ||
1315 rec.ir_startino + XFS_IALLOC_INODES(mp) <= agino)
1316 return EINVAL;
1317
Dave Chinner7124fe02010-06-24 11:15:33 +10001318 /* for untrusted inodes check it is allocated first */
Dave Chinner19207792010-06-24 11:15:47 +10001319 if ((flags & XFS_IGET_UNTRUSTED) &&
Dave Chinner7124fe02010-06-24 11:15:33 +10001320 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
1321 return EINVAL;
1322
1323 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1324 *offset_agbno = agbno - *chunk_agbno;
1325 return 0;
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001329 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001332xfs_imap(
1333 xfs_mount_t *mp, /* file system mount structure */
1334 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001336 struct xfs_imap *imap, /* location map structure */
1337 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 xfs_agino_t agino; /* inode number within alloc group */
1341 xfs_agnumber_t agno; /* allocation group number */
1342 int blks_per_cluster; /* num blocks per inode cluster */
1343 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int offset; /* index of inode in its buffer */
Dave Chinner836a94a2013-08-12 20:49:44 +10001347 xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * Split up the inode number into its parts.
1353 */
1354 agno = XFS_INO_TO_AGNO(mp, ino);
1355 agino = XFS_INO_TO_AGINO(mp, ino);
1356 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1357 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1358 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1359#ifdef DEBUG
Dave Chinner19207792010-06-24 11:15:47 +10001360 /*
1361 * Don't output diagnostic information for untrusted inodes
1362 * as they can be invalid without implying corruption.
1363 */
1364 if (flags & XFS_IGET_UNTRUSTED)
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001365 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner53487782011-03-07 10:05:35 +11001367 xfs_alert(mp,
1368 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1369 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner53487782011-03-07 10:05:35 +11001372 xfs_alert(mp,
1373 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1374 __func__, (unsigned long long)agbno,
1375 (unsigned long)mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner53487782011-03-07 10:05:35 +11001378 xfs_alert(mp,
1379 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1380 __func__, ino,
1381 XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001383 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#endif /* DEBUG */
1385 return XFS_ERROR(EINVAL);
1386 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001387
Dave Chinner7124fe02010-06-24 11:15:33 +10001388 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1389
1390 /*
1391 * For bulkstat and handle lookups, we have an untrusted inode number
1392 * that we have to verify is valid. We cannot do this just by reading
1393 * the inode buffer as it may have been unlinked and removed leaving
1394 * inodes in stale state on disk. Hence we have to do a btree lookup
1395 * in all cases where an untrusted inode number is passed.
1396 */
Dave Chinner19207792010-06-24 11:15:47 +10001397 if (flags & XFS_IGET_UNTRUSTED) {
Dave Chinner7124fe02010-06-24 11:15:33 +10001398 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1399 &chunk_agbno, &offset_agbno, flags);
1400 if (error)
1401 return error;
1402 goto out_map;
1403 }
1404
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001405 /*
1406 * If the inode cluster size is the same as the blocksize or
1407 * smaller we get to the buffer by simple arithmetics.
1408 */
1409 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 offset = XFS_INO_TO_OFFSET(mp, ino);
1411 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001412
1413 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1414 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1415 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return 0;
1417 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001418
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001419 /*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001420 * If the inode chunks are aligned then use simple maths to
1421 * find the location. Otherwise we have to do a btree
1422 * lookup to find the location.
1423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (mp->m_inoalign_mask) {
1425 offset_agbno = agbno & mp->m_inoalign_mask;
1426 chunk_agbno = agbno - offset_agbno;
1427 } else {
Dave Chinner7124fe02010-06-24 11:15:33 +10001428 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1429 &chunk_agbno, &offset_agbno, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (error)
1431 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001433
Dave Chinner7124fe02010-06-24 11:15:33 +10001434out_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 ASSERT(agbno >= chunk_agbno);
1436 cluster_agbno = chunk_agbno +
1437 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1438 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1439 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001440
1441 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1442 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1443 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1444
1445 /*
1446 * If the inode number maps to a block outside the bounds
1447 * of the file system then return NULL rather than calling
1448 * read_buf and panicing when we get an error from the
1449 * driver.
1450 */
1451 if ((imap->im_blkno + imap->im_len) >
1452 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
Dave Chinner53487782011-03-07 10:05:35 +11001453 xfs_alert(mp,
1454 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1455 __func__, (unsigned long long) imap->im_blkno,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001456 (unsigned long long) imap->im_len,
1457 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1458 return XFS_ERROR(EINVAL);
1459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
1463/*
1464 * Compute and fill in value of m_in_maxlevels.
1465 */
1466void
1467xfs_ialloc_compute_maxlevels(
1468 xfs_mount_t *mp) /* file system mount structure */
1469{
1470 int level;
1471 uint maxblocks;
1472 uint maxleafents;
1473 int minleafrecs;
1474 int minnoderecs;
1475
1476 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1477 XFS_INODES_PER_CHUNK_LOG;
1478 minleafrecs = mp->m_alloc_mnr[0];
1479 minnoderecs = mp->m_alloc_mnr[1];
1480 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1481 for (level = 1; maxblocks > 1; level++)
1482 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1483 mp->m_in_maxlevels = level;
1484}
1485
1486/*
1487 * Log specified fields for the ag hdr (inode section)
1488 */
1489void
1490xfs_ialloc_log_agi(
1491 xfs_trans_t *tp, /* transaction pointer */
1492 xfs_buf_t *bp, /* allocation group header buffer */
1493 int fields) /* bitmask of fields to log */
1494{
1495 int first; /* first byte number */
1496 int last; /* last byte number */
1497 static const short offsets[] = { /* field starting offsets */
1498 /* keep in sync with bit definitions */
1499 offsetof(xfs_agi_t, agi_magicnum),
1500 offsetof(xfs_agi_t, agi_versionnum),
1501 offsetof(xfs_agi_t, agi_seqno),
1502 offsetof(xfs_agi_t, agi_length),
1503 offsetof(xfs_agi_t, agi_count),
1504 offsetof(xfs_agi_t, agi_root),
1505 offsetof(xfs_agi_t, agi_level),
1506 offsetof(xfs_agi_t, agi_freecount),
1507 offsetof(xfs_agi_t, agi_newino),
1508 offsetof(xfs_agi_t, agi_dirino),
1509 offsetof(xfs_agi_t, agi_unlinked),
1510 sizeof(xfs_agi_t)
1511 };
1512#ifdef DEBUG
1513 xfs_agi_t *agi; /* allocation group header */
1514
1515 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001516 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517#endif
1518 /*
1519 * Compute byte offsets for the first and last fields.
1520 */
1521 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1522 /*
1523 * Log the allocation group inode header buffer.
1524 */
Dave Chinner61fe1352013-04-03 16:11:30 +11001525 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 xfs_trans_log_buf(tp, bp, first, last);
1527}
1528
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001529#ifdef DEBUG
1530STATIC void
1531xfs_check_agi_unlinked(
1532 struct xfs_agi *agi)
1533{
1534 int i;
1535
1536 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1537 ASSERT(agi->agi_unlinked[i]);
1538}
1539#else
1540#define xfs_check_agi_unlinked(agi)
1541#endif
1542
Dave Chinner983d09f2013-04-03 16:11:15 +11001543static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11001544xfs_agi_verify(
Dave Chinner3702ce62012-11-12 22:54:05 +11001545 struct xfs_buf *bp)
1546{
1547 struct xfs_mount *mp = bp->b_target->bt_mount;
1548 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
Dave Chinner3702ce62012-11-12 22:54:05 +11001549
Dave Chinner983d09f2013-04-03 16:11:15 +11001550 if (xfs_sb_version_hascrc(&mp->m_sb) &&
1551 !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
1552 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11001553 /*
1554 * Validate the magic number of the agi block.
1555 */
Dave Chinner983d09f2013-04-03 16:11:15 +11001556 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
1557 return false;
1558 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
1559 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11001560
1561 /*
1562 * during growfs operations, the perag is not fully initialised,
1563 * so we can't use it for any useful checking. growfs ensures we can't
1564 * use it by using uncached buffers that don't have the perag attached
1565 * so we can detect and avoid this problem.
1566 */
Dave Chinner983d09f2013-04-03 16:11:15 +11001567 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
1568 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11001569
Dave Chinner3702ce62012-11-12 22:54:05 +11001570 xfs_check_agi_unlinked(agi);
Dave Chinner983d09f2013-04-03 16:11:15 +11001571 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +11001572}
1573
Dave Chinner1813dd62012-11-14 17:54:40 +11001574static void
1575xfs_agi_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11001576 struct xfs_buf *bp)
1577{
Dave Chinner983d09f2013-04-03 16:11:15 +11001578 struct xfs_mount *mp = bp->b_target->bt_mount;
1579 int agi_ok = 1;
1580
1581 if (xfs_sb_version_hascrc(&mp->m_sb))
1582 agi_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
1583 offsetof(struct xfs_agi, agi_crc));
1584 agi_ok = agi_ok && xfs_agi_verify(bp);
1585
1586 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1587 XFS_RANDOM_IALLOC_READ_AGI))) {
1588 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1589 xfs_buf_ioerror(bp, EFSCORRUPTED);
1590 }
Dave Chinner612cfbf2012-11-14 17:52:32 +11001591}
1592
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001593static void
Dave Chinner1813dd62012-11-14 17:54:40 +11001594xfs_agi_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11001595 struct xfs_buf *bp)
1596{
Dave Chinner983d09f2013-04-03 16:11:15 +11001597 struct xfs_mount *mp = bp->b_target->bt_mount;
1598 struct xfs_buf_log_item *bip = bp->b_fspriv;
1599
1600 if (!xfs_agi_verify(bp)) {
1601 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1602 xfs_buf_ioerror(bp, EFSCORRUPTED);
1603 return;
1604 }
1605
1606 if (!xfs_sb_version_hascrc(&mp->m_sb))
1607 return;
1608
1609 if (bip)
1610 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
1611 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
1612 offsetof(struct xfs_agi, agi_crc));
Dave Chinner3702ce62012-11-12 22:54:05 +11001613}
1614
Dave Chinner1813dd62012-11-14 17:54:40 +11001615const struct xfs_buf_ops xfs_agi_buf_ops = {
1616 .verify_read = xfs_agi_read_verify,
1617 .verify_write = xfs_agi_write_verify,
1618};
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620/*
1621 * Read in the allocation group header (inode allocation section)
1622 */
1623int
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001624xfs_read_agi(
1625 struct xfs_mount *mp, /* file system mount structure */
1626 struct xfs_trans *tp, /* transaction pointer */
1627 xfs_agnumber_t agno, /* allocation group number */
1628 struct xfs_buf **bpp) /* allocation group hdr buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001630 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 ASSERT(agno != NULLAGNUMBER);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001633
1634 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11001636 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (error)
1638 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001639
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001640 ASSERT(!xfs_buf_geterror(*bpp));
Christoph Hellwig38f23232011-10-10 16:52:45 +00001641 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001642 return 0;
1643}
1644
1645int
1646xfs_ialloc_read_agi(
1647 struct xfs_mount *mp, /* file system mount structure */
1648 struct xfs_trans *tp, /* transaction pointer */
1649 xfs_agnumber_t agno, /* allocation group number */
1650 struct xfs_buf **bpp) /* allocation group hdr buf */
1651{
1652 struct xfs_agi *agi; /* allocation group header */
1653 struct xfs_perag *pag; /* per allocation group data */
1654 int error;
1655
1656 error = xfs_read_agi(mp, tp, agno, bpp);
1657 if (error)
1658 return error;
1659
1660 agi = XFS_BUF_TO_AGI(*bpp);
Dave Chinner44b56e02010-01-11 11:47:43 +00001661 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11001663 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10001664 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001668 /*
1669 * It's possible for these to be out of sync if
1670 * we are in the middle of a forced shutdown.
1671 */
1672 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1673 XFS_FORCED_SHUTDOWN(mp));
Dave Chinner44b56e02010-01-11 11:47:43 +00001674 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return 0;
1676}
David Chinner92821e22007-05-24 15:26:31 +10001677
1678/*
1679 * Read in the agi to initialise the per-ag data in the mount structure
1680 */
1681int
1682xfs_ialloc_pagi_init(
1683 xfs_mount_t *mp, /* file system mount structure */
1684 xfs_trans_t *tp, /* transaction pointer */
1685 xfs_agnumber_t agno) /* allocation group number */
1686{
1687 xfs_buf_t *bp = NULL;
1688 int error;
1689
1690 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1691 if (error)
1692 return error;
1693 if (bp)
1694 xfs_trans_brelse(tp, bp);
1695 return 0;
1696}