xfs_mount.c 51 KB

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