blob: 1313df7ff0d83a45583cfbb6f27c7c555bc0109e [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);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100283 } else if (xfs_sb_version_hasnlink(&mp->m_sb))
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300284 version = 2;
285 else
286 version = 1;
287
288 for (j = 0; j < nbufs; j++) {
289 /*
290 * Get the block.
291 */
292 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
293 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
Dave Chinner7c4cebe2012-11-23 14:24:23 +1100294 mp->m_bsize * blks_per_cluster,
295 XBF_UNMAPPED);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000296 if (!fbuf)
297 return ENOMEM;
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000298
299 /* Initialize the inode buffers and log them appropriately. */
Dave Chinner1813dd62012-11-14 17:54:40 +1100300 fbuf->b_ops = &xfs_inode_buf_ops;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100301 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
Jie Liu6e0c7b82013-12-13 15:51:49 +1100302 for (i = 0; i < inodes_per_cluster; i++) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300303 int ioffset = i << mp->m_sb.sb_inodelog;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100304 uint isize = xfs_dinode_size(version);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300305
306 free = xfs_make_iptr(mp, fbuf, i);
307 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
308 free->di_version = version;
309 free->di_gen = cpu_to_be32(gen);
310 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig93848a92013-04-03 16:11:17 +1100311
312 if (version == 3) {
313 free->di_ino = cpu_to_be64(ino);
314 ino++;
315 uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
316 xfs_dinode_calc_crc(mp, free);
Dave Chinner28c8e412013-06-27 16:04:55 +1000317 } else if (tp) {
Christoph Hellwig93848a92013-04-03 16:11:17 +1100318 /* just log the inode core */
319 xfs_trans_log_buf(tp, fbuf, ioffset,
320 ioffset + isize - 1);
321 }
322 }
Dave Chinner28c8e412013-06-27 16:04:55 +1000323
324 if (tp) {
325 /*
326 * Mark the buffer as an inode allocation buffer so it
327 * sticks in AIL at the point of this allocation
328 * transaction. This ensures the they are on disk before
329 * the tail of the log can be moved past this
330 * transaction (i.e. by preventing relogging from moving
331 * it forward in the log).
332 */
333 xfs_trans_inode_alloc_buf(tp, fbuf);
334 if (version == 3) {
Dave Chinnerddf6ad02013-06-27 16:04:56 +1000335 /*
336 * Mark the buffer as ordered so that they are
337 * not physically logged in the transaction but
338 * still tracked in the AIL as part of the
339 * transaction and pin the log appropriately.
340 */
341 xfs_trans_ordered_buf(tp, fbuf);
Dave Chinner28c8e412013-06-27 16:04:55 +1000342 xfs_trans_log_buf(tp, fbuf, 0,
343 BBTOB(fbuf->b_length) - 1);
344 }
345 } else {
346 fbuf->b_flags |= XBF_DONE;
347 xfs_buf_delwri_queue(fbuf, buffer_list);
348 xfs_buf_relse(fbuf);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300349 }
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300350 }
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000351 return 0;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300352}
353
354/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * Allocate new inodes in the allocation group specified by agbp.
356 * Return 0 for success, else error code.
357 */
358STATIC int /* error code or 0 */
359xfs_ialloc_ag_alloc(
360 xfs_trans_t *tp, /* transaction pointer */
361 xfs_buf_t *agbp, /* alloc group buffer */
362 int *alloc)
363{
364 xfs_agi_t *agi; /* allocation group header */
365 xfs_alloc_arg_t args; /* allocation argument structure */
David Chinner92821e22007-05-24 15:26:31 +1000366 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 xfs_agino_t newino; /* new first inode's number */
369 xfs_agino_t newlen; /* new number of inodes */
Glen Overby3ccb8b52006-03-29 09:52:28 +1000370 int isaligned = 0; /* inode allocation at stripe unit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* boundary */
Dave Chinner44b56e02010-01-11 11:47:43 +0000372 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Mark Tinguelya0041682012-09-20 13:16:45 -0500374 memset(&args, 0, sizeof(args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 args.tp = tp;
376 args.mp = tp->t_mountp;
377
378 /*
379 * Locking will ensure that we don't have two callers in here
380 * at one time.
381 */
Jie Liu71783432013-12-13 15:51:46 +1100382 newlen = args.mp->m_ialloc_inos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (args.mp->m_maxicount &&
384 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
385 return XFS_ERROR(ENOSPC);
Jie Liu126cd102013-12-13 15:51:48 +1100386 args.minlen = args.maxlen = args.mp->m_ialloc_blks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
Glen Overby3ccb8b52006-03-29 09:52:28 +1000388 * First try to allocate inodes contiguous with the last-allocated
389 * chunk of inodes. If the filesystem is striped, this will fill
390 * an entire stripe unit with inodes.
Dave Chinner28c8e412013-06-27 16:04:55 +1000391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 agi = XFS_BUF_TO_AGI(agbp);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000393 newino = be32_to_cpu(agi->agi_newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300394 agno = be32_to_cpu(agi->agi_seqno);
Nathan Scott019ff2d2006-04-11 15:45:05 +1000395 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
Jie Liu126cd102013-12-13 15:51:48 +1100396 args.mp->m_ialloc_blks;
Nathan Scott019ff2d2006-04-11 15:45:05 +1000397 if (likely(newino != NULLAGINO &&
398 (args.agbno < be32_to_cpu(agi->agi_length)))) {
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300399 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000400 args.type = XFS_ALLOCTYPE_THIS_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000401 args.prod = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100402
Glen Overby3ccb8b52006-03-29 09:52:28 +1000403 /*
David Chinner75de2a92008-03-27 18:00:38 +1100404 * We need to take into account alignment here to ensure that
405 * we don't modify the free list if we fail to have an exact
406 * block. If we don't have an exact match, and every oher
407 * attempt allocation attempt fails, we'll end up cancelling
408 * a dirty transaction and shutting down.
409 *
410 * For an exact allocation, alignment must be 1,
411 * however we need to take cluster alignment into account when
412 * fixing up the freelist. Use the minalignslop field to
413 * indicate that extra blocks might be required for alignment,
414 * but not to use them in the actual exact allocation.
Glen Overby3ccb8b52006-03-29 09:52:28 +1000415 */
David Chinner75de2a92008-03-27 18:00:38 +1100416 args.alignment = 1;
417 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
418
419 /* Allow space for the inode btree to split. */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100420 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000421 if ((error = xfs_alloc_vextent(&args)))
422 return error;
Brian Fostere480a722014-03-07 16:19:14 +1100423
424 /*
425 * This request might have dirtied the transaction if the AG can
426 * satisfy the request, but the exact block was not available.
427 * If the allocation did fail, subsequent requests will relax
428 * the exact agbno requirement and increase the alignment
429 * instead. It is critical that the total size of the request
430 * (len + alignment + slop) does not increase from this point
431 * on, so reset minalignslop to ensure it is not included in
432 * subsequent requests.
433 */
434 args.minalignslop = 0;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000435 } else
436 args.fsbno = NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Glen Overby3ccb8b52006-03-29 09:52:28 +1000438 if (unlikely(args.fsbno == NULLFSBLOCK)) {
439 /*
440 * Set the alignment for the allocation.
441 * If stripe alignment is turned on then align at stripe unit
442 * boundary.
Nathan Scott019ff2d2006-04-11 15:45:05 +1000443 * If the cluster size is smaller than a filesystem block
444 * then we're doing I/O for inodes in filesystem block size
Glen Overby3ccb8b52006-03-29 09:52:28 +1000445 * pieces, so don't need alignment anyway.
446 */
447 isaligned = 0;
448 if (args.mp->m_sinoalign) {
449 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
450 args.alignment = args.mp->m_dalign;
451 isaligned = 1;
David Chinner75de2a92008-03-27 18:00:38 +1100452 } else
453 args.alignment = xfs_ialloc_cluster_alignment(&args);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000454 /*
455 * Need to figure out where to allocate the inode blocks.
456 * Ideally they should be spaced out through the a.g.
457 * For now, just allocate blocks up front.
458 */
459 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300460 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
Glen Overby3ccb8b52006-03-29 09:52:28 +1000461 /*
462 * Allocate a fixed-size extent of inodes.
463 */
464 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000465 args.prod = 1;
466 /*
467 * Allow space for the inode btree to split.
468 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100469 args.minleft = args.mp->m_in_maxlevels - 1;
Glen Overby3ccb8b52006-03-29 09:52:28 +1000470 if ((error = xfs_alloc_vextent(&args)))
471 return error;
472 }
Nathan Scott019ff2d2006-04-11 15:45:05 +1000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /*
475 * If stripe alignment is turned on, then try again with cluster
476 * alignment.
477 */
478 if (isaligned && args.fsbno == NULLFSBLOCK) {
479 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Christoph Hellwig16259e72005-11-02 15:11:25 +1100480 args.agbno = be32_to_cpu(agi->agi_root);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300481 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
David Chinner75de2a92008-03-27 18:00:38 +1100482 args.alignment = xfs_ialloc_cluster_alignment(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if ((error = xfs_alloc_vextent(&args)))
484 return error;
485 }
486
487 if (args.fsbno == NULLFSBLOCK) {
488 *alloc = 0;
489 return 0;
490 }
491 ASSERT(args.len == args.minlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David Chinner359346a2008-04-29 12:53:32 +1000493 /*
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300494 * Stamp and write the inode buffers.
495 *
David Chinner359346a2008-04-29 12:53:32 +1000496 * Seed the new inode cluster with a random generation number. This
497 * prevents short-term reuse of generation numbers if a chunk is
498 * freed and then immediately reallocated. We use random numbers
499 * rather than a linear progression to prevent the next generation
500 * number from being easily guessable.
501 */
Dave Chinner28c8e412013-06-27 16:04:55 +1000502 error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
Akinobu Mitaecb34032013-03-04 21:58:20 +0900503 args.len, prandom_u32());
Christoph Hellwigd42f08f2008-11-28 14:23:38 +1100504
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000505 if (error)
506 return error;
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300507 /*
508 * Convert the results.
509 */
510 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800511 be32_add_cpu(&agi->agi_count, newlen);
512 be32_add_cpu(&agi->agi_freecount, newlen);
Dave Chinner44b56e02010-01-11 11:47:43 +0000513 pag = xfs_perag_get(args.mp, agno);
514 pag->pagi_freecount += newlen;
515 xfs_perag_put(pag);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100516 agi->agi_newino = cpu_to_be32(newino);
Dave Chinner85c0b2a2009-08-31 20:56:51 -0300517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
Brian Foster0aa0a752014-04-24 16:00:53 +1000519 * Insert records describing the new inode chunk into the btrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
Brian Foster0aa0a752014-04-24 16:00:53 +1000521 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
522 XFS_BTNUM_INO);
523 if (error)
524 return error;
525
526 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
527 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
528 XFS_BTNUM_FINO);
529 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
533 * Log allocation group header fields
534 */
535 xfs_ialloc_log_agi(tp, agbp,
536 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
537 /*
538 * Modify/log superblock values for inode count and inode free count.
539 */
540 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
541 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
542 *alloc = 1;
543 return 0;
544}
545
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000546STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547xfs_ialloc_next_ag(
548 xfs_mount_t *mp)
549{
550 xfs_agnumber_t agno;
551
552 spin_lock(&mp->m_agirotor_lock);
553 agno = mp->m_agirotor;
Carlos Maiolino8aea3ff2012-09-20 10:32:36 -0300554 if (++mp->m_agirotor >= mp->m_maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 mp->m_agirotor = 0;
556 spin_unlock(&mp->m_agirotor_lock);
557
558 return agno;
559}
560
561/*
562 * Select an allocation group to look for a free inode in, based on the parent
Zhi Yong Wu2f21ff12013-08-12 03:14:58 +0000563 * inode and the mode. Return the allocation group buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400565STATIC xfs_agnumber_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566xfs_ialloc_ag_select(
567 xfs_trans_t *tp, /* transaction pointer */
568 xfs_ino_t parent, /* parent directory inode number */
Al Viro576b1d62011-07-26 02:50:15 -0400569 umode_t mode, /* bits set to indicate file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int okalloc) /* ok to allocate more space */
571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
573 xfs_agnumber_t agno; /* current ag number */
574 int flags; /* alloc buffer locking flags */
575 xfs_extlen_t ineed; /* blocks needed for inode allocation */
576 xfs_extlen_t longest = 0; /* longest extent available */
577 xfs_mount_t *mp; /* mount point structure */
578 int needspace; /* file mode implies space allocated */
579 xfs_perag_t *pag; /* per allocation group data */
580 xfs_agnumber_t pagno; /* parent (starting) ag number */
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400581 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /*
584 * Files of these types need at least one block if length > 0
585 * (and they won't fit in the inode, but that's hard to figure out).
586 */
587 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
588 mp = tp->t_mountp;
589 agcount = mp->m_maxagi;
590 if (S_ISDIR(mode))
591 pagno = xfs_ialloc_next_ag(mp);
592 else {
593 pagno = XFS_INO_TO_AGNO(mp, parent);
594 if (pagno >= agcount)
595 pagno = 0;
596 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ASSERT(pagno < agcount);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
601 * Loop through allocation groups, looking for one with a little
602 * free space in it. Note we don't look for free inodes, exactly.
603 * Instead, we include whether there is a need to allocate inodes
604 * to mean that blocks must be allocated for them,
605 * if none are currently free.
606 */
607 agno = pagno;
608 flags = XFS_ALLOC_FLAG_TRYLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 for (;;) {
Dave Chinner44b56e02010-01-11 11:47:43 +0000610 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!pag->pagi_inodeok) {
612 xfs_ialloc_next_ag(mp);
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400613 goto nextag;
614 }
615
616 if (!pag->pagi_init) {
617 error = xfs_ialloc_pagi_init(mp, tp, agno);
618 if (error)
619 goto nextag;
620 }
621
622 if (pag->pagi_freecount) {
623 xfs_perag_put(pag);
624 return agno;
625 }
626
627 if (!okalloc)
628 goto nextag;
629
630 if (!pag->pagf_init) {
631 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
632 if (error)
633 goto nextag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 /*
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400637 * Is there enough free space for the file plus a block of
638 * inodes? (if we need to allocate some)?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
Jie Liu126cd102013-12-13 15:51:48 +1100640 ineed = mp->m_ialloc_blks;
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400641 longest = pag->pagf_longest;
642 if (!longest)
643 longest = pag->pagf_flcount > 0;
644
645 if (pag->pagf_freeblks >= needspace + ineed &&
646 longest >= ineed) {
647 xfs_perag_put(pag);
648 return agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650nextag:
Dave Chinner44b56e02010-01-11 11:47:43 +0000651 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * No point in iterating over the rest, if we're shutting
654 * down.
655 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000656 if (XFS_FORCED_SHUTDOWN(mp))
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400657 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 agno++;
659 if (agno >= agcount)
660 agno = 0;
661 if (agno == pagno) {
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000662 if (flags == 0)
Christoph Hellwig55d6af62012-07-04 10:54:49 -0400663 return NULLAGNUMBER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 flags = 0;
665 }
666 }
667}
668
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300669/*
670 * Try to retrieve the next record to the left/right from the current one.
671 */
672STATIC int
673xfs_ialloc_next_rec(
674 struct xfs_btree_cur *cur,
675 xfs_inobt_rec_incore_t *rec,
676 int *done,
677 int left)
678{
679 int error;
680 int i;
681
682 if (left)
683 error = xfs_btree_decrement(cur, 0, &i);
684 else
685 error = xfs_btree_increment(cur, 0, &i);
686
687 if (error)
688 return error;
689 *done = !i;
690 if (i) {
691 error = xfs_inobt_get_rec(cur, rec, &i);
692 if (error)
693 return error;
694 XFS_WANT_CORRUPTED_RETURN(i == 1);
695 }
696
697 return 0;
698}
699
Dave Chinnerbd169562009-08-31 20:58:28 -0300700STATIC int
701xfs_ialloc_get_rec(
702 struct xfs_btree_cur *cur,
703 xfs_agino_t agino,
704 xfs_inobt_rec_incore_t *rec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800705 int *done)
Dave Chinnerbd169562009-08-31 20:58:28 -0300706{
707 int error;
708 int i;
709
710 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
711 if (error)
712 return error;
713 *done = !i;
714 if (i) {
715 error = xfs_inobt_get_rec(cur, rec, &i);
716 if (error)
717 return error;
718 XFS_WANT_CORRUPTED_RETURN(i == 1);
719 }
720
721 return 0;
722}
Dave Chinner0b48db82009-08-31 20:57:09 -0300723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/*
Brian Foster6dd86382014-04-24 16:00:53 +1000725 * Allocate an inode using the inobt-only algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400727STATIC int
Brian Foster6dd86382014-04-24 16:00:53 +1000728xfs_dialloc_ag_inobt(
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400729 struct xfs_trans *tp,
730 struct xfs_buf *agbp,
731 xfs_ino_t parent,
732 xfs_ino_t *inop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -0400734 struct xfs_mount *mp = tp->t_mountp;
735 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
736 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
737 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
738 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
739 struct xfs_perag *pag;
740 struct xfs_btree_cur *cur, *tcur;
741 struct xfs_inobt_rec_incore rec, trec;
742 xfs_ino_t ino;
743 int error;
744 int offset;
745 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dave Chinner44b56e02010-01-11 11:47:43 +0000747 pag = xfs_perag_get(mp, agno);
Dave Chinnerbd169562009-08-31 20:58:28 -0300748
Christoph Hellwig4bb61062012-07-04 10:54:48 -0400749 ASSERT(pag->pagi_init);
750 ASSERT(pag->pagi_inodeok);
751 ASSERT(pag->pagi_freecount > 0);
752
Dave Chinnerbd169562009-08-31 20:58:28 -0300753 restart_pagno:
Brian Foster57bd3db2014-04-24 16:00:50 +1000754 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /*
756 * If pagino is 0 (this is the root inode allocation) use newino.
757 * This must work because we've just allocated some.
758 */
759 if (!pagino)
Christoph Hellwig16259e72005-11-02 15:11:25 +1100760 pagino = be32_to_cpu(agi->agi_newino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Dave Chinner0b48db82009-08-31 20:57:09 -0300762 error = xfs_check_agi_freecount(cur, agi);
763 if (error)
764 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300767 * If in the same AG as the parent, try to get near the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
769 if (pagno == agno) {
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300770 int doneleft; /* done, to the left */
771 int doneright; /* done, to the right */
Dave Chinnerbd169562009-08-31 20:58:28 -0300772 int searchdistance = 10;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300773
Christoph Hellwig21875502009-08-31 20:58:21 -0300774 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300777 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
778
779 error = xfs_inobt_get_rec(cur, &rec, &j);
780 if (error)
781 goto error0;
Brian Fosterb1210992013-08-27 17:15:45 -0400782 XFS_WANT_CORRUPTED_GOTO(j == 1, error0);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300783
784 if (rec.ir_freecount > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /*
786 * Found a free inode in the same chunk
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300787 * as the parent, done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300789 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300791
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300794 * In the same AG as parent, but parent's chunk is full.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300797 /* duplicate the cursor, search left & right simultaneously */
798 error = xfs_btree_dup_cursor(cur, &tcur);
799 if (error)
800 goto error0;
801
Dave Chinnerbd169562009-08-31 20:58:28 -0300802 /*
803 * Skip to last blocks looked up if same parent inode.
804 */
805 if (pagino != NULLAGINO &&
806 pag->pagl_pagino == pagino &&
807 pag->pagl_leftrec != NULLAGINO &&
808 pag->pagl_rightrec != NULLAGINO) {
809 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800810 &trec, &doneleft);
Dave Chinnerbd169562009-08-31 20:58:28 -0300811 if (error)
812 goto error1;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300813
Dave Chinnerbd169562009-08-31 20:58:28 -0300814 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
Jie Liu43df2ee2013-06-24 13:37:23 +0800815 &rec, &doneright);
Dave Chinnerbd169562009-08-31 20:58:28 -0300816 if (error)
817 goto error1;
818 } else {
819 /* search left with tcur, back up 1 record */
820 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
821 if (error)
822 goto error1;
823
824 /* search right with cur, go forward 1 record. */
825 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
826 if (error)
827 goto error1;
828 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300829
830 /*
831 * Loop until we find an inode chunk with a free inode.
832 */
833 while (!doneleft || !doneright) {
834 int useleft; /* using left inode chunk this time */
835
Dave Chinnerbd169562009-08-31 20:58:28 -0300836 if (!--searchdistance) {
837 /*
838 * Not in range - save last search
839 * location and allocate a new inode
840 */
Eric Sandeen3b826382009-10-30 09:27:07 +0100841 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300842 pag->pagl_leftrec = trec.ir_startino;
843 pag->pagl_rightrec = rec.ir_startino;
844 pag->pagl_pagino = pagino;
845 goto newino;
846 }
847
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300848 /* figure out the closer block if both are valid. */
849 if (!doneleft && !doneright) {
850 useleft = pagino -
851 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
852 rec.ir_startino - pagino;
853 } else {
854 useleft = !doneleft;
855 }
856
857 /* free inodes to the left? */
858 if (useleft && trec.ir_freecount) {
859 rec = trec;
860 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
861 cur = tcur;
Dave Chinnerbd169562009-08-31 20:58:28 -0300862
863 pag->pagl_leftrec = trec.ir_startino;
864 pag->pagl_rightrec = rec.ir_startino;
865 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300866 goto alloc_inode;
867 }
868
869 /* free inodes to the right? */
870 if (!useleft && rec.ir_freecount) {
871 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Dave Chinnerbd169562009-08-31 20:58:28 -0300872
873 pag->pagl_leftrec = trec.ir_startino;
874 pag->pagl_rightrec = rec.ir_startino;
875 pag->pagl_pagino = pagino;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300876 goto alloc_inode;
877 }
878
879 /* get next record to check */
880 if (useleft) {
881 error = xfs_ialloc_next_rec(tcur, &trec,
882 &doneleft, 1);
883 } else {
884 error = xfs_ialloc_next_rec(cur, &rec,
885 &doneright, 0);
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300890
891 /*
892 * We've reached the end of the btree. because
893 * we are only searching a small chunk of the
894 * btree each search, there is obviously free
895 * inodes closer to the parent inode than we
896 * are now. restart the search again.
897 */
898 pag->pagl_pagino = NULLAGINO;
899 pag->pagl_leftrec = NULLAGINO;
900 pag->pagl_rightrec = NULLAGINO;
901 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
902 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
903 goto restart_pagno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300907 * In a different AG from the parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * See if the most recently allocated block has any free.
909 */
Dave Chinnerbd169562009-08-31 20:58:28 -0300910newino:
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200911 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
Christoph Hellwig21875502009-08-31 20:58:21 -0300912 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
913 XFS_LOOKUP_EQ, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300914 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300916
917 if (i == 1) {
918 error = xfs_inobt_get_rec(cur, &rec, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (error)
920 goto error0;
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300921
922 if (j == 1 && rec.ir_freecount > 0) {
923 /*
924 * The last chunk allocated in the group
925 * still has a free inode.
926 */
927 goto alloc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 }
Dave Chinnerbd169562009-08-31 20:58:28 -0300930 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300931
Dave Chinnerbd169562009-08-31 20:58:28 -0300932 /*
933 * None left in the last group, search the whole AG
934 */
935 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
936 if (error)
937 goto error0;
938 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
939
940 for (;;) {
941 error = xfs_inobt_get_rec(cur, &rec, &i);
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300942 if (error)
943 goto error0;
944 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Dave Chinnerbd169562009-08-31 20:58:28 -0300945 if (rec.ir_freecount > 0)
946 break;
947 error = xfs_btree_increment(cur, 0, &i);
948 if (error)
949 goto error0;
950 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Christoph Hellwig4254b0b2009-08-31 20:57:14 -0300952
953alloc_inode:
Christoph Hellwig824c3132012-07-04 10:54:45 -0400954 offset = xfs_lowbit64(rec.ir_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 ASSERT(offset >= 0);
956 ASSERT(offset < XFS_INODES_PER_CHUNK);
957 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
958 XFS_INODES_PER_CHUNK) == 0);
959 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
Christoph Hellwig0d87e652009-02-09 08:37:14 +0100960 rec.ir_free &= ~XFS_INOBT_MASK(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 rec.ir_freecount--;
Christoph Hellwigafabc242009-08-31 20:57:03 -0300962 error = xfs_inobt_update(cur, &rec);
963 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto error0;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800965 be32_add_cpu(&agi->agi_freecount, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +0000967 pag->pagi_freecount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Dave Chinner0b48db82009-08-31 20:57:09 -0300969 error = xfs_check_agi_freecount(cur, agi);
970 if (error)
971 goto error0;
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
974 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
Dave Chinner44b56e02010-01-11 11:47:43 +0000975 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 *inop = ino;
977 return 0;
978error1:
979 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
980error0:
981 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
Dave Chinner44b56e02010-01-11 11:47:43 +0000982 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return error;
984}
985
986/*
Brian Foster6dd86382014-04-24 16:00:53 +1000987 * Use the free inode btree to allocate an inode based on distance from the
988 * parent. Note that the provided cursor may be deleted and replaced.
989 */
990STATIC int
991xfs_dialloc_ag_finobt_near(
992 xfs_agino_t pagino,
993 struct xfs_btree_cur **ocur,
994 struct xfs_inobt_rec_incore *rec)
995{
996 struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
997 struct xfs_btree_cur *rcur; /* right search cursor */
998 struct xfs_inobt_rec_incore rrec;
999 int error;
1000 int i, j;
1001
1002 error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
1003 if (error)
1004 return error;
1005
1006 if (i == 1) {
1007 error = xfs_inobt_get_rec(lcur, rec, &i);
1008 if (error)
1009 return error;
1010 XFS_WANT_CORRUPTED_RETURN(i == 1);
1011
1012 /*
1013 * See if we've landed in the parent inode record. The finobt
1014 * only tracks chunks with at least one free inode, so record
1015 * existence is enough.
1016 */
1017 if (pagino >= rec->ir_startino &&
1018 pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
1019 return 0;
1020 }
1021
1022 error = xfs_btree_dup_cursor(lcur, &rcur);
1023 if (error)
1024 return error;
1025
1026 error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
1027 if (error)
1028 goto error_rcur;
1029 if (j == 1) {
1030 error = xfs_inobt_get_rec(rcur, &rrec, &j);
1031 if (error)
1032 goto error_rcur;
1033 XFS_WANT_CORRUPTED_GOTO(j == 1, error_rcur);
1034 }
1035
1036 XFS_WANT_CORRUPTED_GOTO(i == 1 || j == 1, error_rcur);
1037 if (i == 1 && j == 1) {
1038 /*
1039 * Both the left and right records are valid. Choose the closer
1040 * inode chunk to the target.
1041 */
1042 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
1043 (rrec.ir_startino - pagino)) {
1044 *rec = rrec;
1045 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1046 *ocur = rcur;
1047 } else {
1048 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1049 }
1050 } else if (j == 1) {
1051 /* only the right record is valid */
1052 *rec = rrec;
1053 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1054 *ocur = rcur;
1055 } else if (i == 1) {
1056 /* only the left record is valid */
1057 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1058 }
1059
1060 return 0;
1061
1062error_rcur:
1063 xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
1064 return error;
1065}
1066
1067/*
1068 * Use the free inode btree to find a free inode based on a newino hint. If
1069 * the hint is NULL, find the first free inode in the AG.
1070 */
1071STATIC int
1072xfs_dialloc_ag_finobt_newino(
1073 struct xfs_agi *agi,
1074 struct xfs_btree_cur *cur,
1075 struct xfs_inobt_rec_incore *rec)
1076{
1077 int error;
1078 int i;
1079
1080 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1081 error = xfs_inobt_lookup(cur, agi->agi_newino, XFS_LOOKUP_EQ,
1082 &i);
1083 if (error)
1084 return error;
1085 if (i == 1) {
1086 error = xfs_inobt_get_rec(cur, rec, &i);
1087 if (error)
1088 return error;
1089 XFS_WANT_CORRUPTED_RETURN(i == 1);
1090
1091 return 0;
1092 }
1093 }
1094
1095 /*
1096 * Find the first inode available in the AG.
1097 */
1098 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1099 if (error)
1100 return error;
1101 XFS_WANT_CORRUPTED_RETURN(i == 1);
1102
1103 error = xfs_inobt_get_rec(cur, rec, &i);
1104 if (error)
1105 return error;
1106 XFS_WANT_CORRUPTED_RETURN(i == 1);
1107
1108 return 0;
1109}
1110
1111/*
1112 * Update the inobt based on a modification made to the finobt. Also ensure that
1113 * the records from both trees are equivalent post-modification.
1114 */
1115STATIC int
1116xfs_dialloc_ag_update_inobt(
1117 struct xfs_btree_cur *cur, /* inobt cursor */
1118 struct xfs_inobt_rec_incore *frec, /* finobt record */
1119 int offset) /* inode offset */
1120{
1121 struct xfs_inobt_rec_incore rec;
1122 int error;
1123 int i;
1124
1125 error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
1126 if (error)
1127 return error;
1128 XFS_WANT_CORRUPTED_RETURN(i == 1);
1129
1130 error = xfs_inobt_get_rec(cur, &rec, &i);
1131 if (error)
1132 return error;
1133 XFS_WANT_CORRUPTED_RETURN(i == 1);
1134 ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
1135 XFS_INODES_PER_CHUNK) == 0);
1136
1137 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1138 rec.ir_freecount--;
1139
1140 XFS_WANT_CORRUPTED_RETURN((rec.ir_free == frec->ir_free) &&
1141 (rec.ir_freecount == frec->ir_freecount));
1142
1143 error = xfs_inobt_update(cur, &rec);
1144 if (error)
1145 return error;
1146
1147 return 0;
1148}
1149
1150/*
1151 * Allocate an inode using the free inode btree, if available. Otherwise, fall
1152 * back to the inobt search algorithm.
1153 *
1154 * The caller selected an AG for us, and made sure that free inodes are
1155 * available.
1156 */
1157STATIC int
1158xfs_dialloc_ag(
1159 struct xfs_trans *tp,
1160 struct xfs_buf *agbp,
1161 xfs_ino_t parent,
1162 xfs_ino_t *inop)
1163{
1164 struct xfs_mount *mp = tp->t_mountp;
1165 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1166 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1167 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1168 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1169 struct xfs_perag *pag;
1170 struct xfs_btree_cur *cur; /* finobt cursor */
1171 struct xfs_btree_cur *icur; /* inobt cursor */
1172 struct xfs_inobt_rec_incore rec;
1173 xfs_ino_t ino;
1174 int error;
1175 int offset;
1176 int i;
1177
1178 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
1179 return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
1180
1181 pag = xfs_perag_get(mp, agno);
1182
1183 /*
1184 * If pagino is 0 (this is the root inode allocation) use newino.
1185 * This must work because we've just allocated some.
1186 */
1187 if (!pagino)
1188 pagino = be32_to_cpu(agi->agi_newino);
1189
1190 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1191
1192 error = xfs_check_agi_freecount(cur, agi);
1193 if (error)
1194 goto error_cur;
1195
1196 /*
1197 * The search algorithm depends on whether we're in the same AG as the
1198 * parent. If so, find the closest available inode to the parent. If
1199 * not, consider the agi hint or find the first free inode in the AG.
1200 */
1201 if (agno == pagno)
1202 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
1203 else
1204 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
1205 if (error)
1206 goto error_cur;
1207
1208 offset = xfs_lowbit64(rec.ir_free);
1209 ASSERT(offset >= 0);
1210 ASSERT(offset < XFS_INODES_PER_CHUNK);
1211 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1212 XFS_INODES_PER_CHUNK) == 0);
1213 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1214
1215 /*
1216 * Modify or remove the finobt record.
1217 */
1218 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1219 rec.ir_freecount--;
1220 if (rec.ir_freecount)
1221 error = xfs_inobt_update(cur, &rec);
1222 else
1223 error = xfs_btree_delete(cur, &i);
1224 if (error)
1225 goto error_cur;
1226
1227 /*
1228 * The finobt has now been updated appropriately. We haven't updated the
1229 * agi and superblock yet, so we can create an inobt cursor and validate
1230 * the original freecount. If all is well, make the equivalent update to
1231 * the inobt using the finobt record and offset information.
1232 */
1233 icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1234
1235 error = xfs_check_agi_freecount(icur, agi);
1236 if (error)
1237 goto error_icur;
1238
1239 error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
1240 if (error)
1241 goto error_icur;
1242
1243 /*
1244 * Both trees have now been updated. We must update the perag and
1245 * superblock before we can check the freecount for each btree.
1246 */
1247 be32_add_cpu(&agi->agi_freecount, -1);
1248 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1249 pag->pagi_freecount--;
1250
1251 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1252
1253 error = xfs_check_agi_freecount(icur, agi);
1254 if (error)
1255 goto error_icur;
1256 error = xfs_check_agi_freecount(cur, agi);
1257 if (error)
1258 goto error_icur;
1259
1260 xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
1261 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1262 xfs_perag_put(pag);
1263 *inop = ino;
1264 return 0;
1265
1266error_icur:
1267 xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
1268error_cur:
1269 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1270 xfs_perag_put(pag);
1271 return error;
1272}
1273
1274/*
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001275 * Allocate an inode on disk.
1276 *
1277 * Mode is used to tell whether the new inode will need space, and whether it
1278 * is a directory.
1279 *
1280 * This function is designed to be called twice if it has to do an allocation
1281 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
1282 * If an inode is available without having to performn an allocation, an inode
Carlos Maiolinocd856db2012-10-20 11:08:19 -03001283 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
1284 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
1285 * The caller should then commit the current transaction, allocate a
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001286 * new transaction, and call xfs_dialloc() again, passing in the previous value
1287 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
1288 * buffer is locked across the two calls, the second call is guaranteed to have
1289 * a free inode available.
1290 *
1291 * Once we successfully pick an inode its number is returned and the on-disk
1292 * data structures are updated. The inode itself is not read in, since doing so
1293 * would break ordering constraints with xfs_reclaim.
1294 */
1295int
1296xfs_dialloc(
1297 struct xfs_trans *tp,
1298 xfs_ino_t parent,
1299 umode_t mode,
1300 int okalloc,
1301 struct xfs_buf **IO_agbp,
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001302 xfs_ino_t *inop)
1303{
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001304 struct xfs_mount *mp = tp->t_mountp;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001305 struct xfs_buf *agbp;
1306 xfs_agnumber_t agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001307 int error;
1308 int ialloced;
1309 int noroom = 0;
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001310 xfs_agnumber_t start_agno;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001311 struct xfs_perag *pag;
1312
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001313 if (*IO_agbp) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001314 /*
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001315 * If the caller passes in a pointer to the AGI buffer,
1316 * continue where we left off before. In this case, we
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001317 * know that the allocation group has free inodes.
1318 */
1319 agbp = *IO_agbp;
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001320 goto out_alloc;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001321 }
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001322
1323 /*
1324 * We do not have an agbp, so select an initial allocation
1325 * group for inode allocation.
1326 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001327 start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
1328 if (start_agno == NULLAGNUMBER) {
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001329 *inop = NULLFSINO;
1330 return 0;
1331 }
Christoph Hellwig55d6af62012-07-04 10:54:49 -04001332
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001333 /*
1334 * If we have already hit the ceiling of inode blocks then clear
1335 * okalloc so we scan all available agi structures for a free
1336 * inode.
1337 */
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001338 if (mp->m_maxicount &&
Jie Liu71783432013-12-13 15:51:46 +11001339 mp->m_sb.sb_icount + mp->m_ialloc_inos > mp->m_maxicount) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001340 noroom = 1;
1341 okalloc = 0;
1342 }
1343
1344 /*
1345 * Loop until we find an allocation group that either has free inodes
1346 * or in which we can allocate some inodes. Iterate through the
1347 * allocation groups upward, wrapping at the end.
1348 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001349 agno = start_agno;
1350 for (;;) {
1351 pag = xfs_perag_get(mp, agno);
1352 if (!pag->pagi_inodeok) {
1353 xfs_ialloc_next_ag(mp);
1354 goto nextag;
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001355 }
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001356
1357 if (!pag->pagi_init) {
1358 error = xfs_ialloc_pagi_init(mp, tp, agno);
1359 if (error)
1360 goto out_error;
1361 }
1362
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001363 /*
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001364 * Do a first racy fast path check if this AG is usable.
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001365 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001366 if (!pag->pagi_freecount && !okalloc)
1367 goto nextag;
1368
Christoph Hellwigc4982112012-08-07 02:02:02 -04001369 /*
1370 * Then read in the AGI buffer and recheck with the AGI buffer
1371 * lock held.
1372 */
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001373 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1374 if (error)
1375 goto out_error;
1376
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001377 if (pag->pagi_freecount) {
1378 xfs_perag_put(pag);
1379 goto out_alloc;
1380 }
1381
Christoph Hellwigc4982112012-08-07 02:02:02 -04001382 if (!okalloc)
1383 goto nextag_relse_buffer;
1384
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001385
1386 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1387 if (error) {
1388 xfs_trans_brelse(tp, agbp);
1389
1390 if (error != ENOSPC)
1391 goto out_error;
1392
1393 xfs_perag_put(pag);
1394 *inop = NULLFSINO;
1395 return 0;
1396 }
1397
1398 if (ialloced) {
1399 /*
1400 * We successfully allocated some inodes, return
1401 * the current context to the caller so that it
1402 * can commit the current transaction and call
1403 * us again where we left off.
1404 */
1405 ASSERT(pag->pagi_freecount > 0);
1406 xfs_perag_put(pag);
1407
1408 *IO_agbp = agbp;
1409 *inop = NULLFSINO;
1410 return 0;
1411 }
1412
Christoph Hellwigc4982112012-08-07 02:02:02 -04001413nextag_relse_buffer:
1414 xfs_trans_brelse(tp, agbp);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001415nextag:
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001416 xfs_perag_put(pag);
1417 if (++agno == mp->m_sb.sb_agcount)
1418 agno = 0;
1419 if (agno == start_agno) {
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001420 *inop = NULLFSINO;
1421 return noroom ? ENOSPC : 0;
1422 }
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001423 }
1424
Christoph Hellwig4bb61062012-07-04 10:54:48 -04001425out_alloc:
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001426 *IO_agbp = NULL;
1427 return xfs_dialloc_ag(tp, agbp, parent, inop);
Christoph Hellwigbe60fe52012-07-04 10:54:50 -04001428out_error:
1429 xfs_perag_put(pag);
1430 return XFS_ERROR(error);
Christoph Hellwigf2ecc5e2012-07-04 10:54:46 -04001431}
1432
Brian Foster2b64ee52014-04-24 16:00:53 +10001433STATIC int
1434xfs_difree_inobt(
1435 struct xfs_mount *mp,
1436 struct xfs_trans *tp,
1437 struct xfs_buf *agbp,
1438 xfs_agino_t agino,
1439 struct xfs_bmap_free *flist,
Dave Chinner0d907a32014-05-20 08:56:00 +10001440 int *deleted,
Brian Foster2b64ee52014-04-24 16:00:53 +10001441 xfs_ino_t *first_ino,
1442 struct xfs_inobt_rec_incore *orec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Brian Foster2b64ee52014-04-24 16:00:53 +10001444 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1445 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1446 struct xfs_perag *pag;
1447 struct xfs_btree_cur *cur;
1448 struct xfs_inobt_rec_incore rec;
1449 int ilen;
1450 int error;
1451 int i;
1452 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001454 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Brian Foster2b64ee52014-04-24 16:00:53 +10001455 ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /*
1458 * Initialize the cursor.
1459 */
Brian Foster57bd3db2014-04-24 16:00:50 +10001460 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Dave Chinner0b48db82009-08-31 20:57:09 -03001462 error = xfs_check_agi_freecount(cur, agi);
1463 if (error)
1464 goto error0;
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /*
1467 * Look for the entry describing this inode.
1468 */
Christoph Hellwig21875502009-08-31 20:58:21 -03001469 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001470 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1471 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto error0;
1473 }
1474 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
Christoph Hellwig2e287a72009-08-31 20:56:58 -03001475 error = xfs_inobt_get_rec(cur, &rec, &i);
1476 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001477 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1478 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 goto error0;
1480 }
1481 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1482 /*
1483 * Get the offset in the inode chunk.
1484 */
1485 off = agino - rec.ir_startino;
1486 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001487 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /*
1489 * Mark the inode free & increment the count.
1490 */
Christoph Hellwig0d87e652009-02-09 08:37:14 +01001491 rec.ir_free |= XFS_INOBT_MASK(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 rec.ir_freecount++;
1493
1494 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001495 * When an inode cluster is free, it becomes eligible for removal
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 */
Josef Jeff Sipek1bd960e2008-02-29 13:58:40 +11001497 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
Jie Liu71783432013-12-13 15:51:46 +11001498 (rec.ir_freecount == mp->m_ialloc_inos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Roger Willcocks376c2f32014-05-20 08:52:21 +10001500 *deleted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1502
1503 /*
1504 * Remove the inode cluster from the AGI B+Tree, adjust the
1505 * AGI and Superblock inode counts, and mark the disk space
1506 * to be freed when the transaction is committed.
1507 */
Jie Liu71783432013-12-13 15:51:46 +11001508 ilen = mp->m_ialloc_inos;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001509 be32_add_cpu(&agi->agi_count, -ilen);
1510 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001512 pag = xfs_perag_get(mp, agno);
1513 pag->pagi_freecount -= ilen - 1;
1514 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1516 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1517
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001518 if ((error = xfs_btree_delete(cur, &i))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001519 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1520 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 goto error0;
1522 }
1523
Jie Liu126cd102013-12-13 15:51:48 +11001524 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno,
1525 XFS_AGINO_TO_AGBNO(mp, rec.ir_startino)),
1526 mp->m_ialloc_blks, flist, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 } else {
Roger Willcocks376c2f32014-05-20 08:52:21 +10001528 *deleted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Christoph Hellwigafabc242009-08-31 20:57:03 -03001530 error = xfs_inobt_update(cur, &rec);
1531 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001532 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1533 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 goto error0;
1535 }
Christoph Hellwigafabc242009-08-31 20:57:03 -03001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 /*
1538 * Change the inode free counts and log the ag/sb changes.
1539 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001540 be32_add_cpu(&agi->agi_freecount, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
Dave Chinner44b56e02010-01-11 11:47:43 +00001542 pag = xfs_perag_get(mp, agno);
1543 pag->pagi_freecount++;
1544 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1546 }
1547
Dave Chinner0b48db82009-08-31 20:57:09 -03001548 error = xfs_check_agi_freecount(cur, agi);
1549 if (error)
1550 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Brian Foster2b64ee52014-04-24 16:00:53 +10001552 *orec = rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1554 return 0;
1555
1556error0:
1557 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1558 return error;
1559}
1560
Brian Foster2b64ee52014-04-24 16:00:53 +10001561/*
Brian Foster3efa4ff2014-04-24 16:00:53 +10001562 * Free an inode in the free inode btree.
1563 */
1564STATIC int
1565xfs_difree_finobt(
1566 struct xfs_mount *mp,
1567 struct xfs_trans *tp,
1568 struct xfs_buf *agbp,
1569 xfs_agino_t agino,
1570 struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
1571{
1572 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1573 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1574 struct xfs_btree_cur *cur;
1575 struct xfs_inobt_rec_incore rec;
1576 int offset = agino - ibtrec->ir_startino;
1577 int error;
1578 int i;
1579
1580 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1581
1582 error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
1583 if (error)
1584 goto error;
1585 if (i == 0) {
1586 /*
1587 * If the record does not exist in the finobt, we must have just
1588 * freed an inode in a previously fully allocated chunk. If not,
1589 * something is out of sync.
1590 */
1591 XFS_WANT_CORRUPTED_GOTO(ibtrec->ir_freecount == 1, error);
1592
1593 error = xfs_inobt_insert_rec(cur, ibtrec->ir_freecount,
1594 ibtrec->ir_free, &i);
1595 if (error)
1596 goto error;
1597 ASSERT(i == 1);
1598
1599 goto out;
1600 }
1601
1602 /*
1603 * Read and update the existing record. We could just copy the ibtrec
1604 * across here, but that would defeat the purpose of having redundant
1605 * metadata. By making the modifications independently, we can catch
1606 * corruptions that we wouldn't see if we just copied from one record
1607 * to another.
1608 */
1609 error = xfs_inobt_get_rec(cur, &rec, &i);
1610 if (error)
1611 goto error;
1612 XFS_WANT_CORRUPTED_GOTO(i == 1, error);
1613
1614 rec.ir_free |= XFS_INOBT_MASK(offset);
1615 rec.ir_freecount++;
1616
1617 XFS_WANT_CORRUPTED_GOTO((rec.ir_free == ibtrec->ir_free) &&
1618 (rec.ir_freecount == ibtrec->ir_freecount),
1619 error);
1620
1621 /*
1622 * The content of inobt records should always match between the inobt
1623 * and finobt. The lifecycle of records in the finobt is different from
1624 * the inobt in that the finobt only tracks records with at least one
1625 * free inode. Hence, if all of the inodes are free and we aren't
1626 * keeping inode chunks permanently on disk, remove the record.
1627 * Otherwise, update the record with the new information.
1628 */
1629 if (rec.ir_freecount == mp->m_ialloc_inos &&
1630 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
1631 error = xfs_btree_delete(cur, &i);
1632 if (error)
1633 goto error;
1634 ASSERT(i == 1);
1635 } else {
1636 error = xfs_inobt_update(cur, &rec);
1637 if (error)
1638 goto error;
1639 }
1640
1641out:
1642 error = xfs_check_agi_freecount(cur, agi);
1643 if (error)
1644 goto error;
1645
1646 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1647 return 0;
1648
1649error:
1650 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1651 return error;
1652}
1653
1654/*
Brian Foster2b64ee52014-04-24 16:00:53 +10001655 * Free disk inode. Carefully avoids touching the incore inode, all
1656 * manipulations incore are the caller's responsibility.
1657 * The on-disk inode is not changed by this operation, only the
1658 * btree (free inode mask) is changed.
1659 */
1660int
1661xfs_difree(
1662 struct xfs_trans *tp, /* transaction pointer */
1663 xfs_ino_t inode, /* inode to be freed */
1664 struct xfs_bmap_free *flist, /* extents to free */
Dave Chinner0d907a32014-05-20 08:56:00 +10001665 int *deleted,/* set if inode cluster was deleted */
Brian Foster2b64ee52014-04-24 16:00:53 +10001666 xfs_ino_t *first_ino)/* first inode in deleted cluster */
1667{
1668 /* REFERENCED */
1669 xfs_agblock_t agbno; /* block number containing inode */
1670 struct xfs_buf *agbp; /* buffer for allocation group header */
1671 xfs_agino_t agino; /* allocation group inode number */
1672 xfs_agnumber_t agno; /* allocation group number */
1673 int error; /* error return value */
1674 struct xfs_mount *mp; /* mount structure for filesystem */
1675 struct xfs_inobt_rec_incore rec;/* btree record */
1676
1677 mp = tp->t_mountp;
1678
1679 /*
1680 * Break up inode number into its components.
1681 */
1682 agno = XFS_INO_TO_AGNO(mp, inode);
1683 if (agno >= mp->m_sb.sb_agcount) {
1684 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1685 __func__, agno, mp->m_sb.sb_agcount);
1686 ASSERT(0);
1687 return XFS_ERROR(EINVAL);
1688 }
1689 agino = XFS_INO_TO_AGINO(mp, inode);
1690 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1691 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1692 __func__, (unsigned long long)inode,
1693 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
1694 ASSERT(0);
1695 return XFS_ERROR(EINVAL);
1696 }
1697 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1698 if (agbno >= mp->m_sb.sb_agblocks) {
1699 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1700 __func__, agbno, mp->m_sb.sb_agblocks);
1701 ASSERT(0);
1702 return XFS_ERROR(EINVAL);
1703 }
1704 /*
1705 * Get the allocation group header.
1706 */
1707 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1708 if (error) {
1709 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1710 __func__, error);
1711 return error;
1712 }
1713
1714 /*
1715 * Fix up the inode allocation btree.
1716 */
Dave Chinner0d907a32014-05-20 08:56:00 +10001717 error = xfs_difree_inobt(mp, tp, agbp, agino, flist, deleted, first_ino,
Brian Foster2b64ee52014-04-24 16:00:53 +10001718 &rec);
1719 if (error)
1720 goto error0;
1721
Brian Foster3efa4ff2014-04-24 16:00:53 +10001722 /*
1723 * Fix up the free inode btree.
1724 */
1725 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
1726 error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
1727 if (error)
1728 goto error0;
1729 }
1730
Brian Foster2b64ee52014-04-24 16:00:53 +10001731 return 0;
1732
1733error0:
1734 return error;
1735}
1736
Dave Chinner7124fe02010-06-24 11:15:33 +10001737STATIC int
1738xfs_imap_lookup(
1739 struct xfs_mount *mp,
1740 struct xfs_trans *tp,
1741 xfs_agnumber_t agno,
1742 xfs_agino_t agino,
1743 xfs_agblock_t agbno,
1744 xfs_agblock_t *chunk_agbno,
1745 xfs_agblock_t *offset_agbno,
1746 int flags)
1747{
1748 struct xfs_inobt_rec_incore rec;
1749 struct xfs_btree_cur *cur;
1750 struct xfs_buf *agbp;
Dave Chinner7124fe02010-06-24 11:15:33 +10001751 int error;
1752 int i;
1753
1754 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1755 if (error) {
Dave Chinner53487782011-03-07 10:05:35 +11001756 xfs_alert(mp,
1757 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1758 __func__, error, agno);
Dave Chinner7124fe02010-06-24 11:15:33 +10001759 return error;
1760 }
1761
1762 /*
Dave Chinner4536f2a2010-08-24 11:42:30 +10001763 * Lookup the inode record for the given agino. If the record cannot be
1764 * found, then it's an invalid inode number and we should abort. Once
1765 * we have a record, we need to ensure it contains the inode number
1766 * we are looking up.
Dave Chinner7124fe02010-06-24 11:15:33 +10001767 */
Brian Foster57bd3db2014-04-24 16:00:50 +10001768 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
Dave Chinner4536f2a2010-08-24 11:42:30 +10001769 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
Dave Chinner7124fe02010-06-24 11:15:33 +10001770 if (!error) {
1771 if (i)
1772 error = xfs_inobt_get_rec(cur, &rec, &i);
1773 if (!error && i == 0)
1774 error = EINVAL;
1775 }
1776
1777 xfs_trans_brelse(tp, agbp);
1778 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1779 if (error)
1780 return error;
1781
Dave Chinner4536f2a2010-08-24 11:42:30 +10001782 /* check that the returned record contains the required inode */
1783 if (rec.ir_startino > agino ||
Jie Liu71783432013-12-13 15:51:46 +11001784 rec.ir_startino + mp->m_ialloc_inos <= agino)
Dave Chinner4536f2a2010-08-24 11:42:30 +10001785 return EINVAL;
1786
Dave Chinner7124fe02010-06-24 11:15:33 +10001787 /* for untrusted inodes check it is allocated first */
Dave Chinner19207792010-06-24 11:15:47 +10001788 if ((flags & XFS_IGET_UNTRUSTED) &&
Dave Chinner7124fe02010-06-24 11:15:33 +10001789 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
1790 return EINVAL;
1791
1792 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1793 *offset_agbno = agbno - *chunk_agbno;
1794 return 0;
1795}
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797/*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001798 * Return the location of the inode in imap, for mapping it into a buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800int
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001801xfs_imap(
1802 xfs_mount_t *mp, /* file system mount structure */
1803 xfs_trans_t *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 xfs_ino_t ino, /* inode to locate */
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001805 struct xfs_imap *imap, /* location map structure */
1806 uint flags) /* flags for inode btree lookup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 xfs_agblock_t agbno; /* block number of inode in the alloc group */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 xfs_agino_t agino; /* inode number within alloc group */
1810 xfs_agnumber_t agno; /* allocation group number */
1811 int blks_per_cluster; /* num blocks per inode cluster */
1812 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 int error; /* error code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 int offset; /* index of inode in its buffer */
Dave Chinner836a94a2013-08-12 20:49:44 +10001816 xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 ASSERT(ino != NULLFSINO);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /*
1821 * Split up the inode number into its parts.
1822 */
1823 agno = XFS_INO_TO_AGNO(mp, ino);
1824 agino = XFS_INO_TO_AGINO(mp, ino);
1825 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1826 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1827 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1828#ifdef DEBUG
Dave Chinner19207792010-06-24 11:15:47 +10001829 /*
1830 * Don't output diagnostic information for untrusted inodes
1831 * as they can be invalid without implying corruption.
1832 */
1833 if (flags & XFS_IGET_UNTRUSTED)
Nathan Scott4d1a2ed2006-06-09 17:12:28 +10001834 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (agno >= mp->m_sb.sb_agcount) {
Dave Chinner53487782011-03-07 10:05:35 +11001836 xfs_alert(mp,
1837 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1838 __func__, agno, mp->m_sb.sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840 if (agbno >= mp->m_sb.sb_agblocks) {
Dave Chinner53487782011-03-07 10:05:35 +11001841 xfs_alert(mp,
1842 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1843 __func__, (unsigned long long)agbno,
1844 (unsigned long)mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
Dave Chinner53487782011-03-07 10:05:35 +11001847 xfs_alert(mp,
1848 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1849 __func__, ino,
1850 XFS_AGINO_TO_INO(mp, agno, agino));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Nathan Scott745b1f472006-09-28 11:02:23 +10001852 xfs_stack_trace();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853#endif /* DEBUG */
1854 return XFS_ERROR(EINVAL);
1855 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001856
Jie Liuf9e5abc2013-12-13 15:51:49 +11001857 blks_per_cluster = xfs_icluster_size_fsb(mp);
Dave Chinner7124fe02010-06-24 11:15:33 +10001858
1859 /*
1860 * For bulkstat and handle lookups, we have an untrusted inode number
1861 * that we have to verify is valid. We cannot do this just by reading
1862 * the inode buffer as it may have been unlinked and removed leaving
1863 * inodes in stale state on disk. Hence we have to do a btree lookup
1864 * in all cases where an untrusted inode number is passed.
1865 */
Dave Chinner19207792010-06-24 11:15:47 +10001866 if (flags & XFS_IGET_UNTRUSTED) {
Dave Chinner7124fe02010-06-24 11:15:33 +10001867 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1868 &chunk_agbno, &offset_agbno, flags);
1869 if (error)
1870 return error;
1871 goto out_map;
1872 }
1873
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001874 /*
1875 * If the inode cluster size is the same as the blocksize or
1876 * smaller we get to the buffer by simple arithmetics.
1877 */
Jie Liuf9e5abc2013-12-13 15:51:49 +11001878 if (blks_per_cluster == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 offset = XFS_INO_TO_OFFSET(mp, ino);
1880 ASSERT(offset < mp->m_sb.sb_inopblock);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001881
1882 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1883 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1884 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return 0;
1886 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001887
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001888 /*
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001889 * If the inode chunks are aligned then use simple maths to
1890 * find the location. Otherwise we have to do a btree
1891 * lookup to find the location.
1892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (mp->m_inoalign_mask) {
1894 offset_agbno = agbno & mp->m_inoalign_mask;
1895 chunk_agbno = agbno - offset_agbno;
1896 } else {
Dave Chinner7124fe02010-06-24 11:15:33 +10001897 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1898 &chunk_agbno, &offset_agbno, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (error)
1900 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001902
Dave Chinner7124fe02010-06-24 11:15:33 +10001903out_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 ASSERT(agbno >= chunk_agbno);
1905 cluster_agbno = chunk_agbno +
1906 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1907 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1908 XFS_INO_TO_OFFSET(mp, ino);
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001909
1910 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1911 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1912 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1913
1914 /*
1915 * If the inode number maps to a block outside the bounds
1916 * of the file system then return NULL rather than calling
1917 * read_buf and panicing when we get an error from the
1918 * driver.
1919 */
1920 if ((imap->im_blkno + imap->im_len) >
1921 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
Dave Chinner53487782011-03-07 10:05:35 +11001922 xfs_alert(mp,
1923 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1924 __func__, (unsigned long long) imap->im_blkno,
Christoph Hellwig94e1b692008-11-28 14:23:41 +11001925 (unsigned long long) imap->im_len,
1926 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1927 return XFS_ERROR(EINVAL);
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
1932/*
1933 * Compute and fill in value of m_in_maxlevels.
1934 */
1935void
1936xfs_ialloc_compute_maxlevels(
1937 xfs_mount_t *mp) /* file system mount structure */
1938{
1939 int level;
1940 uint maxblocks;
1941 uint maxleafents;
1942 int minleafrecs;
1943 int minnoderecs;
1944
1945 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1946 XFS_INODES_PER_CHUNK_LOG;
1947 minleafrecs = mp->m_alloc_mnr[0];
1948 minnoderecs = mp->m_alloc_mnr[1];
1949 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1950 for (level = 1; maxblocks > 1; level++)
1951 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1952 mp->m_in_maxlevels = level;
1953}
1954
1955/*
Brian Fosteraafc3c22014-04-24 16:00:52 +10001956 * Log specified fields for the ag hdr (inode section). The growth of the agi
1957 * structure over time requires that we interpret the buffer as two logical
1958 * regions delineated by the end of the unlinked list. This is due to the size
1959 * of the hash table and its location in the middle of the agi.
1960 *
1961 * For example, a request to log a field before agi_unlinked and a field after
1962 * agi_unlinked could cause us to log the entire hash table and use an excessive
1963 * amount of log space. To avoid this behavior, log the region up through
1964 * agi_unlinked in one call and the region after agi_unlinked through the end of
1965 * the structure in another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 */
1967void
1968xfs_ialloc_log_agi(
1969 xfs_trans_t *tp, /* transaction pointer */
1970 xfs_buf_t *bp, /* allocation group header buffer */
1971 int fields) /* bitmask of fields to log */
1972{
1973 int first; /* first byte number */
1974 int last; /* last byte number */
1975 static const short offsets[] = { /* field starting offsets */
1976 /* keep in sync with bit definitions */
1977 offsetof(xfs_agi_t, agi_magicnum),
1978 offsetof(xfs_agi_t, agi_versionnum),
1979 offsetof(xfs_agi_t, agi_seqno),
1980 offsetof(xfs_agi_t, agi_length),
1981 offsetof(xfs_agi_t, agi_count),
1982 offsetof(xfs_agi_t, agi_root),
1983 offsetof(xfs_agi_t, agi_level),
1984 offsetof(xfs_agi_t, agi_freecount),
1985 offsetof(xfs_agi_t, agi_newino),
1986 offsetof(xfs_agi_t, agi_dirino),
1987 offsetof(xfs_agi_t, agi_unlinked),
Brian Fosteraafc3c22014-04-24 16:00:52 +10001988 offsetof(xfs_agi_t, agi_free_root),
1989 offsetof(xfs_agi_t, agi_free_level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 sizeof(xfs_agi_t)
1991 };
1992#ifdef DEBUG
1993 xfs_agi_t *agi; /* allocation group header */
1994
1995 agi = XFS_BUF_TO_AGI(bp);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001996 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997#endif
Brian Fosteraafc3c22014-04-24 16:00:52 +10001998
Dave Chinner61fe1352013-04-03 16:11:30 +11001999 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
Brian Fosteraafc3c22014-04-24 16:00:52 +10002000
2001 /*
2002 * Compute byte offsets for the first and last fields in the first
2003 * region and log the agi buffer. This only logs up through
2004 * agi_unlinked.
2005 */
2006 if (fields & XFS_AGI_ALL_BITS_R1) {
2007 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
2008 &first, &last);
2009 xfs_trans_log_buf(tp, bp, first, last);
2010 }
2011
2012 /*
2013 * Mask off the bits in the first region and calculate the first and
2014 * last field offsets for any bits in the second region.
2015 */
2016 fields &= ~XFS_AGI_ALL_BITS_R1;
2017 if (fields) {
2018 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
2019 &first, &last);
2020 xfs_trans_log_buf(tp, bp, first, last);
2021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002024#ifdef DEBUG
2025STATIC void
2026xfs_check_agi_unlinked(
2027 struct xfs_agi *agi)
2028{
2029 int i;
2030
2031 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
2032 ASSERT(agi->agi_unlinked[i]);
2033}
2034#else
2035#define xfs_check_agi_unlinked(agi)
2036#endif
2037
Dave Chinner983d09f2013-04-03 16:11:15 +11002038static bool
Dave Chinner612cfbf2012-11-14 17:52:32 +11002039xfs_agi_verify(
Dave Chinner3702ce62012-11-12 22:54:05 +11002040 struct xfs_buf *bp)
2041{
2042 struct xfs_mount *mp = bp->b_target->bt_mount;
2043 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
Dave Chinner3702ce62012-11-12 22:54:05 +11002044
Dave Chinner983d09f2013-04-03 16:11:15 +11002045 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2046 !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
2047 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002048 /*
2049 * Validate the magic number of the agi block.
2050 */
Dave Chinner983d09f2013-04-03 16:11:15 +11002051 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
2052 return false;
2053 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
2054 return false;
Dave Chinner3702ce62012-11-12 22:54:05 +11002055
2056 /*
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))
2077 xfs_buf_ioerror(bp, EFSBADCRC);
2078 else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp,
2079 XFS_ERRTAG_IALLOC_READ_AGI,
2080 XFS_RANDOM_IALLOC_READ_AGI))
Dave Chinner983d09f2013-04-03 16:11:15 +11002081 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 Chinner983d09f2013-04-03 16:11:15 +11002095 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
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002134 ASSERT(!xfs_buf_geterror(*bpp));
Christoph Hellwig38f23232011-10-10 16:52:45 +00002135 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002136 return 0;
2137}
2138
2139int
2140xfs_ialloc_read_agi(
2141 struct xfs_mount *mp, /* file system mount structure */
2142 struct xfs_trans *tp, /* transaction pointer */
2143 xfs_agnumber_t agno, /* allocation group number */
2144 struct xfs_buf **bpp) /* allocation group hdr buf */
2145{
2146 struct xfs_agi *agi; /* allocation group header */
2147 struct xfs_perag *pag; /* per allocation group data */
2148 int error;
2149
Dave Chinnerd1230312013-11-01 15:27:19 +11002150 trace_xfs_ialloc_read_agi(mp, agno);
2151
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002152 error = xfs_read_agi(mp, tp, agno, bpp);
2153 if (error)
2154 return error;
2155
2156 agi = XFS_BUF_TO_AGI(*bpp);
Dave Chinner44b56e02010-01-11 11:47:43 +00002157 pag = xfs_perag_get(mp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (!pag->pagi_init) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11002159 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
David Chinner92821e22007-05-24 15:26:31 +10002160 pag->pagi_count = be32_to_cpu(agi->agi_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 pag->pagi_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11002164 /*
2165 * It's possible for these to be out of sync if
2166 * we are in the middle of a forced shutdown.
2167 */
2168 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
2169 XFS_FORCED_SHUTDOWN(mp));
Dave Chinner44b56e02010-01-11 11:47:43 +00002170 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return 0;
2172}
David Chinner92821e22007-05-24 15:26:31 +10002173
2174/*
2175 * Read in the agi to initialise the per-ag data in the mount structure
2176 */
2177int
2178xfs_ialloc_pagi_init(
2179 xfs_mount_t *mp, /* file system mount structure */
2180 xfs_trans_t *tp, /* transaction pointer */
2181 xfs_agnumber_t agno) /* allocation group number */
2182{
2183 xfs_buf_t *bp = NULL;
2184 int error;
2185
2186 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
2187 if (error)
2188 return error;
2189 if (bp)
2190 xfs_trans_brelse(tp, bp);
2191 return 0;
2192}