xfs_mount.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. /*
  2. * Copyright (c) 2000-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_da_format.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_dir2.h"
  32. #include "xfs_ialloc.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_rtalloc.h"
  35. #include "xfs_bmap.h"
  36. #include "xfs_trans.h"
  37. #include "xfs_trans_priv.h"
  38. #include "xfs_log.h"
  39. #include "xfs_error.h"
  40. #include "xfs_quota.h"
  41. #include "xfs_fsops.h"
  42. #include "xfs_trace.h"
  43. #include "xfs_icache.h"
  44. #include "xfs_dinode.h"
  45. #ifdef HAVE_PERCPU_SB
  46. STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
  47. int);
  48. STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
  49. int);
  50. STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
  51. #else
  52. #define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
  53. #define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
  54. #endif
  55. static DEFINE_MUTEX(xfs_uuid_table_mutex);
  56. static int xfs_uuid_table_size;
  57. static uuid_t *xfs_uuid_table;
  58. /*
  59. * See if the UUID is unique among mounted XFS filesystems.
  60. * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
  61. */
  62. STATIC int
  63. xfs_uuid_mount(
  64. struct xfs_mount *mp)
  65. {
  66. uuid_t *uuid = &mp->m_sb.sb_uuid;
  67. int hole, i;
  68. if (mp->m_flags & XFS_MOUNT_NOUUID)
  69. return 0;
  70. if (uuid_is_nil(uuid)) {
  71. xfs_warn(mp, "Filesystem has nil UUID - can't mount");
  72. return XFS_ERROR(EINVAL);
  73. }
  74. mutex_lock(&xfs_uuid_table_mutex);
  75. for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
  76. if (uuid_is_nil(&xfs_uuid_table[i])) {
  77. hole = i;
  78. continue;
  79. }
  80. if (uuid_equal(uuid, &xfs_uuid_table[i]))
  81. goto out_duplicate;
  82. }
  83. if (hole < 0) {
  84. xfs_uuid_table = kmem_realloc(xfs_uuid_table,
  85. (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
  86. xfs_uuid_table_size * sizeof(*xfs_uuid_table),
  87. KM_SLEEP);
  88. hole = xfs_uuid_table_size++;
  89. }
  90. xfs_uuid_table[hole] = *uuid;
  91. mutex_unlock(&xfs_uuid_table_mutex);
  92. return 0;
  93. out_duplicate:
  94. mutex_unlock(&xfs_uuid_table_mutex);
  95. xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
  96. return XFS_ERROR(EINVAL);
  97. }
  98. STATIC void
  99. xfs_uuid_unmount(
  100. struct xfs_mount *mp)
  101. {
  102. uuid_t *uuid = &mp->m_sb.sb_uuid;
  103. int i;
  104. if (mp->m_flags & XFS_MOUNT_NOUUID)
  105. return;
  106. mutex_lock(&xfs_uuid_table_mutex);
  107. for (i = 0; i < xfs_uuid_table_size; i++) {
  108. if (uuid_is_nil(&xfs_uuid_table[i]))
  109. continue;
  110. if (!uuid_equal(uuid, &xfs_uuid_table[i]))
  111. continue;
  112. memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
  113. break;
  114. }
  115. ASSERT(i < xfs_uuid_table_size);
  116. mutex_unlock(&xfs_uuid_table_mutex);
  117. }
  118. STATIC void
  119. __xfs_free_perag(
  120. struct rcu_head *head)
  121. {
  122. struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
  123. ASSERT(atomic_read(&pag->pag_ref) == 0);
  124. kmem_free(pag);
  125. }
  126. /*
  127. * Free up the per-ag resources associated with the mount structure.
  128. */
  129. STATIC void
  130. xfs_free_perag(
  131. xfs_mount_t *mp)
  132. {
  133. xfs_agnumber_t agno;
  134. struct xfs_perag *pag;
  135. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  136. spin_lock(&mp->m_perag_lock);
  137. pag = radix_tree_delete(&mp->m_perag_tree, agno);
  138. spin_unlock(&mp->m_perag_lock);
  139. ASSERT(pag);
  140. ASSERT(atomic_read(&pag->pag_ref) == 0);
  141. call_rcu(&pag->rcu_head, __xfs_free_perag);
  142. }
  143. }
  144. /*
  145. * Check size of device based on the (data/realtime) block count.
  146. * Note: this check is used by the growfs code as well as mount.
  147. */
  148. int
  149. xfs_sb_validate_fsb_count(
  150. xfs_sb_t *sbp,
  151. __uint64_t nblocks)
  152. {
  153. ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
  154. ASSERT(sbp->sb_blocklog >= BBSHIFT);
  155. #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
  156. if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
  157. return EFBIG;
  158. #else /* Limited by UINT_MAX of sectors */
  159. if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
  160. return EFBIG;
  161. #endif
  162. return 0;
  163. }
  164. int
  165. xfs_initialize_perag(
  166. xfs_mount_t *mp,
  167. xfs_agnumber_t agcount,
  168. xfs_agnumber_t *maxagi)
  169. {
  170. xfs_agnumber_t index;
  171. xfs_agnumber_t first_initialised = 0;
  172. xfs_perag_t *pag;
  173. xfs_agino_t agino;
  174. xfs_ino_t ino;
  175. xfs_sb_t *sbp = &mp->m_sb;
  176. int error = -ENOMEM;
  177. /*
  178. * Walk the current per-ag tree so we don't try to initialise AGs
  179. * that already exist (growfs case). Allocate and insert all the
  180. * AGs we don't find ready for initialisation.
  181. */
  182. for (index = 0; index < agcount; index++) {
  183. pag = xfs_perag_get(mp, index);
  184. if (pag) {
  185. xfs_perag_put(pag);
  186. continue;
  187. }
  188. if (!first_initialised)
  189. first_initialised = index;
  190. pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
  191. if (!pag)
  192. goto out_unwind;
  193. pag->pag_agno = index;
  194. pag->pag_mount = mp;
  195. spin_lock_init(&pag->pag_ici_lock);
  196. mutex_init(&pag->pag_ici_reclaim_lock);
  197. INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
  198. spin_lock_init(&pag->pag_buf_lock);
  199. pag->pag_buf_tree = RB_ROOT;
  200. if (radix_tree_preload(GFP_NOFS))
  201. goto out_unwind;
  202. spin_lock(&mp->m_perag_lock);
  203. if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
  204. BUG();
  205. spin_unlock(&mp->m_perag_lock);
  206. radix_tree_preload_end();
  207. error = -EEXIST;
  208. goto out_unwind;
  209. }
  210. spin_unlock(&mp->m_perag_lock);
  211. radix_tree_preload_end();
  212. }
  213. /*
  214. * If we mount with the inode64 option, or no inode overflows
  215. * the legacy 32-bit address space clear the inode32 option.
  216. */
  217. agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
  218. ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
  219. if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
  220. mp->m_flags |= XFS_MOUNT_32BITINODES;
  221. else
  222. mp->m_flags &= ~XFS_MOUNT_32BITINODES;
  223. if (mp->m_flags & XFS_MOUNT_32BITINODES)
  224. index = xfs_set_inode32(mp);
  225. else
  226. index = xfs_set_inode64(mp);
  227. if (maxagi)
  228. *maxagi = index;
  229. return 0;
  230. out_unwind:
  231. kmem_free(pag);
  232. for (; index > first_initialised; index--) {
  233. pag = radix_tree_delete(&mp->m_perag_tree, index);
  234. kmem_free(pag);
  235. }
  236. return error;
  237. }
  238. /*
  239. * xfs_readsb
  240. *
  241. * Does the initial read of the superblock.
  242. */
  243. int
  244. xfs_readsb(
  245. struct xfs_mount *mp,
  246. int flags)
  247. {
  248. unsigned int sector_size;
  249. struct xfs_buf *bp;
  250. struct xfs_sb *sbp = &mp->m_sb;
  251. int error;
  252. int loud = !(flags & XFS_MFSI_QUIET);
  253. const struct xfs_buf_ops *buf_ops;
  254. ASSERT(mp->m_sb_bp == NULL);
  255. ASSERT(mp->m_ddev_targp != NULL);
  256. /*
  257. * For the initial read, we must guess at the sector
  258. * size based on the block device. It's enough to
  259. * get the sb_sectsize out of the superblock and
  260. * then reread with the proper length.
  261. * We don't verify it yet, because it may not be complete.
  262. */
  263. sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
  264. buf_ops = NULL;
  265. /*
  266. * Allocate a (locked) buffer to hold the superblock.
  267. * This will be kept around at all times to optimize
  268. * access to the superblock.
  269. */
  270. reread:
  271. bp = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
  272. BTOBB(sector_size), 0, buf_ops);
  273. if (!bp) {
  274. if (loud)
  275. xfs_warn(mp, "SB buffer read failed");
  276. return EIO;
  277. }
  278. if (bp->b_error) {
  279. error = bp->b_error;
  280. if (loud)
  281. xfs_warn(mp, "SB validate failed with error %d.", error);
  282. /* bad CRC means corrupted metadata */
  283. if (error == EFSBADCRC)
  284. error = EFSCORRUPTED;
  285. goto release_buf;
  286. }
  287. /*
  288. * Initialize the mount structure from the superblock.
  289. */
  290. xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
  291. xfs_sb_quota_from_disk(sbp);
  292. /*
  293. * If we haven't validated the superblock, do so now before we try
  294. * to check the sector size and reread the superblock appropriately.
  295. */
  296. if (sbp->sb_magicnum != XFS_SB_MAGIC) {
  297. if (loud)
  298. xfs_warn(mp, "Invalid superblock magic number");
  299. error = EINVAL;
  300. goto release_buf;
  301. }
  302. /*
  303. * We must be able to do sector-sized and sector-aligned IO.
  304. */
  305. if (sector_size > sbp->sb_sectsize) {
  306. if (loud)
  307. xfs_warn(mp, "device supports %u byte sectors (not %u)",
  308. sector_size, sbp->sb_sectsize);
  309. error = ENOSYS;
  310. goto release_buf;
  311. }
  312. if (buf_ops == NULL) {
  313. /*
  314. * Re-read the superblock so the buffer is correctly sized,
  315. * and properly verified.
  316. */
  317. xfs_buf_relse(bp);
  318. sector_size = sbp->sb_sectsize;
  319. buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
  320. goto reread;
  321. }
  322. /* Initialize per-cpu counters */
  323. xfs_icsb_reinit_counters(mp);
  324. /* no need to be quiet anymore, so reset the buf ops */
  325. bp->b_ops = &xfs_sb_buf_ops;
  326. mp->m_sb_bp = bp;
  327. xfs_buf_unlock(bp);
  328. return 0;
  329. release_buf:
  330. xfs_buf_relse(bp);
  331. return error;
  332. }
  333. /*
  334. * Update alignment values based on mount options and sb values
  335. */
  336. STATIC int
  337. xfs_update_alignment(xfs_mount_t *mp)
  338. {
  339. xfs_sb_t *sbp = &(mp->m_sb);
  340. if (mp->m_dalign) {
  341. /*
  342. * If stripe unit and stripe width are not multiples
  343. * of the fs blocksize turn off alignment.
  344. */
  345. if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
  346. (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
  347. xfs_warn(mp,
  348. "alignment check failed: sunit/swidth vs. blocksize(%d)",
  349. sbp->sb_blocksize);
  350. return XFS_ERROR(EINVAL);
  351. } else {
  352. /*
  353. * Convert the stripe unit and width to FSBs.
  354. */
  355. mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
  356. if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
  357. xfs_warn(mp,
  358. "alignment check failed: sunit/swidth vs. agsize(%d)",
  359. sbp->sb_agblocks);
  360. return XFS_ERROR(EINVAL);
  361. } else if (mp->m_dalign) {
  362. mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
  363. } else {
  364. xfs_warn(mp,
  365. "alignment check failed: sunit(%d) less than bsize(%d)",
  366. mp->m_dalign, sbp->sb_blocksize);
  367. return XFS_ERROR(EINVAL);
  368. }
  369. }
  370. /*
  371. * Update superblock with new values
  372. * and log changes
  373. */
  374. if (xfs_sb_version_hasdalign(sbp)) {
  375. if (sbp->sb_unit != mp->m_dalign) {
  376. sbp->sb_unit = mp->m_dalign;
  377. mp->m_update_flags |= XFS_SB_UNIT;
  378. }
  379. if (sbp->sb_width != mp->m_swidth) {
  380. sbp->sb_width = mp->m_swidth;
  381. mp->m_update_flags |= XFS_SB_WIDTH;
  382. }
  383. } else {
  384. xfs_warn(mp,
  385. "cannot change alignment: superblock does not support data alignment");
  386. return XFS_ERROR(EINVAL);
  387. }
  388. } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
  389. xfs_sb_version_hasdalign(&mp->m_sb)) {
  390. mp->m_dalign = sbp->sb_unit;
  391. mp->m_swidth = sbp->sb_width;
  392. }
  393. return 0;
  394. }
  395. /*
  396. * Set the maximum inode count for this filesystem
  397. */
  398. STATIC void
  399. xfs_set_maxicount(xfs_mount_t *mp)
  400. {
  401. xfs_sb_t *sbp = &(mp->m_sb);
  402. __uint64_t icount;
  403. if (sbp->sb_imax_pct) {
  404. /*
  405. * Make sure the maximum inode count is a multiple
  406. * of the units we allocate inodes in.
  407. */
  408. icount = sbp->sb_dblocks * sbp->sb_imax_pct;
  409. do_div(icount, 100);
  410. do_div(icount, mp->m_ialloc_blks);
  411. mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
  412. sbp->sb_inopblog;
  413. } else {
  414. mp->m_maxicount = 0;
  415. }
  416. }
  417. /*
  418. * Set the default minimum read and write sizes unless
  419. * already specified in a mount option.
  420. * We use smaller I/O sizes when the file system
  421. * is being used for NFS service (wsync mount option).
  422. */
  423. STATIC void
  424. xfs_set_rw_sizes(xfs_mount_t *mp)
  425. {
  426. xfs_sb_t *sbp = &(mp->m_sb);
  427. int readio_log, writeio_log;
  428. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
  429. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  430. readio_log = XFS_WSYNC_READIO_LOG;
  431. writeio_log = XFS_WSYNC_WRITEIO_LOG;
  432. } else {
  433. readio_log = XFS_READIO_LOG_LARGE;
  434. writeio_log = XFS_WRITEIO_LOG_LARGE;
  435. }
  436. } else {
  437. readio_log = mp->m_readio_log;
  438. writeio_log = mp->m_writeio_log;
  439. }
  440. if (sbp->sb_blocklog > readio_log) {
  441. mp->m_readio_log = sbp->sb_blocklog;
  442. } else {
  443. mp->m_readio_log = readio_log;
  444. }
  445. mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
  446. if (sbp->sb_blocklog > writeio_log) {
  447. mp->m_writeio_log = sbp->sb_blocklog;
  448. } else {
  449. mp->m_writeio_log = writeio_log;
  450. }
  451. mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
  452. }
  453. /*
  454. * precalculate the low space thresholds for dynamic speculative preallocation.
  455. */
  456. void
  457. xfs_set_low_space_thresholds(
  458. struct xfs_mount *mp)
  459. {
  460. int i;
  461. for (i = 0; i < XFS_LOWSP_MAX; i++) {
  462. __uint64_t space = mp->m_sb.sb_dblocks;
  463. do_div(space, 100);
  464. mp->m_low_space[i] = space * (i + 1);
  465. }
  466. }
  467. /*
  468. * Set whether we're using inode alignment.
  469. */
  470. STATIC void
  471. xfs_set_inoalignment(xfs_mount_t *mp)
  472. {
  473. if (xfs_sb_version_hasalign(&mp->m_sb) &&
  474. mp->m_sb.sb_inoalignmt >=
  475. XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
  476. mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
  477. else
  478. mp->m_inoalign_mask = 0;
  479. /*
  480. * If we are using stripe alignment, check whether
  481. * the stripe unit is a multiple of the inode alignment
  482. */
  483. if (mp->m_dalign && mp->m_inoalign_mask &&
  484. !(mp->m_dalign & mp->m_inoalign_mask))
  485. mp->m_sinoalign = mp->m_dalign;
  486. else
  487. mp->m_sinoalign = 0;
  488. }
  489. /*
  490. * Check that the data (and log if separate) is an ok size.
  491. */
  492. STATIC int
  493. xfs_check_sizes(xfs_mount_t *mp)
  494. {
  495. xfs_buf_t *bp;
  496. xfs_daddr_t d;
  497. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  498. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
  499. xfs_warn(mp, "filesystem size mismatch detected");
  500. return XFS_ERROR(EFBIG);
  501. }
  502. bp = xfs_buf_read_uncached(mp->m_ddev_targp,
  503. d - XFS_FSS_TO_BB(mp, 1),
  504. XFS_FSS_TO_BB(mp, 1), 0, NULL);
  505. if (!bp) {
  506. xfs_warn(mp, "last sector read failed");
  507. return EIO;
  508. }
  509. xfs_buf_relse(bp);
  510. if (mp->m_logdev_targp != mp->m_ddev_targp) {
  511. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
  512. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
  513. xfs_warn(mp, "log size mismatch detected");
  514. return XFS_ERROR(EFBIG);
  515. }
  516. bp = xfs_buf_read_uncached(mp->m_logdev_targp,
  517. d - XFS_FSB_TO_BB(mp, 1),
  518. XFS_FSB_TO_BB(mp, 1), 0, NULL);
  519. if (!bp) {
  520. xfs_warn(mp, "log device read failed");
  521. return EIO;
  522. }
  523. xfs_buf_relse(bp);
  524. }
  525. return 0;
  526. }
  527. /*
  528. * Clear the quotaflags in memory and in the superblock.
  529. */
  530. int
  531. xfs_mount_reset_sbqflags(
  532. struct xfs_mount *mp)
  533. {
  534. int error;
  535. struct xfs_trans *tp;
  536. mp->m_qflags = 0;
  537. /*
  538. * It is OK to look at sb_qflags here in mount path,
  539. * without m_sb_lock.
  540. */
  541. if (mp->m_sb.sb_qflags == 0)
  542. return 0;
  543. spin_lock(&mp->m_sb_lock);
  544. mp->m_sb.sb_qflags = 0;
  545. spin_unlock(&mp->m_sb_lock);
  546. /*
  547. * If the fs is readonly, let the incore superblock run
  548. * with quotas off but don't flush the update out to disk
  549. */
  550. if (mp->m_flags & XFS_MOUNT_RDONLY)
  551. return 0;
  552. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
  553. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_sbchange, 0, 0);
  554. if (error) {
  555. xfs_trans_cancel(tp, 0);
  556. xfs_alert(mp, "%s: Superblock update failed!", __func__);
  557. return error;
  558. }
  559. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  560. return xfs_trans_commit(tp, 0);
  561. }
  562. __uint64_t
  563. xfs_default_resblks(xfs_mount_t *mp)
  564. {
  565. __uint64_t resblks;
  566. /*
  567. * We default to 5% or 8192 fsbs of space reserved, whichever is
  568. * smaller. This is intended to cover concurrent allocation
  569. * transactions when we initially hit enospc. These each require a 4
  570. * block reservation. Hence by default we cover roughly 2000 concurrent
  571. * allocation reservations.
  572. */
  573. resblks = mp->m_sb.sb_dblocks;
  574. do_div(resblks, 20);
  575. resblks = min_t(__uint64_t, resblks, 8192);
  576. return resblks;
  577. }
  578. /*
  579. * This function does the following on an initial mount of a file system:
  580. * - reads the superblock from disk and init the mount struct
  581. * - if we're a 32-bit kernel, do a size check on the superblock
  582. * so we don't mount terabyte filesystems
  583. * - init mount struct realtime fields
  584. * - allocate inode hash table for fs
  585. * - init directory manager
  586. * - perform recovery and init the log manager
  587. */
  588. int
  589. xfs_mountfs(
  590. xfs_mount_t *mp)
  591. {
  592. xfs_sb_t *sbp = &(mp->m_sb);
  593. xfs_inode_t *rip;
  594. __uint64_t resblks;
  595. uint quotamount = 0;
  596. uint quotaflags = 0;
  597. int error = 0;
  598. xfs_sb_mount_common(mp, sbp);
  599. /*
  600. * Check for a mismatched features2 values. Older kernels
  601. * read & wrote into the wrong sb offset for sb_features2
  602. * on some platforms due to xfs_sb_t not being 64bit size aligned
  603. * when sb_features2 was added, which made older superblock
  604. * reading/writing routines swap it as a 64-bit value.
  605. *
  606. * For backwards compatibility, we make both slots equal.
  607. *
  608. * If we detect a mismatched field, we OR the set bits into the
  609. * existing features2 field in case it has already been modified; we
  610. * don't want to lose any features. We then update the bad location
  611. * with the ORed value so that older kernels will see any features2
  612. * flags, and mark the two fields as needing updates once the
  613. * transaction subsystem is online.
  614. */
  615. if (xfs_sb_has_mismatched_features2(sbp)) {
  616. xfs_warn(mp, "correcting sb_features alignment problem");
  617. sbp->sb_features2 |= sbp->sb_bad_features2;
  618. sbp->sb_bad_features2 = sbp->sb_features2;
  619. mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
  620. /*
  621. * Re-check for ATTR2 in case it was found in bad_features2
  622. * slot.
  623. */
  624. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  625. !(mp->m_flags & XFS_MOUNT_NOATTR2))
  626. mp->m_flags |= XFS_MOUNT_ATTR2;
  627. }
  628. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  629. (mp->m_flags & XFS_MOUNT_NOATTR2)) {
  630. xfs_sb_version_removeattr2(&mp->m_sb);
  631. mp->m_update_flags |= XFS_SB_FEATURES2;
  632. /* update sb_versionnum for the clearing of the morebits */
  633. if (!sbp->sb_features2)
  634. mp->m_update_flags |= XFS_SB_VERSIONNUM;
  635. }
  636. /* always use v2 inodes by default now */
  637. if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
  638. mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
  639. mp->m_update_flags |= XFS_SB_VERSIONNUM;
  640. }
  641. /*
  642. * Check if sb_agblocks is aligned at stripe boundary
  643. * If sb_agblocks is NOT aligned turn off m_dalign since
  644. * allocator alignment is within an ag, therefore ag has
  645. * to be aligned at stripe boundary.
  646. */
  647. error = xfs_update_alignment(mp);
  648. if (error)
  649. goto out;
  650. xfs_alloc_compute_maxlevels(mp);
  651. xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
  652. xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
  653. xfs_ialloc_compute_maxlevels(mp);
  654. xfs_set_maxicount(mp);
  655. error = xfs_uuid_mount(mp);
  656. if (error)
  657. goto out;
  658. /*
  659. * Set the minimum read and write sizes
  660. */
  661. xfs_set_rw_sizes(mp);
  662. /* set the low space thresholds for dynamic preallocation */
  663. xfs_set_low_space_thresholds(mp);
  664. /*
  665. * Set the inode cluster size.
  666. * This may still be overridden by the file system
  667. * block size if it is larger than the chosen cluster size.
  668. *
  669. * For v5 filesystems, scale the cluster size with the inode size to
  670. * keep a constant ratio of inode per cluster buffer, but only if mkfs
  671. * has set the inode alignment value appropriately for larger cluster
  672. * sizes.
  673. */
  674. mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
  675. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  676. int new_size = mp->m_inode_cluster_size;
  677. new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
  678. if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
  679. mp->m_inode_cluster_size = new_size;
  680. }
  681. /*
  682. * Set inode alignment fields
  683. */
  684. xfs_set_inoalignment(mp);
  685. /*
  686. * Check that the data (and log if separate) is an ok size.
  687. */
  688. error = xfs_check_sizes(mp);
  689. if (error)
  690. goto out_remove_uuid;
  691. /*
  692. * Initialize realtime fields in the mount structure
  693. */
  694. error = xfs_rtmount_init(mp);
  695. if (error) {
  696. xfs_warn(mp, "RT mount failed");
  697. goto out_remove_uuid;
  698. }
  699. /*
  700. * Copies the low order bits of the timestamp and the randomly
  701. * set "sequence" number out of a UUID.
  702. */
  703. uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
  704. mp->m_dmevmask = 0; /* not persistent; set after each mount */
  705. error = xfs_da_mount(mp);
  706. if (error) {
  707. xfs_warn(mp, "Failed dir/attr init: %d", error);
  708. goto out_remove_uuid;
  709. }
  710. /*
  711. * Initialize the precomputed transaction reservations values.
  712. */
  713. xfs_trans_init(mp);
  714. /*
  715. * Allocate and initialize the per-ag data.
  716. */
  717. spin_lock_init(&mp->m_perag_lock);
  718. INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
  719. error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
  720. if (error) {
  721. xfs_warn(mp, "Failed per-ag init: %d", error);
  722. goto out_free_dir;
  723. }
  724. if (!sbp->sb_logblocks) {
  725. xfs_warn(mp, "no log defined");
  726. XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
  727. error = XFS_ERROR(EFSCORRUPTED);
  728. goto out_free_perag;
  729. }
  730. /*
  731. * log's mount-time initialization. Perform 1st part recovery if needed
  732. */
  733. error = xfs_log_mount(mp, mp->m_logdev_targp,
  734. XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
  735. XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
  736. if (error) {
  737. xfs_warn(mp, "log mount failed");
  738. goto out_fail_wait;
  739. }
  740. /*
  741. * Now the log is mounted, we know if it was an unclean shutdown or
  742. * not. If it was, with the first phase of recovery has completed, we
  743. * have consistent AG blocks on disk. We have not recovered EFIs yet,
  744. * but they are recovered transactionally in the second recovery phase
  745. * later.
  746. *
  747. * Hence we can safely re-initialise incore superblock counters from
  748. * the per-ag data. These may not be correct if the filesystem was not
  749. * cleanly unmounted, so we need to wait for recovery to finish before
  750. * doing this.
  751. *
  752. * If the filesystem was cleanly unmounted, then we can trust the
  753. * values in the superblock to be correct and we don't need to do
  754. * anything here.
  755. *
  756. * If we are currently making the filesystem, the initialisation will
  757. * fail as the perag data is in an undefined state.
  758. */
  759. if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
  760. !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
  761. !mp->m_sb.sb_inprogress) {
  762. error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
  763. if (error)
  764. goto out_fail_wait;
  765. }
  766. /*
  767. * Get and sanity-check the root inode.
  768. * Save the pointer to it in the mount structure.
  769. */
  770. error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
  771. if (error) {
  772. xfs_warn(mp, "failed to read root inode");
  773. goto out_log_dealloc;
  774. }
  775. ASSERT(rip != NULL);
  776. if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
  777. xfs_warn(mp, "corrupted root inode %llu: not a directory",
  778. (unsigned long long)rip->i_ino);
  779. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  780. XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
  781. mp);
  782. error = XFS_ERROR(EFSCORRUPTED);
  783. goto out_rele_rip;
  784. }
  785. mp->m_rootip = rip; /* save it */
  786. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  787. /*
  788. * Initialize realtime inode pointers in the mount structure
  789. */
  790. error = xfs_rtmount_inodes(mp);
  791. if (error) {
  792. /*
  793. * Free up the root inode.
  794. */
  795. xfs_warn(mp, "failed to read RT inodes");
  796. goto out_rele_rip;
  797. }
  798. /*
  799. * If this is a read-only mount defer the superblock updates until
  800. * the next remount into writeable mode. Otherwise we would never
  801. * perform the update e.g. for the root filesystem.
  802. */
  803. if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
  804. error = xfs_mount_log_sb(mp, mp->m_update_flags);
  805. if (error) {
  806. xfs_warn(mp, "failed to write sb changes");
  807. goto out_rtunmount;
  808. }
  809. }
  810. /*
  811. * Initialise the XFS quota management subsystem for this mount
  812. */
  813. if (XFS_IS_QUOTA_RUNNING(mp)) {
  814. error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
  815. if (error)
  816. goto out_rtunmount;
  817. } else {
  818. ASSERT(!XFS_IS_QUOTA_ON(mp));
  819. /*
  820. * If a file system had quotas running earlier, but decided to
  821. * mount without -o uquota/pquota/gquota options, revoke the
  822. * quotachecked license.
  823. */
  824. if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
  825. xfs_notice(mp, "resetting quota flags");
  826. error = xfs_mount_reset_sbqflags(mp);
  827. if (error)
  828. return error;
  829. }
  830. }
  831. /*
  832. * Finish recovering the file system. This part needed to be
  833. * delayed until after the root and real-time bitmap inodes
  834. * were consistently read in.
  835. */
  836. error = xfs_log_mount_finish(mp);
  837. if (error) {
  838. xfs_warn(mp, "log mount finish failed");
  839. goto out_rtunmount;
  840. }
  841. /*
  842. * Complete the quota initialisation, post-log-replay component.
  843. */
  844. if (quotamount) {
  845. ASSERT(mp->m_qflags == 0);
  846. mp->m_qflags = quotaflags;
  847. xfs_qm_mount_quotas(mp);
  848. }
  849. /*
  850. * Now we are mounted, reserve a small amount of unused space for
  851. * privileged transactions. This is needed so that transaction
  852. * space required for critical operations can dip into this pool
  853. * when at ENOSPC. This is needed for operations like create with
  854. * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
  855. * are not allowed to use this reserved space.
  856. *
  857. * This may drive us straight to ENOSPC on mount, but that implies
  858. * we were already there on the last unmount. Warn if this occurs.
  859. */
  860. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  861. resblks = xfs_default_resblks(mp);
  862. error = xfs_reserve_blocks(mp, &resblks, NULL);
  863. if (error)
  864. xfs_warn(mp,
  865. "Unable to allocate reserve blocks. Continuing without reserve pool.");
  866. }
  867. return 0;
  868. out_rtunmount:
  869. xfs_rtunmount_inodes(mp);
  870. out_rele_rip:
  871. IRELE(rip);
  872. out_log_dealloc:
  873. xfs_log_unmount(mp);
  874. out_fail_wait:
  875. if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
  876. xfs_wait_buftarg(mp->m_logdev_targp);
  877. xfs_wait_buftarg(mp->m_ddev_targp);
  878. out_free_perag:
  879. xfs_free_perag(mp);
  880. out_free_dir:
  881. xfs_da_unmount(mp);
  882. out_remove_uuid:
  883. xfs_uuid_unmount(mp);
  884. out:
  885. return error;
  886. }
  887. /*
  888. * This flushes out the inodes,dquots and the superblock, unmounts the
  889. * log and makes sure that incore structures are freed.
  890. */
  891. void
  892. xfs_unmountfs(
  893. struct xfs_mount *mp)
  894. {
  895. __uint64_t resblks;
  896. int error;
  897. cancel_delayed_work_sync(&mp->m_eofblocks_work);
  898. xfs_qm_unmount_quotas(mp);
  899. xfs_rtunmount_inodes(mp);
  900. IRELE(mp->m_rootip);
  901. /*
  902. * We can potentially deadlock here if we have an inode cluster
  903. * that has been freed has its buffer still pinned in memory because
  904. * the transaction is still sitting in a iclog. The stale inodes
  905. * on that buffer will have their flush locks held until the
  906. * transaction hits the disk and the callbacks run. the inode
  907. * flush takes the flush lock unconditionally and with nothing to
  908. * push out the iclog we will never get that unlocked. hence we
  909. * need to force the log first.
  910. */
  911. xfs_log_force(mp, XFS_LOG_SYNC);
  912. /*
  913. * Flush all pending changes from the AIL.
  914. */
  915. xfs_ail_push_all_sync(mp->m_ail);
  916. /*
  917. * And reclaim all inodes. At this point there should be no dirty
  918. * inodes and none should be pinned or locked, but use synchronous
  919. * reclaim just to be sure. We can stop background inode reclaim
  920. * here as well if it is still running.
  921. */
  922. cancel_delayed_work_sync(&mp->m_reclaim_work);
  923. xfs_reclaim_inodes(mp, SYNC_WAIT);
  924. xfs_qm_unmount(mp);
  925. /*
  926. * Unreserve any blocks we have so that when we unmount we don't account
  927. * the reserved free space as used. This is really only necessary for
  928. * lazy superblock counting because it trusts the incore superblock
  929. * counters to be absolutely correct on clean unmount.
  930. *
  931. * We don't bother correcting this elsewhere for lazy superblock
  932. * counting because on mount of an unclean filesystem we reconstruct the
  933. * correct counter value and this is irrelevant.
  934. *
  935. * For non-lazy counter filesystems, this doesn't matter at all because
  936. * we only every apply deltas to the superblock and hence the incore
  937. * value does not matter....
  938. */
  939. resblks = 0;
  940. error = xfs_reserve_blocks(mp, &resblks, NULL);
  941. if (error)
  942. xfs_warn(mp, "Unable to free reserved block pool. "
  943. "Freespace may not be correct on next mount.");
  944. error = xfs_log_sbcount(mp);
  945. if (error)
  946. xfs_warn(mp, "Unable to update superblock counters. "
  947. "Freespace may not be correct on next mount.");
  948. xfs_log_unmount(mp);
  949. xfs_da_unmount(mp);
  950. xfs_uuid_unmount(mp);
  951. #if defined(DEBUG)
  952. xfs_errortag_clearall(mp, 0);
  953. #endif
  954. xfs_free_perag(mp);
  955. }
  956. int
  957. xfs_fs_writable(xfs_mount_t *mp)
  958. {
  959. return !(mp->m_super->s_writers.frozen || XFS_FORCED_SHUTDOWN(mp) ||
  960. (mp->m_flags & XFS_MOUNT_RDONLY));
  961. }
  962. /*
  963. * xfs_log_sbcount
  964. *
  965. * Sync the superblock counters to disk.
  966. *
  967. * Note this code can be called during the process of freezing, so
  968. * we may need to use the transaction allocator which does not
  969. * block when the transaction subsystem is in its frozen state.
  970. */
  971. int
  972. xfs_log_sbcount(xfs_mount_t *mp)
  973. {
  974. xfs_trans_t *tp;
  975. int error;
  976. if (!xfs_fs_writable(mp))
  977. return 0;
  978. xfs_icsb_sync_counters(mp, 0);
  979. /*
  980. * we don't need to do this if we are updating the superblock
  981. * counters on every modification.
  982. */
  983. if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
  984. return 0;
  985. tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
  986. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
  987. if (error) {
  988. xfs_trans_cancel(tp, 0);
  989. return error;
  990. }
  991. xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
  992. xfs_trans_set_sync(tp);
  993. error = xfs_trans_commit(tp, 0);
  994. return error;
  995. }
  996. /*
  997. * xfs_mod_incore_sb_unlocked() is a utility routine commonly used to apply
  998. * a delta to a specified field in the in-core superblock. Simply
  999. * switch on the field indicated and apply the delta to that field.
  1000. * Fields are not allowed to dip below zero, so if the delta would
  1001. * do this do not apply it and return EINVAL.
  1002. *
  1003. * The m_sb_lock must be held when this routine is called.
  1004. */
  1005. STATIC int
  1006. xfs_mod_incore_sb_unlocked(
  1007. xfs_mount_t *mp,
  1008. xfs_sb_field_t field,
  1009. int64_t delta,
  1010. int rsvd)
  1011. {
  1012. int scounter; /* short counter for 32 bit fields */
  1013. long long lcounter; /* long counter for 64 bit fields */
  1014. long long res_used, rem;
  1015. /*
  1016. * With the in-core superblock spin lock held, switch
  1017. * on the indicated field. Apply the delta to the
  1018. * proper field. If the fields value would dip below
  1019. * 0, then do not apply the delta and return EINVAL.
  1020. */
  1021. switch (field) {
  1022. case XFS_SBS_ICOUNT:
  1023. lcounter = (long long)mp->m_sb.sb_icount;
  1024. lcounter += delta;
  1025. if (lcounter < 0) {
  1026. ASSERT(0);
  1027. return XFS_ERROR(EINVAL);
  1028. }
  1029. mp->m_sb.sb_icount = lcounter;
  1030. return 0;
  1031. case XFS_SBS_IFREE:
  1032. lcounter = (long long)mp->m_sb.sb_ifree;
  1033. lcounter += delta;
  1034. if (lcounter < 0) {
  1035. ASSERT(0);
  1036. return XFS_ERROR(EINVAL);
  1037. }
  1038. mp->m_sb.sb_ifree = lcounter;
  1039. return 0;
  1040. case XFS_SBS_FDBLOCKS:
  1041. lcounter = (long long)
  1042. mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  1043. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1044. if (delta > 0) { /* Putting blocks back */
  1045. if (res_used > delta) {
  1046. mp->m_resblks_avail += delta;
  1047. } else {
  1048. rem = delta - res_used;
  1049. mp->m_resblks_avail = mp->m_resblks;
  1050. lcounter += rem;
  1051. }
  1052. } else { /* Taking blocks away */
  1053. lcounter += delta;
  1054. if (lcounter >= 0) {
  1055. mp->m_sb.sb_fdblocks = lcounter +
  1056. XFS_ALLOC_SET_ASIDE(mp);
  1057. return 0;
  1058. }
  1059. /*
  1060. * We are out of blocks, use any available reserved
  1061. * blocks if were allowed to.
  1062. */
  1063. if (!rsvd)
  1064. return XFS_ERROR(ENOSPC);
  1065. lcounter = (long long)mp->m_resblks_avail + delta;
  1066. if (lcounter >= 0) {
  1067. mp->m_resblks_avail = lcounter;
  1068. return 0;
  1069. }
  1070. printk_once(KERN_WARNING
  1071. "Filesystem \"%s\": reserve blocks depleted! "
  1072. "Consider increasing reserve pool size.",
  1073. mp->m_fsname);
  1074. return XFS_ERROR(ENOSPC);
  1075. }
  1076. mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
  1077. return 0;
  1078. case XFS_SBS_FREXTENTS:
  1079. lcounter = (long long)mp->m_sb.sb_frextents;
  1080. lcounter += delta;
  1081. if (lcounter < 0) {
  1082. return XFS_ERROR(ENOSPC);
  1083. }
  1084. mp->m_sb.sb_frextents = lcounter;
  1085. return 0;
  1086. case XFS_SBS_DBLOCKS:
  1087. lcounter = (long long)mp->m_sb.sb_dblocks;
  1088. lcounter += delta;
  1089. if (lcounter < 0) {
  1090. ASSERT(0);
  1091. return XFS_ERROR(EINVAL);
  1092. }
  1093. mp->m_sb.sb_dblocks = lcounter;
  1094. return 0;
  1095. case XFS_SBS_AGCOUNT:
  1096. scounter = mp->m_sb.sb_agcount;
  1097. scounter += delta;
  1098. if (scounter < 0) {
  1099. ASSERT(0);
  1100. return XFS_ERROR(EINVAL);
  1101. }
  1102. mp->m_sb.sb_agcount = scounter;
  1103. return 0;
  1104. case XFS_SBS_IMAX_PCT:
  1105. scounter = mp->m_sb.sb_imax_pct;
  1106. scounter += delta;
  1107. if (scounter < 0) {
  1108. ASSERT(0);
  1109. return XFS_ERROR(EINVAL);
  1110. }
  1111. mp->m_sb.sb_imax_pct = scounter;
  1112. return 0;
  1113. case XFS_SBS_REXTSIZE:
  1114. scounter = mp->m_sb.sb_rextsize;
  1115. scounter += delta;
  1116. if (scounter < 0) {
  1117. ASSERT(0);
  1118. return XFS_ERROR(EINVAL);
  1119. }
  1120. mp->m_sb.sb_rextsize = scounter;
  1121. return 0;
  1122. case XFS_SBS_RBMBLOCKS:
  1123. scounter = mp->m_sb.sb_rbmblocks;
  1124. scounter += delta;
  1125. if (scounter < 0) {
  1126. ASSERT(0);
  1127. return XFS_ERROR(EINVAL);
  1128. }
  1129. mp->m_sb.sb_rbmblocks = scounter;
  1130. return 0;
  1131. case XFS_SBS_RBLOCKS:
  1132. lcounter = (long long)mp->m_sb.sb_rblocks;
  1133. lcounter += delta;
  1134. if (lcounter < 0) {
  1135. ASSERT(0);
  1136. return XFS_ERROR(EINVAL);
  1137. }
  1138. mp->m_sb.sb_rblocks = lcounter;
  1139. return 0;
  1140. case XFS_SBS_REXTENTS:
  1141. lcounter = (long long)mp->m_sb.sb_rextents;
  1142. lcounter += delta;
  1143. if (lcounter < 0) {
  1144. ASSERT(0);
  1145. return XFS_ERROR(EINVAL);
  1146. }
  1147. mp->m_sb.sb_rextents = lcounter;
  1148. return 0;
  1149. case XFS_SBS_REXTSLOG:
  1150. scounter = mp->m_sb.sb_rextslog;
  1151. scounter += delta;
  1152. if (scounter < 0) {
  1153. ASSERT(0);
  1154. return XFS_ERROR(EINVAL);
  1155. }
  1156. mp->m_sb.sb_rextslog = scounter;
  1157. return 0;
  1158. default:
  1159. ASSERT(0);
  1160. return XFS_ERROR(EINVAL);
  1161. }
  1162. }
  1163. /*
  1164. * xfs_mod_incore_sb() is used to change a field in the in-core
  1165. * superblock structure by the specified delta. This modification
  1166. * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
  1167. * routine to do the work.
  1168. */
  1169. int
  1170. xfs_mod_incore_sb(
  1171. struct xfs_mount *mp,
  1172. xfs_sb_field_t field,
  1173. int64_t delta,
  1174. int rsvd)
  1175. {
  1176. int status;
  1177. #ifdef HAVE_PERCPU_SB
  1178. ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
  1179. #endif
  1180. spin_lock(&mp->m_sb_lock);
  1181. status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1182. spin_unlock(&mp->m_sb_lock);
  1183. return status;
  1184. }
  1185. /*
  1186. * Change more than one field in the in-core superblock structure at a time.
  1187. *
  1188. * The fields and changes to those fields are specified in the array of
  1189. * xfs_mod_sb structures passed in. Either all of the specified deltas
  1190. * will be applied or none of them will. If any modified field dips below 0,
  1191. * then all modifications will be backed out and EINVAL will be returned.
  1192. *
  1193. * Note that this function may not be used for the superblock values that
  1194. * are tracked with the in-memory per-cpu counters - a direct call to
  1195. * xfs_icsb_modify_counters is required for these.
  1196. */
  1197. int
  1198. xfs_mod_incore_sb_batch(
  1199. struct xfs_mount *mp,
  1200. xfs_mod_sb_t *msb,
  1201. uint nmsb,
  1202. int rsvd)
  1203. {
  1204. xfs_mod_sb_t *msbp;
  1205. int error = 0;
  1206. /*
  1207. * Loop through the array of mod structures and apply each individually.
  1208. * If any fail, then back out all those which have already been applied.
  1209. * Do all of this within the scope of the m_sb_lock so that all of the
  1210. * changes will be atomic.
  1211. */
  1212. spin_lock(&mp->m_sb_lock);
  1213. for (msbp = msb; msbp < (msb + nmsb); msbp++) {
  1214. ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
  1215. msbp->msb_field > XFS_SBS_FDBLOCKS);
  1216. error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
  1217. msbp->msb_delta, rsvd);
  1218. if (error)
  1219. goto unwind;
  1220. }
  1221. spin_unlock(&mp->m_sb_lock);
  1222. return 0;
  1223. unwind:
  1224. while (--msbp >= msb) {
  1225. error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
  1226. -msbp->msb_delta, rsvd);
  1227. ASSERT(error == 0);
  1228. }
  1229. spin_unlock(&mp->m_sb_lock);
  1230. return error;
  1231. }
  1232. /*
  1233. * xfs_getsb() is called to obtain the buffer for the superblock.
  1234. * The buffer is returned locked and read in from disk.
  1235. * The buffer should be released with a call to xfs_brelse().
  1236. *
  1237. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1238. * the superblock buffer if it can be locked without sleeping.
  1239. * If it can't then we'll return NULL.
  1240. */
  1241. struct xfs_buf *
  1242. xfs_getsb(
  1243. struct xfs_mount *mp,
  1244. int flags)
  1245. {
  1246. struct xfs_buf *bp = mp->m_sb_bp;
  1247. if (!xfs_buf_trylock(bp)) {
  1248. if (flags & XBF_TRYLOCK)
  1249. return NULL;
  1250. xfs_buf_lock(bp);
  1251. }
  1252. xfs_buf_hold(bp);
  1253. ASSERT(XFS_BUF_ISDONE(bp));
  1254. return bp;
  1255. }
  1256. /*
  1257. * Used to free the superblock along various error paths.
  1258. */
  1259. void
  1260. xfs_freesb(
  1261. struct xfs_mount *mp)
  1262. {
  1263. struct xfs_buf *bp = mp->m_sb_bp;
  1264. xfs_buf_lock(bp);
  1265. mp->m_sb_bp = NULL;
  1266. xfs_buf_relse(bp);
  1267. }
  1268. /*
  1269. * Used to log changes to the superblock unit and width fields which could
  1270. * be altered by the mount options, as well as any potential sb_features2
  1271. * fixup. Only the first superblock is updated.
  1272. */
  1273. int
  1274. xfs_mount_log_sb(
  1275. xfs_mount_t *mp,
  1276. __int64_t fields)
  1277. {
  1278. xfs_trans_t *tp;
  1279. int error;
  1280. ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
  1281. XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
  1282. XFS_SB_VERSIONNUM));
  1283. tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
  1284. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
  1285. if (error) {
  1286. xfs_trans_cancel(tp, 0);
  1287. return error;
  1288. }
  1289. xfs_mod_sb(tp, fields);
  1290. error = xfs_trans_commit(tp, 0);
  1291. return error;
  1292. }
  1293. /*
  1294. * If the underlying (data/log/rt) device is readonly, there are some
  1295. * operations that cannot proceed.
  1296. */
  1297. int
  1298. xfs_dev_is_read_only(
  1299. struct xfs_mount *mp,
  1300. char *message)
  1301. {
  1302. if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
  1303. xfs_readonly_buftarg(mp->m_logdev_targp) ||
  1304. (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
  1305. xfs_notice(mp, "%s required on read-only device.", message);
  1306. xfs_notice(mp, "write access unavailable, cannot proceed.");
  1307. return EROFS;
  1308. }
  1309. return 0;
  1310. }
  1311. #ifdef HAVE_PERCPU_SB
  1312. /*
  1313. * Per-cpu incore superblock counters
  1314. *
  1315. * Simple concept, difficult implementation
  1316. *
  1317. * Basically, replace the incore superblock counters with a distributed per cpu
  1318. * counter for contended fields (e.g. free block count).
  1319. *
  1320. * Difficulties arise in that the incore sb is used for ENOSPC checking, and
  1321. * hence needs to be accurately read when we are running low on space. Hence
  1322. * there is a method to enable and disable the per-cpu counters based on how
  1323. * much "stuff" is available in them.
  1324. *
  1325. * Basically, a counter is enabled if there is enough free resource to justify
  1326. * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
  1327. * ENOSPC), then we disable the counters to synchronise all callers and
  1328. * re-distribute the available resources.
  1329. *
  1330. * If, once we redistributed the available resources, we still get a failure,
  1331. * we disable the per-cpu counter and go through the slow path.
  1332. *
  1333. * The slow path is the current xfs_mod_incore_sb() function. This means that
  1334. * when we disable a per-cpu counter, we need to drain its resources back to
  1335. * the global superblock. We do this after disabling the counter to prevent
  1336. * more threads from queueing up on the counter.
  1337. *
  1338. * Essentially, this means that we still need a lock in the fast path to enable
  1339. * synchronisation between the global counters and the per-cpu counters. This
  1340. * is not a problem because the lock will be local to a CPU almost all the time
  1341. * and have little contention except when we get to ENOSPC conditions.
  1342. *
  1343. * Basically, this lock becomes a barrier that enables us to lock out the fast
  1344. * path while we do things like enabling and disabling counters and
  1345. * synchronising the counters.
  1346. *
  1347. * Locking rules:
  1348. *
  1349. * 1. m_sb_lock before picking up per-cpu locks
  1350. * 2. per-cpu locks always picked up via for_each_online_cpu() order
  1351. * 3. accurate counter sync requires m_sb_lock + per cpu locks
  1352. * 4. modifying per-cpu counters requires holding per-cpu lock
  1353. * 5. modifying global counters requires holding m_sb_lock
  1354. * 6. enabling or disabling a counter requires holding the m_sb_lock
  1355. * and _none_ of the per-cpu locks.
  1356. *
  1357. * Disabled counters are only ever re-enabled by a balance operation
  1358. * that results in more free resources per CPU than a given threshold.
  1359. * To ensure counters don't remain disabled, they are rebalanced when
  1360. * the global resource goes above a higher threshold (i.e. some hysteresis
  1361. * is present to prevent thrashing).
  1362. */
  1363. #ifdef CONFIG_HOTPLUG_CPU
  1364. /*
  1365. * hot-plug CPU notifier support.
  1366. *
  1367. * We need a notifier per filesystem as we need to be able to identify
  1368. * the filesystem to balance the counters out. This is achieved by
  1369. * having a notifier block embedded in the xfs_mount_t and doing pointer
  1370. * magic to get the mount pointer from the notifier block address.
  1371. */
  1372. STATIC int
  1373. xfs_icsb_cpu_notify(
  1374. struct notifier_block *nfb,
  1375. unsigned long action,
  1376. void *hcpu)
  1377. {
  1378. xfs_icsb_cnts_t *cntp;
  1379. xfs_mount_t *mp;
  1380. mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
  1381. cntp = (xfs_icsb_cnts_t *)
  1382. per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
  1383. switch (action) {
  1384. case CPU_UP_PREPARE:
  1385. case CPU_UP_PREPARE_FROZEN:
  1386. /* Easy Case - initialize the area and locks, and
  1387. * then rebalance when online does everything else for us. */
  1388. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1389. break;
  1390. case CPU_ONLINE:
  1391. case CPU_ONLINE_FROZEN:
  1392. xfs_icsb_lock(mp);
  1393. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
  1394. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
  1395. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
  1396. xfs_icsb_unlock(mp);
  1397. break;
  1398. case CPU_DEAD:
  1399. case CPU_DEAD_FROZEN:
  1400. /* Disable all the counters, then fold the dead cpu's
  1401. * count into the total on the global superblock and
  1402. * re-enable the counters. */
  1403. xfs_icsb_lock(mp);
  1404. spin_lock(&mp->m_sb_lock);
  1405. xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
  1406. xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
  1407. xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
  1408. mp->m_sb.sb_icount += cntp->icsb_icount;
  1409. mp->m_sb.sb_ifree += cntp->icsb_ifree;
  1410. mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
  1411. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1412. xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
  1413. xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
  1414. xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
  1415. spin_unlock(&mp->m_sb_lock);
  1416. xfs_icsb_unlock(mp);
  1417. break;
  1418. }
  1419. return NOTIFY_OK;
  1420. }
  1421. #endif /* CONFIG_HOTPLUG_CPU */
  1422. int
  1423. xfs_icsb_init_counters(
  1424. xfs_mount_t *mp)
  1425. {
  1426. xfs_icsb_cnts_t *cntp;
  1427. int i;
  1428. mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
  1429. if (mp->m_sb_cnts == NULL)
  1430. return -ENOMEM;
  1431. for_each_online_cpu(i) {
  1432. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1433. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1434. }
  1435. mutex_init(&mp->m_icsb_mutex);
  1436. /*
  1437. * start with all counters disabled so that the
  1438. * initial balance kicks us off correctly
  1439. */
  1440. mp->m_icsb_counters = -1;
  1441. #ifdef CONFIG_HOTPLUG_CPU
  1442. mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
  1443. mp->m_icsb_notifier.priority = 0;
  1444. register_hotcpu_notifier(&mp->m_icsb_notifier);
  1445. #endif /* CONFIG_HOTPLUG_CPU */
  1446. return 0;
  1447. }
  1448. void
  1449. xfs_icsb_reinit_counters(
  1450. xfs_mount_t *mp)
  1451. {
  1452. xfs_icsb_lock(mp);
  1453. /*
  1454. * start with all counters disabled so that the
  1455. * initial balance kicks us off correctly
  1456. */
  1457. mp->m_icsb_counters = -1;
  1458. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
  1459. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
  1460. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
  1461. xfs_icsb_unlock(mp);
  1462. }
  1463. void
  1464. xfs_icsb_destroy_counters(
  1465. xfs_mount_t *mp)
  1466. {
  1467. if (mp->m_sb_cnts) {
  1468. unregister_hotcpu_notifier(&mp->m_icsb_notifier);
  1469. free_percpu(mp->m_sb_cnts);
  1470. }
  1471. mutex_destroy(&mp->m_icsb_mutex);
  1472. }
  1473. STATIC void
  1474. xfs_icsb_lock_cntr(
  1475. xfs_icsb_cnts_t *icsbp)
  1476. {
  1477. while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
  1478. ndelay(1000);
  1479. }
  1480. }
  1481. STATIC void
  1482. xfs_icsb_unlock_cntr(
  1483. xfs_icsb_cnts_t *icsbp)
  1484. {
  1485. clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
  1486. }
  1487. STATIC void
  1488. xfs_icsb_lock_all_counters(
  1489. xfs_mount_t *mp)
  1490. {
  1491. xfs_icsb_cnts_t *cntp;
  1492. int i;
  1493. for_each_online_cpu(i) {
  1494. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1495. xfs_icsb_lock_cntr(cntp);
  1496. }
  1497. }
  1498. STATIC void
  1499. xfs_icsb_unlock_all_counters(
  1500. xfs_mount_t *mp)
  1501. {
  1502. xfs_icsb_cnts_t *cntp;
  1503. int i;
  1504. for_each_online_cpu(i) {
  1505. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1506. xfs_icsb_unlock_cntr(cntp);
  1507. }
  1508. }
  1509. STATIC void
  1510. xfs_icsb_count(
  1511. xfs_mount_t *mp,
  1512. xfs_icsb_cnts_t *cnt,
  1513. int flags)
  1514. {
  1515. xfs_icsb_cnts_t *cntp;
  1516. int i;
  1517. memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
  1518. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1519. xfs_icsb_lock_all_counters(mp);
  1520. for_each_online_cpu(i) {
  1521. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1522. cnt->icsb_icount += cntp->icsb_icount;
  1523. cnt->icsb_ifree += cntp->icsb_ifree;
  1524. cnt->icsb_fdblocks += cntp->icsb_fdblocks;
  1525. }
  1526. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1527. xfs_icsb_unlock_all_counters(mp);
  1528. }
  1529. STATIC int
  1530. xfs_icsb_counter_disabled(
  1531. xfs_mount_t *mp,
  1532. xfs_sb_field_t field)
  1533. {
  1534. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1535. return test_bit(field, &mp->m_icsb_counters);
  1536. }
  1537. STATIC void
  1538. xfs_icsb_disable_counter(
  1539. xfs_mount_t *mp,
  1540. xfs_sb_field_t field)
  1541. {
  1542. xfs_icsb_cnts_t cnt;
  1543. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1544. /*
  1545. * If we are already disabled, then there is nothing to do
  1546. * here. We check before locking all the counters to avoid
  1547. * the expensive lock operation when being called in the
  1548. * slow path and the counter is already disabled. This is
  1549. * safe because the only time we set or clear this state is under
  1550. * the m_icsb_mutex.
  1551. */
  1552. if (xfs_icsb_counter_disabled(mp, field))
  1553. return;
  1554. xfs_icsb_lock_all_counters(mp);
  1555. if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
  1556. /* drain back to superblock */
  1557. xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
  1558. switch(field) {
  1559. case XFS_SBS_ICOUNT:
  1560. mp->m_sb.sb_icount = cnt.icsb_icount;
  1561. break;
  1562. case XFS_SBS_IFREE:
  1563. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1564. break;
  1565. case XFS_SBS_FDBLOCKS:
  1566. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1567. break;
  1568. default:
  1569. BUG();
  1570. }
  1571. }
  1572. xfs_icsb_unlock_all_counters(mp);
  1573. }
  1574. STATIC void
  1575. xfs_icsb_enable_counter(
  1576. xfs_mount_t *mp,
  1577. xfs_sb_field_t field,
  1578. uint64_t count,
  1579. uint64_t resid)
  1580. {
  1581. xfs_icsb_cnts_t *cntp;
  1582. int i;
  1583. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1584. xfs_icsb_lock_all_counters(mp);
  1585. for_each_online_cpu(i) {
  1586. cntp = per_cpu_ptr(mp->m_sb_cnts, i);
  1587. switch (field) {
  1588. case XFS_SBS_ICOUNT:
  1589. cntp->icsb_icount = count + resid;
  1590. break;
  1591. case XFS_SBS_IFREE:
  1592. cntp->icsb_ifree = count + resid;
  1593. break;
  1594. case XFS_SBS_FDBLOCKS:
  1595. cntp->icsb_fdblocks = count + resid;
  1596. break;
  1597. default:
  1598. BUG();
  1599. break;
  1600. }
  1601. resid = 0;
  1602. }
  1603. clear_bit(field, &mp->m_icsb_counters);
  1604. xfs_icsb_unlock_all_counters(mp);
  1605. }
  1606. void
  1607. xfs_icsb_sync_counters_locked(
  1608. xfs_mount_t *mp,
  1609. int flags)
  1610. {
  1611. xfs_icsb_cnts_t cnt;
  1612. xfs_icsb_count(mp, &cnt, flags);
  1613. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
  1614. mp->m_sb.sb_icount = cnt.icsb_icount;
  1615. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
  1616. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1617. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
  1618. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1619. }
  1620. /*
  1621. * Accurate update of per-cpu counters to incore superblock
  1622. */
  1623. void
  1624. xfs_icsb_sync_counters(
  1625. xfs_mount_t *mp,
  1626. int flags)
  1627. {
  1628. spin_lock(&mp->m_sb_lock);
  1629. xfs_icsb_sync_counters_locked(mp, flags);
  1630. spin_unlock(&mp->m_sb_lock);
  1631. }
  1632. /*
  1633. * Balance and enable/disable counters as necessary.
  1634. *
  1635. * Thresholds for re-enabling counters are somewhat magic. inode counts are
  1636. * chosen to be the same number as single on disk allocation chunk per CPU, and
  1637. * free blocks is something far enough zero that we aren't going thrash when we
  1638. * get near ENOSPC. We also need to supply a minimum we require per cpu to
  1639. * prevent looping endlessly when xfs_alloc_space asks for more than will
  1640. * be distributed to a single CPU but each CPU has enough blocks to be
  1641. * reenabled.
  1642. *
  1643. * Note that we can be called when counters are already disabled.
  1644. * xfs_icsb_disable_counter() optimises the counter locking in this case to
  1645. * prevent locking every per-cpu counter needlessly.
  1646. */
  1647. #define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
  1648. #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
  1649. (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
  1650. STATIC void
  1651. xfs_icsb_balance_counter_locked(
  1652. xfs_mount_t *mp,
  1653. xfs_sb_field_t field,
  1654. int min_per_cpu)
  1655. {
  1656. uint64_t count, resid;
  1657. int weight = num_online_cpus();
  1658. uint64_t min = (uint64_t)min_per_cpu;
  1659. /* disable counter and sync counter */
  1660. xfs_icsb_disable_counter(mp, field);
  1661. /* update counters - first CPU gets residual*/
  1662. switch (field) {
  1663. case XFS_SBS_ICOUNT:
  1664. count = mp->m_sb.sb_icount;
  1665. resid = do_div(count, weight);
  1666. if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
  1667. return;
  1668. break;
  1669. case XFS_SBS_IFREE:
  1670. count = mp->m_sb.sb_ifree;
  1671. resid = do_div(count, weight);
  1672. if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
  1673. return;
  1674. break;
  1675. case XFS_SBS_FDBLOCKS:
  1676. count = mp->m_sb.sb_fdblocks;
  1677. resid = do_div(count, weight);
  1678. if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
  1679. return;
  1680. break;
  1681. default:
  1682. BUG();
  1683. count = resid = 0; /* quiet, gcc */
  1684. break;
  1685. }
  1686. xfs_icsb_enable_counter(mp, field, count, resid);
  1687. }
  1688. STATIC void
  1689. xfs_icsb_balance_counter(
  1690. xfs_mount_t *mp,
  1691. xfs_sb_field_t fields,
  1692. int min_per_cpu)
  1693. {
  1694. spin_lock(&mp->m_sb_lock);
  1695. xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
  1696. spin_unlock(&mp->m_sb_lock);
  1697. }
  1698. int
  1699. xfs_icsb_modify_counters(
  1700. xfs_mount_t *mp,
  1701. xfs_sb_field_t field,
  1702. int64_t delta,
  1703. int rsvd)
  1704. {
  1705. xfs_icsb_cnts_t *icsbp;
  1706. long long lcounter; /* long counter for 64 bit fields */
  1707. int ret = 0;
  1708. might_sleep();
  1709. again:
  1710. preempt_disable();
  1711. icsbp = this_cpu_ptr(mp->m_sb_cnts);
  1712. /*
  1713. * if the counter is disabled, go to slow path
  1714. */
  1715. if (unlikely(xfs_icsb_counter_disabled(mp, field)))
  1716. goto slow_path;
  1717. xfs_icsb_lock_cntr(icsbp);
  1718. if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
  1719. xfs_icsb_unlock_cntr(icsbp);
  1720. goto slow_path;
  1721. }
  1722. switch (field) {
  1723. case XFS_SBS_ICOUNT:
  1724. lcounter = icsbp->icsb_icount;
  1725. lcounter += delta;
  1726. if (unlikely(lcounter < 0))
  1727. goto balance_counter;
  1728. icsbp->icsb_icount = lcounter;
  1729. break;
  1730. case XFS_SBS_IFREE:
  1731. lcounter = icsbp->icsb_ifree;
  1732. lcounter += delta;
  1733. if (unlikely(lcounter < 0))
  1734. goto balance_counter;
  1735. icsbp->icsb_ifree = lcounter;
  1736. break;
  1737. case XFS_SBS_FDBLOCKS:
  1738. BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
  1739. lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  1740. lcounter += delta;
  1741. if (unlikely(lcounter < 0))
  1742. goto balance_counter;
  1743. icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
  1744. break;
  1745. default:
  1746. BUG();
  1747. break;
  1748. }
  1749. xfs_icsb_unlock_cntr(icsbp);
  1750. preempt_enable();
  1751. return 0;
  1752. slow_path:
  1753. preempt_enable();
  1754. /*
  1755. * serialise with a mutex so we don't burn lots of cpu on
  1756. * the superblock lock. We still need to hold the superblock
  1757. * lock, however, when we modify the global structures.
  1758. */
  1759. xfs_icsb_lock(mp);
  1760. /*
  1761. * Now running atomically.
  1762. *
  1763. * If the counter is enabled, someone has beaten us to rebalancing.
  1764. * Drop the lock and try again in the fast path....
  1765. */
  1766. if (!(xfs_icsb_counter_disabled(mp, field))) {
  1767. xfs_icsb_unlock(mp);
  1768. goto again;
  1769. }
  1770. /*
  1771. * The counter is currently disabled. Because we are
  1772. * running atomically here, we know a rebalance cannot
  1773. * be in progress. Hence we can go straight to operating
  1774. * on the global superblock. We do not call xfs_mod_incore_sb()
  1775. * here even though we need to get the m_sb_lock. Doing so
  1776. * will cause us to re-enter this function and deadlock.
  1777. * Hence we get the m_sb_lock ourselves and then call
  1778. * xfs_mod_incore_sb_unlocked() as the unlocked path operates
  1779. * directly on the global counters.
  1780. */
  1781. spin_lock(&mp->m_sb_lock);
  1782. ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1783. spin_unlock(&mp->m_sb_lock);
  1784. /*
  1785. * Now that we've modified the global superblock, we
  1786. * may be able to re-enable the distributed counters
  1787. * (e.g. lots of space just got freed). After that
  1788. * we are done.
  1789. */
  1790. if (ret != ENOSPC)
  1791. xfs_icsb_balance_counter(mp, field, 0);
  1792. xfs_icsb_unlock(mp);
  1793. return ret;
  1794. balance_counter:
  1795. xfs_icsb_unlock_cntr(icsbp);
  1796. preempt_enable();
  1797. /*
  1798. * We may have multiple threads here if multiple per-cpu
  1799. * counters run dry at the same time. This will mean we can
  1800. * do more balances than strictly necessary but it is not
  1801. * the common slowpath case.
  1802. */
  1803. xfs_icsb_lock(mp);
  1804. /*
  1805. * running atomically.
  1806. *
  1807. * This will leave the counter in the correct state for future
  1808. * accesses. After the rebalance, we simply try again and our retry
  1809. * will either succeed through the fast path or slow path without
  1810. * another balance operation being required.
  1811. */
  1812. xfs_icsb_balance_counter(mp, field, delta);
  1813. xfs_icsb_unlock(mp);
  1814. goto again;
  1815. }
  1816. #endif