xfs_mount.c 51 KB

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