xfs_ialloc.c 76 KB

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