xfs_ialloc.c 69 KB

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