xfs_ialloc.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * 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.
  13. *
  14. * 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
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_btree.h"
  29. #include "xfs_ialloc.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_alloc.h"
  32. #include "xfs_rtalloc.h"
  33. #include "xfs_error.h"
  34. #include "xfs_bmap.h"
  35. #include "xfs_cksum.h"
  36. #include "xfs_trans.h"
  37. #include "xfs_buf_item.h"
  38. #include "xfs_icreate_item.h"
  39. #include "xfs_icache.h"
  40. #include "xfs_trace.h"
  41. /*
  42. * Allocation group level functions.
  43. */
  44. static inline int
  45. xfs_ialloc_cluster_alignment(
  46. struct xfs_mount *mp)
  47. {
  48. if (xfs_sb_version_hasalign(&mp->m_sb) &&
  49. mp->m_sb.sb_inoalignmt >=
  50. XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
  51. return mp->m_sb.sb_inoalignmt;
  52. return 1;
  53. }
  54. /*
  55. * Lookup a record by ino in the btree given by cur.
  56. */
  57. int /* error */
  58. xfs_inobt_lookup(
  59. struct xfs_btree_cur *cur, /* btree cursor */
  60. xfs_agino_t ino, /* starting inode of chunk */
  61. xfs_lookup_t dir, /* <=, >=, == */
  62. int *stat) /* success/failure */
  63. {
  64. cur->bc_rec.i.ir_startino = ino;
  65. cur->bc_rec.i.ir_freecount = 0;
  66. cur->bc_rec.i.ir_free = 0;
  67. return xfs_btree_lookup(cur, dir, stat);
  68. }
  69. /*
  70. * Update the record referred to by cur to the value given.
  71. * This either works (return 0) or gets an EFSCORRUPTED error.
  72. */
  73. STATIC int /* error */
  74. xfs_inobt_update(
  75. struct xfs_btree_cur *cur, /* btree cursor */
  76. xfs_inobt_rec_incore_t *irec) /* btree record */
  77. {
  78. union xfs_btree_rec rec;
  79. rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
  80. rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
  81. rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
  82. return xfs_btree_update(cur, &rec);
  83. }
  84. /*
  85. * Get the data from the pointed-to record.
  86. */
  87. int /* error */
  88. xfs_inobt_get_rec(
  89. struct xfs_btree_cur *cur, /* btree cursor */
  90. xfs_inobt_rec_incore_t *irec, /* btree record */
  91. int *stat) /* output: success/failure */
  92. {
  93. union xfs_btree_rec *rec;
  94. int error;
  95. error = xfs_btree_get_rec(cur, &rec, stat);
  96. if (!error && *stat == 1) {
  97. irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
  98. irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
  99. irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
  100. }
  101. return error;
  102. }
  103. /*
  104. * Insert a single inobt record. Cursor must already point to desired location.
  105. */
  106. STATIC int
  107. xfs_inobt_insert_rec(
  108. struct xfs_btree_cur *cur,
  109. __int32_t freecount,
  110. xfs_inofree_t free,
  111. int *stat)
  112. {
  113. cur->bc_rec.i.ir_freecount = freecount;
  114. cur->bc_rec.i.ir_free = free;
  115. return xfs_btree_insert(cur, stat);
  116. }
  117. /*
  118. * Insert records describing a newly allocated inode chunk into the inobt.
  119. */
  120. STATIC int
  121. xfs_inobt_insert(
  122. struct xfs_mount *mp,
  123. struct xfs_trans *tp,
  124. struct xfs_buf *agbp,
  125. xfs_agino_t newino,
  126. xfs_agino_t newlen,
  127. xfs_btnum_t btnum)
  128. {
  129. struct xfs_btree_cur *cur;
  130. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  131. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  132. xfs_agino_t thisino;
  133. int i;
  134. int error;
  135. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
  136. for (thisino = newino;
  137. thisino < newino + newlen;
  138. thisino += XFS_INODES_PER_CHUNK) {
  139. error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
  140. if (error) {
  141. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  142. return error;
  143. }
  144. ASSERT(i == 0);
  145. error = xfs_inobt_insert_rec(cur, XFS_INODES_PER_CHUNK,
  146. XFS_INOBT_ALL_FREE, &i);
  147. if (error) {
  148. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  149. return error;
  150. }
  151. ASSERT(i == 1);
  152. }
  153. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  154. return 0;
  155. }
  156. /*
  157. * Verify that the number of free inodes in the AGI is correct.
  158. */
  159. #ifdef DEBUG
  160. STATIC int
  161. xfs_check_agi_freecount(
  162. struct xfs_btree_cur *cur,
  163. struct xfs_agi *agi)
  164. {
  165. if (cur->bc_nlevels == 1) {
  166. xfs_inobt_rec_incore_t rec;
  167. int freecount = 0;
  168. int error;
  169. int i;
  170. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
  171. if (error)
  172. return error;
  173. do {
  174. error = xfs_inobt_get_rec(cur, &rec, &i);
  175. if (error)
  176. return error;
  177. if (i) {
  178. freecount += rec.ir_freecount;
  179. error = xfs_btree_increment(cur, 0, &i);
  180. if (error)
  181. return error;
  182. }
  183. } while (i == 1);
  184. if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
  185. ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
  186. }
  187. return 0;
  188. }
  189. #else
  190. #define xfs_check_agi_freecount(cur, agi) 0
  191. #endif
  192. /*
  193. * Initialise a new set of inodes. When called without a transaction context
  194. * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
  195. * than logging them (which in a transaction context puts them into the AIL
  196. * for writeback rather than the xfsbufd queue).
  197. */
  198. int
  199. xfs_ialloc_inode_init(
  200. struct xfs_mount *mp,
  201. struct xfs_trans *tp,
  202. struct list_head *buffer_list,
  203. xfs_agnumber_t agno,
  204. xfs_agblock_t agbno,
  205. xfs_agblock_t length,
  206. unsigned int gen)
  207. {
  208. struct xfs_buf *fbuf;
  209. struct xfs_dinode *free;
  210. int nbufs, blks_per_cluster, inodes_per_cluster;
  211. int version;
  212. int i, j;
  213. xfs_daddr_t d;
  214. xfs_ino_t ino = 0;
  215. /*
  216. * Loop over the new block(s), filling in the inodes. For small block
  217. * sizes, manipulate the inodes in buffers which are multiples of the
  218. * blocks size.
  219. */
  220. blks_per_cluster = xfs_icluster_size_fsb(mp);
  221. inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
  222. nbufs = length / blks_per_cluster;
  223. /*
  224. * Figure out what version number to use in the inodes we create. If
  225. * the superblock version has caught up to the one that supports the new
  226. * inode format, then use the new inode version. Otherwise use the old
  227. * version so that old kernels will continue to be able to use the file
  228. * system.
  229. *
  230. * For v3 inodes, we also need to write the inode number into the inode,
  231. * so calculate the first inode number of the chunk here as
  232. * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
  233. * across multiple filesystem blocks (such as a cluster) and so cannot
  234. * be used in the cluster buffer loop below.
  235. *
  236. * Further, because we are writing the inode directly into the buffer
  237. * and calculating a CRC on the entire inode, we have ot log the entire
  238. * inode so that the entire range the CRC covers is present in the log.
  239. * That means for v3 inode we log the entire buffer rather than just the
  240. * inode cores.
  241. */
  242. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  243. version = 3;
  244. ino = XFS_AGINO_TO_INO(mp, agno,
  245. XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
  246. /*
  247. * log the initialisation that is about to take place as an
  248. * logical operation. This means the transaction does not
  249. * need to log the physical changes to the inode buffers as log
  250. * recovery will know what initialisation is actually needed.
  251. * Hence we only need to log the buffers as "ordered" buffers so
  252. * they track in the AIL as if they were physically logged.
  253. */
  254. if (tp)
  255. xfs_icreate_log(tp, agno, agbno, mp->m_ialloc_inos,
  256. mp->m_sb.sb_inodesize, length, gen);
  257. } else
  258. version = 2;
  259. for (j = 0; j < nbufs; j++) {
  260. /*
  261. * Get the block.
  262. */
  263. d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
  264. fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  265. mp->m_bsize * blks_per_cluster,
  266. XBF_UNMAPPED);
  267. if (!fbuf)
  268. return -ENOMEM;
  269. /* Initialize the inode buffers and log them appropriately. */
  270. fbuf->b_ops = &xfs_inode_buf_ops;
  271. xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
  272. for (i = 0; i < inodes_per_cluster; i++) {
  273. int ioffset = i << mp->m_sb.sb_inodelog;
  274. uint isize = xfs_dinode_size(version);
  275. free = xfs_make_iptr(mp, fbuf, i);
  276. free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
  277. free->di_version = version;
  278. free->di_gen = cpu_to_be32(gen);
  279. free->di_next_unlinked = cpu_to_be32(NULLAGINO);
  280. if (version == 3) {
  281. free->di_ino = cpu_to_be64(ino);
  282. ino++;
  283. uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
  284. xfs_dinode_calc_crc(mp, free);
  285. } else if (tp) {
  286. /* just log the inode core */
  287. xfs_trans_log_buf(tp, fbuf, ioffset,
  288. ioffset + isize - 1);
  289. }
  290. }
  291. if (tp) {
  292. /*
  293. * Mark the buffer as an inode allocation buffer so it
  294. * sticks in AIL at the point of this allocation
  295. * transaction. This ensures the they are on disk before
  296. * the tail of the log can be moved past this
  297. * transaction (i.e. by preventing relogging from moving
  298. * it forward in the log).
  299. */
  300. xfs_trans_inode_alloc_buf(tp, fbuf);
  301. if (version == 3) {
  302. /*
  303. * Mark the buffer as ordered so that they are
  304. * not physically logged in the transaction but
  305. * still tracked in the AIL as part of the
  306. * transaction and pin the log appropriately.
  307. */
  308. xfs_trans_ordered_buf(tp, fbuf);
  309. xfs_trans_log_buf(tp, fbuf, 0,
  310. BBTOB(fbuf->b_length) - 1);
  311. }
  312. } else {
  313. fbuf->b_flags |= XBF_DONE;
  314. xfs_buf_delwri_queue(fbuf, buffer_list);
  315. xfs_buf_relse(fbuf);
  316. }
  317. }
  318. return 0;
  319. }
  320. /*
  321. * Allocate new inodes in the allocation group specified by agbp.
  322. * Return 0 for success, else error code.
  323. */
  324. STATIC int /* error code or 0 */
  325. xfs_ialloc_ag_alloc(
  326. xfs_trans_t *tp, /* transaction pointer */
  327. xfs_buf_t *agbp, /* alloc group buffer */
  328. int *alloc)
  329. {
  330. xfs_agi_t *agi; /* allocation group header */
  331. xfs_alloc_arg_t args; /* allocation argument structure */
  332. xfs_agnumber_t agno;
  333. int error;
  334. xfs_agino_t newino; /* new first inode's number */
  335. xfs_agino_t newlen; /* new number of inodes */
  336. int isaligned = 0; /* inode allocation at stripe unit */
  337. /* boundary */
  338. struct xfs_perag *pag;
  339. memset(&args, 0, sizeof(args));
  340. args.tp = tp;
  341. args.mp = tp->t_mountp;
  342. /*
  343. * Locking will ensure that we don't have two callers in here
  344. * at one time.
  345. */
  346. newlen = args.mp->m_ialloc_inos;
  347. if (args.mp->m_maxicount &&
  348. percpu_counter_read_positive(&args.mp->m_icount) + newlen >
  349. args.mp->m_maxicount)
  350. return -ENOSPC;
  351. args.minlen = args.maxlen = args.mp->m_ialloc_blks;
  352. /*
  353. * First try to allocate inodes contiguous with the last-allocated
  354. * chunk of inodes. If the filesystem is striped, this will fill
  355. * an entire stripe unit with inodes.
  356. */
  357. agi = XFS_BUF_TO_AGI(agbp);
  358. newino = be32_to_cpu(agi->agi_newino);
  359. agno = be32_to_cpu(agi->agi_seqno);
  360. args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
  361. args.mp->m_ialloc_blks;
  362. if (likely(newino != NULLAGINO &&
  363. (args.agbno < be32_to_cpu(agi->agi_length)))) {
  364. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  365. args.type = XFS_ALLOCTYPE_THIS_BNO;
  366. args.prod = 1;
  367. /*
  368. * We need to take into account alignment here to ensure that
  369. * we don't modify the free list if we fail to have an exact
  370. * block. If we don't have an exact match, and every oher
  371. * attempt allocation attempt fails, we'll end up cancelling
  372. * a dirty transaction and shutting down.
  373. *
  374. * For an exact allocation, alignment must be 1,
  375. * however we need to take cluster alignment into account when
  376. * fixing up the freelist. Use the minalignslop field to
  377. * indicate that extra blocks might be required for alignment,
  378. * but not to use them in the actual exact allocation.
  379. */
  380. args.alignment = 1;
  381. args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1;
  382. /* Allow space for the inode btree to split. */
  383. args.minleft = args.mp->m_in_maxlevels - 1;
  384. if ((error = xfs_alloc_vextent(&args)))
  385. return error;
  386. /*
  387. * This request might have dirtied the transaction if the AG can
  388. * satisfy the request, but the exact block was not available.
  389. * If the allocation did fail, subsequent requests will relax
  390. * the exact agbno requirement and increase the alignment
  391. * instead. It is critical that the total size of the request
  392. * (len + alignment + slop) does not increase from this point
  393. * on, so reset minalignslop to ensure it is not included in
  394. * subsequent requests.
  395. */
  396. args.minalignslop = 0;
  397. } else
  398. args.fsbno = NULLFSBLOCK;
  399. if (unlikely(args.fsbno == NULLFSBLOCK)) {
  400. /*
  401. * Set the alignment for the allocation.
  402. * If stripe alignment is turned on then align at stripe unit
  403. * boundary.
  404. * If the cluster size is smaller than a filesystem block
  405. * then we're doing I/O for inodes in filesystem block size
  406. * pieces, so don't need alignment anyway.
  407. */
  408. isaligned = 0;
  409. if (args.mp->m_sinoalign) {
  410. ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
  411. args.alignment = args.mp->m_dalign;
  412. isaligned = 1;
  413. } else
  414. args.alignment = xfs_ialloc_cluster_alignment(args.mp);
  415. /*
  416. * Need to figure out where to allocate the inode blocks.
  417. * Ideally they should be spaced out through the a.g.
  418. * For now, just allocate blocks up front.
  419. */
  420. args.agbno = be32_to_cpu(agi->agi_root);
  421. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  422. /*
  423. * Allocate a fixed-size extent of inodes.
  424. */
  425. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  426. args.prod = 1;
  427. /*
  428. * Allow space for the inode btree to split.
  429. */
  430. args.minleft = args.mp->m_in_maxlevels - 1;
  431. if ((error = xfs_alloc_vextent(&args)))
  432. return error;
  433. }
  434. /*
  435. * If stripe alignment is turned on, then try again with cluster
  436. * alignment.
  437. */
  438. if (isaligned && args.fsbno == NULLFSBLOCK) {
  439. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  440. args.agbno = be32_to_cpu(agi->agi_root);
  441. args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
  442. args.alignment = xfs_ialloc_cluster_alignment(args.mp);
  443. if ((error = xfs_alloc_vextent(&args)))
  444. return error;
  445. }
  446. if (args.fsbno == NULLFSBLOCK) {
  447. *alloc = 0;
  448. return 0;
  449. }
  450. ASSERT(args.len == args.minlen);
  451. /*
  452. * Stamp and write the inode buffers.
  453. *
  454. * Seed the new inode cluster with a random generation number. This
  455. * prevents short-term reuse of generation numbers if a chunk is
  456. * freed and then immediately reallocated. We use random numbers
  457. * rather than a linear progression to prevent the next generation
  458. * number from being easily guessable.
  459. */
  460. error = xfs_ialloc_inode_init(args.mp, tp, NULL, agno, args.agbno,
  461. args.len, prandom_u32());
  462. if (error)
  463. return error;
  464. /*
  465. * Convert the results.
  466. */
  467. newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
  468. be32_add_cpu(&agi->agi_count, newlen);
  469. be32_add_cpu(&agi->agi_freecount, newlen);
  470. pag = xfs_perag_get(args.mp, agno);
  471. pag->pagi_freecount += newlen;
  472. xfs_perag_put(pag);
  473. agi->agi_newino = cpu_to_be32(newino);
  474. /*
  475. * Insert records describing the new inode chunk into the btrees.
  476. */
  477. error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
  478. XFS_BTNUM_INO);
  479. if (error)
  480. return error;
  481. if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
  482. error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
  483. XFS_BTNUM_FINO);
  484. if (error)
  485. return error;
  486. }
  487. /*
  488. * Log allocation group header fields
  489. */
  490. xfs_ialloc_log_agi(tp, agbp,
  491. XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
  492. /*
  493. * Modify/log superblock values for inode count and inode free count.
  494. */
  495. xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
  496. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
  497. *alloc = 1;
  498. return 0;
  499. }
  500. STATIC xfs_agnumber_t
  501. xfs_ialloc_next_ag(
  502. xfs_mount_t *mp)
  503. {
  504. xfs_agnumber_t agno;
  505. spin_lock(&mp->m_agirotor_lock);
  506. agno = mp->m_agirotor;
  507. if (++mp->m_agirotor >= mp->m_maxagi)
  508. mp->m_agirotor = 0;
  509. spin_unlock(&mp->m_agirotor_lock);
  510. return agno;
  511. }
  512. /*
  513. * Select an allocation group to look for a free inode in, based on the parent
  514. * inode and the mode. Return the allocation group buffer.
  515. */
  516. STATIC xfs_agnumber_t
  517. xfs_ialloc_ag_select(
  518. xfs_trans_t *tp, /* transaction pointer */
  519. xfs_ino_t parent, /* parent directory inode number */
  520. umode_t mode, /* bits set to indicate file type */
  521. int okalloc) /* ok to allocate more space */
  522. {
  523. xfs_agnumber_t agcount; /* number of ag's in the filesystem */
  524. xfs_agnumber_t agno; /* current ag number */
  525. int flags; /* alloc buffer locking flags */
  526. xfs_extlen_t ineed; /* blocks needed for inode allocation */
  527. xfs_extlen_t longest = 0; /* longest extent available */
  528. xfs_mount_t *mp; /* mount point structure */
  529. int needspace; /* file mode implies space allocated */
  530. xfs_perag_t *pag; /* per allocation group data */
  531. xfs_agnumber_t pagno; /* parent (starting) ag number */
  532. int error;
  533. /*
  534. * Files of these types need at least one block if length > 0
  535. * (and they won't fit in the inode, but that's hard to figure out).
  536. */
  537. needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
  538. mp = tp->t_mountp;
  539. agcount = mp->m_maxagi;
  540. if (S_ISDIR(mode))
  541. pagno = xfs_ialloc_next_ag(mp);
  542. else {
  543. pagno = XFS_INO_TO_AGNO(mp, parent);
  544. if (pagno >= agcount)
  545. pagno = 0;
  546. }
  547. ASSERT(pagno < agcount);
  548. /*
  549. * Loop through allocation groups, looking for one with a little
  550. * free space in it. Note we don't look for free inodes, exactly.
  551. * Instead, we include whether there is a need to allocate inodes
  552. * to mean that blocks must be allocated for them,
  553. * if none are currently free.
  554. */
  555. agno = pagno;
  556. flags = XFS_ALLOC_FLAG_TRYLOCK;
  557. for (;;) {
  558. pag = xfs_perag_get(mp, agno);
  559. if (!pag->pagi_inodeok) {
  560. xfs_ialloc_next_ag(mp);
  561. goto nextag;
  562. }
  563. if (!pag->pagi_init) {
  564. error = xfs_ialloc_pagi_init(mp, tp, agno);
  565. if (error)
  566. goto nextag;
  567. }
  568. if (pag->pagi_freecount) {
  569. xfs_perag_put(pag);
  570. return agno;
  571. }
  572. if (!okalloc)
  573. goto nextag;
  574. if (!pag->pagf_init) {
  575. error = xfs_alloc_pagf_init(mp, tp, agno, flags);
  576. if (error)
  577. goto nextag;
  578. }
  579. /*
  580. * Check that there is enough free space for the file plus a
  581. * chunk of inodes if we need to allocate some. If this is the
  582. * first pass across the AGs, take into account the potential
  583. * space needed for alignment of inode chunks when checking the
  584. * longest contiguous free space in the AG - this prevents us
  585. * from getting ENOSPC because we have free space larger than
  586. * m_ialloc_blks but alignment constraints prevent us from using
  587. * it.
  588. *
  589. * If we can't find an AG with space for full alignment slack to
  590. * be taken into account, we must be near ENOSPC in all AGs.
  591. * Hence we don't include alignment for the second pass and so
  592. * if we fail allocation due to alignment issues then it is most
  593. * likely a real ENOSPC condition.
  594. */
  595. ineed = mp->m_ialloc_blks;
  596. if (flags && ineed > 1)
  597. ineed += xfs_ialloc_cluster_alignment(mp);
  598. longest = pag->pagf_longest;
  599. if (!longest)
  600. longest = pag->pagf_flcount > 0;
  601. if (pag->pagf_freeblks >= needspace + ineed &&
  602. longest >= ineed) {
  603. xfs_perag_put(pag);
  604. return agno;
  605. }
  606. nextag:
  607. xfs_perag_put(pag);
  608. /*
  609. * No point in iterating over the rest, if we're shutting
  610. * down.
  611. */
  612. if (XFS_FORCED_SHUTDOWN(mp))
  613. return NULLAGNUMBER;
  614. agno++;
  615. if (agno >= agcount)
  616. agno = 0;
  617. if (agno == pagno) {
  618. if (flags == 0)
  619. return NULLAGNUMBER;
  620. flags = 0;
  621. }
  622. }
  623. }
  624. /*
  625. * Try to retrieve the next record to the left/right from the current one.
  626. */
  627. STATIC int
  628. xfs_ialloc_next_rec(
  629. struct xfs_btree_cur *cur,
  630. xfs_inobt_rec_incore_t *rec,
  631. int *done,
  632. int left)
  633. {
  634. int error;
  635. int i;
  636. if (left)
  637. error = xfs_btree_decrement(cur, 0, &i);
  638. else
  639. error = xfs_btree_increment(cur, 0, &i);
  640. if (error)
  641. return error;
  642. *done = !i;
  643. if (i) {
  644. error = xfs_inobt_get_rec(cur, rec, &i);
  645. if (error)
  646. return error;
  647. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  648. }
  649. return 0;
  650. }
  651. STATIC int
  652. xfs_ialloc_get_rec(
  653. struct xfs_btree_cur *cur,
  654. xfs_agino_t agino,
  655. xfs_inobt_rec_incore_t *rec,
  656. int *done)
  657. {
  658. int error;
  659. int i;
  660. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
  661. if (error)
  662. return error;
  663. *done = !i;
  664. if (i) {
  665. error = xfs_inobt_get_rec(cur, rec, &i);
  666. if (error)
  667. return error;
  668. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  669. }
  670. return 0;
  671. }
  672. /*
  673. * Allocate an inode using the inobt-only algorithm.
  674. */
  675. STATIC int
  676. xfs_dialloc_ag_inobt(
  677. struct xfs_trans *tp,
  678. struct xfs_buf *agbp,
  679. xfs_ino_t parent,
  680. xfs_ino_t *inop)
  681. {
  682. struct xfs_mount *mp = tp->t_mountp;
  683. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  684. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  685. xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
  686. xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
  687. struct xfs_perag *pag;
  688. struct xfs_btree_cur *cur, *tcur;
  689. struct xfs_inobt_rec_incore rec, trec;
  690. xfs_ino_t ino;
  691. int error;
  692. int offset;
  693. int i, j;
  694. pag = xfs_perag_get(mp, agno);
  695. ASSERT(pag->pagi_init);
  696. ASSERT(pag->pagi_inodeok);
  697. ASSERT(pag->pagi_freecount > 0);
  698. restart_pagno:
  699. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  700. /*
  701. * If pagino is 0 (this is the root inode allocation) use newino.
  702. * This must work because we've just allocated some.
  703. */
  704. if (!pagino)
  705. pagino = be32_to_cpu(agi->agi_newino);
  706. error = xfs_check_agi_freecount(cur, agi);
  707. if (error)
  708. goto error0;
  709. /*
  710. * If in the same AG as the parent, try to get near the parent.
  711. */
  712. if (pagno == agno) {
  713. int doneleft; /* done, to the left */
  714. int doneright; /* done, to the right */
  715. int searchdistance = 10;
  716. error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
  717. if (error)
  718. goto error0;
  719. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  720. error = xfs_inobt_get_rec(cur, &rec, &j);
  721. if (error)
  722. goto error0;
  723. XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0);
  724. if (rec.ir_freecount > 0) {
  725. /*
  726. * Found a free inode in the same chunk
  727. * as the parent, done.
  728. */
  729. goto alloc_inode;
  730. }
  731. /*
  732. * In the same AG as parent, but parent's chunk is full.
  733. */
  734. /* duplicate the cursor, search left & right simultaneously */
  735. error = xfs_btree_dup_cursor(cur, &tcur);
  736. if (error)
  737. goto error0;
  738. /*
  739. * Skip to last blocks looked up if same parent inode.
  740. */
  741. if (pagino != NULLAGINO &&
  742. pag->pagl_pagino == pagino &&
  743. pag->pagl_leftrec != NULLAGINO &&
  744. pag->pagl_rightrec != NULLAGINO) {
  745. error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
  746. &trec, &doneleft);
  747. if (error)
  748. goto error1;
  749. error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
  750. &rec, &doneright);
  751. if (error)
  752. goto error1;
  753. } else {
  754. /* search left with tcur, back up 1 record */
  755. error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
  756. if (error)
  757. goto error1;
  758. /* search right with cur, go forward 1 record. */
  759. error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
  760. if (error)
  761. goto error1;
  762. }
  763. /*
  764. * Loop until we find an inode chunk with a free inode.
  765. */
  766. while (!doneleft || !doneright) {
  767. int useleft; /* using left inode chunk this time */
  768. if (!--searchdistance) {
  769. /*
  770. * Not in range - save last search
  771. * location and allocate a new inode
  772. */
  773. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  774. pag->pagl_leftrec = trec.ir_startino;
  775. pag->pagl_rightrec = rec.ir_startino;
  776. pag->pagl_pagino = pagino;
  777. goto newino;
  778. }
  779. /* figure out the closer block if both are valid. */
  780. if (!doneleft && !doneright) {
  781. useleft = pagino -
  782. (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
  783. rec.ir_startino - pagino;
  784. } else {
  785. useleft = !doneleft;
  786. }
  787. /* free inodes to the left? */
  788. if (useleft && trec.ir_freecount) {
  789. rec = trec;
  790. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  791. cur = tcur;
  792. pag->pagl_leftrec = trec.ir_startino;
  793. pag->pagl_rightrec = rec.ir_startino;
  794. pag->pagl_pagino = pagino;
  795. goto alloc_inode;
  796. }
  797. /* free inodes to the right? */
  798. if (!useleft && rec.ir_freecount) {
  799. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  800. pag->pagl_leftrec = trec.ir_startino;
  801. pag->pagl_rightrec = rec.ir_startino;
  802. pag->pagl_pagino = pagino;
  803. goto alloc_inode;
  804. }
  805. /* get next record to check */
  806. if (useleft) {
  807. error = xfs_ialloc_next_rec(tcur, &trec,
  808. &doneleft, 1);
  809. } else {
  810. error = xfs_ialloc_next_rec(cur, &rec,
  811. &doneright, 0);
  812. }
  813. if (error)
  814. goto error1;
  815. }
  816. /*
  817. * We've reached the end of the btree. because
  818. * we are only searching a small chunk of the
  819. * btree each search, there is obviously free
  820. * inodes closer to the parent inode than we
  821. * are now. restart the search again.
  822. */
  823. pag->pagl_pagino = NULLAGINO;
  824. pag->pagl_leftrec = NULLAGINO;
  825. pag->pagl_rightrec = NULLAGINO;
  826. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  827. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  828. goto restart_pagno;
  829. }
  830. /*
  831. * In a different AG from the parent.
  832. * See if the most recently allocated block has any free.
  833. */
  834. newino:
  835. if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
  836. error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
  837. XFS_LOOKUP_EQ, &i);
  838. if (error)
  839. goto error0;
  840. if (i == 1) {
  841. error = xfs_inobt_get_rec(cur, &rec, &j);
  842. if (error)
  843. goto error0;
  844. if (j == 1 && rec.ir_freecount > 0) {
  845. /*
  846. * The last chunk allocated in the group
  847. * still has a free inode.
  848. */
  849. goto alloc_inode;
  850. }
  851. }
  852. }
  853. /*
  854. * None left in the last group, search the whole AG
  855. */
  856. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
  857. if (error)
  858. goto error0;
  859. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  860. for (;;) {
  861. error = xfs_inobt_get_rec(cur, &rec, &i);
  862. if (error)
  863. goto error0;
  864. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  865. if (rec.ir_freecount > 0)
  866. break;
  867. error = xfs_btree_increment(cur, 0, &i);
  868. if (error)
  869. goto error0;
  870. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  871. }
  872. alloc_inode:
  873. offset = xfs_lowbit64(rec.ir_free);
  874. ASSERT(offset >= 0);
  875. ASSERT(offset < XFS_INODES_PER_CHUNK);
  876. ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
  877. XFS_INODES_PER_CHUNK) == 0);
  878. ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
  879. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  880. rec.ir_freecount--;
  881. error = xfs_inobt_update(cur, &rec);
  882. if (error)
  883. goto error0;
  884. be32_add_cpu(&agi->agi_freecount, -1);
  885. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  886. pag->pagi_freecount--;
  887. error = xfs_check_agi_freecount(cur, agi);
  888. if (error)
  889. goto error0;
  890. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  891. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
  892. xfs_perag_put(pag);
  893. *inop = ino;
  894. return 0;
  895. error1:
  896. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  897. error0:
  898. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  899. xfs_perag_put(pag);
  900. return error;
  901. }
  902. /*
  903. * Use the free inode btree to allocate an inode based on distance from the
  904. * parent. Note that the provided cursor may be deleted and replaced.
  905. */
  906. STATIC int
  907. xfs_dialloc_ag_finobt_near(
  908. xfs_agino_t pagino,
  909. struct xfs_btree_cur **ocur,
  910. struct xfs_inobt_rec_incore *rec)
  911. {
  912. struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
  913. struct xfs_btree_cur *rcur; /* right search cursor */
  914. struct xfs_inobt_rec_incore rrec;
  915. int error;
  916. int i, j;
  917. error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
  918. if (error)
  919. return error;
  920. if (i == 1) {
  921. error = xfs_inobt_get_rec(lcur, rec, &i);
  922. if (error)
  923. return error;
  924. XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1);
  925. /*
  926. * See if we've landed in the parent inode record. The finobt
  927. * only tracks chunks with at least one free inode, so record
  928. * existence is enough.
  929. */
  930. if (pagino >= rec->ir_startino &&
  931. pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
  932. return 0;
  933. }
  934. error = xfs_btree_dup_cursor(lcur, &rcur);
  935. if (error)
  936. return error;
  937. error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
  938. if (error)
  939. goto error_rcur;
  940. if (j == 1) {
  941. error = xfs_inobt_get_rec(rcur, &rrec, &j);
  942. if (error)
  943. goto error_rcur;
  944. XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur);
  945. }
  946. XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur);
  947. if (i == 1 && j == 1) {
  948. /*
  949. * Both the left and right records are valid. Choose the closer
  950. * inode chunk to the target.
  951. */
  952. if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
  953. (rrec.ir_startino - pagino)) {
  954. *rec = rrec;
  955. xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
  956. *ocur = rcur;
  957. } else {
  958. xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
  959. }
  960. } else if (j == 1) {
  961. /* only the right record is valid */
  962. *rec = rrec;
  963. xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
  964. *ocur = rcur;
  965. } else if (i == 1) {
  966. /* only the left record is valid */
  967. xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
  968. }
  969. return 0;
  970. error_rcur:
  971. xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
  972. return error;
  973. }
  974. /*
  975. * Use the free inode btree to find a free inode based on a newino hint. If
  976. * the hint is NULL, find the first free inode in the AG.
  977. */
  978. STATIC int
  979. xfs_dialloc_ag_finobt_newino(
  980. struct xfs_agi *agi,
  981. struct xfs_btree_cur *cur,
  982. struct xfs_inobt_rec_incore *rec)
  983. {
  984. int error;
  985. int i;
  986. if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
  987. error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
  988. XFS_LOOKUP_EQ, &i);
  989. if (error)
  990. return error;
  991. if (i == 1) {
  992. error = xfs_inobt_get_rec(cur, rec, &i);
  993. if (error)
  994. return error;
  995. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  996. return 0;
  997. }
  998. }
  999. /*
  1000. * Find the first inode available in the AG.
  1001. */
  1002. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
  1003. if (error)
  1004. return error;
  1005. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1006. error = xfs_inobt_get_rec(cur, rec, &i);
  1007. if (error)
  1008. return error;
  1009. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1010. return 0;
  1011. }
  1012. /*
  1013. * Update the inobt based on a modification made to the finobt. Also ensure that
  1014. * the records from both trees are equivalent post-modification.
  1015. */
  1016. STATIC int
  1017. xfs_dialloc_ag_update_inobt(
  1018. struct xfs_btree_cur *cur, /* inobt cursor */
  1019. struct xfs_inobt_rec_incore *frec, /* finobt record */
  1020. int offset) /* inode offset */
  1021. {
  1022. struct xfs_inobt_rec_incore rec;
  1023. int error;
  1024. int i;
  1025. error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
  1026. if (error)
  1027. return error;
  1028. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1029. error = xfs_inobt_get_rec(cur, &rec, &i);
  1030. if (error)
  1031. return error;
  1032. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
  1033. ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
  1034. XFS_INODES_PER_CHUNK) == 0);
  1035. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  1036. rec.ir_freecount--;
  1037. XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) &&
  1038. (rec.ir_freecount == frec->ir_freecount));
  1039. return xfs_inobt_update(cur, &rec);
  1040. }
  1041. /*
  1042. * Allocate an inode using the free inode btree, if available. Otherwise, fall
  1043. * back to the inobt search algorithm.
  1044. *
  1045. * The caller selected an AG for us, and made sure that free inodes are
  1046. * available.
  1047. */
  1048. STATIC int
  1049. xfs_dialloc_ag(
  1050. struct xfs_trans *tp,
  1051. struct xfs_buf *agbp,
  1052. xfs_ino_t parent,
  1053. xfs_ino_t *inop)
  1054. {
  1055. struct xfs_mount *mp = tp->t_mountp;
  1056. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1057. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1058. xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
  1059. xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
  1060. struct xfs_perag *pag;
  1061. struct xfs_btree_cur *cur; /* finobt cursor */
  1062. struct xfs_btree_cur *icur; /* inobt cursor */
  1063. struct xfs_inobt_rec_incore rec;
  1064. xfs_ino_t ino;
  1065. int error;
  1066. int offset;
  1067. int i;
  1068. if (!xfs_sb_version_hasfinobt(&mp->m_sb))
  1069. return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
  1070. pag = xfs_perag_get(mp, agno);
  1071. /*
  1072. * If pagino is 0 (this is the root inode allocation) use newino.
  1073. * This must work because we've just allocated some.
  1074. */
  1075. if (!pagino)
  1076. pagino = be32_to_cpu(agi->agi_newino);
  1077. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
  1078. error = xfs_check_agi_freecount(cur, agi);
  1079. if (error)
  1080. goto error_cur;
  1081. /*
  1082. * The search algorithm depends on whether we're in the same AG as the
  1083. * parent. If so, find the closest available inode to the parent. If
  1084. * not, consider the agi hint or find the first free inode in the AG.
  1085. */
  1086. if (agno == pagno)
  1087. error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
  1088. else
  1089. error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
  1090. if (error)
  1091. goto error_cur;
  1092. offset = xfs_lowbit64(rec.ir_free);
  1093. ASSERT(offset >= 0);
  1094. ASSERT(offset < XFS_INODES_PER_CHUNK);
  1095. ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
  1096. XFS_INODES_PER_CHUNK) == 0);
  1097. ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
  1098. /*
  1099. * Modify or remove the finobt record.
  1100. */
  1101. rec.ir_free &= ~XFS_INOBT_MASK(offset);
  1102. rec.ir_freecount--;
  1103. if (rec.ir_freecount)
  1104. error = xfs_inobt_update(cur, &rec);
  1105. else
  1106. error = xfs_btree_delete(cur, &i);
  1107. if (error)
  1108. goto error_cur;
  1109. /*
  1110. * The finobt has now been updated appropriately. We haven't updated the
  1111. * agi and superblock yet, so we can create an inobt cursor and validate
  1112. * the original freecount. If all is well, make the equivalent update to
  1113. * the inobt using the finobt record and offset information.
  1114. */
  1115. icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  1116. error = xfs_check_agi_freecount(icur, agi);
  1117. if (error)
  1118. goto error_icur;
  1119. error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
  1120. if (error)
  1121. goto error_icur;
  1122. /*
  1123. * Both trees have now been updated. We must update the perag and
  1124. * superblock before we can check the freecount for each btree.
  1125. */
  1126. be32_add_cpu(&agi->agi_freecount, -1);
  1127. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  1128. pag->pagi_freecount--;
  1129. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
  1130. error = xfs_check_agi_freecount(icur, agi);
  1131. if (error)
  1132. goto error_icur;
  1133. error = xfs_check_agi_freecount(cur, agi);
  1134. if (error)
  1135. goto error_icur;
  1136. xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
  1137. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1138. xfs_perag_put(pag);
  1139. *inop = ino;
  1140. return 0;
  1141. error_icur:
  1142. xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
  1143. error_cur:
  1144. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1145. xfs_perag_put(pag);
  1146. return error;
  1147. }
  1148. /*
  1149. * Allocate an inode on disk.
  1150. *
  1151. * Mode is used to tell whether the new inode will need space, and whether it
  1152. * is a directory.
  1153. *
  1154. * This function is designed to be called twice if it has to do an allocation
  1155. * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
  1156. * If an inode is available without having to performn an allocation, an inode
  1157. * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
  1158. * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
  1159. * The caller should then commit the current transaction, allocate a
  1160. * new transaction, and call xfs_dialloc() again, passing in the previous value
  1161. * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
  1162. * buffer is locked across the two calls, the second call is guaranteed to have
  1163. * a free inode available.
  1164. *
  1165. * Once we successfully pick an inode its number is returned and the on-disk
  1166. * data structures are updated. The inode itself is not read in, since doing so
  1167. * would break ordering constraints with xfs_reclaim.
  1168. */
  1169. int
  1170. xfs_dialloc(
  1171. struct xfs_trans *tp,
  1172. xfs_ino_t parent,
  1173. umode_t mode,
  1174. int okalloc,
  1175. struct xfs_buf **IO_agbp,
  1176. xfs_ino_t *inop)
  1177. {
  1178. struct xfs_mount *mp = tp->t_mountp;
  1179. struct xfs_buf *agbp;
  1180. xfs_agnumber_t agno;
  1181. int error;
  1182. int ialloced;
  1183. int noroom = 0;
  1184. xfs_agnumber_t start_agno;
  1185. struct xfs_perag *pag;
  1186. if (*IO_agbp) {
  1187. /*
  1188. * If the caller passes in a pointer to the AGI buffer,
  1189. * continue where we left off before. In this case, we
  1190. * know that the allocation group has free inodes.
  1191. */
  1192. agbp = *IO_agbp;
  1193. goto out_alloc;
  1194. }
  1195. /*
  1196. * We do not have an agbp, so select an initial allocation
  1197. * group for inode allocation.
  1198. */
  1199. start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
  1200. if (start_agno == NULLAGNUMBER) {
  1201. *inop = NULLFSINO;
  1202. return 0;
  1203. }
  1204. /*
  1205. * If we have already hit the ceiling of inode blocks then clear
  1206. * okalloc so we scan all available agi structures for a free
  1207. * inode.
  1208. *
  1209. * Read rough value of mp->m_icount by percpu_counter_read_positive,
  1210. * which will sacrifice the preciseness but improve the performance.
  1211. */
  1212. if (mp->m_maxicount &&
  1213. percpu_counter_read_positive(&mp->m_icount) + mp->m_ialloc_inos
  1214. > mp->m_maxicount) {
  1215. noroom = 1;
  1216. okalloc = 0;
  1217. }
  1218. /*
  1219. * Loop until we find an allocation group that either has free inodes
  1220. * or in which we can allocate some inodes. Iterate through the
  1221. * allocation groups upward, wrapping at the end.
  1222. */
  1223. agno = start_agno;
  1224. for (;;) {
  1225. pag = xfs_perag_get(mp, agno);
  1226. if (!pag->pagi_inodeok) {
  1227. xfs_ialloc_next_ag(mp);
  1228. goto nextag;
  1229. }
  1230. if (!pag->pagi_init) {
  1231. error = xfs_ialloc_pagi_init(mp, tp, agno);
  1232. if (error)
  1233. goto out_error;
  1234. }
  1235. /*
  1236. * Do a first racy fast path check if this AG is usable.
  1237. */
  1238. if (!pag->pagi_freecount && !okalloc)
  1239. goto nextag;
  1240. /*
  1241. * Then read in the AGI buffer and recheck with the AGI buffer
  1242. * lock held.
  1243. */
  1244. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1245. if (error)
  1246. goto out_error;
  1247. if (pag->pagi_freecount) {
  1248. xfs_perag_put(pag);
  1249. goto out_alloc;
  1250. }
  1251. if (!okalloc)
  1252. goto nextag_relse_buffer;
  1253. error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
  1254. if (error) {
  1255. xfs_trans_brelse(tp, agbp);
  1256. if (error != -ENOSPC)
  1257. goto out_error;
  1258. xfs_perag_put(pag);
  1259. *inop = NULLFSINO;
  1260. return 0;
  1261. }
  1262. if (ialloced) {
  1263. /*
  1264. * We successfully allocated some inodes, return
  1265. * the current context to the caller so that it
  1266. * can commit the current transaction and call
  1267. * us again where we left off.
  1268. */
  1269. ASSERT(pag->pagi_freecount > 0);
  1270. xfs_perag_put(pag);
  1271. *IO_agbp = agbp;
  1272. *inop = NULLFSINO;
  1273. return 0;
  1274. }
  1275. nextag_relse_buffer:
  1276. xfs_trans_brelse(tp, agbp);
  1277. nextag:
  1278. xfs_perag_put(pag);
  1279. if (++agno == mp->m_sb.sb_agcount)
  1280. agno = 0;
  1281. if (agno == start_agno) {
  1282. *inop = NULLFSINO;
  1283. return noroom ? -ENOSPC : 0;
  1284. }
  1285. }
  1286. out_alloc:
  1287. *IO_agbp = NULL;
  1288. return xfs_dialloc_ag(tp, agbp, parent, inop);
  1289. out_error:
  1290. xfs_perag_put(pag);
  1291. return error;
  1292. }
  1293. STATIC int
  1294. xfs_difree_inobt(
  1295. struct xfs_mount *mp,
  1296. struct xfs_trans *tp,
  1297. struct xfs_buf *agbp,
  1298. xfs_agino_t agino,
  1299. struct xfs_bmap_free *flist,
  1300. int *deleted,
  1301. xfs_ino_t *first_ino,
  1302. struct xfs_inobt_rec_incore *orec)
  1303. {
  1304. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1305. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1306. struct xfs_perag *pag;
  1307. struct xfs_btree_cur *cur;
  1308. struct xfs_inobt_rec_incore rec;
  1309. int ilen;
  1310. int error;
  1311. int i;
  1312. int off;
  1313. ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
  1314. ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
  1315. /*
  1316. * Initialize the cursor.
  1317. */
  1318. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  1319. error = xfs_check_agi_freecount(cur, agi);
  1320. if (error)
  1321. goto error0;
  1322. /*
  1323. * Look for the entry describing this inode.
  1324. */
  1325. if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
  1326. xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
  1327. __func__, error);
  1328. goto error0;
  1329. }
  1330. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1331. error = xfs_inobt_get_rec(cur, &rec, &i);
  1332. if (error) {
  1333. xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
  1334. __func__, error);
  1335. goto error0;
  1336. }
  1337. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1338. /*
  1339. * Get the offset in the inode chunk.
  1340. */
  1341. off = agino - rec.ir_startino;
  1342. ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
  1343. ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
  1344. /*
  1345. * Mark the inode free & increment the count.
  1346. */
  1347. rec.ir_free |= XFS_INOBT_MASK(off);
  1348. rec.ir_freecount++;
  1349. /*
  1350. * When an inode cluster is free, it becomes eligible for removal
  1351. */
  1352. if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
  1353. (rec.ir_freecount == mp->m_ialloc_inos)) {
  1354. *deleted = 1;
  1355. *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
  1356. /*
  1357. * Remove the inode cluster from the AGI B+Tree, adjust the
  1358. * AGI and Superblock inode counts, and mark the disk space
  1359. * to be freed when the transaction is committed.
  1360. */
  1361. ilen = mp->m_ialloc_inos;
  1362. be32_add_cpu(&agi->agi_count, -ilen);
  1363. be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
  1364. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
  1365. pag = xfs_perag_get(mp, agno);
  1366. pag->pagi_freecount -= ilen - 1;
  1367. xfs_perag_put(pag);
  1368. xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
  1369. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
  1370. if ((error = xfs_btree_delete(cur, &i))) {
  1371. xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
  1372. __func__, error);
  1373. goto error0;
  1374. }
  1375. xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno,
  1376. XFS_AGINO_TO_AGBNO(mp, rec.ir_startino)),
  1377. mp->m_ialloc_blks, flist, mp);
  1378. } else {
  1379. *deleted = 0;
  1380. error = xfs_inobt_update(cur, &rec);
  1381. if (error) {
  1382. xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
  1383. __func__, error);
  1384. goto error0;
  1385. }
  1386. /*
  1387. * Change the inode free counts and log the ag/sb changes.
  1388. */
  1389. be32_add_cpu(&agi->agi_freecount, 1);
  1390. xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
  1391. pag = xfs_perag_get(mp, agno);
  1392. pag->pagi_freecount++;
  1393. xfs_perag_put(pag);
  1394. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
  1395. }
  1396. error = xfs_check_agi_freecount(cur, agi);
  1397. if (error)
  1398. goto error0;
  1399. *orec = rec;
  1400. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1401. return 0;
  1402. error0:
  1403. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1404. return error;
  1405. }
  1406. /*
  1407. * Free an inode in the free inode btree.
  1408. */
  1409. STATIC int
  1410. xfs_difree_finobt(
  1411. struct xfs_mount *mp,
  1412. struct xfs_trans *tp,
  1413. struct xfs_buf *agbp,
  1414. xfs_agino_t agino,
  1415. struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
  1416. {
  1417. struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
  1418. xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
  1419. struct xfs_btree_cur *cur;
  1420. struct xfs_inobt_rec_incore rec;
  1421. int offset = agino - ibtrec->ir_startino;
  1422. int error;
  1423. int i;
  1424. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
  1425. error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
  1426. if (error)
  1427. goto error;
  1428. if (i == 0) {
  1429. /*
  1430. * If the record does not exist in the finobt, we must have just
  1431. * freed an inode in a previously fully allocated chunk. If not,
  1432. * something is out of sync.
  1433. */
  1434. XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error);
  1435. error = xfs_inobt_insert_rec(cur, ibtrec->ir_freecount,
  1436. ibtrec->ir_free, &i);
  1437. if (error)
  1438. goto error;
  1439. ASSERT(i == 1);
  1440. goto out;
  1441. }
  1442. /*
  1443. * Read and update the existing record. We could just copy the ibtrec
  1444. * across here, but that would defeat the purpose of having redundant
  1445. * metadata. By making the modifications independently, we can catch
  1446. * corruptions that we wouldn't see if we just copied from one record
  1447. * to another.
  1448. */
  1449. error = xfs_inobt_get_rec(cur, &rec, &i);
  1450. if (error)
  1451. goto error;
  1452. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
  1453. rec.ir_free |= XFS_INOBT_MASK(offset);
  1454. rec.ir_freecount++;
  1455. XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) &&
  1456. (rec.ir_freecount == ibtrec->ir_freecount),
  1457. error);
  1458. /*
  1459. * The content of inobt records should always match between the inobt
  1460. * and finobt. The lifecycle of records in the finobt is different from
  1461. * the inobt in that the finobt only tracks records with at least one
  1462. * free inode. Hence, if all of the inodes are free and we aren't
  1463. * keeping inode chunks permanently on disk, remove the record.
  1464. * Otherwise, update the record with the new information.
  1465. */
  1466. if (rec.ir_freecount == mp->m_ialloc_inos &&
  1467. !(mp->m_flags & XFS_MOUNT_IKEEP)) {
  1468. error = xfs_btree_delete(cur, &i);
  1469. if (error)
  1470. goto error;
  1471. ASSERT(i == 1);
  1472. } else {
  1473. error = xfs_inobt_update(cur, &rec);
  1474. if (error)
  1475. goto error;
  1476. }
  1477. out:
  1478. error = xfs_check_agi_freecount(cur, agi);
  1479. if (error)
  1480. goto error;
  1481. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1482. return 0;
  1483. error:
  1484. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  1485. return error;
  1486. }
  1487. /*
  1488. * Free disk inode. Carefully avoids touching the incore inode, all
  1489. * manipulations incore are the caller's responsibility.
  1490. * The on-disk inode is not changed by this operation, only the
  1491. * btree (free inode mask) is changed.
  1492. */
  1493. int
  1494. xfs_difree(
  1495. struct xfs_trans *tp, /* transaction pointer */
  1496. xfs_ino_t inode, /* inode to be freed */
  1497. struct xfs_bmap_free *flist, /* extents to free */
  1498. int *deleted,/* set if inode cluster was deleted */
  1499. xfs_ino_t *first_ino)/* first inode in deleted cluster */
  1500. {
  1501. /* REFERENCED */
  1502. xfs_agblock_t agbno; /* block number containing inode */
  1503. struct xfs_buf *agbp; /* buffer for allocation group header */
  1504. xfs_agino_t agino; /* allocation group inode number */
  1505. xfs_agnumber_t agno; /* allocation group number */
  1506. int error; /* error return value */
  1507. struct xfs_mount *mp; /* mount structure for filesystem */
  1508. struct xfs_inobt_rec_incore rec;/* btree record */
  1509. mp = tp->t_mountp;
  1510. /*
  1511. * Break up inode number into its components.
  1512. */
  1513. agno = XFS_INO_TO_AGNO(mp, inode);
  1514. if (agno >= mp->m_sb.sb_agcount) {
  1515. xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
  1516. __func__, agno, mp->m_sb.sb_agcount);
  1517. ASSERT(0);
  1518. return -EINVAL;
  1519. }
  1520. agino = XFS_INO_TO_AGINO(mp, inode);
  1521. if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1522. xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
  1523. __func__, (unsigned long long)inode,
  1524. (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
  1525. ASSERT(0);
  1526. return -EINVAL;
  1527. }
  1528. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  1529. if (agbno >= mp->m_sb.sb_agblocks) {
  1530. xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
  1531. __func__, agbno, mp->m_sb.sb_agblocks);
  1532. ASSERT(0);
  1533. return -EINVAL;
  1534. }
  1535. /*
  1536. * Get the allocation group header.
  1537. */
  1538. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1539. if (error) {
  1540. xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
  1541. __func__, error);
  1542. return error;
  1543. }
  1544. /*
  1545. * Fix up the inode allocation btree.
  1546. */
  1547. error = xfs_difree_inobt(mp, tp, agbp, agino, flist, deleted, first_ino,
  1548. &rec);
  1549. if (error)
  1550. goto error0;
  1551. /*
  1552. * Fix up the free inode btree.
  1553. */
  1554. if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
  1555. error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
  1556. if (error)
  1557. goto error0;
  1558. }
  1559. return 0;
  1560. error0:
  1561. return error;
  1562. }
  1563. STATIC int
  1564. xfs_imap_lookup(
  1565. struct xfs_mount *mp,
  1566. struct xfs_trans *tp,
  1567. xfs_agnumber_t agno,
  1568. xfs_agino_t agino,
  1569. xfs_agblock_t agbno,
  1570. xfs_agblock_t *chunk_agbno,
  1571. xfs_agblock_t *offset_agbno,
  1572. int flags)
  1573. {
  1574. struct xfs_inobt_rec_incore rec;
  1575. struct xfs_btree_cur *cur;
  1576. struct xfs_buf *agbp;
  1577. int error;
  1578. int i;
  1579. error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
  1580. if (error) {
  1581. xfs_alert(mp,
  1582. "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
  1583. __func__, error, agno);
  1584. return error;
  1585. }
  1586. /*
  1587. * Lookup the inode record for the given agino. If the record cannot be
  1588. * found, then it's an invalid inode number and we should abort. Once
  1589. * we have a record, we need to ensure it contains the inode number
  1590. * we are looking up.
  1591. */
  1592. cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
  1593. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
  1594. if (!error) {
  1595. if (i)
  1596. error = xfs_inobt_get_rec(cur, &rec, &i);
  1597. if (!error && i == 0)
  1598. error = -EINVAL;
  1599. }
  1600. xfs_trans_brelse(tp, agbp);
  1601. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  1602. if (error)
  1603. return error;
  1604. /* check that the returned record contains the required inode */
  1605. if (rec.ir_startino > agino ||
  1606. rec.ir_startino + mp->m_ialloc_inos <= agino)
  1607. return -EINVAL;
  1608. /* for untrusted inodes check it is allocated first */
  1609. if ((flags & XFS_IGET_UNTRUSTED) &&
  1610. (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
  1611. return -EINVAL;
  1612. *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
  1613. *offset_agbno = agbno - *chunk_agbno;
  1614. return 0;
  1615. }
  1616. /*
  1617. * Return the location of the inode in imap, for mapping it into a buffer.
  1618. */
  1619. int
  1620. xfs_imap(
  1621. xfs_mount_t *mp, /* file system mount structure */
  1622. xfs_trans_t *tp, /* transaction pointer */
  1623. xfs_ino_t ino, /* inode to locate */
  1624. struct xfs_imap *imap, /* location map structure */
  1625. uint flags) /* flags for inode btree lookup */
  1626. {
  1627. xfs_agblock_t agbno; /* block number of inode in the alloc group */
  1628. xfs_agino_t agino; /* inode number within alloc group */
  1629. xfs_agnumber_t agno; /* allocation group number */
  1630. int blks_per_cluster; /* num blocks per inode cluster */
  1631. xfs_agblock_t chunk_agbno; /* first block in inode chunk */
  1632. xfs_agblock_t cluster_agbno; /* first block in inode cluster */
  1633. int error; /* error code */
  1634. int offset; /* index of inode in its buffer */
  1635. xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
  1636. ASSERT(ino != NULLFSINO);
  1637. /*
  1638. * Split up the inode number into its parts.
  1639. */
  1640. agno = XFS_INO_TO_AGNO(mp, ino);
  1641. agino = XFS_INO_TO_AGINO(mp, ino);
  1642. agbno = XFS_AGINO_TO_AGBNO(mp, agino);
  1643. if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
  1644. ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1645. #ifdef DEBUG
  1646. /*
  1647. * Don't output diagnostic information for untrusted inodes
  1648. * as they can be invalid without implying corruption.
  1649. */
  1650. if (flags & XFS_IGET_UNTRUSTED)
  1651. return -EINVAL;
  1652. if (agno >= mp->m_sb.sb_agcount) {
  1653. xfs_alert(mp,
  1654. "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
  1655. __func__, agno, mp->m_sb.sb_agcount);
  1656. }
  1657. if (agbno >= mp->m_sb.sb_agblocks) {
  1658. xfs_alert(mp,
  1659. "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
  1660. __func__, (unsigned long long)agbno,
  1661. (unsigned long)mp->m_sb.sb_agblocks);
  1662. }
  1663. if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  1664. xfs_alert(mp,
  1665. "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
  1666. __func__, ino,
  1667. XFS_AGINO_TO_INO(mp, agno, agino));
  1668. }
  1669. xfs_stack_trace();
  1670. #endif /* DEBUG */
  1671. return -EINVAL;
  1672. }
  1673. blks_per_cluster = xfs_icluster_size_fsb(mp);
  1674. /*
  1675. * For bulkstat and handle lookups, we have an untrusted inode number
  1676. * that we have to verify is valid. We cannot do this just by reading
  1677. * the inode buffer as it may have been unlinked and removed leaving
  1678. * inodes in stale state on disk. Hence we have to do a btree lookup
  1679. * in all cases where an untrusted inode number is passed.
  1680. */
  1681. if (flags & XFS_IGET_UNTRUSTED) {
  1682. error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
  1683. &chunk_agbno, &offset_agbno, flags);
  1684. if (error)
  1685. return error;
  1686. goto out_map;
  1687. }
  1688. /*
  1689. * If the inode cluster size is the same as the blocksize or
  1690. * smaller we get to the buffer by simple arithmetics.
  1691. */
  1692. if (blks_per_cluster == 1) {
  1693. offset = XFS_INO_TO_OFFSET(mp, ino);
  1694. ASSERT(offset < mp->m_sb.sb_inopblock);
  1695. imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
  1696. imap->im_len = XFS_FSB_TO_BB(mp, 1);
  1697. imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
  1698. return 0;
  1699. }
  1700. /*
  1701. * If the inode chunks are aligned then use simple maths to
  1702. * find the location. Otherwise we have to do a btree
  1703. * lookup to find the location.
  1704. */
  1705. if (mp->m_inoalign_mask) {
  1706. offset_agbno = agbno & mp->m_inoalign_mask;
  1707. chunk_agbno = agbno - offset_agbno;
  1708. } else {
  1709. error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
  1710. &chunk_agbno, &offset_agbno, flags);
  1711. if (error)
  1712. return error;
  1713. }
  1714. out_map:
  1715. ASSERT(agbno >= chunk_agbno);
  1716. cluster_agbno = chunk_agbno +
  1717. ((offset_agbno / blks_per_cluster) * blks_per_cluster);
  1718. offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
  1719. XFS_INO_TO_OFFSET(mp, ino);
  1720. imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
  1721. imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
  1722. imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
  1723. /*
  1724. * If the inode number maps to a block outside the bounds
  1725. * of the file system then return NULL rather than calling
  1726. * read_buf and panicing when we get an error from the
  1727. * driver.
  1728. */
  1729. if ((imap->im_blkno + imap->im_len) >
  1730. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
  1731. xfs_alert(mp,
  1732. "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
  1733. __func__, (unsigned long long) imap->im_blkno,
  1734. (unsigned long long) imap->im_len,
  1735. XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
  1736. return -EINVAL;
  1737. }
  1738. return 0;
  1739. }
  1740. /*
  1741. * Compute and fill in value of m_in_maxlevels.
  1742. */
  1743. void
  1744. xfs_ialloc_compute_maxlevels(
  1745. xfs_mount_t *mp) /* file system mount structure */
  1746. {
  1747. int level;
  1748. uint maxblocks;
  1749. uint maxleafents;
  1750. int minleafrecs;
  1751. int minnoderecs;
  1752. maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
  1753. XFS_INODES_PER_CHUNK_LOG;
  1754. minleafrecs = mp->m_alloc_mnr[0];
  1755. minnoderecs = mp->m_alloc_mnr[1];
  1756. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  1757. for (level = 1; maxblocks > 1; level++)
  1758. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  1759. mp->m_in_maxlevels = level;
  1760. }
  1761. /*
  1762. * Log specified fields for the ag hdr (inode section). The growth of the agi
  1763. * structure over time requires that we interpret the buffer as two logical
  1764. * regions delineated by the end of the unlinked list. This is due to the size
  1765. * of the hash table and its location in the middle of the agi.
  1766. *
  1767. * For example, a request to log a field before agi_unlinked and a field after
  1768. * agi_unlinked could cause us to log the entire hash table and use an excessive
  1769. * amount of log space. To avoid this behavior, log the region up through
  1770. * agi_unlinked in one call and the region after agi_unlinked through the end of
  1771. * the structure in another.
  1772. */
  1773. void
  1774. xfs_ialloc_log_agi(
  1775. xfs_trans_t *tp, /* transaction pointer */
  1776. xfs_buf_t *bp, /* allocation group header buffer */
  1777. int fields) /* bitmask of fields to log */
  1778. {
  1779. int first; /* first byte number */
  1780. int last; /* last byte number */
  1781. static const short offsets[] = { /* field starting offsets */
  1782. /* keep in sync with bit definitions */
  1783. offsetof(xfs_agi_t, agi_magicnum),
  1784. offsetof(xfs_agi_t, agi_versionnum),
  1785. offsetof(xfs_agi_t, agi_seqno),
  1786. offsetof(xfs_agi_t, agi_length),
  1787. offsetof(xfs_agi_t, agi_count),
  1788. offsetof(xfs_agi_t, agi_root),
  1789. offsetof(xfs_agi_t, agi_level),
  1790. offsetof(xfs_agi_t, agi_freecount),
  1791. offsetof(xfs_agi_t, agi_newino),
  1792. offsetof(xfs_agi_t, agi_dirino),
  1793. offsetof(xfs_agi_t, agi_unlinked),
  1794. offsetof(xfs_agi_t, agi_free_root),
  1795. offsetof(xfs_agi_t, agi_free_level),
  1796. sizeof(xfs_agi_t)
  1797. };
  1798. #ifdef DEBUG
  1799. xfs_agi_t *agi; /* allocation group header */
  1800. agi = XFS_BUF_TO_AGI(bp);
  1801. ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
  1802. #endif
  1803. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
  1804. /*
  1805. * Compute byte offsets for the first and last fields in the first
  1806. * region and log the agi buffer. This only logs up through
  1807. * agi_unlinked.
  1808. */
  1809. if (fields & XFS_AGI_ALL_BITS_R1) {
  1810. xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
  1811. &first, &last);
  1812. xfs_trans_log_buf(tp, bp, first, last);
  1813. }
  1814. /*
  1815. * Mask off the bits in the first region and calculate the first and
  1816. * last field offsets for any bits in the second region.
  1817. */
  1818. fields &= ~XFS_AGI_ALL_BITS_R1;
  1819. if (fields) {
  1820. xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
  1821. &first, &last);
  1822. xfs_trans_log_buf(tp, bp, first, last);
  1823. }
  1824. }
  1825. #ifdef DEBUG
  1826. STATIC void
  1827. xfs_check_agi_unlinked(
  1828. struct xfs_agi *agi)
  1829. {
  1830. int i;
  1831. for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
  1832. ASSERT(agi->agi_unlinked[i]);
  1833. }
  1834. #else
  1835. #define xfs_check_agi_unlinked(agi)
  1836. #endif
  1837. static bool
  1838. xfs_agi_verify(
  1839. struct xfs_buf *bp)
  1840. {
  1841. struct xfs_mount *mp = bp->b_target->bt_mount;
  1842. struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
  1843. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  1844. !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
  1845. return false;
  1846. /*
  1847. * Validate the magic number of the agi block.
  1848. */
  1849. if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
  1850. return false;
  1851. if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
  1852. return false;
  1853. if (be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
  1854. return false;
  1855. /*
  1856. * during growfs operations, the perag is not fully initialised,
  1857. * so we can't use it for any useful checking. growfs ensures we can't
  1858. * use it by using uncached buffers that don't have the perag attached
  1859. * so we can detect and avoid this problem.
  1860. */
  1861. if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
  1862. return false;
  1863. xfs_check_agi_unlinked(agi);
  1864. return true;
  1865. }
  1866. static void
  1867. xfs_agi_read_verify(
  1868. struct xfs_buf *bp)
  1869. {
  1870. struct xfs_mount *mp = bp->b_target->bt_mount;
  1871. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  1872. !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
  1873. xfs_buf_ioerror(bp, -EFSBADCRC);
  1874. else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp,
  1875. XFS_ERRTAG_IALLOC_READ_AGI,
  1876. XFS_RANDOM_IALLOC_READ_AGI))
  1877. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  1878. if (bp->b_error)
  1879. xfs_verifier_error(bp);
  1880. }
  1881. static void
  1882. xfs_agi_write_verify(
  1883. struct xfs_buf *bp)
  1884. {
  1885. struct xfs_mount *mp = bp->b_target->bt_mount;
  1886. struct xfs_buf_log_item *bip = bp->b_fspriv;
  1887. if (!xfs_agi_verify(bp)) {
  1888. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  1889. xfs_verifier_error(bp);
  1890. return;
  1891. }
  1892. if (!xfs_sb_version_hascrc(&mp->m_sb))
  1893. return;
  1894. if (bip)
  1895. XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  1896. xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
  1897. }
  1898. const struct xfs_buf_ops xfs_agi_buf_ops = {
  1899. .verify_read = xfs_agi_read_verify,
  1900. .verify_write = xfs_agi_write_verify,
  1901. };
  1902. /*
  1903. * Read in the allocation group header (inode allocation section)
  1904. */
  1905. int
  1906. xfs_read_agi(
  1907. struct xfs_mount *mp, /* file system mount structure */
  1908. struct xfs_trans *tp, /* transaction pointer */
  1909. xfs_agnumber_t agno, /* allocation group number */
  1910. struct xfs_buf **bpp) /* allocation group hdr buf */
  1911. {
  1912. int error;
  1913. trace_xfs_read_agi(mp, agno);
  1914. ASSERT(agno != NULLAGNUMBER);
  1915. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  1916. XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
  1917. XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
  1918. if (error)
  1919. return error;
  1920. xfs_buf_set_ref(*bpp, XFS_AGI_REF);
  1921. return 0;
  1922. }
  1923. int
  1924. xfs_ialloc_read_agi(
  1925. struct xfs_mount *mp, /* file system mount structure */
  1926. struct xfs_trans *tp, /* transaction pointer */
  1927. xfs_agnumber_t agno, /* allocation group number */
  1928. struct xfs_buf **bpp) /* allocation group hdr buf */
  1929. {
  1930. struct xfs_agi *agi; /* allocation group header */
  1931. struct xfs_perag *pag; /* per allocation group data */
  1932. int error;
  1933. trace_xfs_ialloc_read_agi(mp, agno);
  1934. error = xfs_read_agi(mp, tp, agno, bpp);
  1935. if (error)
  1936. return error;
  1937. agi = XFS_BUF_TO_AGI(*bpp);
  1938. pag = xfs_perag_get(mp, agno);
  1939. if (!pag->pagi_init) {
  1940. pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
  1941. pag->pagi_count = be32_to_cpu(agi->agi_count);
  1942. pag->pagi_init = 1;
  1943. }
  1944. /*
  1945. * It's possible for these to be out of sync if
  1946. * we are in the middle of a forced shutdown.
  1947. */
  1948. ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
  1949. XFS_FORCED_SHUTDOWN(mp));
  1950. xfs_perag_put(pag);
  1951. return 0;
  1952. }
  1953. /*
  1954. * Read in the agi to initialise the per-ag data in the mount structure
  1955. */
  1956. int
  1957. xfs_ialloc_pagi_init(
  1958. xfs_mount_t *mp, /* file system mount structure */
  1959. xfs_trans_t *tp, /* transaction pointer */
  1960. xfs_agnumber_t agno) /* allocation group number */
  1961. {
  1962. xfs_buf_t *bp = NULL;
  1963. int error;
  1964. error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
  1965. if (error)
  1966. return error;
  1967. if (bp)
  1968. xfs_trans_brelse(tp, bp);
  1969. return 0;
  1970. }