xfs_ialloc.c 72 KB

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