blob: 4411565b718b26a7e4c055b42f7433d5f702a74b [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Allocation group level functions.
45 */
David Chinner75de2a92008-03-27 18:00:38 +110046static inline int
47xfs_ialloc_cluster_alignment(
48 xfs_alloc_arg_t *args)
49{
50 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
51 args->mp->m_sb.sb_inoalignmt >=
52 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
53 return args->mp->m_sb.sb_inoalignmt;
54 return 1;
55}
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
Christoph Hellwig21875502009-08-31 20:58:21 -030058 * Lookup a record by ino in the btree given by cur.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110059 */
Christoph Hellwig81e25172009-09-01 19:56:55 -040060int /* error */
Christoph Hellwig21875502009-08-31 20:58:21 -030061xfs_inobt_lookup(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110062 struct xfs_btree_cur *cur, /* btree cursor */
63 xfs_agino_t ino, /* starting inode of chunk */
Christoph Hellwig21875502009-08-31 20:58:21 -030064 xfs_lookup_t dir, /* <=, >=, == */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110065 int *stat) /* success/failure */
66{
67 cur->bc_rec.i.ir_startino = ino;
Christoph Hellwig21875502009-08-31 20:58:21 -030068 cur->bc_rec.i.ir_freecount = 0;
69 cur->bc_rec.i.ir_free = 0;
70 return xfs_btree_lookup(cur, dir, stat);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110071}
72
73/*
Christoph Hellwigafabc242009-08-31 20:57:03 -030074 * Update the record referred to by cur to the value given.
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110075 * This either works (return 0) or gets an EFSCORRUPTED error.
76 */
77STATIC int /* error */
78xfs_inobt_update(
79 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigafabc242009-08-31 20:57:03 -030080 xfs_inobt_rec_incore_t *irec) /* btree record */
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110081{
82 union xfs_btree_rec rec;
83
Christoph Hellwigafabc242009-08-31 20:57:03 -030084 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
85 rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
86 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110087 return xfs_btree_update(cur, &rec);
88}
89
90/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +110091 * Get the data from the pointed-to record.
92 */
93int /* error */
94xfs_inobt_get_rec(
95 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig2e287a72009-08-31 20:56:58 -030096 xfs_inobt_rec_incore_t *irec, /* btree record */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +110097 int *stat) /* output: success/failure */
98{
99 union xfs_btree_rec *rec;
100 int error;
101
102 error = xfs_btree_get_rec(cur, &rec, stat);
103 if (!error && *stat == 1) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300104 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
105 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
106 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100107 }
108 return error;
109}
110
111/*
Dave Chinner0b48db82009-08-31 20:57:09 -0300112 * Verify that the number of free inodes in the AGI is correct.
113 */
114#ifdef DEBUG
115STATIC int
116xfs_check_agi_freecount(
117 struct xfs_btree_cur *cur,
118 struct xfs_agi *agi)
119{
120 if (cur->bc_nlevels == 1) {
121 xfs_inobt_rec_incore_t rec;
122 int freecount = 0;
123 int error;
124 int i;
125
Christoph Hellwig21875502009-08-31 20:58:21 -0300126 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
Dave Chinner0b48db82009-08-31 20:57:09 -0300127 if (error)
128 return error;
129
130 do {
131 error = xfs_inobt_get_rec(cur, &rec, &i);
132 if (error)
133 return error;
134
135 if (i) {
136 freecount += rec.ir_freecount;
137 error = xfs_btree_increment(cur, 0, &i);
138 if (error)
139 return error;
140 }
141 } while (i == 1);
142
143 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
144 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
145 }
146 return 0;
147}
148#else
149#define xfs_check_agi_freecount(cur, agi) 0
150#endif
151
152/*
Dave Chinner28c8e412013-06-27 16:04:55 +1000153 * Initialise a new set of inodes. When called without a transaction context
154 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
155 * than logging them (which in a transaction context puts them into the AIL
156 * for writeback rather than the xfsbufd queue).
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300157 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000158STATIC int
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300159xfs_ialloc_inode_init(
160 struct xfs_mount *mp,
161 struct xfs_trans *tp,
Dave Chinner28c8e412013-06-27 16:04:55 +1000162 struct list_head *buffer_list,
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300163 xfs_agnumber_t agno,
164 xfs_agblock_t agbno,
165 xfs_agblock_t length,
166 unsigned int gen)
167{
168 struct xfs_buf *fbuf;
169 struct xfs_dinode *free;
170 int blks_per_cluster, nbufs, ninodes;
171 int version;
172 int i, j;
173 xfs_daddr_t d;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100174 xfs_ino_t ino = 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300175
176 /*
177 * Loop over the new block(s), filling in the inodes.
178 * For small block sizes, manipulate the inodes in buffers
179 * which are multiples of the blocks size.
180 */
181 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
182 blks_per_cluster = 1;
183 nbufs = length;
184 ninodes = mp->m_sb.sb_inopblock;
185 } else {
186 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
187 mp->m_sb.sb_blocksize;
188 nbufs = length / blks_per_cluster;
189 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
190 }
191
192 /*
Christoph Hellwig93848a92013-04-03 16:11:17 +1100193 * Figure out what version number to use in the inodes we create. If
194 * the superblock version has caught up to the one that supports the new
195 * inode format, then use the new inode version. Otherwise use the old
196 * version so that old kernels will continue to be able to use the file
197 * system.
198 *
199 * For v3 inodes, we also need to write the inode number into the inode,
200 * so calculate the first inode number of the chunk here as
201 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
202 * across multiple filesystem blocks (such as a cluster) and so cannot
203 * be used in the cluster buffer loop below.
204 *
205 * Further, because we are writing the inode directly into the buffer
206 * and calculating a CRC on the entire inode, we have ot log the entire
207 * inode so that the entire range the CRC covers is present in the log.
208 * That means for v3 inode we log the entire buffer rather than just the
209 * inode cores.
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300210 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100211 if (xfs_sb_version_hascrc(&mp->m_sb)) {
212 version = 3;
213 ino = XFS_AGINO_TO_INO(mp, agno,
214 XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
215 } else if (xfs_sb_version_hasnlink(&mp->m_sb))
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300216 version = 2;
217 else
218 version = 1;
219
220 for (j = 0; j < nbufs; j++) {
221 /*
222 * Get the block.
223 */
224 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
225 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
Dave Chinner7c4cebe2012-11-23 14:24:23 +1100226 mp->m_bsize * blks_per_cluster,
227 XBF_UNMAPPED);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000228 if (!fbuf)
229 return ENOMEM;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300230 /*
231 * Initialize all inodes in this buffer and then log them.
232 *
233 * XXX: It would be much better if we had just one transaction
234 * to log a whole cluster of inodes instead of all the
235 * individual transactions causing a lot of log traffic.
236 */
Dave Chinner1813dd62012-11-14 17:54:40 +1100237 fbuf->b_ops = &xfs_inode_buf_ops;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100238 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300239 for (i = 0; i < ninodes; i++) {
240 int ioffset = i << mp->m_sb.sb_inodelog;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100241 uint isize = xfs_dinode_size(version);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300242
243 free = xfs_make_iptr(mp, fbuf, i);
244 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
245 free->di_version = version;
246 free->di_gen = cpu_to_be32(gen);
247 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100248
249 if (version == 3) {
250 free->di_ino = cpu_to_be64(ino);
251 ino++;
252 uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
253 xfs_dinode_calc_crc(mp, free);
Dave Chinner28c8e412013-06-27 16:04:55 +1000254 } else if (tp) {
Christoph Hellwig93848a92013-04-03 16:11:17 +1100255 /* just log the inode core */
256 xfs_trans_log_buf(tp, fbuf, ioffset,
257 ioffset + isize - 1);
258 }
259 }
Dave Chinner28c8e412013-06-27 16:04:55 +1000260
261 if (tp) {
262 /*
263 * Mark the buffer as an inode allocation buffer so it
264 * sticks in AIL at the point of this allocation
265 * transaction. This ensures the they are on disk before
266 * the tail of the log can be moved past this
267 * transaction (i.e. by preventing relogging from moving
268 * it forward in the log).
269 */
270 xfs_trans_inode_alloc_buf(tp, fbuf);
271 if (version == 3) {
272 /* need to log the entire buffer */
273 xfs_trans_log_buf(tp, fbuf, 0,
274 BBTOB(fbuf->b_length) - 1);
275 }
276 } else {
277 fbuf->b_flags |= XBF_DONE;
278 xfs_buf_delwri_queue(fbuf, buffer_list);
279 xfs_buf_relse(fbuf);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300280 }
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300281 }
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000282 return 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300283}
284
285/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * Allocate new inodes in the allocation group specified by agbp.
287 * Return 0 for success, else error code.
288 */
289STATIC int /* error code or 0 */
290xfs_ialloc_ag_alloc(
291 xfs_trans_t *tp, /* transaction pointer */
292 xfs_buf_t *agbp, /* alloc group buffer */
293 int *alloc)
294{
295 xfs_agi_t *agi; /* allocation group header */
296 xfs_alloc_arg_t args; /* allocation argument structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 xfs_btree_cur_t *cur; /* inode btree cursor */
David Chinner92821e22007-05-24 15:26:31 +1000298 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300300 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 xfs_agino_t newino; /* new first inode's number */
302 xfs_agino_t newlen; /* new number of inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 xfs_agino_t thisino; /* current inode number, for loop */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000304 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* boundary */
Dave Chinner44b56e02010-01-11 11:47:43 +0000306 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Mark Tinguelya0041682012-09-20 13:16:45 -0500308 memset(&args, 0, sizeof(args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 args.tp = tp;
310 args.mp = tp->t_mountp;
311
312 /*
313 * Locking will ensure that we don't have two callers in here
314 * at one time.
315 */
316 newlen = XFS_IALLOC_INODES(args.mp);
317 if (args.mp->m_maxicount &&
318 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
319 return XFS_ERROR(ENOSPC);
320 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
321 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000322 * First try to allocate inodes contiguous with the last-allocated
323 * chunk of inodes. If the filesystem is striped, this will fill
324 * an entire stripe unit with inodes.
Dave Chinner28c8e412013-06-27 16:04:55 +1000325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000327 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300328 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000329 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
330 XFS_IALLOC_BLOCKS(args.mp);
331 if (likely(newino != NULLAGINO &&
332 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300333 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000334 args.type = XFS_ALLOCTYPE_THIS_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000335 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100336
Glen Overby3ccb8b52006-03-29 09:52:28 +1000337 /*
David Chinner75de2a92008-03-27 18:00:38 +1100338 * We need to take into account alignment here to ensure that
339 * we don't modify the free list if we fail to have an exact
340 * block. If we don't have an exact match, and every oher
341 * attempt allocation attempt fails, we'll end up cancelling
342 * a dirty transaction and shutting down.
343 *
344 * For an exact allocation, alignment must be 1,
345 * however we need to take cluster alignment into account when
346 * fixing up the freelist. Use the minalignslop field to
347 * indicate that extra blocks might be required for alignment,
348 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000349 */
David Chinner75de2a92008-03-27 18:00:38 +1100350 args.alignment = 1;
351 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
352
353 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100354 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000355 if ((error = xfs_alloc_vextent(&args)))
356 return error;
357 } else
358 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Glen Overby3ccb8b52006-03-29 09:52:28 +1000360 if (unlikely(args.fsbno == NULLFSBLOCK)) {
361 /*
362 * Set the alignment for the allocation.
363 * If stripe alignment is turned on then align at stripe unit
364 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000365 * If the cluster size is smaller than a filesystem block
366 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000367 * pieces, so don't need alignment anyway.
368 */
369 isaligned = 0;
370 if (args.mp->m_sinoalign) {
371 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
372 args.alignment = args.mp->m_dalign;
373 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100374 } else
375 args.alignment = xfs_ialloc_cluster_alignment(&args);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000376 /*
377 * Need to figure out where to allocate the inode blocks.
378 * Ideally they should be spaced out through the a.g.
379 * For now, just allocate blocks up front.
380 */
381 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300382 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000383 /*
384 * Allocate a fixed-size extent of inodes.
385 */
386 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000387 args.prod = 1;
388 /*
389 * Allow space for the inode btree to split.
390 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100391 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000392 if ((error = xfs_alloc_vextent(&args)))
393 return error;
394 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /*
397 * If stripe alignment is turned on, then try again with cluster
398 * alignment.
399 */
400 if (isaligned && args.fsbno == NULLFSBLOCK) {
401 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100402 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300403 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
David Chinner75de2a92008-03-27 18:00:38 +1100404 args.alignment = xfs_ialloc_cluster_alignment(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if ((error = xfs_alloc_vextent(&args)))
406 return error;
407 }
408
409 if (args.fsbno == NULLFSBLOCK) {
410 *alloc = 0;
411 return 0;
412 }
413 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
David Chinner359346a2008-04-29 12:53:32 +1000415 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300416 * Stamp and write the inode buffers.
417 *
David Chinner359346a2008-04-29 12:53:32 +1000418 * Seed the new inode cluster with a random generation number. This
419 * prevents short-term reuse of generation numbers if a chunk is
420 * freed and then immediately reallocated. We use random numbers
421 * rather than a linear progression to prevent the next generation
422 * number from being easily guessable.
423 */
Dave Chinner28c8e412013-06-27 16:04:55 +1000424 error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
Akinobu Mitaecb34032013-03-04 21:58:20 +0900425 args.len, prandom_u32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100426
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000427 if (error)
428 return error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300429 /*
430 * Convert the results.
431 */
432 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800433 be32_add_cpu(&agi->agi_count, newlen);
434 be32_add_cpu(&agi->agi_freecount, newlen);
Dave Chinner44b56e02010-01-11 11:47:43 +0000435 pag = xfs_perag_get(args.mp, agno);
436 pag->pagi_freecount += newlen;
437 xfs_perag_put(pag);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100438 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
441 * Insert records describing the new inode chunk into the btree.
442 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100443 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 for (thisino = newino;
445 thisino < newino + newlen;
446 thisino += XFS_INODES_PER_CHUNK) {
Christoph Hellwig21875502009-08-31 20:58:21 -0300447 cur->bc_rec.i.ir_startino = thisino;
448 cur->bc_rec.i.ir_freecount = XFS_INODES_PER_CHUNK;
449 cur->bc_rec.i.ir_free = XFS_INOBT_ALL_FREE;
450 error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, &i);
451 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
453 return error;
454 }
455 ASSERT(i == 0);
Christoph Hellwig21875502009-08-31 20:58:21 -0300456 error = xfs_btree_insert(cur, &i);
457 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
459 return error;
460 }
461 ASSERT(i == 1);
462 }
463 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
464 /*
465 * Log allocation group header fields
466 */
467 xfs_ialloc_log_agi(tp, agbp,
468 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
469 /*
470 * Modify/log superblock values for inode count and inode free count.
471 */
472 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
473 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
474 *alloc = 1;
475 return 0;
476}
477
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000478STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479xfs_ialloc_next_ag(
480 xfs_mount_t *mp)
481{
482 xfs_agnumber_t agno;
483
484 spin_lock(&mp->m_agirotor_lock);
485 agno = mp->m_agirotor;
Carlos Maiolino8aea3ff2012-09-20 10:32:36 -0300486 if (++mp->m_agirotor >= mp->m_maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 mp->m_agirotor = 0;
488 spin_unlock(&mp->m_agirotor_lock);
489
490 return agno;
491}
492
493/*
494 * Select an allocation group to look for a free inode in, based on the parent
495 * inode and then mode. Return the allocation group buffer.
496 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400497STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498xfs_ialloc_ag_select(
499 xfs_trans_t *tp, /* transaction pointer */
500 xfs_ino_t parent, /* parent directory inode number */
Al Viro576b1d62011-07-26 02:50:15 -0400501 umode_t mode, /* bits set to indicate file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int okalloc) /* ok to allocate more space */
503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
505 xfs_agnumber_t agno; /* current ag number */
506 int flags; /* alloc buffer locking flags */
507 xfs_extlen_t ineed; /* blocks needed for inode allocation */
508 xfs_extlen_t longest = 0; /* longest extent available */
509 xfs_mount_t *mp; /* mount point structure */
510 int needspace; /* file mode implies space allocated */
511 xfs_perag_t *pag; /* per allocation group data */
512 xfs_agnumber_t pagno; /* parent (starting) ag number */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400513 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /*
516 * Files of these types need at least one block if length > 0
517 * (and they won't fit in the inode, but that's hard to figure out).
518 */
519 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
520 mp = tp->t_mountp;
521 agcount = mp->m_maxagi;
522 if (S_ISDIR(mode))
523 pagno = xfs_ialloc_next_ag(mp);
524 else {
525 pagno = XFS_INO_TO_AGNO(mp, parent);
526 if (pagno >= agcount)
527 pagno = 0;
528 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ASSERT(pagno < agcount);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
533 * Loop through allocation groups, looking for one with a little
534 * free space in it. Note we don't look for free inodes, exactly.
535 * Instead, we include whether there is a need to allocate inodes
536 * to mean that blocks must be allocated for them,
537 * if none are currently free.
538 */
539 agno = pagno;
540 flags = XFS_ALLOC_FLAG_TRYLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 for (;;) {
Dave Chinner44b56e02010-01-11 11:47:43 +0000542 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!pag->pagi_inodeok) {
544 xfs_ialloc_next_ag(mp);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400545 goto nextag;
546 }
547
548 if (!pag->pagi_init) {
549 error = xfs_ialloc_pagi_init(mp, tp, agno);
550 if (error)
551 goto nextag;
552 }
553
554 if (pag->pagi_freecount) {
555 xfs_perag_put(pag);
556 return agno;
557 }
558
559 if (!okalloc)
560 goto nextag;
561
562 if (!pag->pagf_init) {
563 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
564 if (error)
565 goto nextag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 /*
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400569 * Is there enough free space for the file plus a block of
570 * inodes? (if we need to allocate some)?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400572 ineed = XFS_IALLOC_BLOCKS(mp);
573 longest = pag->pagf_longest;
574 if (!longest)
575 longest = pag->pagf_flcount > 0;
576
577 if (pag->pagf_freeblks >= needspace + ineed &&
578 longest >= ineed) {
579 xfs_perag_put(pag);
580 return agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582nextag:
Dave Chinner44b56e02010-01-11 11:47:43 +0000583 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
585 * No point in iterating over the rest, if we're shutting
586 * down.
587 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000588 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400589 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 agno++;
591 if (agno >= agcount)
592 agno = 0;
593 if (agno == pagno) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000594 if (flags == 0)
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400595 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 flags = 0;
597 }
598 }
599}
600
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300601/*
602 * Try to retrieve the next record to the left/right from the current one.
603 */
604STATIC int
605xfs_ialloc_next_rec(
606 struct xfs_btree_cur *cur,
607 xfs_inobt_rec_incore_t *rec,
608 int *done,
609 int left)
610{
611 int error;
612 int i;
613
614 if (left)
615 error = xfs_btree_decrement(cur, 0, &i);
616 else
617 error = xfs_btree_increment(cur, 0, &i);
618
619 if (error)
620 return error;
621 *done = !i;
622 if (i) {
623 error = xfs_inobt_get_rec(cur, rec, &i);
624 if (error)
625 return error;
626 XFS_WANT_CORRUPTED_RETURN(i == 1);
627 }
628
629 return 0;
630}
631
Dave Chinnerbd169562009-08-31 20:58:28 -0300632STATIC int
633xfs_ialloc_get_rec(
634 struct xfs_btree_cur *cur,
635 xfs_agino_t agino,
636 xfs_inobt_rec_incore_t *rec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800637 int *done)
Dave Chinnerbd169562009-08-31 20:58:28 -0300638{
639 int error;
640 int i;
641
642 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
643 if (error)
644 return error;
645 *done = !i;
646 if (i) {
647 error = xfs_inobt_get_rec(cur, rec, &i);
648 if (error)
649 return error;
650 XFS_WANT_CORRUPTED_RETURN(i == 1);
651 }
652
653 return 0;
654}
Dave Chinner0b48db82009-08-31 20:57:09 -0300655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400657 * Allocate an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 *
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400659 * The caller selected an AG for us, and made sure that free inodes are
660 * available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400662STATIC int
663xfs_dialloc_ag(
664 struct xfs_trans *tp,
665 struct xfs_buf *agbp,
666 xfs_ino_t parent,
667 xfs_ino_t *inop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400669 struct xfs_mount *mp = tp->t_mountp;
670 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
671 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
672 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
673 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
674 struct xfs_perag *pag;
675 struct xfs_btree_cur *cur, *tcur;
676 struct xfs_inobt_rec_incore rec, trec;
677 xfs_ino_t ino;
678 int error;
679 int offset;
680 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Dave Chinner44b56e02010-01-11 11:47:43 +0000682 pag = xfs_perag_get(mp, agno);
Dave Chinnerbd169562009-08-31 20:58:28 -0300683
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400684 ASSERT(pag->pagi_init);
685 ASSERT(pag->pagi_inodeok);
686 ASSERT(pag->pagi_freecount > 0);
687
Dave Chinnerbd169562009-08-31 20:58:28 -0300688 restart_pagno:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400689 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * If pagino is 0 (this is the root inode allocation) use newino.
692 * This must work because we've just allocated some.
693 */
694 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100695 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dave Chinner0b48db82009-08-31 20:57:09 -0300697 error = xfs_check_agi_freecount(cur, agi);
698 if (error)
699 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300702 * If in the same AG as the parent, try to get near the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
704 if (pagno == agno) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300705 int doneleft; /* done, to the left */
706 int doneright; /* done, to the right */
Dave Chinnerbd169562009-08-31 20:58:28 -0300707 int searchdistance = 10;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300708
Christoph Hellwig21875502009-08-31 20:58:21 -0300709 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300710 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300712 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
713
714 error = xfs_inobt_get_rec(cur, &rec, &j);
715 if (error)
716 goto error0;
717 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
718
719 if (rec.ir_freecount > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * Found a free inode in the same chunk
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300722 * as the parent, done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300724 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300726
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300729 * In the same AG as parent, but parent's chunk is full.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300732 /* duplicate the cursor, search left & right simultaneously */
733 error = xfs_btree_dup_cursor(cur, &tcur);
734 if (error)
735 goto error0;
736
Dave Chinnerbd169562009-08-31 20:58:28 -0300737 /*
738 * Skip to last blocks looked up if same parent inode.
739 */
740 if (pagino != NULLAGINO &&
741 pag->pagl_pagino == pagino &&
742 pag->pagl_leftrec != NULLAGINO &&
743 pag->pagl_rightrec != NULLAGINO) {
744 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800745 &trec, &doneleft);
Dave Chinnerbd169562009-08-31 20:58:28 -0300746 if (error)
747 goto error1;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300748
Dave Chinnerbd169562009-08-31 20:58:28 -0300749 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800750 &rec, &doneright);
Dave Chinnerbd169562009-08-31 20:58:28 -0300751 if (error)
752 goto error1;
753 } else {
754 /* search left with tcur, back up 1 record */
755 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
756 if (error)
757 goto error1;
758
759 /* search right with cur, go forward 1 record. */
760 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
761 if (error)
762 goto error1;
763 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300764
765 /*
766 * Loop until we find an inode chunk with a free inode.
767 */
768 while (!doneleft || !doneright) {
769 int useleft; /* using left inode chunk this time */
770
Dave Chinnerbd169562009-08-31 20:58:28 -0300771 if (!--searchdistance) {
772 /*
773 * Not in range - save last search
774 * location and allocate a new inode
775 */
Eric Sandeen3b826382009-10-30 09:27:07 +0100776 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300777 pag->pagl_leftrec = trec.ir_startino;
778 pag->pagl_rightrec = rec.ir_startino;
779 pag->pagl_pagino = pagino;
780 goto newino;
781 }
782
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300783 /* figure out the closer block if both are valid. */
784 if (!doneleft && !doneright) {
785 useleft = pagino -
786 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
787 rec.ir_startino - pagino;
788 } else {
789 useleft = !doneleft;
790 }
791
792 /* free inodes to the left? */
793 if (useleft && trec.ir_freecount) {
794 rec = trec;
795 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
796 cur = tcur;
Dave Chinnerbd169562009-08-31 20:58:28 -0300797
798 pag->pagl_leftrec = trec.ir_startino;
799 pag->pagl_rightrec = rec.ir_startino;
800 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300801 goto alloc_inode;
802 }
803
804 /* free inodes to the right? */
805 if (!useleft && rec.ir_freecount) {
806 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300807
808 pag->pagl_leftrec = trec.ir_startino;
809 pag->pagl_rightrec = rec.ir_startino;
810 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300811 goto alloc_inode;
812 }
813
814 /* get next record to check */
815 if (useleft) {
816 error = xfs_ialloc_next_rec(tcur, &trec,
817 &doneleft, 1);
818 } else {
819 error = xfs_ialloc_next_rec(cur, &rec,
820 &doneright, 0);
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300825
826 /*
827 * We've reached the end of the btree. because
828 * we are only searching a small chunk of the
829 * btree each search, there is obviously free
830 * inodes closer to the parent inode than we
831 * are now. restart the search again.
832 */
833 pag->pagl_pagino = NULLAGINO;
834 pag->pagl_leftrec = NULLAGINO;
835 pag->pagl_rightrec = NULLAGINO;
836 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
837 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
838 goto restart_pagno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300842 * In a different AG from the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * See if the most recently allocated block has any free.
844 */
Dave Chinnerbd169562009-08-31 20:58:28 -0300845newino:
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200846 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
Christoph Hellwig21875502009-08-31 20:58:21 -0300847 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
848 XFS_LOOKUP_EQ, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300849 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300851
852 if (i == 1) {
853 error = xfs_inobt_get_rec(cur, &rec, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (error)
855 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300856
857 if (j == 1 && rec.ir_freecount > 0) {
858 /*
859 * The last chunk allocated in the group
860 * still has a free inode.
861 */
862 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300865 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300866
Dave Chinnerbd169562009-08-31 20:58:28 -0300867 /*
868 * None left in the last group, search the whole AG
869 */
870 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
871 if (error)
872 goto error0;
873 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
874
875 for (;;) {
876 error = xfs_inobt_get_rec(cur, &rec, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300877 if (error)
878 goto error0;
879 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Dave Chinnerbd169562009-08-31 20:58:28 -0300880 if (rec.ir_freecount > 0)
881 break;
882 error = xfs_btree_increment(cur, 0, &i);
883 if (error)
884 goto error0;
885 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300887
888alloc_inode:
Christoph Hellwig824c3132012-07-04 10:54:45 -0400889 offset = xfs_lowbit64(rec.ir_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ASSERT(offset >= 0);
891 ASSERT(offset < XFS_INODES_PER_CHUNK);
892 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
893 XFS_INODES_PER_CHUNK) == 0);
894 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100895 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 rec.ir_freecount--;
Christoph Hellwigafabc242009-08-31 20:57:03 -0300897 error = xfs_inobt_update(cur, &rec);
898 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800900 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +0000902 pag->pagi_freecount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Dave Chinner0b48db82009-08-31 20:57:09 -0300904 error = xfs_check_agi_freecount(cur, agi);
905 if (error)
906 goto error0;
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
909 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
Dave Chinner44b56e02010-01-11 11:47:43 +0000910 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *inop = ino;
912 return 0;
913error1:
914 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
915error0:
916 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
Dave Chinner44b56e02010-01-11 11:47:43 +0000917 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return error;
919}
920
921/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400922 * Allocate an inode on disk.
923 *
924 * Mode is used to tell whether the new inode will need space, and whether it
925 * is a directory.
926 *
927 * This function is designed to be called twice if it has to do an allocation
928 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
929 * If an inode is available without having to performn an allocation, an inode
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300930 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
931 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
932 * The caller should then commit the current transaction, allocate a
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400933 * new transaction, and call xfs_dialloc() again, passing in the previous value
934 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
935 * buffer is locked across the two calls, the second call is guaranteed to have
936 * a free inode available.
937 *
938 * Once we successfully pick an inode its number is returned and the on-disk
939 * data structures are updated. The inode itself is not read in, since doing so
940 * would break ordering constraints with xfs_reclaim.
941 */
942int
943xfs_dialloc(
944 struct xfs_trans *tp,
945 xfs_ino_t parent,
946 umode_t mode,
947 int okalloc,
948 struct xfs_buf **IO_agbp,
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400949 xfs_ino_t *inop)
950{
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400951 struct xfs_mount *mp = tp->t_mountp;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400952 struct xfs_buf *agbp;
953 xfs_agnumber_t agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400954 int error;
955 int ialloced;
956 int noroom = 0;
Christoph Hellwigbe60fe52012-07-04 10:54:50 -0400957 xfs_agnumber_t start_agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400958 struct xfs_perag *pag;
959
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400960 if (*IO_agbp) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400961 /*
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400962 * If the caller passes in a pointer to the AGI buffer,
963 * continue where we left off before. In this case, we
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400964 * know that the allocation group has free inodes.
965 */
966 agbp = *IO_agbp;
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400967 goto out_alloc;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400968 }
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400969
970 /*
971 * We do not have an agbp, so select an initial allocation
972 * group for inode allocation.
973 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -0400974 start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
975 if (start_agno == NULLAGNUMBER) {
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400976 *inop = NULLFSINO;
977 return 0;
978 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400979
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400980 /*
981 * If we have already hit the ceiling of inode blocks then clear
982 * okalloc so we scan all available agi structures for a free
983 * inode.
984 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400985 if (mp->m_maxicount &&
986 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
987 noroom = 1;
988 okalloc = 0;
989 }
990
991 /*
992 * Loop until we find an allocation group that either has free inodes
993 * or in which we can allocate some inodes. Iterate through the
994 * allocation groups upward, wrapping at the end.
995 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -0400996 agno = start_agno;
997 for (;;) {
998 pag = xfs_perag_get(mp, agno);
999 if (!pag->pagi_inodeok) {
1000 xfs_ialloc_next_ag(mp);
1001 goto nextag;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001002 }
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001003
1004 if (!pag->pagi_init) {
1005 error = xfs_ialloc_pagi_init(mp, tp, agno);
1006 if (error)
1007 goto out_error;
1008 }
1009
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001010 /*
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001011 * Do a first racy fast path check if this AG is usable.
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001012 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001013 if (!pag->pagi_freecount && !okalloc)
1014 goto nextag;
1015
Christoph Hellwigc4982112012-08-07 02:02:02 -04001016 /*
1017 * Then read in the AGI buffer and recheck with the AGI buffer
1018 * lock held.
1019 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001020 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1021 if (error)
1022 goto out_error;
1023
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001024 if (pag->pagi_freecount) {
1025 xfs_perag_put(pag);
1026 goto out_alloc;
1027 }
1028
Christoph Hellwigc4982112012-08-07 02:02:02 -04001029 if (!okalloc)
1030 goto nextag_relse_buffer;
1031
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001032
1033 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1034 if (error) {
1035 xfs_trans_brelse(tp, agbp);
1036
1037 if (error != ENOSPC)
1038 goto out_error;
1039
1040 xfs_perag_put(pag);
1041 *inop = NULLFSINO;
1042 return 0;
1043 }
1044
1045 if (ialloced) {
1046 /*
1047 * We successfully allocated some inodes, return
1048 * the current context to the caller so that it
1049 * can commit the current transaction and call
1050 * us again where we left off.
1051 */
1052 ASSERT(pag->pagi_freecount > 0);
1053 xfs_perag_put(pag);
1054
1055 *IO_agbp = agbp;
1056 *inop = NULLFSINO;
1057 return 0;
1058 }
1059
Christoph Hellwigc4982112012-08-07 02:02:02 -04001060nextag_relse_buffer:
1061 xfs_trans_brelse(tp, agbp);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001062nextag:
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001063 xfs_perag_put(pag);
1064 if (++agno == mp->m_sb.sb_agcount)
1065 agno = 0;
1066 if (agno == start_agno) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001067 *inop = NULLFSINO;
1068 return noroom ? ENOSPC : 0;
1069 }
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001070 }
1071
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001072out_alloc:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001073 *IO_agbp = NULL;
1074 return xfs_dialloc_ag(tp, agbp, parent, inop);
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001075out_error:
1076 xfs_perag_put(pag);
1077 return XFS_ERROR(error);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001078}
1079
1080/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * Free disk inode. Carefully avoids touching the incore inode, all
1082 * manipulations incore are the caller's responsibility.
1083 * The on-disk inode is not changed by this operation, only the
1084 * btree (free inode mask) is changed.
1085 */
1086int
1087xfs_difree(
1088 xfs_trans_t *tp, /* transaction pointer */
1089 xfs_ino_t inode, /* inode to be freed */
1090 xfs_bmap_free_t *flist, /* extents to free */
1091 int *delete, /* set if inode cluster was deleted */
1092 xfs_ino_t *first_ino) /* first inode in deleted cluster */
1093{
1094 /* REFERENCED */
1095 xfs_agblock_t agbno; /* block number containing inode */
1096 xfs_buf_t *agbp; /* buffer containing allocation group header */
1097 xfs_agino_t agino; /* inode number relative to allocation group */
1098 xfs_agnumber_t agno; /* allocation group number */
1099 xfs_agi_t *agi; /* allocation group header */
1100 xfs_btree_cur_t *cur; /* inode btree cursor */
1101 int error; /* error return value */
1102 int i; /* result code */
1103 int ilen; /* inodes in an inode cluster */
1104 xfs_mount_t *mp; /* mount structure for filesystem */
1105 int off; /* offset of inode in inode chunk */
Christoph Hellwig61a25842006-09-28 10:57:04 +10001106 xfs_inobt_rec_incore_t rec; /* btree record */
Dave Chinner44b56e02010-01-11 11:47:43 +00001107 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 mp = tp->t_mountp;
1110
1111 /*
1112 * Break up inode number into its components.
1113 */
1114 agno = XFS_INO_TO_AGNO(mp, inode);
1115 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001116 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1117 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 ASSERT(0);
1119 return XFS_ERROR(EINVAL);
1120 }
1121 agino = XFS_INO_TO_AGINO(mp, inode);
1122 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001123 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1124 __func__, (unsigned long long)inode,
1125 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 ASSERT(0);
1127 return XFS_ERROR(EINVAL);
1128 }
1129 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1130 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001131 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1132 __func__, agbno, mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 ASSERT(0);
1134 return XFS_ERROR(EINVAL);
1135 }
1136 /*
1137 * Get the allocation group header.
1138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001141 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1142 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return error;
1144 }
1145 agi = XFS_BUF_TO_AGI(agbp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001146 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Christoph Hellwig16259e72005-11-02 15:11:25 +11001147 ASSERT(agbno < be32_to_cpu(agi->agi_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
1149 * Initialize the cursor.
1150 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001151 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Dave Chinner0b48db82009-08-31 20:57:09 -03001153 error = xfs_check_agi_freecount(cur, agi);
1154 if (error)
1155 goto error0;
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
1158 * Look for the entry describing this inode.
1159 */
Christoph Hellwig21875502009-08-31 20:58:21 -03001160 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001161 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1162 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 goto error0;
1164 }
1165 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001166 error = xfs_inobt_get_rec(cur, &rec, &i);
1167 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001168 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1169 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 goto error0;
1171 }
1172 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1173 /*
1174 * Get the offset in the inode chunk.
1175 */
1176 off = agino - rec.ir_startino;
1177 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001178 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /*
1180 * Mark the inode free & increment the count.
1181 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001182 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 rec.ir_freecount++;
1184
1185 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001186 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001188 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1190
1191 *delete = 1;
1192 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1193
1194 /*
1195 * Remove the inode cluster from the AGI B+Tree, adjust the
1196 * AGI and Superblock inode counts, and mark the disk space
1197 * to be freed when the transaction is committed.
1198 */
1199 ilen = XFS_IALLOC_INODES(mp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001200 be32_add_cpu(&agi->agi_count, -ilen);
1201 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001203 pag = xfs_perag_get(mp, agno);
1204 pag->pagi_freecount -= ilen - 1;
1205 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1207 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1208
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001209 if ((error = xfs_btree_delete(cur, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001210 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1211 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto error0;
1213 }
1214
1215 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1216 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1217 XFS_IALLOC_BLOCKS(mp), flist, mp);
1218 } else {
1219 *delete = 0;
1220
Christoph Hellwigafabc242009-08-31 20:57:03 -03001221 error = xfs_inobt_update(cur, &rec);
1222 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001223 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1224 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 goto error0;
1226 }
Christoph Hellwigafabc242009-08-31 20:57:03 -03001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /*
1229 * Change the inode free counts and log the ag/sb changes.
1230 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001231 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001233 pag = xfs_perag_get(mp, agno);
1234 pag->pagi_freecount++;
1235 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1237 }
1238
Dave Chinner0b48db82009-08-31 20:57:09 -03001239 error = xfs_check_agi_freecount(cur, agi);
1240 if (error)
1241 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1244 return 0;
1245
1246error0:
1247 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1248 return error;
1249}
1250
Dave Chinner7124fe02010-06-24 11:15:33 +10001251STATIC int
1252xfs_imap_lookup(
1253 struct xfs_mount *mp,
1254 struct xfs_trans *tp,
1255 xfs_agnumber_t agno,
1256 xfs_agino_t agino,
1257 xfs_agblock_t agbno,
1258 xfs_agblock_t *chunk_agbno,
1259 xfs_agblock_t *offset_agbno,
1260 int flags)
1261{
1262 struct xfs_inobt_rec_incore rec;
1263 struct xfs_btree_cur *cur;
1264 struct xfs_buf *agbp;
Dave Chinner7124fe02010-06-24 11:15:33 +10001265 int error;
1266 int i;
1267
1268 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1269 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +11001270 xfs_alert(mp,
1271 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1272 __func__, error, agno);
Dave Chinner7124fe02010-06-24 11:15:33 +10001273 return error;
1274 }
1275
1276 /*
Dave Chinner4536f2a2010-08-24 11:42:30 +10001277 * Lookup the inode record for the given agino. If the record cannot be
1278 * found, then it's an invalid inode number and we should abort. Once
1279 * we have a record, we need to ensure it contains the inode number
1280 * we are looking up.
Dave Chinner7124fe02010-06-24 11:15:33 +10001281 */
1282 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
Dave Chinner4536f2a2010-08-24 11:42:30 +10001283 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
Dave Chinner7124fe02010-06-24 11:15:33 +10001284 if (!error) {
1285 if (i)
1286 error = xfs_inobt_get_rec(cur, &rec, &i);
1287 if (!error && i == 0)
1288 error = EINVAL;
1289 }
1290
1291 xfs_trans_brelse(tp, agbp);
1292 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1293 if (error)
1294 return error;
1295
Dave Chinner4536f2a2010-08-24 11:42:30 +10001296 /* check that the returned record contains the required inode */
1297 if (rec.ir_startino > agino ||
1298 rec.ir_startino + XFS_IALLOC_INODES(mp) <= agino)
1299 return EINVAL;
1300
Dave Chinner7124fe02010-06-24 11:15:33 +10001301 /* for untrusted inodes check it is allocated first */
Dave Chinner19207792010-06-24 11:15:47 +10001302 if ((flags & XFS_IGET_UNTRUSTED) &&
Dave Chinner7124fe02010-06-24 11:15:33 +10001303 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
1304 return EINVAL;
1305
1306 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1307 *offset_agbno = agbno - *chunk_agbno;
1308 return 0;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001312 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001315xfs_imap(
1316 xfs_mount_t *mp, /* file system mount structure */
1317 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001319 struct xfs_imap *imap, /* location map structure */
1320 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 xfs_agino_t agino; /* inode number within alloc group */
1324 xfs_agnumber_t agno; /* allocation group number */
1325 int blks_per_cluster; /* num blocks per inode cluster */
1326 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int offset; /* index of inode in its buffer */
1330 int offset_agbno; /* blks from chunk start to inode */
1331
1332 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
1335 * Split up the inode number into its parts.
1336 */
1337 agno = XFS_INO_TO_AGNO(mp, ino);
1338 agino = XFS_INO_TO_AGINO(mp, ino);
1339 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1340 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1341 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1342#ifdef DEBUG
Dave Chinner19207792010-06-24 11:15:47 +10001343 /*
1344 * Don't output diagnostic information for untrusted inodes
1345 * as they can be invalid without implying corruption.
1346 */
1347 if (flags & XFS_IGET_UNTRUSTED)
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001348 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner53487782011-03-07 10:05:35 +11001350 xfs_alert(mp,
1351 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1352 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner53487782011-03-07 10:05:35 +11001355 xfs_alert(mp,
1356 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1357 __func__, (unsigned long long)agbno,
1358 (unsigned long)mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner53487782011-03-07 10:05:35 +11001361 xfs_alert(mp,
1362 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1363 __func__, ino,
1364 XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001366 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367#endif /* DEBUG */
1368 return XFS_ERROR(EINVAL);
1369 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001370
Dave Chinner7124fe02010-06-24 11:15:33 +10001371 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1372
1373 /*
1374 * For bulkstat and handle lookups, we have an untrusted inode number
1375 * that we have to verify is valid. We cannot do this just by reading
1376 * the inode buffer as it may have been unlinked and removed leaving
1377 * inodes in stale state on disk. Hence we have to do a btree lookup
1378 * in all cases where an untrusted inode number is passed.
1379 */
Dave Chinner19207792010-06-24 11:15:47 +10001380 if (flags & XFS_IGET_UNTRUSTED) {
Dave Chinner7124fe02010-06-24 11:15:33 +10001381 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1382 &chunk_agbno, &offset_agbno, flags);
1383 if (error)
1384 return error;
1385 goto out_map;
1386 }
1387
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001388 /*
1389 * If the inode cluster size is the same as the blocksize or
1390 * smaller we get to the buffer by simple arithmetics.
1391 */
1392 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 offset = XFS_INO_TO_OFFSET(mp, ino);
1394 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001395
1396 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1397 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1398 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return 0;
1400 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001401
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001402 /*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001403 * If the inode chunks are aligned then use simple maths to
1404 * find the location. Otherwise we have to do a btree
1405 * lookup to find the location.
1406 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (mp->m_inoalign_mask) {
1408 offset_agbno = agbno & mp->m_inoalign_mask;
1409 chunk_agbno = agbno - offset_agbno;
1410 } else {
Dave Chinner7124fe02010-06-24 11:15:33 +10001411 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1412 &chunk_agbno, &offset_agbno, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (error)
1414 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001416
Dave Chinner7124fe02010-06-24 11:15:33 +10001417out_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 ASSERT(agbno >= chunk_agbno);
1419 cluster_agbno = chunk_agbno +
1420 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1421 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1422 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001423
1424 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1425 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1426 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1427
1428 /*
1429 * If the inode number maps to a block outside the bounds
1430 * of the file system then return NULL rather than calling
1431 * read_buf and panicing when we get an error from the
1432 * driver.
1433 */
1434 if ((imap->im_blkno + imap->im_len) >
1435 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
Dave Chinner53487782011-03-07 10:05:35 +11001436 xfs_alert(mp,
1437 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1438 __func__, (unsigned long long) imap->im_blkno,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001439 (unsigned long long) imap->im_len,
1440 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1441 return XFS_ERROR(EINVAL);
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
1446/*
1447 * Compute and fill in value of m_in_maxlevels.
1448 */
1449void
1450xfs_ialloc_compute_maxlevels(
1451 xfs_mount_t *mp) /* file system mount structure */
1452{
1453 int level;
1454 uint maxblocks;
1455 uint maxleafents;
1456 int minleafrecs;
1457 int minnoderecs;
1458
1459 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1460 XFS_INODES_PER_CHUNK_LOG;
1461 minleafrecs = mp->m_alloc_mnr[0];
1462 minnoderecs = mp->m_alloc_mnr[1];
1463 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1464 for (level = 1; maxblocks > 1; level++)
1465 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1466 mp->m_in_maxlevels = level;
1467}
1468
1469/*
1470 * Log specified fields for the ag hdr (inode section)
1471 */
1472void
1473xfs_ialloc_log_agi(
1474 xfs_trans_t *tp, /* transaction pointer */
1475 xfs_buf_t *bp, /* allocation group header buffer */
1476 int fields) /* bitmask of fields to log */
1477{
1478 int first; /* first byte number */
1479 int last; /* last byte number */
1480 static const short offsets[] = { /* field starting offsets */
1481 /* keep in sync with bit definitions */
1482 offsetof(xfs_agi_t, agi_magicnum),
1483 offsetof(xfs_agi_t, agi_versionnum),
1484 offsetof(xfs_agi_t, agi_seqno),
1485 offsetof(xfs_agi_t, agi_length),
1486 offsetof(xfs_agi_t, agi_count),
1487 offsetof(xfs_agi_t, agi_root),
1488 offsetof(xfs_agi_t, agi_level),
1489 offsetof(xfs_agi_t, agi_freecount),
1490 offsetof(xfs_agi_t, agi_newino),
1491 offsetof(xfs_agi_t, agi_dirino),
1492 offsetof(xfs_agi_t, agi_unlinked),
1493 sizeof(xfs_agi_t)
1494 };
1495#ifdef DEBUG
1496 xfs_agi_t *agi; /* allocation group header */
1497
1498 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001499 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500#endif
1501 /*
1502 * Compute byte offsets for the first and last fields.
1503 */
1504 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1505 /*
1506 * Log the allocation group inode header buffer.
1507 */
Dave Chinner61fe1352013-04-03 16:11:30 +11001508 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 xfs_trans_log_buf(tp, bp, first, last);
1510}
1511
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001512#ifdef DEBUG
1513STATIC void
1514xfs_check_agi_unlinked(
1515 struct xfs_agi *agi)
1516{
1517 int i;
1518
1519 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1520 ASSERT(agi->agi_unlinked[i]);
1521}
1522#else
1523#define xfs_check_agi_unlinked(agi)
1524#endif
1525
Dave Chinner983d09f2013-04-03 16:11:15 +11001526static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11001527xfs_agi_verify(
Dave Chinner3702ce62012-11-12 22:54:05 +11001528 struct xfs_buf *bp)
1529{
1530 struct xfs_mount *mp = bp->b_target->bt_mount;
1531 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
Dave Chinner3702ce62012-11-12 22:54:05 +11001532
Dave Chinner983d09f2013-04-03 16:11:15 +11001533 if (xfs_sb_version_hascrc(&mp->m_sb) &&
1534 !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
1535 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11001536 /*
1537 * Validate the magic number of the agi block.
1538 */
Dave Chinner983d09f2013-04-03 16:11:15 +11001539 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
1540 return false;
1541 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
1542 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11001543
1544 /*
1545 * during growfs operations, the perag is not fully initialised,
1546 * so we can't use it for any useful checking. growfs ensures we can't
1547 * use it by using uncached buffers that don't have the perag attached
1548 * so we can detect and avoid this problem.
1549 */
Dave Chinner983d09f2013-04-03 16:11:15 +11001550 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
1551 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11001552
Dave Chinner3702ce62012-11-12 22:54:05 +11001553 xfs_check_agi_unlinked(agi);
Dave Chinner983d09f2013-04-03 16:11:15 +11001554 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +11001555}
1556
Dave Chinner1813dd62012-11-14 17:54:40 +11001557static void
1558xfs_agi_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11001559 struct xfs_buf *bp)
1560{
Dave Chinner983d09f2013-04-03 16:11:15 +11001561 struct xfs_mount *mp = bp->b_target->bt_mount;
1562 int agi_ok = 1;
1563
1564 if (xfs_sb_version_hascrc(&mp->m_sb))
1565 agi_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
1566 offsetof(struct xfs_agi, agi_crc));
1567 agi_ok = agi_ok && xfs_agi_verify(bp);
1568
1569 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1570 XFS_RANDOM_IALLOC_READ_AGI))) {
1571 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1572 xfs_buf_ioerror(bp, EFSCORRUPTED);
1573 }
Dave Chinner612cfbf2012-11-14 17:52:32 +11001574}
1575
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001576static void
Dave Chinner1813dd62012-11-14 17:54:40 +11001577xfs_agi_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11001578 struct xfs_buf *bp)
1579{
Dave Chinner983d09f2013-04-03 16:11:15 +11001580 struct xfs_mount *mp = bp->b_target->bt_mount;
1581 struct xfs_buf_log_item *bip = bp->b_fspriv;
1582
1583 if (!xfs_agi_verify(bp)) {
1584 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1585 xfs_buf_ioerror(bp, EFSCORRUPTED);
1586 return;
1587 }
1588
1589 if (!xfs_sb_version_hascrc(&mp->m_sb))
1590 return;
1591
1592 if (bip)
1593 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
1594 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
1595 offsetof(struct xfs_agi, agi_crc));
Dave Chinner3702ce62012-11-12 22:54:05 +11001596}
1597
Dave Chinner1813dd62012-11-14 17:54:40 +11001598const struct xfs_buf_ops xfs_agi_buf_ops = {
1599 .verify_read = xfs_agi_read_verify,
1600 .verify_write = xfs_agi_write_verify,
1601};
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603/*
1604 * Read in the allocation group header (inode allocation section)
1605 */
1606int
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001607xfs_read_agi(
1608 struct xfs_mount *mp, /* file system mount structure */
1609 struct xfs_trans *tp, /* transaction pointer */
1610 xfs_agnumber_t agno, /* allocation group number */
1611 struct xfs_buf **bpp) /* allocation group hdr buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001613 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 ASSERT(agno != NULLAGNUMBER);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001616
1617 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11001619 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (error)
1621 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001622
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001623 ASSERT(!xfs_buf_geterror(*bpp));
Christoph Hellwig38f23232011-10-10 16:52:45 +00001624 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001625 return 0;
1626}
1627
1628int
1629xfs_ialloc_read_agi(
1630 struct xfs_mount *mp, /* file system mount structure */
1631 struct xfs_trans *tp, /* transaction pointer */
1632 xfs_agnumber_t agno, /* allocation group number */
1633 struct xfs_buf **bpp) /* allocation group hdr buf */
1634{
1635 struct xfs_agi *agi; /* allocation group header */
1636 struct xfs_perag *pag; /* per allocation group data */
1637 int error;
1638
1639 error = xfs_read_agi(mp, tp, agno, bpp);
1640 if (error)
1641 return error;
1642
1643 agi = XFS_BUF_TO_AGI(*bpp);
Dave Chinner44b56e02010-01-11 11:47:43 +00001644 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11001646 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10001647 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001651 /*
1652 * It's possible for these to be out of sync if
1653 * we are in the middle of a forced shutdown.
1654 */
1655 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1656 XFS_FORCED_SHUTDOWN(mp));
Dave Chinner44b56e02010-01-11 11:47:43 +00001657 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return 0;
1659}
David Chinner92821e22007-05-24 15:26:31 +10001660
1661/*
1662 * Read in the agi to initialise the per-ag data in the mount structure
1663 */
1664int
1665xfs_ialloc_pagi_init(
1666 xfs_mount_t *mp, /* file system mount structure */
1667 xfs_trans_t *tp, /* transaction pointer */
1668 xfs_agnumber_t agno) /* allocation group number */
1669{
1670 xfs_buf_t *bp = NULL;
1671 int error;
1672
1673 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1674 if (error)
1675 return error;
1676 if (bp)
1677 xfs_trans_brelse(tp, bp);
1678 return 0;
1679}