xfs_ialloc.c 72 KB

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