xfs_ialloc.c 75 KB

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