xfs_ialloc.c 72 KB

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