xfs_ialloc.c 72 KB

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