xfs_ialloc.c 74 KB

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