xfs_ialloc.c 75 KB

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