xfs_mount.c 50 KB

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