xfs_mount.c 50 KB

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