blob: d213a2eae95e0984ea5f4045c15f4dfd1e9d857f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
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_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_btree.h"
31#include "xfs_ialloc.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_rtalloc.h"
35#include "xfs_error.h"
36#include "xfs_bmap.h"
Dave Chinner983d09f2013-04-03 16:11:15 +110037#include "xfs_cksum.h"
Dave Chinner239880e2013-10-23 10:50:10 +110038#include "xfs_trans.h"
Dave Chinner983d09f2013-04-03 16:11:15 +110039#include "xfs_buf_item.h"
Dave Chinnerddf6ad02013-06-27 16:04:56 +100040#include "xfs_icreate_item.h"
Dave Chinner7bb85ef2013-08-12 20:49:34 +100041#include "xfs_icache.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110042#include "xfs_dinode.h"
Dave Chinnerd1230312013-11-01 15:27:19 +110043#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * Allocation group level functions.
48 */
David Chinner75de2a92008-03-27 18:00:38 +110049static inline int
50xfs_ialloc_cluster_alignment(
51 xfs_alloc_arg_t *args)
52{
53 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
54 args->mp->m_sb.sb_inoalignmt >=
Jie Liu0f49efd2013-12-13 15:51:48 +110055 XFS_B_TO_FSBT(args->mp, args->mp->m_inode_cluster_size))
David Chinner75de2a92008-03-27 18:00:38 +110056 return args->mp->m_sb.sb_inoalignmt;
57 return 1;
58}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
Christoph Hellwig21875502009-08-31 20:58:21 -030061 * Lookup a record by ino in the btree given by cur.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110062 */
Christoph Hellwig81e25172009-09-01 19:56:55 -040063int /* error */
Christoph Hellwig21875502009-08-31 20:58:21 -030064xfs_inobt_lookup(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110065 struct xfs_btree_cur *cur, /* btree cursor */
66 xfs_agino_t ino, /* starting inode of chunk */
Christoph Hellwig21875502009-08-31 20:58:21 -030067 xfs_lookup_t dir, /* <=, >=, == */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110068 int *stat) /* success/failure */
69{
70 cur->bc_rec.i.ir_startino = ino;
Christoph Hellwig21875502009-08-31 20:58:21 -030071 cur->bc_rec.i.ir_freecount = 0;
72 cur->bc_rec.i.ir_free = 0;
73 return xfs_btree_lookup(cur, dir, stat);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +110074}
75
76/*
Christoph Hellwigafabc242009-08-31 20:57:03 -030077 * Update the record referred to by cur to the value given.
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110078 * This either works (return 0) or gets an EFSCORRUPTED error.
79 */
80STATIC int /* error */
81xfs_inobt_update(
82 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigafabc242009-08-31 20:57:03 -030083 xfs_inobt_rec_incore_t *irec) /* btree record */
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110084{
85 union xfs_btree_rec rec;
86
Christoph Hellwigafabc242009-08-31 20:57:03 -030087 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
88 rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
89 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +110090 return xfs_btree_update(cur, &rec);
91}
92
93/*
Christoph Hellwig8cc938f2008-10-30 16:58:11 +110094 * Get the data from the pointed-to record.
95 */
96int /* error */
97xfs_inobt_get_rec(
98 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig2e287a72009-08-31 20:56:58 -030099 xfs_inobt_rec_incore_t *irec, /* btree record */
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100100 int *stat) /* output: success/failure */
101{
102 union xfs_btree_rec *rec;
103 int error;
104
105 error = xfs_btree_get_rec(cur, &rec, stat);
106 if (!error && *stat == 1) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300107 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
108 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
109 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
Christoph Hellwig8cc938f2008-10-30 16:58:11 +1100110 }
111 return error;
112}
113
114/*
Brian Foster0aa0a752014-04-24 16:00:53 +1000115 * Insert a single inobt record. Cursor must already point to desired location.
116 */
117STATIC int
118xfs_inobt_insert_rec(
119 struct xfs_btree_cur *cur,
120 __int32_t freecount,
121 xfs_inofree_t free,
122 int *stat)
123{
124 cur->bc_rec.i.ir_freecount = freecount;
125 cur->bc_rec.i.ir_free = free;
126 return xfs_btree_insert(cur, stat);
127}
128
129/*
130 * Insert records describing a newly allocated inode chunk into the inobt.
131 */
132STATIC int
133xfs_inobt_insert(
134 struct xfs_mount *mp,
135 struct xfs_trans *tp,
136 struct xfs_buf *agbp,
137 xfs_agino_t newino,
138 xfs_agino_t newlen,
139 xfs_btnum_t btnum)
140{
141 struct xfs_btree_cur *cur;
142 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
143 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
144 xfs_agino_t thisino;
145 int i;
146 int error;
147
148 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
149
150 for (thisino = newino;
151 thisino < newino + newlen;
152 thisino += XFS_INODES_PER_CHUNK) {
153 error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
154 if (error) {
155 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
156 return error;
157 }
158 ASSERT(i == 0);
159
160 error = xfs_inobt_insert_rec(cur, XFS_INODES_PER_CHUNK,
161 XFS_INOBT_ALL_FREE, &i);
162 if (error) {
163 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
164 return error;
165 }
166 ASSERT(i == 1);
167 }
168
169 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
170
171 return 0;
172}
173
174/*
Dave Chinner0b48db82009-08-31 20:57:09 -0300175 * Verify that the number of free inodes in the AGI is correct.
176 */
177#ifdef DEBUG
178STATIC int
179xfs_check_agi_freecount(
180 struct xfs_btree_cur *cur,
181 struct xfs_agi *agi)
182{
183 if (cur->bc_nlevels == 1) {
184 xfs_inobt_rec_incore_t rec;
185 int freecount = 0;
186 int error;
187 int i;
188
Christoph Hellwig21875502009-08-31 20:58:21 -0300189 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
Dave Chinner0b48db82009-08-31 20:57:09 -0300190 if (error)
191 return error;
192
193 do {
194 error = xfs_inobt_get_rec(cur, &rec, &i);
195 if (error)
196 return error;
197
198 if (i) {
199 freecount += rec.ir_freecount;
200 error = xfs_btree_increment(cur, 0, &i);
201 if (error)
202 return error;
203 }
204 } while (i == 1);
205
206 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
207 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
208 }
209 return 0;
210}
211#else
212#define xfs_check_agi_freecount(cur, agi) 0
213#endif
214
215/*
Dave Chinner28c8e412013-06-27 16:04:55 +1000216 * Initialise a new set of inodes. When called without a transaction context
217 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
218 * than logging them (which in a transaction context puts them into the AIL
219 * for writeback rather than the xfsbufd queue).
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300220 */
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000221int
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300222xfs_ialloc_inode_init(
223 struct xfs_mount *mp,
224 struct xfs_trans *tp,
Dave Chinner28c8e412013-06-27 16:04:55 +1000225 struct list_head *buffer_list,
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300226 xfs_agnumber_t agno,
227 xfs_agblock_t agbno,
228 xfs_agblock_t length,
229 unsigned int gen)
230{
231 struct xfs_buf *fbuf;
232 struct xfs_dinode *free;
Jie Liu6e0c7b82013-12-13 15:51:49 +1100233 int nbufs, blks_per_cluster, inodes_per_cluster;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300234 int version;
235 int i, j;
236 xfs_daddr_t d;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100237 xfs_ino_t ino = 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300238
239 /*
Jie Liu6e0c7b82013-12-13 15:51:49 +1100240 * Loop over the new block(s), filling in the inodes. For small block
241 * sizes, manipulate the inodes in buffers which are multiples of the
242 * blocks size.
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300243 */
Jie Liu6e0c7b82013-12-13 15:51:49 +1100244 blks_per_cluster = xfs_icluster_size_fsb(mp);
245 inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
246 nbufs = length / blks_per_cluster;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300247
248 /*
Christoph Hellwig93848a92013-04-03 16:11:17 +1100249 * Figure out what version number to use in the inodes we create. If
250 * the superblock version has caught up to the one that supports the new
251 * inode format, then use the new inode version. Otherwise use the old
252 * version so that old kernels will continue to be able to use the file
253 * system.
254 *
255 * For v3 inodes, we also need to write the inode number into the inode,
256 * so calculate the first inode number of the chunk here as
257 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
258 * across multiple filesystem blocks (such as a cluster) and so cannot
259 * be used in the cluster buffer loop below.
260 *
261 * Further, because we are writing the inode directly into the buffer
262 * and calculating a CRC on the entire inode, we have ot log the entire
263 * inode so that the entire range the CRC covers is present in the log.
264 * That means for v3 inode we log the entire buffer rather than just the
265 * inode cores.
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300266 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100267 if (xfs_sb_version_hascrc(&mp->m_sb)) {
268 version = 3;
269 ino = XFS_AGINO_TO_INO(mp, agno,
270 XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000271
272 /*
273 * log the initialisation that is about to take place as an
274 * logical operation. This means the transaction does not
275 * need to log the physical changes to the inode buffers as log
276 * recovery will know what initialisation is actually needed.
277 * Hence we only need to log the buffers as "ordered" buffers so
278 * they track in the AIL as if they were physically logged.
279 */
280 if (tp)
Jie Liu71783432013-12-13 15:51:46 +1100281 xfs_icreate_log(tp, agno, agbno, mp->m_ialloc_inos,
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000282 mp->m_sb.sb_inodesize, length, gen);
Dave Chinner263997a2014-05-20 07:46:40 +1000283 } else
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300284 version = 2;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300285
286 for (j = 0; j < nbufs; j++) {
287 /*
288 * Get the block.
289 */
290 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
291 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
Dave Chinner7c4cebe2012-11-23 14:24:23 +1100292 mp->m_bsize * blks_per_cluster,
293 XBF_UNMAPPED);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000294 if (!fbuf)
Dave Chinner24513372014-06-25 14:58:08 +1000295 return -ENOMEM;
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000296
297 /* Initialize the inode buffers and log them appropriately. */
Dave Chinner1813dd62012-11-14 17:54:40 +1100298 fbuf->b_ops = &xfs_inode_buf_ops;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100299 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
Jie Liu6e0c7b82013-12-13 15:51:49 +1100300 for (i = 0; i < inodes_per_cluster; i++) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300301 int ioffset = i << mp->m_sb.sb_inodelog;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100302 uint isize = xfs_dinode_size(version);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300303
304 free = xfs_make_iptr(mp, fbuf, i);
305 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
306 free->di_version = version;
307 free->di_gen = cpu_to_be32(gen);
308 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100309
310 if (version == 3) {
311 free->di_ino = cpu_to_be64(ino);
312 ino++;
313 uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
314 xfs_dinode_calc_crc(mp, free);
Dave Chinner28c8e412013-06-27 16:04:55 +1000315 } else if (tp) {
Christoph Hellwig93848a92013-04-03 16:11:17 +1100316 /* just log the inode core */
317 xfs_trans_log_buf(tp, fbuf, ioffset,
318 ioffset + isize - 1);
319 }
320 }
Dave Chinner28c8e412013-06-27 16:04:55 +1000321
322 if (tp) {
323 /*
324 * Mark the buffer as an inode allocation buffer so it
325 * sticks in AIL at the point of this allocation
326 * transaction. This ensures the they are on disk before
327 * the tail of the log can be moved past this
328 * transaction (i.e. by preventing relogging from moving
329 * it forward in the log).
330 */
331 xfs_trans_inode_alloc_buf(tp, fbuf);
332 if (version == 3) {
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000333 /*
334 * Mark the buffer as ordered so that they are
335 * not physically logged in the transaction but
336 * still tracked in the AIL as part of the
337 * transaction and pin the log appropriately.
338 */
339 xfs_trans_ordered_buf(tp, fbuf);
Dave Chinner28c8e412013-06-27 16:04:55 +1000340 xfs_trans_log_buf(tp, fbuf, 0,
341 BBTOB(fbuf->b_length) - 1);
342 }
343 } else {
344 fbuf->b_flags |= XBF_DONE;
345 xfs_buf_delwri_queue(fbuf, buffer_list);
346 xfs_buf_relse(fbuf);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300347 }
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300348 }
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000349 return 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300350}
351
352/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * Allocate new inodes in the allocation group specified by agbp.
354 * Return 0 for success, else error code.
355 */
356STATIC int /* error code or 0 */
357xfs_ialloc_ag_alloc(
358 xfs_trans_t *tp, /* transaction pointer */
359 xfs_buf_t *agbp, /* alloc group buffer */
360 int *alloc)
361{
362 xfs_agi_t *agi; /* allocation group header */
363 xfs_alloc_arg_t args; /* allocation argument structure */
David Chinner92821e22007-05-24 15:26:31 +1000364 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 xfs_agino_t newino; /* new first inode's number */
367 xfs_agino_t newlen; /* new number of inodes */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000368 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* boundary */
Dave Chinner44b56e02010-01-11 11:47:43 +0000370 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Mark Tinguelya0041682012-09-20 13:16:45 -0500372 memset(&args, 0, sizeof(args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 args.tp = tp;
374 args.mp = tp->t_mountp;
375
376 /*
377 * Locking will ensure that we don't have two callers in here
378 * at one time.
379 */
Jie Liu71783432013-12-13 15:51:46 +1100380 newlen = args.mp->m_ialloc_inos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (args.mp->m_maxicount &&
382 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
Dave Chinner24513372014-06-25 14:58:08 +1000383 return -ENOSPC;
Jie Liu126cd102013-12-13 15:51:48 +1100384 args.minlen = args.maxlen = args.mp->m_ialloc_blks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000386 * First try to allocate inodes contiguous with the last-allocated
387 * chunk of inodes. If the filesystem is striped, this will fill
388 * an entire stripe unit with inodes.
Dave Chinner28c8e412013-06-27 16:04:55 +1000389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000391 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300392 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000393 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
Jie Liu126cd102013-12-13 15:51:48 +1100394 args.mp->m_ialloc_blks;
Nathan Scott019ff2d2006-04-11 15:45:05 +1000395 if (likely(newino != NULLAGINO &&
396 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300397 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000398 args.type = XFS_ALLOCTYPE_THIS_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000399 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100400
Glen Overby3ccb8b52006-03-29 09:52:28 +1000401 /*
David Chinner75de2a92008-03-27 18:00:38 +1100402 * We need to take into account alignment here to ensure that
403 * we don't modify the free list if we fail to have an exact
404 * block. If we don't have an exact match, and every oher
405 * attempt allocation attempt fails, we'll end up cancelling
406 * a dirty transaction and shutting down.
407 *
408 * For an exact allocation, alignment must be 1,
409 * however we need to take cluster alignment into account when
410 * fixing up the freelist. Use the minalignslop field to
411 * indicate that extra blocks might be required for alignment,
412 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000413 */
David Chinner75de2a92008-03-27 18:00:38 +1100414 args.alignment = 1;
415 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
416
417 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100418 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000419 if ((error = xfs_alloc_vextent(&args)))
420 return error;
Brian Fostere480a722014-03-07 16:19:14 +1100421
422 /*
423 * This request might have dirtied the transaction if the AG can
424 * satisfy the request, but the exact block was not available.
425 * If the allocation did fail, subsequent requests will relax
426 * the exact agbno requirement and increase the alignment
427 * instead. It is critical that the total size of the request
428 * (len + alignment + slop) does not increase from this point
429 * on, so reset minalignslop to ensure it is not included in
430 * subsequent requests.
431 */
432 args.minalignslop = 0;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000433 } else
434 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Glen Overby3ccb8b52006-03-29 09:52:28 +1000436 if (unlikely(args.fsbno == NULLFSBLOCK)) {
437 /*
438 * Set the alignment for the allocation.
439 * If stripe alignment is turned on then align at stripe unit
440 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000441 * If the cluster size is smaller than a filesystem block
442 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000443 * pieces, so don't need alignment anyway.
444 */
445 isaligned = 0;
446 if (args.mp->m_sinoalign) {
447 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
448 args.alignment = args.mp->m_dalign;
449 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100450 } else
451 args.alignment = xfs_ialloc_cluster_alignment(&args);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000452 /*
453 * Need to figure out where to allocate the inode blocks.
454 * Ideally they should be spaced out through the a.g.
455 * For now, just allocate blocks up front.
456 */
457 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300458 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000459 /*
460 * Allocate a fixed-size extent of inodes.
461 */
462 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000463 args.prod = 1;
464 /*
465 * Allow space for the inode btree to split.
466 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100467 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000468 if ((error = xfs_alloc_vextent(&args)))
469 return error;
470 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 * If stripe alignment is turned on, then try again with cluster
474 * alignment.
475 */
476 if (isaligned && args.fsbno == NULLFSBLOCK) {
477 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100478 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300479 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
David Chinner75de2a92008-03-27 18:00:38 +1100480 args.alignment = xfs_ialloc_cluster_alignment(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if ((error = xfs_alloc_vextent(&args)))
482 return error;
483 }
484
485 if (args.fsbno == NULLFSBLOCK) {
486 *alloc = 0;
487 return 0;
488 }
489 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
David Chinner359346a2008-04-29 12:53:32 +1000491 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300492 * Stamp and write the inode buffers.
493 *
David Chinner359346a2008-04-29 12:53:32 +1000494 * Seed the new inode cluster with a random generation number. This
495 * prevents short-term reuse of generation numbers if a chunk is
496 * freed and then immediately reallocated. We use random numbers
497 * rather than a linear progression to prevent the next generation
498 * number from being easily guessable.
499 */
Dave Chinner28c8e412013-06-27 16:04:55 +1000500 error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
Akinobu Mitaecb34032013-03-04 21:58:20 +0900501 args.len, prandom_u32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100502
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000503 if (error)
504 return error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300505 /*
506 * Convert the results.
507 */
508 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800509 be32_add_cpu(&agi->agi_count, newlen);
510 be32_add_cpu(&agi->agi_freecount, newlen);
Dave Chinner44b56e02010-01-11 11:47:43 +0000511 pag = xfs_perag_get(args.mp, agno);
512 pag->pagi_freecount += newlen;
513 xfs_perag_put(pag);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100514 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
Brian Foster0aa0a752014-04-24 16:00:53 +1000517 * Insert records describing the new inode chunk into the btrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
Brian Foster0aa0a752014-04-24 16:00:53 +1000519 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
520 XFS_BTNUM_INO);
521 if (error)
522 return error;
523
524 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
525 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
526 XFS_BTNUM_FINO);
527 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * Log allocation group header fields
532 */
533 xfs_ialloc_log_agi(tp, agbp,
534 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
535 /*
536 * Modify/log superblock values for inode count and inode free count.
537 */
538 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
539 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
540 *alloc = 1;
541 return 0;
542}
543
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000544STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545xfs_ialloc_next_ag(
546 xfs_mount_t *mp)
547{
548 xfs_agnumber_t agno;
549
550 spin_lock(&mp->m_agirotor_lock);
551 agno = mp->m_agirotor;
Carlos Maiolino8aea3ff2012-09-20 10:32:36 -0300552 if (++mp->m_agirotor >= mp->m_maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 mp->m_agirotor = 0;
554 spin_unlock(&mp->m_agirotor_lock);
555
556 return agno;
557}
558
559/*
560 * Select an allocation group to look for a free inode in, based on the parent
Zhi Yong Wu2f21ff12013-08-12 03:14:58 +0000561 * inode and the mode. Return the allocation group buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400563STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564xfs_ialloc_ag_select(
565 xfs_trans_t *tp, /* transaction pointer */
566 xfs_ino_t parent, /* parent directory inode number */
Al Viro576b1d62011-07-26 02:50:15 -0400567 umode_t mode, /* bits set to indicate file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int okalloc) /* ok to allocate more space */
569{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
571 xfs_agnumber_t agno; /* current ag number */
572 int flags; /* alloc buffer locking flags */
573 xfs_extlen_t ineed; /* blocks needed for inode allocation */
574 xfs_extlen_t longest = 0; /* longest extent available */
575 xfs_mount_t *mp; /* mount point structure */
576 int needspace; /* file mode implies space allocated */
577 xfs_perag_t *pag; /* per allocation group data */
578 xfs_agnumber_t pagno; /* parent (starting) ag number */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400579 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /*
582 * Files of these types need at least one block if length > 0
583 * (and they won't fit in the inode, but that's hard to figure out).
584 */
585 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
586 mp = tp->t_mountp;
587 agcount = mp->m_maxagi;
588 if (S_ISDIR(mode))
589 pagno = xfs_ialloc_next_ag(mp);
590 else {
591 pagno = XFS_INO_TO_AGNO(mp, parent);
592 if (pagno >= agcount)
593 pagno = 0;
594 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ASSERT(pagno < agcount);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * Loop through allocation groups, looking for one with a little
600 * free space in it. Note we don't look for free inodes, exactly.
601 * Instead, we include whether there is a need to allocate inodes
602 * to mean that blocks must be allocated for them,
603 * if none are currently free.
604 */
605 agno = pagno;
606 flags = XFS_ALLOC_FLAG_TRYLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 for (;;) {
Dave Chinner44b56e02010-01-11 11:47:43 +0000608 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!pag->pagi_inodeok) {
610 xfs_ialloc_next_ag(mp);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400611 goto nextag;
612 }
613
614 if (!pag->pagi_init) {
615 error = xfs_ialloc_pagi_init(mp, tp, agno);
616 if (error)
617 goto nextag;
618 }
619
620 if (pag->pagi_freecount) {
621 xfs_perag_put(pag);
622 return agno;
623 }
624
625 if (!okalloc)
626 goto nextag;
627
628 if (!pag->pagf_init) {
629 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
630 if (error)
631 goto nextag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 /*
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400635 * Is there enough free space for the file plus a block of
636 * inodes? (if we need to allocate some)?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Jie Liu126cd102013-12-13 15:51:48 +1100638 ineed = mp->m_ialloc_blks;
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400639 longest = pag->pagf_longest;
640 if (!longest)
641 longest = pag->pagf_flcount > 0;
642
643 if (pag->pagf_freeblks >= needspace + ineed &&
644 longest >= ineed) {
645 xfs_perag_put(pag);
646 return agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648nextag:
Dave Chinner44b56e02010-01-11 11:47:43 +0000649 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /*
651 * No point in iterating over the rest, if we're shutting
652 * down.
653 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000654 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400655 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 agno++;
657 if (agno >= agcount)
658 agno = 0;
659 if (agno == pagno) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000660 if (flags == 0)
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400661 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 flags = 0;
663 }
664 }
665}
666
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300667/*
668 * Try to retrieve the next record to the left/right from the current one.
669 */
670STATIC int
671xfs_ialloc_next_rec(
672 struct xfs_btree_cur *cur,
673 xfs_inobt_rec_incore_t *rec,
674 int *done,
675 int left)
676{
677 int error;
678 int i;
679
680 if (left)
681 error = xfs_btree_decrement(cur, 0, &i);
682 else
683 error = xfs_btree_increment(cur, 0, &i);
684
685 if (error)
686 return error;
687 *done = !i;
688 if (i) {
689 error = xfs_inobt_get_rec(cur, rec, &i);
690 if (error)
691 return error;
692 XFS_WANT_CORRUPTED_RETURN(i == 1);
693 }
694
695 return 0;
696}
697
Dave Chinnerbd169562009-08-31 20:58:28 -0300698STATIC int
699xfs_ialloc_get_rec(
700 struct xfs_btree_cur *cur,
701 xfs_agino_t agino,
702 xfs_inobt_rec_incore_t *rec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800703 int *done)
Dave Chinnerbd169562009-08-31 20:58:28 -0300704{
705 int error;
706 int i;
707
708 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
709 if (error)
710 return error;
711 *done = !i;
712 if (i) {
713 error = xfs_inobt_get_rec(cur, rec, &i);
714 if (error)
715 return error;
716 XFS_WANT_CORRUPTED_RETURN(i == 1);
717 }
718
719 return 0;
720}
Dave Chinner0b48db82009-08-31 20:57:09 -0300721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
Brian Foster6dd86382014-04-24 16:00:53 +1000723 * Allocate an inode using the inobt-only algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400725STATIC int
Brian Foster6dd86382014-04-24 16:00:53 +1000726xfs_dialloc_ag_inobt(
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400727 struct xfs_trans *tp,
728 struct xfs_buf *agbp,
729 xfs_ino_t parent,
730 xfs_ino_t *inop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400732 struct xfs_mount *mp = tp->t_mountp;
733 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
734 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
735 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
736 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
737 struct xfs_perag *pag;
738 struct xfs_btree_cur *cur, *tcur;
739 struct xfs_inobt_rec_incore rec, trec;
740 xfs_ino_t ino;
741 int error;
742 int offset;
743 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dave Chinner44b56e02010-01-11 11:47:43 +0000745 pag = xfs_perag_get(mp, agno);
Dave Chinnerbd169562009-08-31 20:58:28 -0300746
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400747 ASSERT(pag->pagi_init);
748 ASSERT(pag->pagi_inodeok);
749 ASSERT(pag->pagi_freecount > 0);
750
Dave Chinnerbd169562009-08-31 20:58:28 -0300751 restart_pagno:
Brian Foster57bd3db2014-04-24 16:00:50 +1000752 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /*
754 * If pagino is 0 (this is the root inode allocation) use newino.
755 * This must work because we've just allocated some.
756 */
757 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100758 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Dave Chinner0b48db82009-08-31 20:57:09 -0300760 error = xfs_check_agi_freecount(cur, agi);
761 if (error)
762 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300765 * If in the same AG as the parent, try to get near the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
767 if (pagno == agno) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300768 int doneleft; /* done, to the left */
769 int doneright; /* done, to the right */
Dave Chinnerbd169562009-08-31 20:58:28 -0300770 int searchdistance = 10;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300771
Christoph Hellwig21875502009-08-31 20:58:21 -0300772 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300773 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300775 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
776
777 error = xfs_inobt_get_rec(cur, &rec, &j);
778 if (error)
779 goto error0;
Brian Fosterb1210992013-08-27 17:15:45 -0400780 XFS_WANT_CORRUPTED_GOTO(j == 1, error0);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300781
782 if (rec.ir_freecount > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /*
784 * Found a free inode in the same chunk
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300785 * as the parent, done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300787 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300789
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300792 * In the same AG as parent, but parent's chunk is full.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300795 /* duplicate the cursor, search left & right simultaneously */
796 error = xfs_btree_dup_cursor(cur, &tcur);
797 if (error)
798 goto error0;
799
Dave Chinnerbd169562009-08-31 20:58:28 -0300800 /*
801 * Skip to last blocks looked up if same parent inode.
802 */
803 if (pagino != NULLAGINO &&
804 pag->pagl_pagino == pagino &&
805 pag->pagl_leftrec != NULLAGINO &&
806 pag->pagl_rightrec != NULLAGINO) {
807 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800808 &trec, &doneleft);
Dave Chinnerbd169562009-08-31 20:58:28 -0300809 if (error)
810 goto error1;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300811
Dave Chinnerbd169562009-08-31 20:58:28 -0300812 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800813 &rec, &doneright);
Dave Chinnerbd169562009-08-31 20:58:28 -0300814 if (error)
815 goto error1;
816 } else {
817 /* search left with tcur, back up 1 record */
818 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
819 if (error)
820 goto error1;
821
822 /* search right with cur, go forward 1 record. */
823 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
824 if (error)
825 goto error1;
826 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300827
828 /*
829 * Loop until we find an inode chunk with a free inode.
830 */
831 while (!doneleft || !doneright) {
832 int useleft; /* using left inode chunk this time */
833
Dave Chinnerbd169562009-08-31 20:58:28 -0300834 if (!--searchdistance) {
835 /*
836 * Not in range - save last search
837 * location and allocate a new inode
838 */
Eric Sandeen3b826382009-10-30 09:27:07 +0100839 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300840 pag->pagl_leftrec = trec.ir_startino;
841 pag->pagl_rightrec = rec.ir_startino;
842 pag->pagl_pagino = pagino;
843 goto newino;
844 }
845
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300846 /* figure out the closer block if both are valid. */
847 if (!doneleft && !doneright) {
848 useleft = pagino -
849 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
850 rec.ir_startino - pagino;
851 } else {
852 useleft = !doneleft;
853 }
854
855 /* free inodes to the left? */
856 if (useleft && trec.ir_freecount) {
857 rec = trec;
858 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
859 cur = tcur;
Dave Chinnerbd169562009-08-31 20:58:28 -0300860
861 pag->pagl_leftrec = trec.ir_startino;
862 pag->pagl_rightrec = rec.ir_startino;
863 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300864 goto alloc_inode;
865 }
866
867 /* free inodes to the right? */
868 if (!useleft && rec.ir_freecount) {
869 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300870
871 pag->pagl_leftrec = trec.ir_startino;
872 pag->pagl_rightrec = rec.ir_startino;
873 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300874 goto alloc_inode;
875 }
876
877 /* get next record to check */
878 if (useleft) {
879 error = xfs_ialloc_next_rec(tcur, &trec,
880 &doneleft, 1);
881 } else {
882 error = xfs_ialloc_next_rec(cur, &rec,
883 &doneright, 0);
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300888
889 /*
890 * We've reached the end of the btree. because
891 * we are only searching a small chunk of the
892 * btree each search, there is obviously free
893 * inodes closer to the parent inode than we
894 * are now. restart the search again.
895 */
896 pag->pagl_pagino = NULLAGINO;
897 pag->pagl_leftrec = NULLAGINO;
898 pag->pagl_rightrec = NULLAGINO;
899 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
900 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
901 goto restart_pagno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300905 * In a different AG from the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * See if the most recently allocated block has any free.
907 */
Dave Chinnerbd169562009-08-31 20:58:28 -0300908newino:
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200909 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
Christoph Hellwig21875502009-08-31 20:58:21 -0300910 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
911 XFS_LOOKUP_EQ, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300912 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300914
915 if (i == 1) {
916 error = xfs_inobt_get_rec(cur, &rec, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (error)
918 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300919
920 if (j == 1 && rec.ir_freecount > 0) {
921 /*
922 * The last chunk allocated in the group
923 * still has a free inode.
924 */
925 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300928 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300929
Dave Chinnerbd169562009-08-31 20:58:28 -0300930 /*
931 * None left in the last group, search the whole AG
932 */
933 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
934 if (error)
935 goto error0;
936 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
937
938 for (;;) {
939 error = xfs_inobt_get_rec(cur, &rec, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300940 if (error)
941 goto error0;
942 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Dave Chinnerbd169562009-08-31 20:58:28 -0300943 if (rec.ir_freecount > 0)
944 break;
945 error = xfs_btree_increment(cur, 0, &i);
946 if (error)
947 goto error0;
948 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300950
951alloc_inode:
Christoph Hellwig824c3132012-07-04 10:54:45 -0400952 offset = xfs_lowbit64(rec.ir_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 ASSERT(offset >= 0);
954 ASSERT(offset < XFS_INODES_PER_CHUNK);
955 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
956 XFS_INODES_PER_CHUNK) == 0);
957 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100958 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 rec.ir_freecount--;
Christoph Hellwigafabc242009-08-31 20:57:03 -0300960 error = xfs_inobt_update(cur, &rec);
961 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800963 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +0000965 pag->pagi_freecount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Dave Chinner0b48db82009-08-31 20:57:09 -0300967 error = xfs_check_agi_freecount(cur, agi);
968 if (error)
969 goto error0;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
972 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
Dave Chinner44b56e02010-01-11 11:47:43 +0000973 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 *inop = ino;
975 return 0;
976error1:
977 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
978error0:
979 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
Dave Chinner44b56e02010-01-11 11:47:43 +0000980 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return error;
982}
983
984/*
Brian Foster6dd86382014-04-24 16:00:53 +1000985 * Use the free inode btree to allocate an inode based on distance from the
986 * parent. Note that the provided cursor may be deleted and replaced.
987 */
988STATIC int
989xfs_dialloc_ag_finobt_near(
990 xfs_agino_t pagino,
991 struct xfs_btree_cur **ocur,
992 struct xfs_inobt_rec_incore *rec)
993{
994 struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
995 struct xfs_btree_cur *rcur; /* right search cursor */
996 struct xfs_inobt_rec_incore rrec;
997 int error;
998 int i, j;
999
1000 error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
1001 if (error)
1002 return error;
1003
1004 if (i == 1) {
1005 error = xfs_inobt_get_rec(lcur, rec, &i);
1006 if (error)
1007 return error;
1008 XFS_WANT_CORRUPTED_RETURN(i == 1);
1009
1010 /*
1011 * See if we've landed in the parent inode record. The finobt
1012 * only tracks chunks with at least one free inode, so record
1013 * existence is enough.
1014 */
1015 if (pagino >= rec->ir_startino &&
1016 pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
1017 return 0;
1018 }
1019
1020 error = xfs_btree_dup_cursor(lcur, &rcur);
1021 if (error)
1022 return error;
1023
1024 error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
1025 if (error)
1026 goto error_rcur;
1027 if (j == 1) {
1028 error = xfs_inobt_get_rec(rcur, &rrec, &j);
1029 if (error)
1030 goto error_rcur;
1031 XFS_WANT_CORRUPTED_GOTO(j == 1, error_rcur);
1032 }
1033
1034 XFS_WANT_CORRUPTED_GOTO(i == 1 || j == 1, error_rcur);
1035 if (i == 1 && j == 1) {
1036 /*
1037 * Both the left and right records are valid. Choose the closer
1038 * inode chunk to the target.
1039 */
1040 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
1041 (rrec.ir_startino - pagino)) {
1042 *rec = rrec;
1043 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1044 *ocur = rcur;
1045 } else {
1046 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1047 }
1048 } else if (j == 1) {
1049 /* only the right record is valid */
1050 *rec = rrec;
1051 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1052 *ocur = rcur;
1053 } else if (i == 1) {
1054 /* only the left record is valid */
1055 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1056 }
1057
1058 return 0;
1059
1060error_rcur:
1061 xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
1062 return error;
1063}
1064
1065/*
1066 * Use the free inode btree to find a free inode based on a newino hint. If
1067 * the hint is NULL, find the first free inode in the AG.
1068 */
1069STATIC int
1070xfs_dialloc_ag_finobt_newino(
1071 struct xfs_agi *agi,
1072 struct xfs_btree_cur *cur,
1073 struct xfs_inobt_rec_incore *rec)
1074{
1075 int error;
1076 int i;
1077
1078 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1079 error = xfs_inobt_lookup(cur, agi->agi_newino, XFS_LOOKUP_EQ,
1080 &i);
1081 if (error)
1082 return error;
1083 if (i == 1) {
1084 error = xfs_inobt_get_rec(cur, rec, &i);
1085 if (error)
1086 return error;
1087 XFS_WANT_CORRUPTED_RETURN(i == 1);
1088
1089 return 0;
1090 }
1091 }
1092
1093 /*
1094 * Find the first inode available in the AG.
1095 */
1096 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1097 if (error)
1098 return error;
1099 XFS_WANT_CORRUPTED_RETURN(i == 1);
1100
1101 error = xfs_inobt_get_rec(cur, rec, &i);
1102 if (error)
1103 return error;
1104 XFS_WANT_CORRUPTED_RETURN(i == 1);
1105
1106 return 0;
1107}
1108
1109/*
1110 * Update the inobt based on a modification made to the finobt. Also ensure that
1111 * the records from both trees are equivalent post-modification.
1112 */
1113STATIC int
1114xfs_dialloc_ag_update_inobt(
1115 struct xfs_btree_cur *cur, /* inobt cursor */
1116 struct xfs_inobt_rec_incore *frec, /* finobt record */
1117 int offset) /* inode offset */
1118{
1119 struct xfs_inobt_rec_incore rec;
1120 int error;
1121 int i;
1122
1123 error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
1124 if (error)
1125 return error;
1126 XFS_WANT_CORRUPTED_RETURN(i == 1);
1127
1128 error = xfs_inobt_get_rec(cur, &rec, &i);
1129 if (error)
1130 return error;
1131 XFS_WANT_CORRUPTED_RETURN(i == 1);
1132 ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
1133 XFS_INODES_PER_CHUNK) == 0);
1134
1135 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1136 rec.ir_freecount--;
1137
1138 XFS_WANT_CORRUPTED_RETURN((rec.ir_free == frec->ir_free) &&
1139 (rec.ir_freecount == frec->ir_freecount));
1140
1141 error = xfs_inobt_update(cur, &rec);
1142 if (error)
1143 return error;
1144
1145 return 0;
1146}
1147
1148/*
1149 * Allocate an inode using the free inode btree, if available. Otherwise, fall
1150 * back to the inobt search algorithm.
1151 *
1152 * The caller selected an AG for us, and made sure that free inodes are
1153 * available.
1154 */
1155STATIC int
1156xfs_dialloc_ag(
1157 struct xfs_trans *tp,
1158 struct xfs_buf *agbp,
1159 xfs_ino_t parent,
1160 xfs_ino_t *inop)
1161{
1162 struct xfs_mount *mp = tp->t_mountp;
1163 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1164 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1165 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1166 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1167 struct xfs_perag *pag;
1168 struct xfs_btree_cur *cur; /* finobt cursor */
1169 struct xfs_btree_cur *icur; /* inobt cursor */
1170 struct xfs_inobt_rec_incore rec;
1171 xfs_ino_t ino;
1172 int error;
1173 int offset;
1174 int i;
1175
1176 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
1177 return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
1178
1179 pag = xfs_perag_get(mp, agno);
1180
1181 /*
1182 * If pagino is 0 (this is the root inode allocation) use newino.
1183 * This must work because we've just allocated some.
1184 */
1185 if (!pagino)
1186 pagino = be32_to_cpu(agi->agi_newino);
1187
1188 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1189
1190 error = xfs_check_agi_freecount(cur, agi);
1191 if (error)
1192 goto error_cur;
1193
1194 /*
1195 * The search algorithm depends on whether we're in the same AG as the
1196 * parent. If so, find the closest available inode to the parent. If
1197 * not, consider the agi hint or find the first free inode in the AG.
1198 */
1199 if (agno == pagno)
1200 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
1201 else
1202 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
1203 if (error)
1204 goto error_cur;
1205
1206 offset = xfs_lowbit64(rec.ir_free);
1207 ASSERT(offset >= 0);
1208 ASSERT(offset < XFS_INODES_PER_CHUNK);
1209 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1210 XFS_INODES_PER_CHUNK) == 0);
1211 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1212
1213 /*
1214 * Modify or remove the finobt record.
1215 */
1216 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1217 rec.ir_freecount--;
1218 if (rec.ir_freecount)
1219 error = xfs_inobt_update(cur, &rec);
1220 else
1221 error = xfs_btree_delete(cur, &i);
1222 if (error)
1223 goto error_cur;
1224
1225 /*
1226 * The finobt has now been updated appropriately. We haven't updated the
1227 * agi and superblock yet, so we can create an inobt cursor and validate
1228 * the original freecount. If all is well, make the equivalent update to
1229 * the inobt using the finobt record and offset information.
1230 */
1231 icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1232
1233 error = xfs_check_agi_freecount(icur, agi);
1234 if (error)
1235 goto error_icur;
1236
1237 error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
1238 if (error)
1239 goto error_icur;
1240
1241 /*
1242 * Both trees have now been updated. We must update the perag and
1243 * superblock before we can check the freecount for each btree.
1244 */
1245 be32_add_cpu(&agi->agi_freecount, -1);
1246 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1247 pag->pagi_freecount--;
1248
1249 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1250
1251 error = xfs_check_agi_freecount(icur, agi);
1252 if (error)
1253 goto error_icur;
1254 error = xfs_check_agi_freecount(cur, agi);
1255 if (error)
1256 goto error_icur;
1257
1258 xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
1259 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1260 xfs_perag_put(pag);
1261 *inop = ino;
1262 return 0;
1263
1264error_icur:
1265 xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
1266error_cur:
1267 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1268 xfs_perag_put(pag);
1269 return error;
1270}
1271
1272/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001273 * Allocate an inode on disk.
1274 *
1275 * Mode is used to tell whether the new inode will need space, and whether it
1276 * is a directory.
1277 *
1278 * This function is designed to be called twice if it has to do an allocation
1279 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
1280 * If an inode is available without having to performn an allocation, an inode
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001281 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
1282 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
1283 * The caller should then commit the current transaction, allocate a
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001284 * new transaction, and call xfs_dialloc() again, passing in the previous value
1285 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
1286 * buffer is locked across the two calls, the second call is guaranteed to have
1287 * a free inode available.
1288 *
1289 * Once we successfully pick an inode its number is returned and the on-disk
1290 * data structures are updated. The inode itself is not read in, since doing so
1291 * would break ordering constraints with xfs_reclaim.
1292 */
1293int
1294xfs_dialloc(
1295 struct xfs_trans *tp,
1296 xfs_ino_t parent,
1297 umode_t mode,
1298 int okalloc,
1299 struct xfs_buf **IO_agbp,
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001300 xfs_ino_t *inop)
1301{
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001302 struct xfs_mount *mp = tp->t_mountp;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001303 struct xfs_buf *agbp;
1304 xfs_agnumber_t agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001305 int error;
1306 int ialloced;
1307 int noroom = 0;
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001308 xfs_agnumber_t start_agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001309 struct xfs_perag *pag;
1310
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001311 if (*IO_agbp) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001312 /*
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001313 * If the caller passes in a pointer to the AGI buffer,
1314 * continue where we left off before. In this case, we
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001315 * know that the allocation group has free inodes.
1316 */
1317 agbp = *IO_agbp;
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001318 goto out_alloc;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001319 }
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001320
1321 /*
1322 * We do not have an agbp, so select an initial allocation
1323 * group for inode allocation.
1324 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001325 start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
1326 if (start_agno == NULLAGNUMBER) {
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001327 *inop = NULLFSINO;
1328 return 0;
1329 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001330
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001331 /*
1332 * If we have already hit the ceiling of inode blocks then clear
1333 * okalloc so we scan all available agi structures for a free
1334 * inode.
1335 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001336 if (mp->m_maxicount &&
Jie Liu71783432013-12-13 15:51:46 +11001337 mp->m_sb.sb_icount + mp->m_ialloc_inos > mp->m_maxicount) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001338 noroom = 1;
1339 okalloc = 0;
1340 }
1341
1342 /*
1343 * Loop until we find an allocation group that either has free inodes
1344 * or in which we can allocate some inodes. Iterate through the
1345 * allocation groups upward, wrapping at the end.
1346 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001347 agno = start_agno;
1348 for (;;) {
1349 pag = xfs_perag_get(mp, agno);
1350 if (!pag->pagi_inodeok) {
1351 xfs_ialloc_next_ag(mp);
1352 goto nextag;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001353 }
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001354
1355 if (!pag->pagi_init) {
1356 error = xfs_ialloc_pagi_init(mp, tp, agno);
1357 if (error)
1358 goto out_error;
1359 }
1360
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001361 /*
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001362 * Do a first racy fast path check if this AG is usable.
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001363 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001364 if (!pag->pagi_freecount && !okalloc)
1365 goto nextag;
1366
Christoph Hellwigc4982112012-08-07 02:02:02 -04001367 /*
1368 * Then read in the AGI buffer and recheck with the AGI buffer
1369 * lock held.
1370 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001371 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1372 if (error)
1373 goto out_error;
1374
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001375 if (pag->pagi_freecount) {
1376 xfs_perag_put(pag);
1377 goto out_alloc;
1378 }
1379
Christoph Hellwigc4982112012-08-07 02:02:02 -04001380 if (!okalloc)
1381 goto nextag_relse_buffer;
1382
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001383
1384 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1385 if (error) {
1386 xfs_trans_brelse(tp, agbp);
1387
Dave Chinner24513372014-06-25 14:58:08 +10001388 if (error != -ENOSPC)
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001389 goto out_error;
1390
1391 xfs_perag_put(pag);
1392 *inop = NULLFSINO;
1393 return 0;
1394 }
1395
1396 if (ialloced) {
1397 /*
1398 * We successfully allocated some inodes, return
1399 * the current context to the caller so that it
1400 * can commit the current transaction and call
1401 * us again where we left off.
1402 */
1403 ASSERT(pag->pagi_freecount > 0);
1404 xfs_perag_put(pag);
1405
1406 *IO_agbp = agbp;
1407 *inop = NULLFSINO;
1408 return 0;
1409 }
1410
Christoph Hellwigc4982112012-08-07 02:02:02 -04001411nextag_relse_buffer:
1412 xfs_trans_brelse(tp, agbp);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001413nextag:
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001414 xfs_perag_put(pag);
1415 if (++agno == mp->m_sb.sb_agcount)
1416 agno = 0;
1417 if (agno == start_agno) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001418 *inop = NULLFSINO;
Dave Chinner24513372014-06-25 14:58:08 +10001419 return noroom ? -ENOSPC : 0;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001420 }
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001421 }
1422
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001423out_alloc:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001424 *IO_agbp = NULL;
1425 return xfs_dialloc_ag(tp, agbp, parent, inop);
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001426out_error:
1427 xfs_perag_put(pag);
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001428 return error;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001429}
1430
Brian Foster2b64ee52014-04-24 16:00:53 +10001431STATIC int
1432xfs_difree_inobt(
1433 struct xfs_mount *mp,
1434 struct xfs_trans *tp,
1435 struct xfs_buf *agbp,
1436 xfs_agino_t agino,
1437 struct xfs_bmap_free *flist,
Dave Chinner0d907a32014-05-20 08:56:00 +10001438 int *deleted,
Brian Foster2b64ee52014-04-24 16:00:53 +10001439 xfs_ino_t *first_ino,
1440 struct xfs_inobt_rec_incore *orec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Brian Foster2b64ee52014-04-24 16:00:53 +10001442 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1443 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1444 struct xfs_perag *pag;
1445 struct xfs_btree_cur *cur;
1446 struct xfs_inobt_rec_incore rec;
1447 int ilen;
1448 int error;
1449 int i;
1450 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001452 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Brian Foster2b64ee52014-04-24 16:00:53 +10001453 ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 /*
1456 * Initialize the cursor.
1457 */
Brian Foster57bd3db2014-04-24 16:00:50 +10001458 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Dave Chinner0b48db82009-08-31 20:57:09 -03001460 error = xfs_check_agi_freecount(cur, agi);
1461 if (error)
1462 goto error0;
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /*
1465 * Look for the entry describing this inode.
1466 */
Christoph Hellwig21875502009-08-31 20:58:21 -03001467 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001468 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1469 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 goto error0;
1471 }
1472 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001473 error = xfs_inobt_get_rec(cur, &rec, &i);
1474 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001475 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1476 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto error0;
1478 }
1479 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1480 /*
1481 * Get the offset in the inode chunk.
1482 */
1483 off = agino - rec.ir_startino;
1484 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001485 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /*
1487 * Mark the inode free & increment the count.
1488 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001489 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 rec.ir_freecount++;
1491
1492 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001493 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001495 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Jie Liu71783432013-12-13 15:51:46 +11001496 (rec.ir_freecount == mp->m_ialloc_inos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Roger Willcocks376c2f32014-05-20 08:52:21 +10001498 *deleted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1500
1501 /*
1502 * Remove the inode cluster from the AGI B+Tree, adjust the
1503 * AGI and Superblock inode counts, and mark the disk space
1504 * to be freed when the transaction is committed.
1505 */
Jie Liu71783432013-12-13 15:51:46 +11001506 ilen = mp->m_ialloc_inos;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001507 be32_add_cpu(&agi->agi_count, -ilen);
1508 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001510 pag = xfs_perag_get(mp, agno);
1511 pag->pagi_freecount -= ilen - 1;
1512 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1514 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1515
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001516 if ((error = xfs_btree_delete(cur, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001517 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1518 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 goto error0;
1520 }
1521
Jie Liu126cd102013-12-13 15:51:48 +11001522 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno,
1523 XFS_AGINO_TO_AGBNO(mp, rec.ir_startino)),
1524 mp->m_ialloc_blks, flist, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 } else {
Roger Willcocks376c2f32014-05-20 08:52:21 +10001526 *deleted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Christoph Hellwigafabc242009-08-31 20:57:03 -03001528 error = xfs_inobt_update(cur, &rec);
1529 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001530 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1531 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 goto error0;
1533 }
Christoph Hellwigafabc242009-08-31 20:57:03 -03001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /*
1536 * Change the inode free counts and log the ag/sb changes.
1537 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001538 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001540 pag = xfs_perag_get(mp, agno);
1541 pag->pagi_freecount++;
1542 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1544 }
1545
Dave Chinner0b48db82009-08-31 20:57:09 -03001546 error = xfs_check_agi_freecount(cur, agi);
1547 if (error)
1548 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Brian Foster2b64ee52014-04-24 16:00:53 +10001550 *orec = rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1552 return 0;
1553
1554error0:
1555 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1556 return error;
1557}
1558
Brian Foster2b64ee52014-04-24 16:00:53 +10001559/*
Brian Foster3efa4ff2014-04-24 16:00:53 +10001560 * Free an inode in the free inode btree.
1561 */
1562STATIC int
1563xfs_difree_finobt(
1564 struct xfs_mount *mp,
1565 struct xfs_trans *tp,
1566 struct xfs_buf *agbp,
1567 xfs_agino_t agino,
1568 struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
1569{
1570 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1571 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1572 struct xfs_btree_cur *cur;
1573 struct xfs_inobt_rec_incore rec;
1574 int offset = agino - ibtrec->ir_startino;
1575 int error;
1576 int i;
1577
1578 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1579
1580 error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
1581 if (error)
1582 goto error;
1583 if (i == 0) {
1584 /*
1585 * If the record does not exist in the finobt, we must have just
1586 * freed an inode in a previously fully allocated chunk. If not,
1587 * something is out of sync.
1588 */
1589 XFS_WANT_CORRUPTED_GOTO(ibtrec->ir_freecount == 1, error);
1590
1591 error = xfs_inobt_insert_rec(cur, ibtrec->ir_freecount,
1592 ibtrec->ir_free, &i);
1593 if (error)
1594 goto error;
1595 ASSERT(i == 1);
1596
1597 goto out;
1598 }
1599
1600 /*
1601 * Read and update the existing record. We could just copy the ibtrec
1602 * across here, but that would defeat the purpose of having redundant
1603 * metadata. By making the modifications independently, we can catch
1604 * corruptions that we wouldn't see if we just copied from one record
1605 * to another.
1606 */
1607 error = xfs_inobt_get_rec(cur, &rec, &i);
1608 if (error)
1609 goto error;
1610 XFS_WANT_CORRUPTED_GOTO(i == 1, error);
1611
1612 rec.ir_free |= XFS_INOBT_MASK(offset);
1613 rec.ir_freecount++;
1614
1615 XFS_WANT_CORRUPTED_GOTO((rec.ir_free == ibtrec->ir_free) &&
1616 (rec.ir_freecount == ibtrec->ir_freecount),
1617 error);
1618
1619 /*
1620 * The content of inobt records should always match between the inobt
1621 * and finobt. The lifecycle of records in the finobt is different from
1622 * the inobt in that the finobt only tracks records with at least one
1623 * free inode. Hence, if all of the inodes are free and we aren't
1624 * keeping inode chunks permanently on disk, remove the record.
1625 * Otherwise, update the record with the new information.
1626 */
1627 if (rec.ir_freecount == mp->m_ialloc_inos &&
1628 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
1629 error = xfs_btree_delete(cur, &i);
1630 if (error)
1631 goto error;
1632 ASSERT(i == 1);
1633 } else {
1634 error = xfs_inobt_update(cur, &rec);
1635 if (error)
1636 goto error;
1637 }
1638
1639out:
1640 error = xfs_check_agi_freecount(cur, agi);
1641 if (error)
1642 goto error;
1643
1644 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1645 return 0;
1646
1647error:
1648 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1649 return error;
1650}
1651
1652/*
Brian Foster2b64ee52014-04-24 16:00:53 +10001653 * Free disk inode. Carefully avoids touching the incore inode, all
1654 * manipulations incore are the caller's responsibility.
1655 * The on-disk inode is not changed by this operation, only the
1656 * btree (free inode mask) is changed.
1657 */
1658int
1659xfs_difree(
1660 struct xfs_trans *tp, /* transaction pointer */
1661 xfs_ino_t inode, /* inode to be freed */
1662 struct xfs_bmap_free *flist, /* extents to free */
Dave Chinner0d907a32014-05-20 08:56:00 +10001663 int *deleted,/* set if inode cluster was deleted */
Brian Foster2b64ee52014-04-24 16:00:53 +10001664 xfs_ino_t *first_ino)/* first inode in deleted cluster */
1665{
1666 /* REFERENCED */
1667 xfs_agblock_t agbno; /* block number containing inode */
1668 struct xfs_buf *agbp; /* buffer for allocation group header */
1669 xfs_agino_t agino; /* allocation group inode number */
1670 xfs_agnumber_t agno; /* allocation group number */
1671 int error; /* error return value */
1672 struct xfs_mount *mp; /* mount structure for filesystem */
1673 struct xfs_inobt_rec_incore rec;/* btree record */
1674
1675 mp = tp->t_mountp;
1676
1677 /*
1678 * Break up inode number into its components.
1679 */
1680 agno = XFS_INO_TO_AGNO(mp, inode);
1681 if (agno >= mp->m_sb.sb_agcount) {
1682 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1683 __func__, agno, mp->m_sb.sb_agcount);
1684 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001685 return -EINVAL;
Brian Foster2b64ee52014-04-24 16:00:53 +10001686 }
1687 agino = XFS_INO_TO_AGINO(mp, inode);
1688 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1689 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1690 __func__, (unsigned long long)inode,
1691 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
1692 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001693 return -EINVAL;
Brian Foster2b64ee52014-04-24 16:00:53 +10001694 }
1695 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1696 if (agbno >= mp->m_sb.sb_agblocks) {
1697 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1698 __func__, agbno, mp->m_sb.sb_agblocks);
1699 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001700 return -EINVAL;
Brian Foster2b64ee52014-04-24 16:00:53 +10001701 }
1702 /*
1703 * Get the allocation group header.
1704 */
1705 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1706 if (error) {
1707 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1708 __func__, error);
1709 return error;
1710 }
1711
1712 /*
1713 * Fix up the inode allocation btree.
1714 */
Dave Chinner0d907a32014-05-20 08:56:00 +10001715 error = xfs_difree_inobt(mp, tp, agbp, agino, flist, deleted, first_ino,
Brian Foster2b64ee52014-04-24 16:00:53 +10001716 &rec);
1717 if (error)
1718 goto error0;
1719
Brian Foster3efa4ff2014-04-24 16:00:53 +10001720 /*
1721 * Fix up the free inode btree.
1722 */
1723 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
1724 error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
1725 if (error)
1726 goto error0;
1727 }
1728
Brian Foster2b64ee52014-04-24 16:00:53 +10001729 return 0;
1730
1731error0:
1732 return error;
1733}
1734
Dave Chinner7124fe02010-06-24 11:15:33 +10001735STATIC int
1736xfs_imap_lookup(
1737 struct xfs_mount *mp,
1738 struct xfs_trans *tp,
1739 xfs_agnumber_t agno,
1740 xfs_agino_t agino,
1741 xfs_agblock_t agbno,
1742 xfs_agblock_t *chunk_agbno,
1743 xfs_agblock_t *offset_agbno,
1744 int flags)
1745{
1746 struct xfs_inobt_rec_incore rec;
1747 struct xfs_btree_cur *cur;
1748 struct xfs_buf *agbp;
Dave Chinner7124fe02010-06-24 11:15:33 +10001749 int error;
1750 int i;
1751
1752 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1753 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +11001754 xfs_alert(mp,
1755 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1756 __func__, error, agno);
Dave Chinner7124fe02010-06-24 11:15:33 +10001757 return error;
1758 }
1759
1760 /*
Dave Chinner4536f2a2010-08-24 11:42:30 +10001761 * Lookup the inode record for the given agino. If the record cannot be
1762 * found, then it's an invalid inode number and we should abort. Once
1763 * we have a record, we need to ensure it contains the inode number
1764 * we are looking up.
Dave Chinner7124fe02010-06-24 11:15:33 +10001765 */
Brian Foster57bd3db2014-04-24 16:00:50 +10001766 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Dave Chinner4536f2a2010-08-24 11:42:30 +10001767 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
Dave Chinner7124fe02010-06-24 11:15:33 +10001768 if (!error) {
1769 if (i)
1770 error = xfs_inobt_get_rec(cur, &rec, &i);
1771 if (!error && i == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001772 error = -EINVAL;
Dave Chinner7124fe02010-06-24 11:15:33 +10001773 }
1774
1775 xfs_trans_brelse(tp, agbp);
1776 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1777 if (error)
1778 return error;
1779
Dave Chinner4536f2a2010-08-24 11:42:30 +10001780 /* check that the returned record contains the required inode */
1781 if (rec.ir_startino > agino ||
Jie Liu71783432013-12-13 15:51:46 +11001782 rec.ir_startino + mp->m_ialloc_inos <= agino)
Dave Chinner24513372014-06-25 14:58:08 +10001783 return -EINVAL;
Dave Chinner4536f2a2010-08-24 11:42:30 +10001784
Dave Chinner7124fe02010-06-24 11:15:33 +10001785 /* for untrusted inodes check it is allocated first */
Dave Chinner19207792010-06-24 11:15:47 +10001786 if ((flags & XFS_IGET_UNTRUSTED) &&
Dave Chinner7124fe02010-06-24 11:15:33 +10001787 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
Dave Chinner24513372014-06-25 14:58:08 +10001788 return -EINVAL;
Dave Chinner7124fe02010-06-24 11:15:33 +10001789
1790 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1791 *offset_agbno = agbno - *chunk_agbno;
1792 return 0;
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001796 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001799xfs_imap(
1800 xfs_mount_t *mp, /* file system mount structure */
1801 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001803 struct xfs_imap *imap, /* location map structure */
1804 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 xfs_agino_t agino; /* inode number within alloc group */
1808 xfs_agnumber_t agno; /* allocation group number */
1809 int blks_per_cluster; /* num blocks per inode cluster */
1810 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 int offset; /* index of inode in its buffer */
Dave Chinner836a94a2013-08-12 20:49:44 +10001814 xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /*
1819 * Split up the inode number into its parts.
1820 */
1821 agno = XFS_INO_TO_AGNO(mp, ino);
1822 agino = XFS_INO_TO_AGINO(mp, ino);
1823 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1824 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1825 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1826#ifdef DEBUG
Dave Chinner19207792010-06-24 11:15:47 +10001827 /*
1828 * Don't output diagnostic information for untrusted inodes
1829 * as they can be invalid without implying corruption.
1830 */
1831 if (flags & XFS_IGET_UNTRUSTED)
Dave Chinner24513372014-06-25 14:58:08 +10001832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner53487782011-03-07 10:05:35 +11001834 xfs_alert(mp,
1835 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1836 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner53487782011-03-07 10:05:35 +11001839 xfs_alert(mp,
1840 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1841 __func__, (unsigned long long)agbno,
1842 (unsigned long)mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner53487782011-03-07 10:05:35 +11001845 xfs_alert(mp,
1846 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1847 __func__, ino,
1848 XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001850 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851#endif /* DEBUG */
Dave Chinner24513372014-06-25 14:58:08 +10001852 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001854
Jie Liuf9e5abc2013-12-13 15:51:49 +11001855 blks_per_cluster = xfs_icluster_size_fsb(mp);
Dave Chinner7124fe02010-06-24 11:15:33 +10001856
1857 /*
1858 * For bulkstat and handle lookups, we have an untrusted inode number
1859 * that we have to verify is valid. We cannot do this just by reading
1860 * the inode buffer as it may have been unlinked and removed leaving
1861 * inodes in stale state on disk. Hence we have to do a btree lookup
1862 * in all cases where an untrusted inode number is passed.
1863 */
Dave Chinner19207792010-06-24 11:15:47 +10001864 if (flags & XFS_IGET_UNTRUSTED) {
Dave Chinner7124fe02010-06-24 11:15:33 +10001865 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1866 &chunk_agbno, &offset_agbno, flags);
1867 if (error)
1868 return error;
1869 goto out_map;
1870 }
1871
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001872 /*
1873 * If the inode cluster size is the same as the blocksize or
1874 * smaller we get to the buffer by simple arithmetics.
1875 */
Jie Liuf9e5abc2013-12-13 15:51:49 +11001876 if (blks_per_cluster == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 offset = XFS_INO_TO_OFFSET(mp, ino);
1878 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001879
1880 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1881 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1882 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return 0;
1884 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001885
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001886 /*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001887 * If the inode chunks are aligned then use simple maths to
1888 * find the location. Otherwise we have to do a btree
1889 * lookup to find the location.
1890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (mp->m_inoalign_mask) {
1892 offset_agbno = agbno & mp->m_inoalign_mask;
1893 chunk_agbno = agbno - offset_agbno;
1894 } else {
Dave Chinner7124fe02010-06-24 11:15:33 +10001895 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1896 &chunk_agbno, &offset_agbno, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (error)
1898 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001900
Dave Chinner7124fe02010-06-24 11:15:33 +10001901out_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 ASSERT(agbno >= chunk_agbno);
1903 cluster_agbno = chunk_agbno +
1904 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1905 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1906 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001907
1908 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1909 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1910 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1911
1912 /*
1913 * If the inode number maps to a block outside the bounds
1914 * of the file system then return NULL rather than calling
1915 * read_buf and panicing when we get an error from the
1916 * driver.
1917 */
1918 if ((imap->im_blkno + imap->im_len) >
1919 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
Dave Chinner53487782011-03-07 10:05:35 +11001920 xfs_alert(mp,
1921 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1922 __func__, (unsigned long long) imap->im_blkno,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001923 (unsigned long long) imap->im_len,
1924 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
Dave Chinner24513372014-06-25 14:58:08 +10001925 return -EINVAL;
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
1930/*
1931 * Compute and fill in value of m_in_maxlevels.
1932 */
1933void
1934xfs_ialloc_compute_maxlevels(
1935 xfs_mount_t *mp) /* file system mount structure */
1936{
1937 int level;
1938 uint maxblocks;
1939 uint maxleafents;
1940 int minleafrecs;
1941 int minnoderecs;
1942
1943 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1944 XFS_INODES_PER_CHUNK_LOG;
1945 minleafrecs = mp->m_alloc_mnr[0];
1946 minnoderecs = mp->m_alloc_mnr[1];
1947 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1948 for (level = 1; maxblocks > 1; level++)
1949 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1950 mp->m_in_maxlevels = level;
1951}
1952
1953/*
Brian Fosteraafc3c22014-04-24 16:00:52 +10001954 * Log specified fields for the ag hdr (inode section). The growth of the agi
1955 * structure over time requires that we interpret the buffer as two logical
1956 * regions delineated by the end of the unlinked list. This is due to the size
1957 * of the hash table and its location in the middle of the agi.
1958 *
1959 * For example, a request to log a field before agi_unlinked and a field after
1960 * agi_unlinked could cause us to log the entire hash table and use an excessive
1961 * amount of log space. To avoid this behavior, log the region up through
1962 * agi_unlinked in one call and the region after agi_unlinked through the end of
1963 * the structure in another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
1965void
1966xfs_ialloc_log_agi(
1967 xfs_trans_t *tp, /* transaction pointer */
1968 xfs_buf_t *bp, /* allocation group header buffer */
1969 int fields) /* bitmask of fields to log */
1970{
1971 int first; /* first byte number */
1972 int last; /* last byte number */
1973 static const short offsets[] = { /* field starting offsets */
1974 /* keep in sync with bit definitions */
1975 offsetof(xfs_agi_t, agi_magicnum),
1976 offsetof(xfs_agi_t, agi_versionnum),
1977 offsetof(xfs_agi_t, agi_seqno),
1978 offsetof(xfs_agi_t, agi_length),
1979 offsetof(xfs_agi_t, agi_count),
1980 offsetof(xfs_agi_t, agi_root),
1981 offsetof(xfs_agi_t, agi_level),
1982 offsetof(xfs_agi_t, agi_freecount),
1983 offsetof(xfs_agi_t, agi_newino),
1984 offsetof(xfs_agi_t, agi_dirino),
1985 offsetof(xfs_agi_t, agi_unlinked),
Brian Fosteraafc3c22014-04-24 16:00:52 +10001986 offsetof(xfs_agi_t, agi_free_root),
1987 offsetof(xfs_agi_t, agi_free_level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 sizeof(xfs_agi_t)
1989 };
1990#ifdef DEBUG
1991 xfs_agi_t *agi; /* allocation group header */
1992
1993 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001994 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995#endif
Brian Fosteraafc3c22014-04-24 16:00:52 +10001996
Dave Chinner61fe1352013-04-03 16:11:30 +11001997 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
Brian Fosteraafc3c22014-04-24 16:00:52 +10001998
1999 /*
2000 * Compute byte offsets for the first and last fields in the first
2001 * region and log the agi buffer. This only logs up through
2002 * agi_unlinked.
2003 */
2004 if (fields & XFS_AGI_ALL_BITS_R1) {
2005 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
2006 &first, &last);
2007 xfs_trans_log_buf(tp, bp, first, last);
2008 }
2009
2010 /*
2011 * Mask off the bits in the first region and calculate the first and
2012 * last field offsets for any bits in the second region.
2013 */
2014 fields &= ~XFS_AGI_ALL_BITS_R1;
2015 if (fields) {
2016 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
2017 &first, &last);
2018 xfs_trans_log_buf(tp, bp, first, last);
2019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002022#ifdef DEBUG
2023STATIC void
2024xfs_check_agi_unlinked(
2025 struct xfs_agi *agi)
2026{
2027 int i;
2028
2029 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
2030 ASSERT(agi->agi_unlinked[i]);
2031}
2032#else
2033#define xfs_check_agi_unlinked(agi)
2034#endif
2035
Dave Chinner983d09f2013-04-03 16:11:15 +11002036static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002037xfs_agi_verify(
Dave Chinner3702ce62012-11-12 22:54:05 +11002038 struct xfs_buf *bp)
2039{
2040 struct xfs_mount *mp = bp->b_target->bt_mount;
2041 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
Dave Chinner3702ce62012-11-12 22:54:05 +11002042
Dave Chinner983d09f2013-04-03 16:11:15 +11002043 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2044 !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
2045 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002046 /*
2047 * Validate the magic number of the agi block.
2048 */
Dave Chinner983d09f2013-04-03 16:11:15 +11002049 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
2050 return false;
2051 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
2052 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002053
Eric Sandeene1b05722014-09-09 11:47:24 +10002054 if (be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
2055 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002056 /*
2057 * during growfs operations, the perag is not fully initialised,
2058 * so we can't use it for any useful checking. growfs ensures we can't
2059 * use it by using uncached buffers that don't have the perag attached
2060 * so we can detect and avoid this problem.
2061 */
Dave Chinner983d09f2013-04-03 16:11:15 +11002062 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
2063 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002064
Dave Chinner3702ce62012-11-12 22:54:05 +11002065 xfs_check_agi_unlinked(agi);
Dave Chinner983d09f2013-04-03 16:11:15 +11002066 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +11002067}
2068
Dave Chinner1813dd62012-11-14 17:54:40 +11002069static void
2070xfs_agi_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002071 struct xfs_buf *bp)
2072{
Dave Chinner983d09f2013-04-03 16:11:15 +11002073 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner983d09f2013-04-03 16:11:15 +11002074
Eric Sandeence5028c2014-02-27 15:23:10 +11002075 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2076 !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +10002077 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +11002078 else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp,
2079 XFS_ERRTAG_IALLOC_READ_AGI,
2080 XFS_RANDOM_IALLOC_READ_AGI))
Dave Chinner24513372014-06-25 14:58:08 +10002081 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002082
2083 if (bp->b_error)
2084 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +11002085}
2086
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002087static void
Dave Chinner1813dd62012-11-14 17:54:40 +11002088xfs_agi_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +11002089 struct xfs_buf *bp)
2090{
Dave Chinner983d09f2013-04-03 16:11:15 +11002091 struct xfs_mount *mp = bp->b_target->bt_mount;
2092 struct xfs_buf_log_item *bip = bp->b_fspriv;
2093
2094 if (!xfs_agi_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +10002095 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +11002096 xfs_verifier_error(bp);
Dave Chinner983d09f2013-04-03 16:11:15 +11002097 return;
2098 }
2099
2100 if (!xfs_sb_version_hascrc(&mp->m_sb))
2101 return;
2102
2103 if (bip)
2104 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +11002105 xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
Dave Chinner3702ce62012-11-12 22:54:05 +11002106}
2107
Dave Chinner1813dd62012-11-14 17:54:40 +11002108const struct xfs_buf_ops xfs_agi_buf_ops = {
2109 .verify_read = xfs_agi_read_verify,
2110 .verify_write = xfs_agi_write_verify,
2111};
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113/*
2114 * Read in the allocation group header (inode allocation section)
2115 */
2116int
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002117xfs_read_agi(
2118 struct xfs_mount *mp, /* file system mount structure */
2119 struct xfs_trans *tp, /* transaction pointer */
2120 xfs_agnumber_t agno, /* allocation group number */
2121 struct xfs_buf **bpp) /* allocation group hdr buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002123 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Dave Chinnerd1230312013-11-01 15:27:19 +11002125 trace_xfs_read_agi(mp, agno);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002126
Dave Chinnerd1230312013-11-01 15:27:19 +11002127 ASSERT(agno != NULLAGNUMBER);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002128 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
Dave Chinner1813dd62012-11-14 17:54:40 +11002130 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (error)
2132 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002133
Christoph Hellwig38f23232011-10-10 16:52:45 +00002134 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002135 return 0;
2136}
2137
2138int
2139xfs_ialloc_read_agi(
2140 struct xfs_mount *mp, /* file system mount structure */
2141 struct xfs_trans *tp, /* transaction pointer */
2142 xfs_agnumber_t agno, /* allocation group number */
2143 struct xfs_buf **bpp) /* allocation group hdr buf */
2144{
2145 struct xfs_agi *agi; /* allocation group header */
2146 struct xfs_perag *pag; /* per allocation group data */
2147 int error;
2148
Dave Chinnerd1230312013-11-01 15:27:19 +11002149 trace_xfs_ialloc_read_agi(mp, agno);
2150
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002151 error = xfs_read_agi(mp, tp, agno, bpp);
2152 if (error)
2153 return error;
2154
2155 agi = XFS_BUF_TO_AGI(*bpp);
Dave Chinner44b56e02010-01-11 11:47:43 +00002156 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002158 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10002159 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002163 /*
2164 * It's possible for these to be out of sync if
2165 * we are in the middle of a forced shutdown.
2166 */
2167 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
2168 XFS_FORCED_SHUTDOWN(mp));
Dave Chinner44b56e02010-01-11 11:47:43 +00002169 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return 0;
2171}
David Chinner92821e22007-05-24 15:26:31 +10002172
2173/*
2174 * Read in the agi to initialise the per-ag data in the mount structure
2175 */
2176int
2177xfs_ialloc_pagi_init(
2178 xfs_mount_t *mp, /* file system mount structure */
2179 xfs_trans_t *tp, /* transaction pointer */
2180 xfs_agnumber_t agno) /* allocation group number */
2181{
2182 xfs_buf_t *bp = NULL;
2183 int error;
2184
2185 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
2186 if (error)
2187 return error;
2188 if (bp)
2189 xfs_trans_brelse(tp, bp);
2190 return 0;
2191}