xfs_ialloc.c 57 KB

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