xfs_mount.c 51 KB

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