xfs_mount.c 50 KB

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