xfs_mount.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  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_sb = true;
  366. }
  367. if (sbp->sb_width != mp->m_swidth) {
  368. sbp->sb_width = mp->m_swidth;
  369. mp->m_update_sb = true;
  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. mp->m_qflags = 0;
  525. /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
  526. if (mp->m_sb.sb_qflags == 0)
  527. return 0;
  528. spin_lock(&mp->m_sb_lock);
  529. mp->m_sb.sb_qflags = 0;
  530. spin_unlock(&mp->m_sb_lock);
  531. if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
  532. return 0;
  533. return xfs_sync_sb(mp, false);
  534. }
  535. __uint64_t
  536. xfs_default_resblks(xfs_mount_t *mp)
  537. {
  538. __uint64_t resblks;
  539. /*
  540. * We default to 5% or 8192 fsbs of space reserved, whichever is
  541. * smaller. This is intended to cover concurrent allocation
  542. * transactions when we initially hit enospc. These each require a 4
  543. * block reservation. Hence by default we cover roughly 2000 concurrent
  544. * allocation reservations.
  545. */
  546. resblks = mp->m_sb.sb_dblocks;
  547. do_div(resblks, 20);
  548. resblks = min_t(__uint64_t, resblks, 8192);
  549. return resblks;
  550. }
  551. /*
  552. * This function does the following on an initial mount of a file system:
  553. * - reads the superblock from disk and init the mount struct
  554. * - if we're a 32-bit kernel, do a size check on the superblock
  555. * so we don't mount terabyte filesystems
  556. * - init mount struct realtime fields
  557. * - allocate inode hash table for fs
  558. * - init directory manager
  559. * - perform recovery and init the log manager
  560. */
  561. int
  562. xfs_mountfs(
  563. xfs_mount_t *mp)
  564. {
  565. xfs_sb_t *sbp = &(mp->m_sb);
  566. xfs_inode_t *rip;
  567. __uint64_t resblks;
  568. uint quotamount = 0;
  569. uint quotaflags = 0;
  570. int error = 0;
  571. xfs_sb_mount_common(mp, sbp);
  572. /*
  573. * Check for a mismatched features2 values. Older kernels read & wrote
  574. * into the wrong sb offset for sb_features2 on some platforms due to
  575. * xfs_sb_t not being 64bit size aligned when sb_features2 was added,
  576. * which made older superblock reading/writing routines swap it as a
  577. * 64-bit value.
  578. *
  579. * For backwards compatibility, we make both slots equal.
  580. *
  581. * If we detect a mismatched field, we OR the set bits into the existing
  582. * features2 field in case it has already been modified; we don't want
  583. * to lose any features. We then update the bad location with the ORed
  584. * value so that older kernels will see any features2 flags. The
  585. * superblock writeback code ensures the new sb_features2 is copied to
  586. * sb_bad_features2 before it is logged or written to disk.
  587. */
  588. if (xfs_sb_has_mismatched_features2(sbp)) {
  589. xfs_warn(mp, "correcting sb_features alignment problem");
  590. sbp->sb_features2 |= sbp->sb_bad_features2;
  591. mp->m_update_sb = true;
  592. /*
  593. * Re-check for ATTR2 in case it was found in bad_features2
  594. * slot.
  595. */
  596. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  597. !(mp->m_flags & XFS_MOUNT_NOATTR2))
  598. mp->m_flags |= XFS_MOUNT_ATTR2;
  599. }
  600. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  601. (mp->m_flags & XFS_MOUNT_NOATTR2)) {
  602. xfs_sb_version_removeattr2(&mp->m_sb);
  603. mp->m_update_sb = true;
  604. /* update sb_versionnum for the clearing of the morebits */
  605. if (!sbp->sb_features2)
  606. mp->m_update_sb = true;
  607. }
  608. /* always use v2 inodes by default now */
  609. if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
  610. mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
  611. mp->m_update_sb = true;
  612. }
  613. /*
  614. * Check if sb_agblocks is aligned at stripe boundary
  615. * If sb_agblocks is NOT aligned turn off m_dalign since
  616. * allocator alignment is within an ag, therefore ag has
  617. * to be aligned at stripe boundary.
  618. */
  619. error = xfs_update_alignment(mp);
  620. if (error)
  621. goto out;
  622. xfs_alloc_compute_maxlevels(mp);
  623. xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
  624. xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
  625. xfs_ialloc_compute_maxlevels(mp);
  626. xfs_set_maxicount(mp);
  627. error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, NULL, mp->m_fsname);
  628. if (error)
  629. goto out;
  630. error = xfs_uuid_mount(mp);
  631. if (error)
  632. goto out_remove_sysfs;
  633. /*
  634. * Set the minimum read and write sizes
  635. */
  636. xfs_set_rw_sizes(mp);
  637. /* set the low space thresholds for dynamic preallocation */
  638. xfs_set_low_space_thresholds(mp);
  639. /*
  640. * Set the inode cluster size.
  641. * This may still be overridden by the file system
  642. * block size if it is larger than the chosen cluster size.
  643. *
  644. * For v5 filesystems, scale the cluster size with the inode size to
  645. * keep a constant ratio of inode per cluster buffer, but only if mkfs
  646. * has set the inode alignment value appropriately for larger cluster
  647. * sizes.
  648. */
  649. mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
  650. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  651. int new_size = mp->m_inode_cluster_size;
  652. new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
  653. if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
  654. mp->m_inode_cluster_size = new_size;
  655. }
  656. /*
  657. * Set inode alignment fields
  658. */
  659. xfs_set_inoalignment(mp);
  660. /*
  661. * Check that the data (and log if separate) is an ok size.
  662. */
  663. error = xfs_check_sizes(mp);
  664. if (error)
  665. goto out_remove_uuid;
  666. /*
  667. * Initialize realtime fields in the mount structure
  668. */
  669. error = xfs_rtmount_init(mp);
  670. if (error) {
  671. xfs_warn(mp, "RT mount failed");
  672. goto out_remove_uuid;
  673. }
  674. /*
  675. * Copies the low order bits of the timestamp and the randomly
  676. * set "sequence" number out of a UUID.
  677. */
  678. uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
  679. mp->m_dmevmask = 0; /* not persistent; set after each mount */
  680. error = xfs_da_mount(mp);
  681. if (error) {
  682. xfs_warn(mp, "Failed dir/attr init: %d", error);
  683. goto out_remove_uuid;
  684. }
  685. /*
  686. * Initialize the precomputed transaction reservations values.
  687. */
  688. xfs_trans_init(mp);
  689. /*
  690. * Allocate and initialize the per-ag data.
  691. */
  692. spin_lock_init(&mp->m_perag_lock);
  693. INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
  694. error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
  695. if (error) {
  696. xfs_warn(mp, "Failed per-ag init: %d", error);
  697. goto out_free_dir;
  698. }
  699. if (!sbp->sb_logblocks) {
  700. xfs_warn(mp, "no log defined");
  701. XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
  702. error = -EFSCORRUPTED;
  703. goto out_free_perag;
  704. }
  705. /*
  706. * log's mount-time initialization. Perform 1st part recovery if needed
  707. */
  708. error = xfs_log_mount(mp, mp->m_logdev_targp,
  709. XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
  710. XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
  711. if (error) {
  712. xfs_warn(mp, "log mount failed");
  713. goto out_fail_wait;
  714. }
  715. /*
  716. * Now the log is mounted, we know if it was an unclean shutdown or
  717. * not. If it was, with the first phase of recovery has completed, we
  718. * have consistent AG blocks on disk. We have not recovered EFIs yet,
  719. * but they are recovered transactionally in the second recovery phase
  720. * later.
  721. *
  722. * Hence we can safely re-initialise incore superblock counters from
  723. * the per-ag data. These may not be correct if the filesystem was not
  724. * cleanly unmounted, so we need to wait for recovery to finish before
  725. * doing this.
  726. *
  727. * If the filesystem was cleanly unmounted, then we can trust the
  728. * values in the superblock to be correct and we don't need to do
  729. * anything here.
  730. *
  731. * If we are currently making the filesystem, the initialisation will
  732. * fail as the perag data is in an undefined state.
  733. */
  734. if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
  735. !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
  736. !mp->m_sb.sb_inprogress) {
  737. error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
  738. if (error)
  739. goto out_log_dealloc;
  740. }
  741. /*
  742. * Get and sanity-check the root inode.
  743. * Save the pointer to it in the mount structure.
  744. */
  745. error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
  746. if (error) {
  747. xfs_warn(mp, "failed to read root inode");
  748. goto out_log_dealloc;
  749. }
  750. ASSERT(rip != NULL);
  751. if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
  752. xfs_warn(mp, "corrupted root inode %llu: not a directory",
  753. (unsigned long long)rip->i_ino);
  754. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  755. XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
  756. mp);
  757. error = -EFSCORRUPTED;
  758. goto out_rele_rip;
  759. }
  760. mp->m_rootip = rip; /* save it */
  761. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  762. /*
  763. * Initialize realtime inode pointers in the mount structure
  764. */
  765. error = xfs_rtmount_inodes(mp);
  766. if (error) {
  767. /*
  768. * Free up the root inode.
  769. */
  770. xfs_warn(mp, "failed to read RT inodes");
  771. goto out_rele_rip;
  772. }
  773. /*
  774. * If this is a read-only mount defer the superblock updates until
  775. * the next remount into writeable mode. Otherwise we would never
  776. * perform the update e.g. for the root filesystem.
  777. */
  778. if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
  779. error = xfs_sync_sb(mp, false);
  780. if (error) {
  781. xfs_warn(mp, "failed to write sb changes");
  782. goto out_rtunmount;
  783. }
  784. }
  785. /*
  786. * Initialise the XFS quota management subsystem for this mount
  787. */
  788. if (XFS_IS_QUOTA_RUNNING(mp)) {
  789. error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
  790. if (error)
  791. goto out_rtunmount;
  792. } else {
  793. ASSERT(!XFS_IS_QUOTA_ON(mp));
  794. /*
  795. * If a file system had quotas running earlier, but decided to
  796. * mount without -o uquota/pquota/gquota options, revoke the
  797. * quotachecked license.
  798. */
  799. if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
  800. xfs_notice(mp, "resetting quota flags");
  801. error = xfs_mount_reset_sbqflags(mp);
  802. if (error)
  803. goto out_rtunmount;
  804. }
  805. }
  806. /*
  807. * Finish recovering the file system. This part needed to be
  808. * delayed until after the root and real-time bitmap inodes
  809. * were consistently read in.
  810. */
  811. error = xfs_log_mount_finish(mp);
  812. if (error) {
  813. xfs_warn(mp, "log mount finish failed");
  814. goto out_rtunmount;
  815. }
  816. /*
  817. * Complete the quota initialisation, post-log-replay component.
  818. */
  819. if (quotamount) {
  820. ASSERT(mp->m_qflags == 0);
  821. mp->m_qflags = quotaflags;
  822. xfs_qm_mount_quotas(mp);
  823. }
  824. /*
  825. * Now we are mounted, reserve a small amount of unused space for
  826. * privileged transactions. This is needed so that transaction
  827. * space required for critical operations can dip into this pool
  828. * when at ENOSPC. This is needed for operations like create with
  829. * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
  830. * are not allowed to use this reserved space.
  831. *
  832. * This may drive us straight to ENOSPC on mount, but that implies
  833. * we were already there on the last unmount. Warn if this occurs.
  834. */
  835. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  836. resblks = xfs_default_resblks(mp);
  837. error = xfs_reserve_blocks(mp, &resblks, NULL);
  838. if (error)
  839. xfs_warn(mp,
  840. "Unable to allocate reserve blocks. Continuing without reserve pool.");
  841. }
  842. return 0;
  843. out_rtunmount:
  844. xfs_rtunmount_inodes(mp);
  845. out_rele_rip:
  846. IRELE(rip);
  847. out_log_dealloc:
  848. xfs_log_unmount(mp);
  849. out_fail_wait:
  850. if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
  851. xfs_wait_buftarg(mp->m_logdev_targp);
  852. xfs_wait_buftarg(mp->m_ddev_targp);
  853. out_free_perag:
  854. xfs_free_perag(mp);
  855. out_free_dir:
  856. xfs_da_unmount(mp);
  857. out_remove_uuid:
  858. xfs_uuid_unmount(mp);
  859. out_remove_sysfs:
  860. xfs_sysfs_del(&mp->m_kobj);
  861. out:
  862. return error;
  863. }
  864. /*
  865. * This flushes out the inodes,dquots and the superblock, unmounts the
  866. * log and makes sure that incore structures are freed.
  867. */
  868. void
  869. xfs_unmountfs(
  870. struct xfs_mount *mp)
  871. {
  872. __uint64_t resblks;
  873. int error;
  874. cancel_delayed_work_sync(&mp->m_eofblocks_work);
  875. xfs_qm_unmount_quotas(mp);
  876. xfs_rtunmount_inodes(mp);
  877. IRELE(mp->m_rootip);
  878. /*
  879. * We can potentially deadlock here if we have an inode cluster
  880. * that has been freed has its buffer still pinned in memory because
  881. * the transaction is still sitting in a iclog. The stale inodes
  882. * on that buffer will have their flush locks held until the
  883. * transaction hits the disk and the callbacks run. the inode
  884. * flush takes the flush lock unconditionally and with nothing to
  885. * push out the iclog we will never get that unlocked. hence we
  886. * need to force the log first.
  887. */
  888. xfs_log_force(mp, XFS_LOG_SYNC);
  889. /*
  890. * Flush all pending changes from the AIL.
  891. */
  892. xfs_ail_push_all_sync(mp->m_ail);
  893. /*
  894. * And reclaim all inodes. At this point there should be no dirty
  895. * inodes and none should be pinned or locked, but use synchronous
  896. * reclaim just to be sure. We can stop background inode reclaim
  897. * here as well if it is still running.
  898. */
  899. cancel_delayed_work_sync(&mp->m_reclaim_work);
  900. xfs_reclaim_inodes(mp, SYNC_WAIT);
  901. xfs_qm_unmount(mp);
  902. /*
  903. * Unreserve any blocks we have so that when we unmount we don't account
  904. * the reserved free space as used. This is really only necessary for
  905. * lazy superblock counting because it trusts the incore superblock
  906. * counters to be absolutely correct on clean unmount.
  907. *
  908. * We don't bother correcting this elsewhere for lazy superblock
  909. * counting because on mount of an unclean filesystem we reconstruct the
  910. * correct counter value and this is irrelevant.
  911. *
  912. * For non-lazy counter filesystems, this doesn't matter at all because
  913. * we only every apply deltas to the superblock and hence the incore
  914. * value does not matter....
  915. */
  916. resblks = 0;
  917. error = xfs_reserve_blocks(mp, &resblks, NULL);
  918. if (error)
  919. xfs_warn(mp, "Unable to free reserved block pool. "
  920. "Freespace may not be correct on next mount.");
  921. error = xfs_log_sbcount(mp);
  922. if (error)
  923. xfs_warn(mp, "Unable to update superblock counters. "
  924. "Freespace may not be correct on next mount.");
  925. xfs_log_unmount(mp);
  926. xfs_da_unmount(mp);
  927. xfs_uuid_unmount(mp);
  928. #if defined(DEBUG)
  929. xfs_errortag_clearall(mp, 0);
  930. #endif
  931. xfs_free_perag(mp);
  932. xfs_sysfs_del(&mp->m_kobj);
  933. }
  934. /*
  935. * Determine whether modifications can proceed. The caller specifies the minimum
  936. * freeze level for which modifications should not be allowed. This allows
  937. * certain operations to proceed while the freeze sequence is in progress, if
  938. * necessary.
  939. */
  940. bool
  941. xfs_fs_writable(
  942. struct xfs_mount *mp,
  943. int level)
  944. {
  945. ASSERT(level > SB_UNFROZEN);
  946. if ((mp->m_super->s_writers.frozen >= level) ||
  947. XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY))
  948. return false;
  949. return true;
  950. }
  951. /*
  952. * xfs_log_sbcount
  953. *
  954. * Sync the superblock counters to disk.
  955. *
  956. * Note this code can be called during the process of freezing, so we use the
  957. * transaction allocator that does not block when the transaction subsystem is
  958. * in its frozen state.
  959. */
  960. int
  961. xfs_log_sbcount(xfs_mount_t *mp)
  962. {
  963. /* allow this to proceed during the freeze sequence... */
  964. if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE))
  965. return 0;
  966. xfs_icsb_sync_counters(mp, 0);
  967. /*
  968. * we don't need to do this if we are updating the superblock
  969. * counters on every modification.
  970. */
  971. if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
  972. return 0;
  973. return xfs_sync_sb(mp, true);
  974. }
  975. /*
  976. * xfs_mod_incore_sb_unlocked() is a utility routine commonly used to apply
  977. * a delta to a specified field in the in-core superblock. Simply
  978. * switch on the field indicated and apply the delta to that field.
  979. * Fields are not allowed to dip below zero, so if the delta would
  980. * do this do not apply it and return EINVAL.
  981. *
  982. * The m_sb_lock must be held when this routine is called.
  983. */
  984. STATIC int
  985. xfs_mod_incore_sb_unlocked(
  986. xfs_mount_t *mp,
  987. xfs_sb_field_t field,
  988. int64_t delta,
  989. int rsvd)
  990. {
  991. int scounter; /* short counter for 32 bit fields */
  992. long long lcounter; /* long counter for 64 bit fields */
  993. long long res_used, rem;
  994. /*
  995. * With the in-core superblock spin lock held, switch
  996. * on the indicated field. Apply the delta to the
  997. * proper field. If the fields value would dip below
  998. * 0, then do not apply the delta and return EINVAL.
  999. */
  1000. switch (field) {
  1001. case XFS_SBS_ICOUNT:
  1002. lcounter = (long long)mp->m_sb.sb_icount;
  1003. lcounter += delta;
  1004. if (lcounter < 0) {
  1005. ASSERT(0);
  1006. return -EINVAL;
  1007. }
  1008. mp->m_sb.sb_icount = lcounter;
  1009. return 0;
  1010. case XFS_SBS_IFREE:
  1011. lcounter = (long long)mp->m_sb.sb_ifree;
  1012. lcounter += delta;
  1013. if (lcounter < 0) {
  1014. ASSERT(0);
  1015. return -EINVAL;
  1016. }
  1017. mp->m_sb.sb_ifree = lcounter;
  1018. return 0;
  1019. case XFS_SBS_FDBLOCKS:
  1020. lcounter = (long long)
  1021. mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  1022. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1023. if (delta > 0) { /* Putting blocks back */
  1024. if (res_used > delta) {
  1025. mp->m_resblks_avail += delta;
  1026. } else {
  1027. rem = delta - res_used;
  1028. mp->m_resblks_avail = mp->m_resblks;
  1029. lcounter += rem;
  1030. }
  1031. } else { /* Taking blocks away */
  1032. lcounter += delta;
  1033. if (lcounter >= 0) {
  1034. mp->m_sb.sb_fdblocks = lcounter +
  1035. XFS_ALLOC_SET_ASIDE(mp);
  1036. return 0;
  1037. }
  1038. /*
  1039. * We are out of blocks, use any available reserved
  1040. * blocks if were allowed to.
  1041. */
  1042. if (!rsvd)
  1043. return -ENOSPC;
  1044. lcounter = (long long)mp->m_resblks_avail + delta;
  1045. if (lcounter >= 0) {
  1046. mp->m_resblks_avail = lcounter;
  1047. return 0;
  1048. }
  1049. printk_once(KERN_WARNING
  1050. "Filesystem \"%s\": reserve blocks depleted! "
  1051. "Consider increasing reserve pool size.",
  1052. mp->m_fsname);
  1053. return -ENOSPC;
  1054. }
  1055. mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
  1056. return 0;
  1057. case XFS_SBS_FREXTENTS:
  1058. lcounter = (long long)mp->m_sb.sb_frextents;
  1059. lcounter += delta;
  1060. if (lcounter < 0) {
  1061. return -ENOSPC;
  1062. }
  1063. mp->m_sb.sb_frextents = lcounter;
  1064. return 0;
  1065. case XFS_SBS_DBLOCKS:
  1066. lcounter = (long long)mp->m_sb.sb_dblocks;
  1067. lcounter += delta;
  1068. if (lcounter < 0) {
  1069. ASSERT(0);
  1070. return -EINVAL;
  1071. }
  1072. mp->m_sb.sb_dblocks = lcounter;
  1073. return 0;
  1074. case XFS_SBS_AGCOUNT:
  1075. scounter = mp->m_sb.sb_agcount;
  1076. scounter += delta;
  1077. if (scounter < 0) {
  1078. ASSERT(0);
  1079. return -EINVAL;
  1080. }
  1081. mp->m_sb.sb_agcount = scounter;
  1082. return 0;
  1083. case XFS_SBS_IMAX_PCT:
  1084. scounter = mp->m_sb.sb_imax_pct;
  1085. scounter += delta;
  1086. if (scounter < 0) {
  1087. ASSERT(0);
  1088. return -EINVAL;
  1089. }
  1090. mp->m_sb.sb_imax_pct = scounter;
  1091. return 0;
  1092. case XFS_SBS_REXTSIZE:
  1093. scounter = mp->m_sb.sb_rextsize;
  1094. scounter += delta;
  1095. if (scounter < 0) {
  1096. ASSERT(0);
  1097. return -EINVAL;
  1098. }
  1099. mp->m_sb.sb_rextsize = scounter;
  1100. return 0;
  1101. case XFS_SBS_RBMBLOCKS:
  1102. scounter = mp->m_sb.sb_rbmblocks;
  1103. scounter += delta;
  1104. if (scounter < 0) {
  1105. ASSERT(0);
  1106. return -EINVAL;
  1107. }
  1108. mp->m_sb.sb_rbmblocks = scounter;
  1109. return 0;
  1110. case XFS_SBS_RBLOCKS:
  1111. lcounter = (long long)mp->m_sb.sb_rblocks;
  1112. lcounter += delta;
  1113. if (lcounter < 0) {
  1114. ASSERT(0);
  1115. return -EINVAL;
  1116. }
  1117. mp->m_sb.sb_rblocks = lcounter;
  1118. return 0;
  1119. case XFS_SBS_REXTENTS:
  1120. lcounter = (long long)mp->m_sb.sb_rextents;
  1121. lcounter += delta;
  1122. if (lcounter < 0) {
  1123. ASSERT(0);
  1124. return -EINVAL;
  1125. }
  1126. mp->m_sb.sb_rextents = lcounter;
  1127. return 0;
  1128. case XFS_SBS_REXTSLOG:
  1129. scounter = mp->m_sb.sb_rextslog;
  1130. scounter += delta;
  1131. if (scounter < 0) {
  1132. ASSERT(0);
  1133. return -EINVAL;
  1134. }
  1135. mp->m_sb.sb_rextslog = scounter;
  1136. return 0;
  1137. default:
  1138. ASSERT(0);
  1139. return -EINVAL;
  1140. }
  1141. }
  1142. /*
  1143. * xfs_mod_incore_sb() is used to change a field in the in-core
  1144. * superblock structure by the specified delta. This modification
  1145. * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
  1146. * routine to do the work.
  1147. */
  1148. int
  1149. xfs_mod_incore_sb(
  1150. struct xfs_mount *mp,
  1151. xfs_sb_field_t field,
  1152. int64_t delta,
  1153. int rsvd)
  1154. {
  1155. int status;
  1156. #ifdef HAVE_PERCPU_SB
  1157. ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
  1158. #endif
  1159. spin_lock(&mp->m_sb_lock);
  1160. status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1161. spin_unlock(&mp->m_sb_lock);
  1162. return status;
  1163. }
  1164. /*
  1165. * Change more than one field in the in-core superblock structure at a time.
  1166. *
  1167. * The fields and changes to those fields are specified in the array of
  1168. * xfs_mod_sb structures passed in. Either all of the specified deltas
  1169. * will be applied or none of them will. If any modified field dips below 0,
  1170. * then all modifications will be backed out and EINVAL will be returned.
  1171. *
  1172. * Note that this function may not be used for the superblock values that
  1173. * are tracked with the in-memory per-cpu counters - a direct call to
  1174. * xfs_icsb_modify_counters is required for these.
  1175. */
  1176. int
  1177. xfs_mod_incore_sb_batch(
  1178. struct xfs_mount *mp,
  1179. xfs_mod_sb_t *msb,
  1180. uint nmsb,
  1181. int rsvd)
  1182. {
  1183. xfs_mod_sb_t *msbp;
  1184. int error = 0;
  1185. /*
  1186. * Loop through the array of mod structures and apply each individually.
  1187. * If any fail, then back out all those which have already been applied.
  1188. * Do all of this within the scope of the m_sb_lock so that all of the
  1189. * changes will be atomic.
  1190. */
  1191. spin_lock(&mp->m_sb_lock);
  1192. for (msbp = msb; msbp < (msb + nmsb); msbp++) {
  1193. ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
  1194. msbp->msb_field > XFS_SBS_FDBLOCKS);
  1195. error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
  1196. msbp->msb_delta, rsvd);
  1197. if (error)
  1198. goto unwind;
  1199. }
  1200. spin_unlock(&mp->m_sb_lock);
  1201. return 0;
  1202. unwind:
  1203. while (--msbp >= msb) {
  1204. error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
  1205. -msbp->msb_delta, rsvd);
  1206. ASSERT(error == 0);
  1207. }
  1208. spin_unlock(&mp->m_sb_lock);
  1209. return error;
  1210. }
  1211. /*
  1212. * xfs_getsb() is called to obtain the buffer for the superblock.
  1213. * The buffer is returned locked and read in from disk.
  1214. * The buffer should be released with a call to xfs_brelse().
  1215. *
  1216. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1217. * the superblock buffer if it can be locked without sleeping.
  1218. * If it can't then we'll return NULL.
  1219. */
  1220. struct xfs_buf *
  1221. xfs_getsb(
  1222. struct xfs_mount *mp,
  1223. int flags)
  1224. {
  1225. struct xfs_buf *bp = mp->m_sb_bp;
  1226. if (!xfs_buf_trylock(bp)) {
  1227. if (flags & XBF_TRYLOCK)
  1228. return NULL;
  1229. xfs_buf_lock(bp);
  1230. }
  1231. xfs_buf_hold(bp);
  1232. ASSERT(XFS_BUF_ISDONE(bp));
  1233. return bp;
  1234. }
  1235. /*
  1236. * Used to free the superblock along various error paths.
  1237. */
  1238. void
  1239. xfs_freesb(
  1240. struct xfs_mount *mp)
  1241. {
  1242. struct xfs_buf *bp = mp->m_sb_bp;
  1243. xfs_buf_lock(bp);
  1244. mp->m_sb_bp = NULL;
  1245. xfs_buf_relse(bp);
  1246. }
  1247. /*
  1248. * If the underlying (data/log/rt) device is readonly, there are some
  1249. * operations that cannot proceed.
  1250. */
  1251. int
  1252. xfs_dev_is_read_only(
  1253. struct xfs_mount *mp,
  1254. char *message)
  1255. {
  1256. if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
  1257. xfs_readonly_buftarg(mp->m_logdev_targp) ||
  1258. (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
  1259. xfs_notice(mp, "%s required on read-only device.", message);
  1260. xfs_notice(mp, "write access unavailable, cannot proceed.");
  1261. return -EROFS;
  1262. }
  1263. return 0;
  1264. }
  1265. #ifdef HAVE_PERCPU_SB
  1266. /*
  1267. * Per-cpu incore superblock counters
  1268. *
  1269. * Simple concept, difficult implementation
  1270. *
  1271. * Basically, replace the incore superblock counters with a distributed per cpu
  1272. * counter for contended fields (e.g. free block count).
  1273. *
  1274. * Difficulties arise in that the incore sb is used for ENOSPC checking, and
  1275. * hence needs to be accurately read when we are running low on space. Hence
  1276. * there is a method to enable and disable the per-cpu counters based on how
  1277. * much "stuff" is available in them.
  1278. *
  1279. * Basically, a counter is enabled if there is enough free resource to justify
  1280. * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
  1281. * ENOSPC), then we disable the counters to synchronise all callers and
  1282. * re-distribute the available resources.
  1283. *
  1284. * If, once we redistributed the available resources, we still get a failure,
  1285. * we disable the per-cpu counter and go through the slow path.
  1286. *
  1287. * The slow path is the current xfs_mod_incore_sb() function. This means that
  1288. * when we disable a per-cpu counter, we need to drain its resources back to
  1289. * the global superblock. We do this after disabling the counter to prevent
  1290. * more threads from queueing up on the counter.
  1291. *
  1292. * Essentially, this means that we still need a lock in the fast path to enable
  1293. * synchronisation between the global counters and the per-cpu counters. This
  1294. * is not a problem because the lock will be local to a CPU almost all the time
  1295. * and have little contention except when we get to ENOSPC conditions.
  1296. *
  1297. * Basically, this lock becomes a barrier that enables us to lock out the fast
  1298. * path while we do things like enabling and disabling counters and
  1299. * synchronising the counters.
  1300. *
  1301. * Locking rules:
  1302. *
  1303. * 1. m_sb_lock before picking up per-cpu locks
  1304. * 2. per-cpu locks always picked up via for_each_online_cpu() order
  1305. * 3. accurate counter sync requires m_sb_lock + per cpu locks
  1306. * 4. modifying per-cpu counters requires holding per-cpu lock
  1307. * 5. modifying global counters requires holding m_sb_lock
  1308. * 6. enabling or disabling a counter requires holding the m_sb_lock
  1309. * and _none_ of the per-cpu locks.
  1310. *
  1311. * Disabled counters are only ever re-enabled by a balance operation
  1312. * that results in more free resources per CPU than a given threshold.
  1313. * To ensure counters don't remain disabled, they are rebalanced when
  1314. * the global resource goes above a higher threshold (i.e. some hysteresis
  1315. * is present to prevent thrashing).
  1316. */
  1317. #ifdef CONFIG_HOTPLUG_CPU
  1318. /*
  1319. * hot-plug CPU notifier support.
  1320. *
  1321. * We need a notifier per filesystem as we need to be able to identify
  1322. * the filesystem to balance the counters out. This is achieved by
  1323. * having a notifier block embedded in the xfs_mount_t and doing pointer
  1324. * magic to get the mount pointer from the notifier block address.
  1325. */
  1326. STATIC int
  1327. xfs_icsb_cpu_notify(
  1328. struct notifier_block *nfb,
  1329. unsigned long action,
  1330. void *hcpu)
  1331. {
  1332. xfs_icsb_cnts_t *cntp;
  1333. xfs_mount_t *mp;
  1334. mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
  1335. cntp = (xfs_icsb_cnts_t *)
  1336. per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
  1337. switch (action) {
  1338. case CPU_UP_PREPARE:
  1339. case CPU_UP_PREPARE_FROZEN:
  1340. /* Easy Case - initialize the area and locks, and
  1341. * then rebalance when online does everything else for us. */
  1342. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1343. break;
  1344. case CPU_ONLINE:
  1345. case CPU_ONLINE_FROZEN:
  1346. xfs_icsb_lock(mp);
  1347. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
  1348. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
  1349. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
  1350. xfs_icsb_unlock(mp);
  1351. break;
  1352. case CPU_DEAD:
  1353. case CPU_DEAD_FROZEN:
  1354. /* Disable all the counters, then fold the dead cpu's
  1355. * count into the total on the global superblock and
  1356. * re-enable the counters. */
  1357. xfs_icsb_lock(mp);
  1358. spin_lock(&mp->m_sb_lock);
  1359. xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
  1360. xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
  1361. xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
  1362. mp->m_sb.sb_icount += cntp->icsb_icount;
  1363. mp->m_sb.sb_ifree += cntp->icsb_ifree;
  1364. mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
  1365. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1366. xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
  1367. xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
  1368. xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
  1369. spin_unlock(&mp->m_sb_lock);
  1370. xfs_icsb_unlock(mp);
  1371. break;
  1372. }
  1373. return NOTIFY_OK;
  1374. }
  1375. #endif /* CONFIG_HOTPLUG_CPU */
  1376. int
  1377. xfs_icsb_init_counters(
  1378. xfs_mount_t *mp)
  1379. {
  1380. xfs_icsb_cnts_t *cntp;
  1381. int i;
  1382. mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
  1383. if (mp->m_sb_cnts == NULL)
  1384. return -ENOMEM;
  1385. for_each_online_cpu(i) {
  1386. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1387. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1388. }
  1389. mutex_init(&mp->m_icsb_mutex);
  1390. /*
  1391. * start with all counters disabled so that the
  1392. * initial balance kicks us off correctly
  1393. */
  1394. mp->m_icsb_counters = -1;
  1395. #ifdef CONFIG_HOTPLUG_CPU
  1396. mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
  1397. mp->m_icsb_notifier.priority = 0;
  1398. register_hotcpu_notifier(&mp->m_icsb_notifier);
  1399. #endif /* CONFIG_HOTPLUG_CPU */
  1400. return 0;
  1401. }
  1402. void
  1403. xfs_icsb_reinit_counters(
  1404. xfs_mount_t *mp)
  1405. {
  1406. xfs_icsb_lock(mp);
  1407. /*
  1408. * start with all counters disabled so that the
  1409. * initial balance kicks us off correctly
  1410. */
  1411. mp->m_icsb_counters = -1;
  1412. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
  1413. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
  1414. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
  1415. xfs_icsb_unlock(mp);
  1416. }
  1417. void
  1418. xfs_icsb_destroy_counters(
  1419. xfs_mount_t *mp)
  1420. {
  1421. if (mp->m_sb_cnts) {
  1422. unregister_hotcpu_notifier(&mp->m_icsb_notifier);
  1423. free_percpu(mp->m_sb_cnts);
  1424. }
  1425. mutex_destroy(&mp->m_icsb_mutex);
  1426. }
  1427. STATIC void
  1428. xfs_icsb_lock_cntr(
  1429. xfs_icsb_cnts_t *icsbp)
  1430. {
  1431. while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
  1432. ndelay(1000);
  1433. }
  1434. }
  1435. STATIC void
  1436. xfs_icsb_unlock_cntr(
  1437. xfs_icsb_cnts_t *icsbp)
  1438. {
  1439. clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
  1440. }
  1441. STATIC void
  1442. xfs_icsb_lock_all_counters(
  1443. xfs_mount_t *mp)
  1444. {
  1445. xfs_icsb_cnts_t *cntp;
  1446. int i;
  1447. for_each_online_cpu(i) {
  1448. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1449. xfs_icsb_lock_cntr(cntp);
  1450. }
  1451. }
  1452. STATIC void
  1453. xfs_icsb_unlock_all_counters(
  1454. xfs_mount_t *mp)
  1455. {
  1456. xfs_icsb_cnts_t *cntp;
  1457. int i;
  1458. for_each_online_cpu(i) {
  1459. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1460. xfs_icsb_unlock_cntr(cntp);
  1461. }
  1462. }
  1463. STATIC void
  1464. xfs_icsb_count(
  1465. xfs_mount_t *mp,
  1466. xfs_icsb_cnts_t *cnt,
  1467. int flags)
  1468. {
  1469. xfs_icsb_cnts_t *cntp;
  1470. int i;
  1471. memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
  1472. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1473. xfs_icsb_lock_all_counters(mp);
  1474. for_each_online_cpu(i) {
  1475. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1476. cnt->icsb_icount += cntp->icsb_icount;
  1477. cnt->icsb_ifree += cntp->icsb_ifree;
  1478. cnt->icsb_fdblocks += cntp->icsb_fdblocks;
  1479. }
  1480. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1481. xfs_icsb_unlock_all_counters(mp);
  1482. }
  1483. STATIC int
  1484. xfs_icsb_counter_disabled(
  1485. xfs_mount_t *mp,
  1486. xfs_sb_field_t field)
  1487. {
  1488. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1489. return test_bit(field, &mp->m_icsb_counters);
  1490. }
  1491. STATIC void
  1492. xfs_icsb_disable_counter(
  1493. xfs_mount_t *mp,
  1494. xfs_sb_field_t field)
  1495. {
  1496. xfs_icsb_cnts_t cnt;
  1497. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1498. /*
  1499. * If we are already disabled, then there is nothing to do
  1500. * here. We check before locking all the counters to avoid
  1501. * the expensive lock operation when being called in the
  1502. * slow path and the counter is already disabled. This is
  1503. * safe because the only time we set or clear this state is under
  1504. * the m_icsb_mutex.
  1505. */
  1506. if (xfs_icsb_counter_disabled(mp, field))
  1507. return;
  1508. xfs_icsb_lock_all_counters(mp);
  1509. if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
  1510. /* drain back to superblock */
  1511. xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
  1512. switch(field) {
  1513. case XFS_SBS_ICOUNT:
  1514. mp->m_sb.sb_icount = cnt.icsb_icount;
  1515. break;
  1516. case XFS_SBS_IFREE:
  1517. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1518. break;
  1519. case XFS_SBS_FDBLOCKS:
  1520. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1521. break;
  1522. default:
  1523. BUG();
  1524. }
  1525. }
  1526. xfs_icsb_unlock_all_counters(mp);
  1527. }
  1528. STATIC void
  1529. xfs_icsb_enable_counter(
  1530. xfs_mount_t *mp,
  1531. xfs_sb_field_t field,
  1532. uint64_t count,
  1533. uint64_t resid)
  1534. {
  1535. xfs_icsb_cnts_t *cntp;
  1536. int i;
  1537. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1538. xfs_icsb_lock_all_counters(mp);
  1539. for_each_online_cpu(i) {
  1540. cntp = per_cpu_ptr(mp->m_sb_cnts, i);
  1541. switch (field) {
  1542. case XFS_SBS_ICOUNT:
  1543. cntp->icsb_icount = count + resid;
  1544. break;
  1545. case XFS_SBS_IFREE:
  1546. cntp->icsb_ifree = count + resid;
  1547. break;
  1548. case XFS_SBS_FDBLOCKS:
  1549. cntp->icsb_fdblocks = count + resid;
  1550. break;
  1551. default:
  1552. BUG();
  1553. break;
  1554. }
  1555. resid = 0;
  1556. }
  1557. clear_bit(field, &mp->m_icsb_counters);
  1558. xfs_icsb_unlock_all_counters(mp);
  1559. }
  1560. void
  1561. xfs_icsb_sync_counters_locked(
  1562. xfs_mount_t *mp,
  1563. int flags)
  1564. {
  1565. xfs_icsb_cnts_t cnt;
  1566. xfs_icsb_count(mp, &cnt, flags);
  1567. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
  1568. mp->m_sb.sb_icount = cnt.icsb_icount;
  1569. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
  1570. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1571. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
  1572. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1573. }
  1574. /*
  1575. * Accurate update of per-cpu counters to incore superblock
  1576. */
  1577. void
  1578. xfs_icsb_sync_counters(
  1579. xfs_mount_t *mp,
  1580. int flags)
  1581. {
  1582. spin_lock(&mp->m_sb_lock);
  1583. xfs_icsb_sync_counters_locked(mp, flags);
  1584. spin_unlock(&mp->m_sb_lock);
  1585. }
  1586. /*
  1587. * Balance and enable/disable counters as necessary.
  1588. *
  1589. * Thresholds for re-enabling counters are somewhat magic. inode counts are
  1590. * chosen to be the same number as single on disk allocation chunk per CPU, and
  1591. * free blocks is something far enough zero that we aren't going thrash when we
  1592. * get near ENOSPC. We also need to supply a minimum we require per cpu to
  1593. * prevent looping endlessly when xfs_alloc_space asks for more than will
  1594. * be distributed to a single CPU but each CPU has enough blocks to be
  1595. * reenabled.
  1596. *
  1597. * Note that we can be called when counters are already disabled.
  1598. * xfs_icsb_disable_counter() optimises the counter locking in this case to
  1599. * prevent locking every per-cpu counter needlessly.
  1600. */
  1601. #define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
  1602. #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
  1603. (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
  1604. STATIC void
  1605. xfs_icsb_balance_counter_locked(
  1606. xfs_mount_t *mp,
  1607. xfs_sb_field_t field,
  1608. int min_per_cpu)
  1609. {
  1610. uint64_t count, resid;
  1611. int weight = num_online_cpus();
  1612. uint64_t min = (uint64_t)min_per_cpu;
  1613. /* disable counter and sync counter */
  1614. xfs_icsb_disable_counter(mp, field);
  1615. /* update counters - first CPU gets residual*/
  1616. switch (field) {
  1617. case XFS_SBS_ICOUNT:
  1618. count = mp->m_sb.sb_icount;
  1619. resid = do_div(count, weight);
  1620. if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
  1621. return;
  1622. break;
  1623. case XFS_SBS_IFREE:
  1624. count = mp->m_sb.sb_ifree;
  1625. resid = do_div(count, weight);
  1626. if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
  1627. return;
  1628. break;
  1629. case XFS_SBS_FDBLOCKS:
  1630. count = mp->m_sb.sb_fdblocks;
  1631. resid = do_div(count, weight);
  1632. if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
  1633. return;
  1634. break;
  1635. default:
  1636. BUG();
  1637. count = resid = 0; /* quiet, gcc */
  1638. break;
  1639. }
  1640. xfs_icsb_enable_counter(mp, field, count, resid);
  1641. }
  1642. STATIC void
  1643. xfs_icsb_balance_counter(
  1644. xfs_mount_t *mp,
  1645. xfs_sb_field_t fields,
  1646. int min_per_cpu)
  1647. {
  1648. spin_lock(&mp->m_sb_lock);
  1649. xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
  1650. spin_unlock(&mp->m_sb_lock);
  1651. }
  1652. int
  1653. xfs_icsb_modify_counters(
  1654. xfs_mount_t *mp,
  1655. xfs_sb_field_t field,
  1656. int64_t delta,
  1657. int rsvd)
  1658. {
  1659. xfs_icsb_cnts_t *icsbp;
  1660. long long lcounter; /* long counter for 64 bit fields */
  1661. int ret = 0;
  1662. might_sleep();
  1663. again:
  1664. preempt_disable();
  1665. icsbp = this_cpu_ptr(mp->m_sb_cnts);
  1666. /*
  1667. * if the counter is disabled, go to slow path
  1668. */
  1669. if (unlikely(xfs_icsb_counter_disabled(mp, field)))
  1670. goto slow_path;
  1671. xfs_icsb_lock_cntr(icsbp);
  1672. if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
  1673. xfs_icsb_unlock_cntr(icsbp);
  1674. goto slow_path;
  1675. }
  1676. switch (field) {
  1677. case XFS_SBS_ICOUNT:
  1678. lcounter = icsbp->icsb_icount;
  1679. lcounter += delta;
  1680. if (unlikely(lcounter < 0))
  1681. goto balance_counter;
  1682. icsbp->icsb_icount = lcounter;
  1683. break;
  1684. case XFS_SBS_IFREE:
  1685. lcounter = icsbp->icsb_ifree;
  1686. lcounter += delta;
  1687. if (unlikely(lcounter < 0))
  1688. goto balance_counter;
  1689. icsbp->icsb_ifree = lcounter;
  1690. break;
  1691. case XFS_SBS_FDBLOCKS:
  1692. BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
  1693. lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  1694. lcounter += delta;
  1695. if (unlikely(lcounter < 0))
  1696. goto balance_counter;
  1697. icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
  1698. break;
  1699. default:
  1700. BUG();
  1701. break;
  1702. }
  1703. xfs_icsb_unlock_cntr(icsbp);
  1704. preempt_enable();
  1705. return 0;
  1706. slow_path:
  1707. preempt_enable();
  1708. /*
  1709. * serialise with a mutex so we don't burn lots of cpu on
  1710. * the superblock lock. We still need to hold the superblock
  1711. * lock, however, when we modify the global structures.
  1712. */
  1713. xfs_icsb_lock(mp);
  1714. /*
  1715. * Now running atomically.
  1716. *
  1717. * If the counter is enabled, someone has beaten us to rebalancing.
  1718. * Drop the lock and try again in the fast path....
  1719. */
  1720. if (!(xfs_icsb_counter_disabled(mp, field))) {
  1721. xfs_icsb_unlock(mp);
  1722. goto again;
  1723. }
  1724. /*
  1725. * The counter is currently disabled. Because we are
  1726. * running atomically here, we know a rebalance cannot
  1727. * be in progress. Hence we can go straight to operating
  1728. * on the global superblock. We do not call xfs_mod_incore_sb()
  1729. * here even though we need to get the m_sb_lock. Doing so
  1730. * will cause us to re-enter this function and deadlock.
  1731. * Hence we get the m_sb_lock ourselves and then call
  1732. * xfs_mod_incore_sb_unlocked() as the unlocked path operates
  1733. * directly on the global counters.
  1734. */
  1735. spin_lock(&mp->m_sb_lock);
  1736. ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1737. spin_unlock(&mp->m_sb_lock);
  1738. /*
  1739. * Now that we've modified the global superblock, we
  1740. * may be able to re-enable the distributed counters
  1741. * (e.g. lots of space just got freed). After that
  1742. * we are done.
  1743. */
  1744. if (ret != -ENOSPC)
  1745. xfs_icsb_balance_counter(mp, field, 0);
  1746. xfs_icsb_unlock(mp);
  1747. return ret;
  1748. balance_counter:
  1749. xfs_icsb_unlock_cntr(icsbp);
  1750. preempt_enable();
  1751. /*
  1752. * We may have multiple threads here if multiple per-cpu
  1753. * counters run dry at the same time. This will mean we can
  1754. * do more balances than strictly necessary but it is not
  1755. * the common slowpath case.
  1756. */
  1757. xfs_icsb_lock(mp);
  1758. /*
  1759. * running atomically.
  1760. *
  1761. * This will leave the counter in the correct state for future
  1762. * accesses. After the rebalance, we simply try again and our retry
  1763. * will either succeed through the fast path or slow path without
  1764. * another balance operation being required.
  1765. */
  1766. xfs_icsb_balance_counter(mp, field, delta);
  1767. xfs_icsb_unlock(mp);
  1768. goto again;
  1769. }
  1770. #endif