xfs_mount.c 51 KB

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