xfs_ialloc.c 57 KB

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