xfs_ialloc.c 57 KB

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