xfs_ialloc.c 72 KB

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