xfs_ialloc.c 72 KB

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