xfs_mount.c 50 KB

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