xfs_ialloc.c 72 KB

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