xfs_ialloc.c 58 KB

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