xfs_mount.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  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_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.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_rw.h"
  44. #include "xfs_quota.h"
  45. #include "xfs_fsops.h"
  46. STATIC void xfs_mount_log_sbunit(xfs_mount_t *, __int64_t);
  47. STATIC int xfs_uuid_mount(xfs_mount_t *);
  48. STATIC void xfs_uuid_unmount(xfs_mount_t *mp);
  49. STATIC void xfs_unmountfs_wait(xfs_mount_t *);
  50. #ifdef HAVE_PERCPU_SB
  51. STATIC void xfs_icsb_destroy_counters(xfs_mount_t *);
  52. STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
  53. int, int);
  54. STATIC void xfs_icsb_sync_counters(xfs_mount_t *);
  55. STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
  56. int64_t, int);
  57. STATIC int xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
  58. #else
  59. #define xfs_icsb_destroy_counters(mp) do { } while (0)
  60. #define xfs_icsb_balance_counter(mp, a, b, c) do { } while (0)
  61. #define xfs_icsb_sync_counters(mp) do { } while (0)
  62. #define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0)
  63. #endif
  64. static const struct {
  65. short offset;
  66. short type; /* 0 = integer
  67. * 1 = binary / string (no translation)
  68. */
  69. } xfs_sb_info[] = {
  70. { offsetof(xfs_sb_t, sb_magicnum), 0 },
  71. { offsetof(xfs_sb_t, sb_blocksize), 0 },
  72. { offsetof(xfs_sb_t, sb_dblocks), 0 },
  73. { offsetof(xfs_sb_t, sb_rblocks), 0 },
  74. { offsetof(xfs_sb_t, sb_rextents), 0 },
  75. { offsetof(xfs_sb_t, sb_uuid), 1 },
  76. { offsetof(xfs_sb_t, sb_logstart), 0 },
  77. { offsetof(xfs_sb_t, sb_rootino), 0 },
  78. { offsetof(xfs_sb_t, sb_rbmino), 0 },
  79. { offsetof(xfs_sb_t, sb_rsumino), 0 },
  80. { offsetof(xfs_sb_t, sb_rextsize), 0 },
  81. { offsetof(xfs_sb_t, sb_agblocks), 0 },
  82. { offsetof(xfs_sb_t, sb_agcount), 0 },
  83. { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
  84. { offsetof(xfs_sb_t, sb_logblocks), 0 },
  85. { offsetof(xfs_sb_t, sb_versionnum), 0 },
  86. { offsetof(xfs_sb_t, sb_sectsize), 0 },
  87. { offsetof(xfs_sb_t, sb_inodesize), 0 },
  88. { offsetof(xfs_sb_t, sb_inopblock), 0 },
  89. { offsetof(xfs_sb_t, sb_fname[0]), 1 },
  90. { offsetof(xfs_sb_t, sb_blocklog), 0 },
  91. { offsetof(xfs_sb_t, sb_sectlog), 0 },
  92. { offsetof(xfs_sb_t, sb_inodelog), 0 },
  93. { offsetof(xfs_sb_t, sb_inopblog), 0 },
  94. { offsetof(xfs_sb_t, sb_agblklog), 0 },
  95. { offsetof(xfs_sb_t, sb_rextslog), 0 },
  96. { offsetof(xfs_sb_t, sb_inprogress), 0 },
  97. { offsetof(xfs_sb_t, sb_imax_pct), 0 },
  98. { offsetof(xfs_sb_t, sb_icount), 0 },
  99. { offsetof(xfs_sb_t, sb_ifree), 0 },
  100. { offsetof(xfs_sb_t, sb_fdblocks), 0 },
  101. { offsetof(xfs_sb_t, sb_frextents), 0 },
  102. { offsetof(xfs_sb_t, sb_uquotino), 0 },
  103. { offsetof(xfs_sb_t, sb_gquotino), 0 },
  104. { offsetof(xfs_sb_t, sb_qflags), 0 },
  105. { offsetof(xfs_sb_t, sb_flags), 0 },
  106. { offsetof(xfs_sb_t, sb_shared_vn), 0 },
  107. { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
  108. { offsetof(xfs_sb_t, sb_unit), 0 },
  109. { offsetof(xfs_sb_t, sb_width), 0 },
  110. { offsetof(xfs_sb_t, sb_dirblklog), 0 },
  111. { offsetof(xfs_sb_t, sb_logsectlog), 0 },
  112. { offsetof(xfs_sb_t, sb_logsectsize),0 },
  113. { offsetof(xfs_sb_t, sb_logsunit), 0 },
  114. { offsetof(xfs_sb_t, sb_features2), 0 },
  115. { sizeof(xfs_sb_t), 0 }
  116. };
  117. /*
  118. * Return a pointer to an initialized xfs_mount structure.
  119. */
  120. xfs_mount_t *
  121. xfs_mount_init(void)
  122. {
  123. xfs_mount_t *mp;
  124. mp = kmem_zalloc(sizeof(xfs_mount_t), KM_SLEEP);
  125. if (xfs_icsb_init_counters(mp)) {
  126. mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB;
  127. }
  128. AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail");
  129. spinlock_init(&mp->m_sb_lock, "xfs_sb");
  130. mutex_init(&mp->m_ilock);
  131. initnsema(&mp->m_growlock, 1, "xfs_grow");
  132. /*
  133. * Initialize the AIL.
  134. */
  135. xfs_trans_ail_init(mp);
  136. atomic_set(&mp->m_active_trans, 0);
  137. return mp;
  138. }
  139. /*
  140. * Free up the resources associated with a mount structure. Assume that
  141. * the structure was initially zeroed, so we can tell which fields got
  142. * initialized.
  143. */
  144. void
  145. xfs_mount_free(
  146. xfs_mount_t *mp,
  147. int remove_bhv)
  148. {
  149. if (mp->m_ihash)
  150. xfs_ihash_free(mp);
  151. if (mp->m_chash)
  152. xfs_chash_free(mp);
  153. if (mp->m_perag) {
  154. int agno;
  155. for (agno = 0; agno < mp->m_maxagi; agno++)
  156. if (mp->m_perag[agno].pagb_list)
  157. kmem_free(mp->m_perag[agno].pagb_list,
  158. sizeof(xfs_perag_busy_t) *
  159. XFS_PAGB_NUM_SLOTS);
  160. kmem_free(mp->m_perag,
  161. sizeof(xfs_perag_t) * mp->m_sb.sb_agcount);
  162. }
  163. AIL_LOCK_DESTROY(&mp->m_ail_lock);
  164. spinlock_destroy(&mp->m_sb_lock);
  165. mutex_destroy(&mp->m_ilock);
  166. freesema(&mp->m_growlock);
  167. if (mp->m_quotainfo)
  168. XFS_QM_DONE(mp);
  169. if (mp->m_fsname != NULL)
  170. kmem_free(mp->m_fsname, mp->m_fsname_len);
  171. if (mp->m_rtname != NULL)
  172. kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1);
  173. if (mp->m_logname != NULL)
  174. kmem_free(mp->m_logname, strlen(mp->m_logname) + 1);
  175. if (remove_bhv) {
  176. struct bhv_vfs *vfsp = XFS_MTOVFS(mp);
  177. bhv_remove_all_vfsops(vfsp, 0);
  178. VFS_REMOVEBHV(vfsp, &mp->m_bhv);
  179. }
  180. xfs_icsb_destroy_counters(mp);
  181. kmem_free(mp, sizeof(xfs_mount_t));
  182. }
  183. /*
  184. * Check size of device based on the (data/realtime) block count.
  185. * Note: this check is used by the growfs code as well as mount.
  186. */
  187. int
  188. xfs_sb_validate_fsb_count(
  189. xfs_sb_t *sbp,
  190. __uint64_t nblocks)
  191. {
  192. ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
  193. ASSERT(sbp->sb_blocklog >= BBSHIFT);
  194. #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
  195. if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
  196. return E2BIG;
  197. #else /* Limited by UINT_MAX of sectors */
  198. if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
  199. return E2BIG;
  200. #endif
  201. return 0;
  202. }
  203. /*
  204. * Check the validity of the SB found.
  205. */
  206. STATIC int
  207. xfs_mount_validate_sb(
  208. xfs_mount_t *mp,
  209. xfs_sb_t *sbp,
  210. int flags)
  211. {
  212. /*
  213. * If the log device and data device have the
  214. * same device number, the log is internal.
  215. * Consequently, the sb_logstart should be non-zero. If
  216. * we have a zero sb_logstart in this case, we may be trying to mount
  217. * a volume filesystem in a non-volume manner.
  218. */
  219. if (sbp->sb_magicnum != XFS_SB_MAGIC) {
  220. xfs_fs_mount_cmn_err(flags, "bad magic number");
  221. return XFS_ERROR(EWRONGFS);
  222. }
  223. if (!XFS_SB_GOOD_VERSION(sbp)) {
  224. xfs_fs_mount_cmn_err(flags, "bad version");
  225. return XFS_ERROR(EWRONGFS);
  226. }
  227. if (unlikely(
  228. sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
  229. xfs_fs_mount_cmn_err(flags,
  230. "filesystem is marked as having an external log; "
  231. "specify logdev on the\nmount command line.");
  232. return XFS_ERROR(EINVAL);
  233. }
  234. if (unlikely(
  235. sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
  236. xfs_fs_mount_cmn_err(flags,
  237. "filesystem is marked as having an internal log; "
  238. "do not specify logdev on\nthe mount command line.");
  239. return XFS_ERROR(EINVAL);
  240. }
  241. /*
  242. * More sanity checking. These were stolen directly from
  243. * xfs_repair.
  244. */
  245. if (unlikely(
  246. sbp->sb_agcount <= 0 ||
  247. sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
  248. sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
  249. sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
  250. sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
  251. sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
  252. sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
  253. sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
  254. sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
  255. sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
  256. sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
  257. sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
  258. sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
  259. (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
  260. (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
  261. (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
  262. (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
  263. xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
  264. return XFS_ERROR(EFSCORRUPTED);
  265. }
  266. /*
  267. * Sanity check AG count, size fields against data size field
  268. */
  269. if (unlikely(
  270. sbp->sb_dblocks == 0 ||
  271. sbp->sb_dblocks >
  272. (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
  273. sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
  274. sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
  275. xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
  276. return XFS_ERROR(EFSCORRUPTED);
  277. }
  278. if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
  279. xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
  280. xfs_fs_mount_cmn_err(flags,
  281. "file system too large to be mounted on this system.");
  282. return XFS_ERROR(E2BIG);
  283. }
  284. if (unlikely(sbp->sb_inprogress)) {
  285. xfs_fs_mount_cmn_err(flags, "file system busy");
  286. return XFS_ERROR(EFSCORRUPTED);
  287. }
  288. /*
  289. * Version 1 directory format has never worked on Linux.
  290. */
  291. if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) {
  292. xfs_fs_mount_cmn_err(flags,
  293. "file system using version 1 directory format");
  294. return XFS_ERROR(ENOSYS);
  295. }
  296. /*
  297. * Until this is fixed only page-sized or smaller data blocks work.
  298. */
  299. if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
  300. xfs_fs_mount_cmn_err(flags,
  301. "file system with blocksize %d bytes",
  302. sbp->sb_blocksize);
  303. xfs_fs_mount_cmn_err(flags,
  304. "only pagesize (%ld) or less will currently work.",
  305. PAGE_SIZE);
  306. return XFS_ERROR(ENOSYS);
  307. }
  308. return 0;
  309. }
  310. xfs_agnumber_t
  311. xfs_initialize_perag(
  312. bhv_vfs_t *vfs,
  313. xfs_mount_t *mp,
  314. xfs_agnumber_t agcount)
  315. {
  316. xfs_agnumber_t index, max_metadata;
  317. xfs_perag_t *pag;
  318. xfs_agino_t agino;
  319. xfs_ino_t ino;
  320. xfs_sb_t *sbp = &mp->m_sb;
  321. xfs_ino_t max_inum = XFS_MAXINUMBER_32;
  322. /* Check to see if the filesystem can overflow 32 bit inodes */
  323. agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
  324. ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
  325. /* Clear the mount flag if no inode can overflow 32 bits
  326. * on this filesystem, or if specifically requested..
  327. */
  328. if ((vfs->vfs_flag & VFS_32BITINODES) && ino > max_inum) {
  329. mp->m_flags |= XFS_MOUNT_32BITINODES;
  330. } else {
  331. mp->m_flags &= ~XFS_MOUNT_32BITINODES;
  332. }
  333. /* If we can overflow then setup the ag headers accordingly */
  334. if (mp->m_flags & XFS_MOUNT_32BITINODES) {
  335. /* Calculate how much should be reserved for inodes to
  336. * meet the max inode percentage.
  337. */
  338. if (mp->m_maxicount) {
  339. __uint64_t icount;
  340. icount = sbp->sb_dblocks * sbp->sb_imax_pct;
  341. do_div(icount, 100);
  342. icount += sbp->sb_agblocks - 1;
  343. do_div(icount, sbp->sb_agblocks);
  344. max_metadata = icount;
  345. } else {
  346. max_metadata = agcount;
  347. }
  348. for (index = 0; index < agcount; index++) {
  349. ino = XFS_AGINO_TO_INO(mp, index, agino);
  350. if (ino > max_inum) {
  351. index++;
  352. break;
  353. }
  354. /* This ag is preferred for inodes */
  355. pag = &mp->m_perag[index];
  356. pag->pagi_inodeok = 1;
  357. if (index < max_metadata)
  358. pag->pagf_metadata = 1;
  359. }
  360. } else {
  361. /* Setup default behavior for smaller filesystems */
  362. for (index = 0; index < agcount; index++) {
  363. pag = &mp->m_perag[index];
  364. pag->pagi_inodeok = 1;
  365. }
  366. }
  367. return index;
  368. }
  369. /*
  370. * xfs_xlatesb
  371. *
  372. * data - on disk version of sb
  373. * sb - a superblock
  374. * dir - conversion direction: <0 - convert sb to buf
  375. * >0 - convert buf to sb
  376. * fields - which fields to copy (bitmask)
  377. */
  378. void
  379. xfs_xlatesb(
  380. void *data,
  381. xfs_sb_t *sb,
  382. int dir,
  383. __int64_t fields)
  384. {
  385. xfs_caddr_t buf_ptr;
  386. xfs_caddr_t mem_ptr;
  387. xfs_sb_field_t f;
  388. int first;
  389. int size;
  390. ASSERT(dir);
  391. ASSERT(fields);
  392. if (!fields)
  393. return;
  394. buf_ptr = (xfs_caddr_t)data;
  395. mem_ptr = (xfs_caddr_t)sb;
  396. while (fields) {
  397. f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
  398. first = xfs_sb_info[f].offset;
  399. size = xfs_sb_info[f + 1].offset - first;
  400. ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
  401. if (size == 1 || xfs_sb_info[f].type == 1) {
  402. if (dir > 0) {
  403. memcpy(mem_ptr + first, buf_ptr + first, size);
  404. } else {
  405. memcpy(buf_ptr + first, mem_ptr + first, size);
  406. }
  407. } else {
  408. switch (size) {
  409. case 2:
  410. INT_XLATE(*(__uint16_t*)(buf_ptr+first),
  411. *(__uint16_t*)(mem_ptr+first),
  412. dir, ARCH_CONVERT);
  413. break;
  414. case 4:
  415. INT_XLATE(*(__uint32_t*)(buf_ptr+first),
  416. *(__uint32_t*)(mem_ptr+first),
  417. dir, ARCH_CONVERT);
  418. break;
  419. case 8:
  420. INT_XLATE(*(__uint64_t*)(buf_ptr+first),
  421. *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT);
  422. break;
  423. default:
  424. ASSERT(0);
  425. }
  426. }
  427. fields &= ~(1LL << f);
  428. }
  429. }
  430. /*
  431. * xfs_readsb
  432. *
  433. * Does the initial read of the superblock.
  434. */
  435. int
  436. xfs_readsb(xfs_mount_t *mp, int flags)
  437. {
  438. unsigned int sector_size;
  439. unsigned int extra_flags;
  440. xfs_buf_t *bp;
  441. xfs_sb_t *sbp;
  442. int error;
  443. ASSERT(mp->m_sb_bp == NULL);
  444. ASSERT(mp->m_ddev_targp != NULL);
  445. /*
  446. * Allocate a (locked) buffer to hold the superblock.
  447. * This will be kept around at all times to optimize
  448. * access to the superblock.
  449. */
  450. sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
  451. extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
  452. bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
  453. BTOBB(sector_size), extra_flags);
  454. if (!bp || XFS_BUF_ISERROR(bp)) {
  455. xfs_fs_mount_cmn_err(flags, "SB read failed");
  456. error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
  457. goto fail;
  458. }
  459. ASSERT(XFS_BUF_ISBUSY(bp));
  460. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  461. /*
  462. * Initialize the mount structure from the superblock.
  463. * But first do some basic consistency checking.
  464. */
  465. sbp = XFS_BUF_TO_SBP(bp);
  466. xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS);
  467. error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
  468. if (error) {
  469. xfs_fs_mount_cmn_err(flags, "SB validate failed");
  470. goto fail;
  471. }
  472. /*
  473. * We must be able to do sector-sized and sector-aligned IO.
  474. */
  475. if (sector_size > mp->m_sb.sb_sectsize) {
  476. xfs_fs_mount_cmn_err(flags,
  477. "device supports only %u byte sectors (not %u)",
  478. sector_size, mp->m_sb.sb_sectsize);
  479. error = ENOSYS;
  480. goto fail;
  481. }
  482. /*
  483. * If device sector size is smaller than the superblock size,
  484. * re-read the superblock so the buffer is correctly sized.
  485. */
  486. if (sector_size < mp->m_sb.sb_sectsize) {
  487. XFS_BUF_UNMANAGE(bp);
  488. xfs_buf_relse(bp);
  489. sector_size = mp->m_sb.sb_sectsize;
  490. bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
  491. BTOBB(sector_size), extra_flags);
  492. if (!bp || XFS_BUF_ISERROR(bp)) {
  493. xfs_fs_mount_cmn_err(flags, "SB re-read failed");
  494. error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
  495. goto fail;
  496. }
  497. ASSERT(XFS_BUF_ISBUSY(bp));
  498. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  499. }
  500. /* Initialize per-cpu counters */
  501. xfs_icsb_reinit_counters(mp);
  502. mp->m_sb_bp = bp;
  503. xfs_buf_relse(bp);
  504. ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
  505. return 0;
  506. fail:
  507. if (bp) {
  508. XFS_BUF_UNMANAGE(bp);
  509. xfs_buf_relse(bp);
  510. }
  511. return error;
  512. }
  513. /*
  514. * xfs_mount_common
  515. *
  516. * Mount initialization code establishing various mount
  517. * fields from the superblock associated with the given
  518. * mount structure
  519. */
  520. STATIC void
  521. xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
  522. {
  523. int i;
  524. mp->m_agfrotor = mp->m_agirotor = 0;
  525. spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock");
  526. mp->m_maxagi = mp->m_sb.sb_agcount;
  527. mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
  528. mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
  529. mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
  530. mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
  531. mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
  532. mp->m_litino = sbp->sb_inodesize -
  533. ((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t));
  534. mp->m_blockmask = sbp->sb_blocksize - 1;
  535. mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
  536. mp->m_blockwmask = mp->m_blockwsize - 1;
  537. INIT_LIST_HEAD(&mp->m_del_inodes);
  538. /*
  539. * Setup for attributes, in case they get created.
  540. * This value is for inodes getting attributes for the first time,
  541. * the per-inode value is for old attribute values.
  542. */
  543. ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
  544. switch (sbp->sb_inodesize) {
  545. case 256:
  546. mp->m_attroffset = XFS_LITINO(mp) -
  547. XFS_BMDR_SPACE_CALC(MINABTPTRS);
  548. break;
  549. case 512:
  550. case 1024:
  551. case 2048:
  552. mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
  553. break;
  554. default:
  555. ASSERT(0);
  556. }
  557. ASSERT(mp->m_attroffset < XFS_LITINO(mp));
  558. for (i = 0; i < 2; i++) {
  559. mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
  560. xfs_alloc, i == 0);
  561. mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
  562. xfs_alloc, i == 0);
  563. }
  564. for (i = 0; i < 2; i++) {
  565. mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
  566. xfs_bmbt, i == 0);
  567. mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
  568. xfs_bmbt, i == 0);
  569. }
  570. for (i = 0; i < 2; i++) {
  571. mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
  572. xfs_inobt, i == 0);
  573. mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
  574. xfs_inobt, i == 0);
  575. }
  576. mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
  577. mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
  578. sbp->sb_inopblock);
  579. mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
  580. }
  581. /*
  582. * xfs_initialize_perag_data
  583. *
  584. * Read in each per-ag structure so we can count up the number of
  585. * allocated inodes, free inodes and used filesystem blocks as this
  586. * information is no longer persistent in the superblock. Once we have
  587. * this information, write it into the in-core superblock structure.
  588. */
  589. STATIC int
  590. xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
  591. {
  592. xfs_agnumber_t index;
  593. xfs_perag_t *pag;
  594. xfs_sb_t *sbp = &mp->m_sb;
  595. uint64_t ifree = 0;
  596. uint64_t ialloc = 0;
  597. uint64_t bfree = 0;
  598. uint64_t bfreelst = 0;
  599. uint64_t btree = 0;
  600. int error;
  601. int s;
  602. for (index = 0; index < agcount; index++) {
  603. /*
  604. * read the agf, then the agi. This gets us
  605. * all the inforamtion we need and populates the
  606. * per-ag structures for us.
  607. */
  608. error = xfs_alloc_pagf_init(mp, NULL, index, 0);
  609. if (error)
  610. return error;
  611. error = xfs_ialloc_pagi_init(mp, NULL, index);
  612. if (error)
  613. return error;
  614. pag = &mp->m_perag[index];
  615. ifree += pag->pagi_freecount;
  616. ialloc += pag->pagi_count;
  617. bfree += pag->pagf_freeblks;
  618. bfreelst += pag->pagf_flcount;
  619. btree += pag->pagf_btreeblks;
  620. }
  621. /*
  622. * Overwrite incore superblock counters with just-read data
  623. */
  624. s = XFS_SB_LOCK(mp);
  625. sbp->sb_ifree = ifree;
  626. sbp->sb_icount = ialloc;
  627. sbp->sb_fdblocks = bfree + bfreelst + btree;
  628. XFS_SB_UNLOCK(mp, s);
  629. /* Fixup the per-cpu counters as well. */
  630. xfs_icsb_reinit_counters(mp);
  631. return 0;
  632. }
  633. /*
  634. * xfs_mountfs
  635. *
  636. * This function does the following on an initial mount of a file system:
  637. * - reads the superblock from disk and init the mount struct
  638. * - if we're a 32-bit kernel, do a size check on the superblock
  639. * so we don't mount terabyte filesystems
  640. * - init mount struct realtime fields
  641. * - allocate inode hash table for fs
  642. * - init directory manager
  643. * - perform recovery and init the log manager
  644. */
  645. int
  646. xfs_mountfs(
  647. bhv_vfs_t *vfsp,
  648. xfs_mount_t *mp,
  649. int mfsi_flags)
  650. {
  651. xfs_buf_t *bp;
  652. xfs_sb_t *sbp = &(mp->m_sb);
  653. xfs_inode_t *rip;
  654. bhv_vnode_t *rvp = NULL;
  655. int readio_log, writeio_log;
  656. xfs_daddr_t d;
  657. __uint64_t resblks;
  658. __int64_t update_flags;
  659. uint quotamount, quotaflags;
  660. int agno;
  661. int uuid_mounted = 0;
  662. int error = 0;
  663. if (mp->m_sb_bp == NULL) {
  664. if ((error = xfs_readsb(mp, mfsi_flags))) {
  665. return error;
  666. }
  667. }
  668. xfs_mount_common(mp, sbp);
  669. /*
  670. * Check if sb_agblocks is aligned at stripe boundary
  671. * If sb_agblocks is NOT aligned turn off m_dalign since
  672. * allocator alignment is within an ag, therefore ag has
  673. * to be aligned at stripe boundary.
  674. */
  675. update_flags = 0LL;
  676. if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
  677. /*
  678. * If stripe unit and stripe width are not multiples
  679. * of the fs blocksize turn off alignment.
  680. */
  681. if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
  682. (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
  683. if (mp->m_flags & XFS_MOUNT_RETERR) {
  684. cmn_err(CE_WARN,
  685. "XFS: alignment check 1 failed");
  686. error = XFS_ERROR(EINVAL);
  687. goto error1;
  688. }
  689. mp->m_dalign = mp->m_swidth = 0;
  690. } else {
  691. /*
  692. * Convert the stripe unit and width to FSBs.
  693. */
  694. mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
  695. if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
  696. if (mp->m_flags & XFS_MOUNT_RETERR) {
  697. error = XFS_ERROR(EINVAL);
  698. goto error1;
  699. }
  700. xfs_fs_cmn_err(CE_WARN, mp,
  701. "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
  702. mp->m_dalign, mp->m_swidth,
  703. sbp->sb_agblocks);
  704. mp->m_dalign = 0;
  705. mp->m_swidth = 0;
  706. } else if (mp->m_dalign) {
  707. mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
  708. } else {
  709. if (mp->m_flags & XFS_MOUNT_RETERR) {
  710. xfs_fs_cmn_err(CE_WARN, mp,
  711. "stripe alignment turned off: sunit(%d) less than bsize(%d)",
  712. mp->m_dalign,
  713. mp->m_blockmask +1);
  714. error = XFS_ERROR(EINVAL);
  715. goto error1;
  716. }
  717. mp->m_swidth = 0;
  718. }
  719. }
  720. /*
  721. * Update superblock with new values
  722. * and log changes
  723. */
  724. if (XFS_SB_VERSION_HASDALIGN(sbp)) {
  725. if (sbp->sb_unit != mp->m_dalign) {
  726. sbp->sb_unit = mp->m_dalign;
  727. update_flags |= XFS_SB_UNIT;
  728. }
  729. if (sbp->sb_width != mp->m_swidth) {
  730. sbp->sb_width = mp->m_swidth;
  731. update_flags |= XFS_SB_WIDTH;
  732. }
  733. }
  734. } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
  735. XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
  736. mp->m_dalign = sbp->sb_unit;
  737. mp->m_swidth = sbp->sb_width;
  738. }
  739. xfs_alloc_compute_maxlevels(mp);
  740. xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
  741. xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
  742. xfs_ialloc_compute_maxlevels(mp);
  743. if (sbp->sb_imax_pct) {
  744. __uint64_t icount;
  745. /* Make sure the maximum inode count is a multiple of the
  746. * units we allocate inodes in.
  747. */
  748. icount = sbp->sb_dblocks * sbp->sb_imax_pct;
  749. do_div(icount, 100);
  750. do_div(icount, mp->m_ialloc_blks);
  751. mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
  752. sbp->sb_inopblog;
  753. } else
  754. mp->m_maxicount = 0;
  755. mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
  756. /*
  757. * XFS uses the uuid from the superblock as the unique
  758. * identifier for fsid. We can not use the uuid from the volume
  759. * since a single partition filesystem is identical to a single
  760. * partition volume/filesystem.
  761. */
  762. if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
  763. (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
  764. __uint64_t ret64;
  765. if (xfs_uuid_mount(mp)) {
  766. error = XFS_ERROR(EINVAL);
  767. goto error1;
  768. }
  769. uuid_mounted=1;
  770. ret64 = uuid_hash64(&sbp->sb_uuid);
  771. memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
  772. }
  773. /*
  774. * Set the default minimum read and write sizes unless
  775. * already specified in a mount option.
  776. * We use smaller I/O sizes when the file system
  777. * is being used for NFS service (wsync mount option).
  778. */
  779. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
  780. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  781. readio_log = XFS_WSYNC_READIO_LOG;
  782. writeio_log = XFS_WSYNC_WRITEIO_LOG;
  783. } else {
  784. readio_log = XFS_READIO_LOG_LARGE;
  785. writeio_log = XFS_WRITEIO_LOG_LARGE;
  786. }
  787. } else {
  788. readio_log = mp->m_readio_log;
  789. writeio_log = mp->m_writeio_log;
  790. }
  791. if (sbp->sb_blocklog > readio_log) {
  792. mp->m_readio_log = sbp->sb_blocklog;
  793. } else {
  794. mp->m_readio_log = readio_log;
  795. }
  796. mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
  797. if (sbp->sb_blocklog > writeio_log) {
  798. mp->m_writeio_log = sbp->sb_blocklog;
  799. } else {
  800. mp->m_writeio_log = writeio_log;
  801. }
  802. mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
  803. /*
  804. * Set the inode cluster size based on the physical memory
  805. * size. This may still be overridden by the file system
  806. * block size if it is larger than the chosen cluster size.
  807. */
  808. if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */
  809. mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE;
  810. } else {
  811. mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
  812. }
  813. /*
  814. * Set whether we're using inode alignment.
  815. */
  816. if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
  817. mp->m_sb.sb_inoalignmt >=
  818. XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
  819. mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
  820. else
  821. mp->m_inoalign_mask = 0;
  822. /*
  823. * If we are using stripe alignment, check whether
  824. * the stripe unit is a multiple of the inode alignment
  825. */
  826. if (mp->m_dalign && mp->m_inoalign_mask &&
  827. !(mp->m_dalign & mp->m_inoalign_mask))
  828. mp->m_sinoalign = mp->m_dalign;
  829. else
  830. mp->m_sinoalign = 0;
  831. /*
  832. * Check that the data (and log if separate) are an ok size.
  833. */
  834. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  835. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
  836. cmn_err(CE_WARN, "XFS: size check 1 failed");
  837. error = XFS_ERROR(E2BIG);
  838. goto error1;
  839. }
  840. error = xfs_read_buf(mp, mp->m_ddev_targp,
  841. d - XFS_FSS_TO_BB(mp, 1),
  842. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  843. if (!error) {
  844. xfs_buf_relse(bp);
  845. } else {
  846. cmn_err(CE_WARN, "XFS: size check 2 failed");
  847. if (error == ENOSPC) {
  848. error = XFS_ERROR(E2BIG);
  849. }
  850. goto error1;
  851. }
  852. if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
  853. mp->m_logdev_targp != mp->m_ddev_targp) {
  854. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
  855. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
  856. cmn_err(CE_WARN, "XFS: size check 3 failed");
  857. error = XFS_ERROR(E2BIG);
  858. goto error1;
  859. }
  860. error = xfs_read_buf(mp, mp->m_logdev_targp,
  861. d - XFS_FSB_TO_BB(mp, 1),
  862. XFS_FSB_TO_BB(mp, 1), 0, &bp);
  863. if (!error) {
  864. xfs_buf_relse(bp);
  865. } else {
  866. cmn_err(CE_WARN, "XFS: size check 3 failed");
  867. if (error == ENOSPC) {
  868. error = XFS_ERROR(E2BIG);
  869. }
  870. goto error1;
  871. }
  872. }
  873. /*
  874. * Initialize realtime fields in the mount structure
  875. */
  876. if ((error = xfs_rtmount_init(mp))) {
  877. cmn_err(CE_WARN, "XFS: RT mount failed");
  878. goto error1;
  879. }
  880. /*
  881. * For client case we are done now
  882. */
  883. if (mfsi_flags & XFS_MFSI_CLIENT) {
  884. return 0;
  885. }
  886. /*
  887. * Copies the low order bits of the timestamp and the randomly
  888. * set "sequence" number out of a UUID.
  889. */
  890. uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
  891. /*
  892. * The vfs structure needs to have a file system independent
  893. * way of checking for the invariant file system ID. Since it
  894. * can't look at mount structures it has a pointer to the data
  895. * in the mount structure.
  896. *
  897. * File systems that don't support user level file handles (i.e.
  898. * all of them except for XFS) will leave vfs_altfsid as NULL.
  899. */
  900. vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
  901. mp->m_dmevmask = 0; /* not persistent; set after each mount */
  902. xfs_dir_mount(mp);
  903. /*
  904. * Initialize the attribute manager's entries.
  905. */
  906. mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
  907. /*
  908. * Initialize the precomputed transaction reservations values.
  909. */
  910. xfs_trans_init(mp);
  911. /*
  912. * Allocate and initialize the inode hash table for this
  913. * file system.
  914. */
  915. xfs_ihash_init(mp);
  916. xfs_chash_init(mp);
  917. /*
  918. * Allocate and initialize the per-ag data.
  919. */
  920. init_rwsem(&mp->m_peraglock);
  921. mp->m_perag =
  922. kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
  923. mp->m_maxagi = xfs_initialize_perag(vfsp, mp, sbp->sb_agcount);
  924. /*
  925. * log's mount-time initialization. Perform 1st part recovery if needed
  926. */
  927. if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */
  928. error = xfs_log_mount(mp, mp->m_logdev_targp,
  929. XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
  930. XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
  931. if (error) {
  932. cmn_err(CE_WARN, "XFS: log mount failed");
  933. goto error2;
  934. }
  935. } else { /* No log has been defined */
  936. cmn_err(CE_WARN, "XFS: no log defined");
  937. XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
  938. error = XFS_ERROR(EFSCORRUPTED);
  939. goto error2;
  940. }
  941. /*
  942. * Now the log is mounted, we know if it was an unclean shutdown or
  943. * not. If it was, with the first phase of recovery has completed, we
  944. * have consistent AG blocks on disk. We have not recovered EFIs yet,
  945. * but they are recovered transactionally in the second recovery phase
  946. * later.
  947. *
  948. * Hence we can safely re-initialise incore superblock counters from
  949. * the per-ag data. These may not be correct if the filesystem was not
  950. * cleanly unmounted, so we need to wait for recovery to finish before
  951. * doing this.
  952. *
  953. * If the filesystem was cleanly unmounted, then we can trust the
  954. * values in the superblock to be correct and we don't need to do
  955. * anything here.
  956. *
  957. * If we are currently making the filesystem, the initialisation will
  958. * fail as the perag data is in an undefined state.
  959. */
  960. if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
  961. !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
  962. !mp->m_sb.sb_inprogress) {
  963. error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
  964. if (error) {
  965. goto error2;
  966. }
  967. }
  968. /*
  969. * Get and sanity-check the root inode.
  970. * Save the pointer to it in the mount structure.
  971. */
  972. error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
  973. if (error) {
  974. cmn_err(CE_WARN, "XFS: failed to read root inode");
  975. goto error3;
  976. }
  977. ASSERT(rip != NULL);
  978. rvp = XFS_ITOV(rip);
  979. if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
  980. cmn_err(CE_WARN, "XFS: corrupted root inode");
  981. cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
  982. XFS_BUFTARG_NAME(mp->m_ddev_targp),
  983. (unsigned long long)rip->i_ino);
  984. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  985. XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
  986. mp);
  987. error = XFS_ERROR(EFSCORRUPTED);
  988. goto error4;
  989. }
  990. mp->m_rootip = rip; /* save it */
  991. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  992. /*
  993. * Initialize realtime inode pointers in the mount structure
  994. */
  995. if ((error = xfs_rtmount_inodes(mp))) {
  996. /*
  997. * Free up the root inode.
  998. */
  999. cmn_err(CE_WARN, "XFS: failed to read RT inodes");
  1000. goto error4;
  1001. }
  1002. /*
  1003. * If fs is not mounted readonly, then update the superblock
  1004. * unit and width changes.
  1005. */
  1006. if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY))
  1007. xfs_mount_log_sbunit(mp, update_flags);
  1008. /*
  1009. * Initialise the XFS quota management subsystem for this mount
  1010. */
  1011. if ((error = XFS_QM_INIT(mp, &quotamount, &quotaflags)))
  1012. goto error4;
  1013. /*
  1014. * Finish recovering the file system. This part needed to be
  1015. * delayed until after the root and real-time bitmap inodes
  1016. * were consistently read in.
  1017. */
  1018. error = xfs_log_mount_finish(mp, mfsi_flags);
  1019. if (error) {
  1020. cmn_err(CE_WARN, "XFS: log mount finish failed");
  1021. goto error4;
  1022. }
  1023. /*
  1024. * Complete the quota initialisation, post-log-replay component.
  1025. */
  1026. if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags)))
  1027. goto error4;
  1028. /*
  1029. * Now we are mounted, reserve a small amount of unused space for
  1030. * privileged transactions. This is needed so that transaction
  1031. * space required for critical operations can dip into this pool
  1032. * when at ENOSPC. This is needed for operations like create with
  1033. * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
  1034. * are not allowed to use this reserved space.
  1035. *
  1036. * We default to 5% or 1024 fsbs of space reserved, whichever is smaller.
  1037. * This may drive us straight to ENOSPC on mount, but that implies
  1038. * we were already there on the last unmount.
  1039. */
  1040. resblks = mp->m_sb.sb_dblocks;
  1041. do_div(resblks, 20);
  1042. resblks = min_t(__uint64_t, resblks, 1024);
  1043. xfs_reserve_blocks(mp, &resblks, NULL);
  1044. return 0;
  1045. error4:
  1046. /*
  1047. * Free up the root inode.
  1048. */
  1049. VN_RELE(rvp);
  1050. error3:
  1051. xfs_log_unmount_dealloc(mp);
  1052. error2:
  1053. xfs_ihash_free(mp);
  1054. xfs_chash_free(mp);
  1055. for (agno = 0; agno < sbp->sb_agcount; agno++)
  1056. if (mp->m_perag[agno].pagb_list)
  1057. kmem_free(mp->m_perag[agno].pagb_list,
  1058. sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS);
  1059. kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t));
  1060. mp->m_perag = NULL;
  1061. /* FALLTHROUGH */
  1062. error1:
  1063. if (uuid_mounted)
  1064. xfs_uuid_unmount(mp);
  1065. xfs_freesb(mp);
  1066. return error;
  1067. }
  1068. /*
  1069. * xfs_unmountfs
  1070. *
  1071. * This flushes out the inodes,dquots and the superblock, unmounts the
  1072. * log and makes sure that incore structures are freed.
  1073. */
  1074. int
  1075. xfs_unmountfs(xfs_mount_t *mp, struct cred *cr)
  1076. {
  1077. struct bhv_vfs *vfsp = XFS_MTOVFS(mp);
  1078. #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
  1079. int64_t fsid;
  1080. #endif
  1081. __uint64_t resblks;
  1082. /*
  1083. * We can potentially deadlock here if we have an inode cluster
  1084. * that has been freed has it's buffer still pinned in memory because
  1085. * the transaction is still sitting in a iclog. The stale inodes
  1086. * on that buffer will have their flush locks held until the
  1087. * transaction hits the disk and the callbacks run. the inode
  1088. * flush takes the flush lock unconditionally and with nothing to
  1089. * push out the iclog we will never get that unlocked. hence we
  1090. * need to force the log first.
  1091. */
  1092. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  1093. xfs_iflush_all(mp);
  1094. XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
  1095. /*
  1096. * Flush out the log synchronously so that we know for sure
  1097. * that nothing is pinned. This is important because bflush()
  1098. * will skip pinned buffers.
  1099. */
  1100. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  1101. xfs_binval(mp->m_ddev_targp);
  1102. if (mp->m_rtdev_targp) {
  1103. xfs_binval(mp->m_rtdev_targp);
  1104. }
  1105. /*
  1106. * Unreserve any blocks we have so that when we unmount we don't account
  1107. * the reserved free space as used. This is really only necessary for
  1108. * lazy superblock counting because it trusts the incore superblock
  1109. * counters to be aboslutely correct on clean unmount.
  1110. *
  1111. * We don't bother correcting this elsewhere for lazy superblock
  1112. * counting because on mount of an unclean filesystem we reconstruct the
  1113. * correct counter value and this is irrelevant.
  1114. *
  1115. * For non-lazy counter filesystems, this doesn't matter at all because
  1116. * we only every apply deltas to the superblock and hence the incore
  1117. * value does not matter....
  1118. */
  1119. resblks = 0;
  1120. xfs_reserve_blocks(mp, &resblks, NULL);
  1121. xfs_log_sbcount(mp, 1);
  1122. xfs_unmountfs_writesb(mp);
  1123. xfs_unmountfs_wait(mp); /* wait for async bufs */
  1124. xfs_log_unmount(mp); /* Done! No more fs ops. */
  1125. xfs_freesb(mp);
  1126. /*
  1127. * All inodes from this mount point should be freed.
  1128. */
  1129. ASSERT(mp->m_inodes == NULL);
  1130. xfs_unmountfs_close(mp, cr);
  1131. if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
  1132. xfs_uuid_unmount(mp);
  1133. #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
  1134. /*
  1135. * clear all error tags on this filesystem
  1136. */
  1137. memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
  1138. xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
  1139. #endif
  1140. XFS_IODONE(vfsp);
  1141. xfs_mount_free(mp, 1);
  1142. return 0;
  1143. }
  1144. void
  1145. xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
  1146. {
  1147. if (mp->m_logdev_targp != mp->m_ddev_targp)
  1148. xfs_free_buftarg(mp->m_logdev_targp, 1);
  1149. if (mp->m_rtdev_targp)
  1150. xfs_free_buftarg(mp->m_rtdev_targp, 1);
  1151. xfs_free_buftarg(mp->m_ddev_targp, 0);
  1152. }
  1153. STATIC void
  1154. xfs_unmountfs_wait(xfs_mount_t *mp)
  1155. {
  1156. if (mp->m_logdev_targp != mp->m_ddev_targp)
  1157. xfs_wait_buftarg(mp->m_logdev_targp);
  1158. if (mp->m_rtdev_targp)
  1159. xfs_wait_buftarg(mp->m_rtdev_targp);
  1160. xfs_wait_buftarg(mp->m_ddev_targp);
  1161. }
  1162. int
  1163. xfs_fs_writable(xfs_mount_t *mp)
  1164. {
  1165. bhv_vfs_t *vfsp = XFS_MTOVFS(mp);
  1166. return !(vfs_test_for_freeze(vfsp) || XFS_FORCED_SHUTDOWN(mp) ||
  1167. (vfsp->vfs_flag & VFS_RDONLY));
  1168. }
  1169. /*
  1170. * xfs_log_sbcount
  1171. *
  1172. * Called either periodically to keep the on disk superblock values
  1173. * roughly up to date or from unmount to make sure the values are
  1174. * correct on a clean unmount.
  1175. *
  1176. * Note this code can be called during the process of freezing, so
  1177. * we may need to use the transaction allocator which does not not
  1178. * block when the transaction subsystem is in its frozen state.
  1179. */
  1180. int
  1181. xfs_log_sbcount(
  1182. xfs_mount_t *mp,
  1183. uint sync)
  1184. {
  1185. xfs_trans_t *tp;
  1186. int error;
  1187. if (!xfs_fs_writable(mp))
  1188. return 0;
  1189. xfs_icsb_sync_counters(mp);
  1190. /*
  1191. * we don't need to do this if we are updating the superblock
  1192. * counters on every modification.
  1193. */
  1194. if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
  1195. return 0;
  1196. tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT);
  1197. error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  1198. XFS_DEFAULT_LOG_COUNT);
  1199. if (error) {
  1200. xfs_trans_cancel(tp, 0);
  1201. return error;
  1202. }
  1203. xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
  1204. if (sync)
  1205. xfs_trans_set_sync(tp);
  1206. xfs_trans_commit(tp, 0);
  1207. return 0;
  1208. }
  1209. int
  1210. xfs_unmountfs_writesb(xfs_mount_t *mp)
  1211. {
  1212. xfs_buf_t *sbp;
  1213. xfs_sb_t *sb;
  1214. int error = 0;
  1215. /*
  1216. * skip superblock write if fs is read-only, or
  1217. * if we are doing a forced umount.
  1218. */
  1219. if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
  1220. XFS_FORCED_SHUTDOWN(mp))) {
  1221. sbp = xfs_getsb(mp, 0);
  1222. sb = XFS_BUF_TO_SBP(sbp);
  1223. /*
  1224. * mark shared-readonly if desired
  1225. */
  1226. if (mp->m_mk_sharedro) {
  1227. if (!(sb->sb_flags & XFS_SBF_READONLY))
  1228. sb->sb_flags |= XFS_SBF_READONLY;
  1229. if (!XFS_SB_VERSION_HASSHARED(sb))
  1230. XFS_SB_VERSION_ADDSHARED(sb);
  1231. xfs_fs_cmn_err(CE_NOTE, mp,
  1232. "Unmounting, marking shared read-only");
  1233. }
  1234. XFS_BUF_UNDONE(sbp);
  1235. XFS_BUF_UNREAD(sbp);
  1236. XFS_BUF_UNDELAYWRITE(sbp);
  1237. XFS_BUF_WRITE(sbp);
  1238. XFS_BUF_UNASYNC(sbp);
  1239. ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
  1240. xfsbdstrat(mp, sbp);
  1241. /* Nevermind errors we might get here. */
  1242. error = xfs_iowait(sbp);
  1243. if (error)
  1244. xfs_ioerror_alert("xfs_unmountfs_writesb",
  1245. mp, sbp, XFS_BUF_ADDR(sbp));
  1246. if (error && mp->m_mk_sharedro)
  1247. xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly");
  1248. xfs_buf_relse(sbp);
  1249. }
  1250. return error;
  1251. }
  1252. /*
  1253. * xfs_mod_sb() can be used to copy arbitrary changes to the
  1254. * in-core superblock into the superblock buffer to be logged.
  1255. * It does not provide the higher level of locking that is
  1256. * needed to protect the in-core superblock from concurrent
  1257. * access.
  1258. */
  1259. void
  1260. xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
  1261. {
  1262. xfs_buf_t *bp;
  1263. int first;
  1264. int last;
  1265. xfs_mount_t *mp;
  1266. xfs_sb_t *sbp;
  1267. xfs_sb_field_t f;
  1268. ASSERT(fields);
  1269. if (!fields)
  1270. return;
  1271. mp = tp->t_mountp;
  1272. bp = xfs_trans_getsb(tp, mp, 0);
  1273. sbp = XFS_BUF_TO_SBP(bp);
  1274. first = sizeof(xfs_sb_t);
  1275. last = 0;
  1276. /* translate/copy */
  1277. xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
  1278. /* find modified range */
  1279. f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
  1280. ASSERT((1LL << f) & XFS_SB_MOD_BITS);
  1281. first = xfs_sb_info[f].offset;
  1282. f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
  1283. ASSERT((1LL << f) & XFS_SB_MOD_BITS);
  1284. last = xfs_sb_info[f + 1].offset - 1;
  1285. xfs_trans_log_buf(tp, bp, first, last);
  1286. }
  1287. /*
  1288. * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
  1289. * a delta to a specified field in the in-core superblock. Simply
  1290. * switch on the field indicated and apply the delta to that field.
  1291. * Fields are not allowed to dip below zero, so if the delta would
  1292. * do this do not apply it and return EINVAL.
  1293. *
  1294. * The SB_LOCK must be held when this routine is called.
  1295. */
  1296. int
  1297. xfs_mod_incore_sb_unlocked(
  1298. xfs_mount_t *mp,
  1299. xfs_sb_field_t field,
  1300. int64_t delta,
  1301. int rsvd)
  1302. {
  1303. int scounter; /* short counter for 32 bit fields */
  1304. long long lcounter; /* long counter for 64 bit fields */
  1305. long long res_used, rem;
  1306. /*
  1307. * With the in-core superblock spin lock held, switch
  1308. * on the indicated field. Apply the delta to the
  1309. * proper field. If the fields value would dip below
  1310. * 0, then do not apply the delta and return EINVAL.
  1311. */
  1312. switch (field) {
  1313. case XFS_SBS_ICOUNT:
  1314. lcounter = (long long)mp->m_sb.sb_icount;
  1315. lcounter += delta;
  1316. if (lcounter < 0) {
  1317. ASSERT(0);
  1318. return XFS_ERROR(EINVAL);
  1319. }
  1320. mp->m_sb.sb_icount = lcounter;
  1321. return 0;
  1322. case XFS_SBS_IFREE:
  1323. lcounter = (long long)mp->m_sb.sb_ifree;
  1324. lcounter += delta;
  1325. if (lcounter < 0) {
  1326. ASSERT(0);
  1327. return XFS_ERROR(EINVAL);
  1328. }
  1329. mp->m_sb.sb_ifree = lcounter;
  1330. return 0;
  1331. case XFS_SBS_FDBLOCKS:
  1332. lcounter = (long long)
  1333. mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  1334. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1335. if (delta > 0) { /* Putting blocks back */
  1336. if (res_used > delta) {
  1337. mp->m_resblks_avail += delta;
  1338. } else {
  1339. rem = delta - res_used;
  1340. mp->m_resblks_avail = mp->m_resblks;
  1341. lcounter += rem;
  1342. }
  1343. } else { /* Taking blocks away */
  1344. lcounter += delta;
  1345. /*
  1346. * If were out of blocks, use any available reserved blocks if
  1347. * were allowed to.
  1348. */
  1349. if (lcounter < 0) {
  1350. if (rsvd) {
  1351. lcounter = (long long)mp->m_resblks_avail + delta;
  1352. if (lcounter < 0) {
  1353. return XFS_ERROR(ENOSPC);
  1354. }
  1355. mp->m_resblks_avail = lcounter;
  1356. return 0;
  1357. } else { /* not reserved */
  1358. return XFS_ERROR(ENOSPC);
  1359. }
  1360. }
  1361. }
  1362. mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
  1363. return 0;
  1364. case XFS_SBS_FREXTENTS:
  1365. lcounter = (long long)mp->m_sb.sb_frextents;
  1366. lcounter += delta;
  1367. if (lcounter < 0) {
  1368. return XFS_ERROR(ENOSPC);
  1369. }
  1370. mp->m_sb.sb_frextents = lcounter;
  1371. return 0;
  1372. case XFS_SBS_DBLOCKS:
  1373. lcounter = (long long)mp->m_sb.sb_dblocks;
  1374. lcounter += delta;
  1375. if (lcounter < 0) {
  1376. ASSERT(0);
  1377. return XFS_ERROR(EINVAL);
  1378. }
  1379. mp->m_sb.sb_dblocks = lcounter;
  1380. return 0;
  1381. case XFS_SBS_AGCOUNT:
  1382. scounter = mp->m_sb.sb_agcount;
  1383. scounter += delta;
  1384. if (scounter < 0) {
  1385. ASSERT(0);
  1386. return XFS_ERROR(EINVAL);
  1387. }
  1388. mp->m_sb.sb_agcount = scounter;
  1389. return 0;
  1390. case XFS_SBS_IMAX_PCT:
  1391. scounter = mp->m_sb.sb_imax_pct;
  1392. scounter += delta;
  1393. if (scounter < 0) {
  1394. ASSERT(0);
  1395. return XFS_ERROR(EINVAL);
  1396. }
  1397. mp->m_sb.sb_imax_pct = scounter;
  1398. return 0;
  1399. case XFS_SBS_REXTSIZE:
  1400. scounter = mp->m_sb.sb_rextsize;
  1401. scounter += delta;
  1402. if (scounter < 0) {
  1403. ASSERT(0);
  1404. return XFS_ERROR(EINVAL);
  1405. }
  1406. mp->m_sb.sb_rextsize = scounter;
  1407. return 0;
  1408. case XFS_SBS_RBMBLOCKS:
  1409. scounter = mp->m_sb.sb_rbmblocks;
  1410. scounter += delta;
  1411. if (scounter < 0) {
  1412. ASSERT(0);
  1413. return XFS_ERROR(EINVAL);
  1414. }
  1415. mp->m_sb.sb_rbmblocks = scounter;
  1416. return 0;
  1417. case XFS_SBS_RBLOCKS:
  1418. lcounter = (long long)mp->m_sb.sb_rblocks;
  1419. lcounter += delta;
  1420. if (lcounter < 0) {
  1421. ASSERT(0);
  1422. return XFS_ERROR(EINVAL);
  1423. }
  1424. mp->m_sb.sb_rblocks = lcounter;
  1425. return 0;
  1426. case XFS_SBS_REXTENTS:
  1427. lcounter = (long long)mp->m_sb.sb_rextents;
  1428. lcounter += delta;
  1429. if (lcounter < 0) {
  1430. ASSERT(0);
  1431. return XFS_ERROR(EINVAL);
  1432. }
  1433. mp->m_sb.sb_rextents = lcounter;
  1434. return 0;
  1435. case XFS_SBS_REXTSLOG:
  1436. scounter = mp->m_sb.sb_rextslog;
  1437. scounter += delta;
  1438. if (scounter < 0) {
  1439. ASSERT(0);
  1440. return XFS_ERROR(EINVAL);
  1441. }
  1442. mp->m_sb.sb_rextslog = scounter;
  1443. return 0;
  1444. default:
  1445. ASSERT(0);
  1446. return XFS_ERROR(EINVAL);
  1447. }
  1448. }
  1449. /*
  1450. * xfs_mod_incore_sb() is used to change a field in the in-core
  1451. * superblock structure by the specified delta. This modification
  1452. * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked()
  1453. * routine to do the work.
  1454. */
  1455. int
  1456. xfs_mod_incore_sb(
  1457. xfs_mount_t *mp,
  1458. xfs_sb_field_t field,
  1459. int64_t delta,
  1460. int rsvd)
  1461. {
  1462. unsigned long s;
  1463. int status;
  1464. /* check for per-cpu counters */
  1465. switch (field) {
  1466. #ifdef HAVE_PERCPU_SB
  1467. case XFS_SBS_ICOUNT:
  1468. case XFS_SBS_IFREE:
  1469. case XFS_SBS_FDBLOCKS:
  1470. if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
  1471. status = xfs_icsb_modify_counters(mp, field,
  1472. delta, rsvd);
  1473. break;
  1474. }
  1475. /* FALLTHROUGH */
  1476. #endif
  1477. default:
  1478. s = XFS_SB_LOCK(mp);
  1479. status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1480. XFS_SB_UNLOCK(mp, s);
  1481. break;
  1482. }
  1483. return status;
  1484. }
  1485. /*
  1486. * xfs_mod_incore_sb_batch() is used to change more than one field
  1487. * in the in-core superblock structure at a time. This modification
  1488. * is protected by a lock internal to this module. The fields and
  1489. * changes to those fields are specified in the array of xfs_mod_sb
  1490. * structures passed in.
  1491. *
  1492. * Either all of the specified deltas will be applied or none of
  1493. * them will. If any modified field dips below 0, then all modifications
  1494. * will be backed out and EINVAL will be returned.
  1495. */
  1496. int
  1497. xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
  1498. {
  1499. unsigned long s;
  1500. int status=0;
  1501. xfs_mod_sb_t *msbp;
  1502. /*
  1503. * Loop through the array of mod structures and apply each
  1504. * individually. If any fail, then back out all those
  1505. * which have already been applied. Do all of this within
  1506. * the scope of the SB_LOCK so that all of the changes will
  1507. * be atomic.
  1508. */
  1509. s = XFS_SB_LOCK(mp);
  1510. msbp = &msb[0];
  1511. for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
  1512. /*
  1513. * Apply the delta at index n. If it fails, break
  1514. * from the loop so we'll fall into the undo loop
  1515. * below.
  1516. */
  1517. switch (msbp->msb_field) {
  1518. #ifdef HAVE_PERCPU_SB
  1519. case XFS_SBS_ICOUNT:
  1520. case XFS_SBS_IFREE:
  1521. case XFS_SBS_FDBLOCKS:
  1522. if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
  1523. XFS_SB_UNLOCK(mp, s);
  1524. status = xfs_icsb_modify_counters(mp,
  1525. msbp->msb_field,
  1526. msbp->msb_delta, rsvd);
  1527. s = XFS_SB_LOCK(mp);
  1528. break;
  1529. }
  1530. /* FALLTHROUGH */
  1531. #endif
  1532. default:
  1533. status = xfs_mod_incore_sb_unlocked(mp,
  1534. msbp->msb_field,
  1535. msbp->msb_delta, rsvd);
  1536. break;
  1537. }
  1538. if (status != 0) {
  1539. break;
  1540. }
  1541. }
  1542. /*
  1543. * If we didn't complete the loop above, then back out
  1544. * any changes made to the superblock. If you add code
  1545. * between the loop above and here, make sure that you
  1546. * preserve the value of status. Loop back until
  1547. * we step below the beginning of the array. Make sure
  1548. * we don't touch anything back there.
  1549. */
  1550. if (status != 0) {
  1551. msbp--;
  1552. while (msbp >= msb) {
  1553. switch (msbp->msb_field) {
  1554. #ifdef HAVE_PERCPU_SB
  1555. case XFS_SBS_ICOUNT:
  1556. case XFS_SBS_IFREE:
  1557. case XFS_SBS_FDBLOCKS:
  1558. if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
  1559. XFS_SB_UNLOCK(mp, s);
  1560. status = xfs_icsb_modify_counters(mp,
  1561. msbp->msb_field,
  1562. -(msbp->msb_delta),
  1563. rsvd);
  1564. s = XFS_SB_LOCK(mp);
  1565. break;
  1566. }
  1567. /* FALLTHROUGH */
  1568. #endif
  1569. default:
  1570. status = xfs_mod_incore_sb_unlocked(mp,
  1571. msbp->msb_field,
  1572. -(msbp->msb_delta),
  1573. rsvd);
  1574. break;
  1575. }
  1576. ASSERT(status == 0);
  1577. msbp--;
  1578. }
  1579. }
  1580. XFS_SB_UNLOCK(mp, s);
  1581. return status;
  1582. }
  1583. /*
  1584. * xfs_getsb() is called to obtain the buffer for the superblock.
  1585. * The buffer is returned locked and read in from disk.
  1586. * The buffer should be released with a call to xfs_brelse().
  1587. *
  1588. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1589. * the superblock buffer if it can be locked without sleeping.
  1590. * If it can't then we'll return NULL.
  1591. */
  1592. xfs_buf_t *
  1593. xfs_getsb(
  1594. xfs_mount_t *mp,
  1595. int flags)
  1596. {
  1597. xfs_buf_t *bp;
  1598. ASSERT(mp->m_sb_bp != NULL);
  1599. bp = mp->m_sb_bp;
  1600. if (flags & XFS_BUF_TRYLOCK) {
  1601. if (!XFS_BUF_CPSEMA(bp)) {
  1602. return NULL;
  1603. }
  1604. } else {
  1605. XFS_BUF_PSEMA(bp, PRIBIO);
  1606. }
  1607. XFS_BUF_HOLD(bp);
  1608. ASSERT(XFS_BUF_ISDONE(bp));
  1609. return bp;
  1610. }
  1611. /*
  1612. * Used to free the superblock along various error paths.
  1613. */
  1614. void
  1615. xfs_freesb(
  1616. xfs_mount_t *mp)
  1617. {
  1618. xfs_buf_t *bp;
  1619. /*
  1620. * Use xfs_getsb() so that the buffer will be locked
  1621. * when we call xfs_buf_relse().
  1622. */
  1623. bp = xfs_getsb(mp, 0);
  1624. XFS_BUF_UNMANAGE(bp);
  1625. xfs_buf_relse(bp);
  1626. mp->m_sb_bp = NULL;
  1627. }
  1628. /*
  1629. * See if the UUID is unique among mounted XFS filesystems.
  1630. * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
  1631. */
  1632. STATIC int
  1633. xfs_uuid_mount(
  1634. xfs_mount_t *mp)
  1635. {
  1636. if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
  1637. cmn_err(CE_WARN,
  1638. "XFS: Filesystem %s has nil UUID - can't mount",
  1639. mp->m_fsname);
  1640. return -1;
  1641. }
  1642. if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
  1643. cmn_err(CE_WARN,
  1644. "XFS: Filesystem %s has duplicate UUID - can't mount",
  1645. mp->m_fsname);
  1646. return -1;
  1647. }
  1648. return 0;
  1649. }
  1650. /*
  1651. * Remove filesystem from the UUID table.
  1652. */
  1653. STATIC void
  1654. xfs_uuid_unmount(
  1655. xfs_mount_t *mp)
  1656. {
  1657. uuid_table_remove(&mp->m_sb.sb_uuid);
  1658. }
  1659. /*
  1660. * Used to log changes to the superblock unit and width fields which could
  1661. * be altered by the mount options. Only the first superblock is updated.
  1662. */
  1663. STATIC void
  1664. xfs_mount_log_sbunit(
  1665. xfs_mount_t *mp,
  1666. __int64_t fields)
  1667. {
  1668. xfs_trans_t *tp;
  1669. ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
  1670. tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
  1671. if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  1672. XFS_DEFAULT_LOG_COUNT)) {
  1673. xfs_trans_cancel(tp, 0);
  1674. return;
  1675. }
  1676. xfs_mod_sb(tp, fields);
  1677. xfs_trans_commit(tp, 0);
  1678. }
  1679. #ifdef HAVE_PERCPU_SB
  1680. /*
  1681. * Per-cpu incore superblock counters
  1682. *
  1683. * Simple concept, difficult implementation
  1684. *
  1685. * Basically, replace the incore superblock counters with a distributed per cpu
  1686. * counter for contended fields (e.g. free block count).
  1687. *
  1688. * Difficulties arise in that the incore sb is used for ENOSPC checking, and
  1689. * hence needs to be accurately read when we are running low on space. Hence
  1690. * there is a method to enable and disable the per-cpu counters based on how
  1691. * much "stuff" is available in them.
  1692. *
  1693. * Basically, a counter is enabled if there is enough free resource to justify
  1694. * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
  1695. * ENOSPC), then we disable the counters to synchronise all callers and
  1696. * re-distribute the available resources.
  1697. *
  1698. * If, once we redistributed the available resources, we still get a failure,
  1699. * we disable the per-cpu counter and go through the slow path.
  1700. *
  1701. * The slow path is the current xfs_mod_incore_sb() function. This means that
  1702. * when we disable a per-cpu counter, we need to drain it's resources back to
  1703. * the global superblock. We do this after disabling the counter to prevent
  1704. * more threads from queueing up on the counter.
  1705. *
  1706. * Essentially, this means that we still need a lock in the fast path to enable
  1707. * synchronisation between the global counters and the per-cpu counters. This
  1708. * is not a problem because the lock will be local to a CPU almost all the time
  1709. * and have little contention except when we get to ENOSPC conditions.
  1710. *
  1711. * Basically, this lock becomes a barrier that enables us to lock out the fast
  1712. * path while we do things like enabling and disabling counters and
  1713. * synchronising the counters.
  1714. *
  1715. * Locking rules:
  1716. *
  1717. * 1. XFS_SB_LOCK() before picking up per-cpu locks
  1718. * 2. per-cpu locks always picked up via for_each_online_cpu() order
  1719. * 3. accurate counter sync requires XFS_SB_LOCK + per cpu locks
  1720. * 4. modifying per-cpu counters requires holding per-cpu lock
  1721. * 5. modifying global counters requires holding XFS_SB_LOCK
  1722. * 6. enabling or disabling a counter requires holding the XFS_SB_LOCK
  1723. * and _none_ of the per-cpu locks.
  1724. *
  1725. * Disabled counters are only ever re-enabled by a balance operation
  1726. * that results in more free resources per CPU than a given threshold.
  1727. * To ensure counters don't remain disabled, they are rebalanced when
  1728. * the global resource goes above a higher threshold (i.e. some hysteresis
  1729. * is present to prevent thrashing).
  1730. */
  1731. #ifdef CONFIG_HOTPLUG_CPU
  1732. /*
  1733. * hot-plug CPU notifier support.
  1734. *
  1735. * We need a notifier per filesystem as we need to be able to identify
  1736. * the filesystem to balance the counters out. This is achieved by
  1737. * having a notifier block embedded in the xfs_mount_t and doing pointer
  1738. * magic to get the mount pointer from the notifier block address.
  1739. */
  1740. STATIC int
  1741. xfs_icsb_cpu_notify(
  1742. struct notifier_block *nfb,
  1743. unsigned long action,
  1744. void *hcpu)
  1745. {
  1746. xfs_icsb_cnts_t *cntp;
  1747. xfs_mount_t *mp;
  1748. int s;
  1749. mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
  1750. cntp = (xfs_icsb_cnts_t *)
  1751. per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
  1752. switch (action) {
  1753. case CPU_UP_PREPARE:
  1754. case CPU_UP_PREPARE_FROZEN:
  1755. /* Easy Case - initialize the area and locks, and
  1756. * then rebalance when online does everything else for us. */
  1757. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1758. break;
  1759. case CPU_ONLINE:
  1760. case CPU_ONLINE_FROZEN:
  1761. xfs_icsb_lock(mp);
  1762. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0, 0);
  1763. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0, 0);
  1764. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0, 0);
  1765. xfs_icsb_unlock(mp);
  1766. break;
  1767. case CPU_DEAD:
  1768. case CPU_DEAD_FROZEN:
  1769. /* Disable all the counters, then fold the dead cpu's
  1770. * count into the total on the global superblock and
  1771. * re-enable the counters. */
  1772. xfs_icsb_lock(mp);
  1773. s = XFS_SB_LOCK(mp);
  1774. xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
  1775. xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
  1776. xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
  1777. mp->m_sb.sb_icount += cntp->icsb_icount;
  1778. mp->m_sb.sb_ifree += cntp->icsb_ifree;
  1779. mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
  1780. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1781. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT,
  1782. XFS_ICSB_SB_LOCKED, 0);
  1783. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE,
  1784. XFS_ICSB_SB_LOCKED, 0);
  1785. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS,
  1786. XFS_ICSB_SB_LOCKED, 0);
  1787. XFS_SB_UNLOCK(mp, s);
  1788. xfs_icsb_unlock(mp);
  1789. break;
  1790. }
  1791. return NOTIFY_OK;
  1792. }
  1793. #endif /* CONFIG_HOTPLUG_CPU */
  1794. int
  1795. xfs_icsb_init_counters(
  1796. xfs_mount_t *mp)
  1797. {
  1798. xfs_icsb_cnts_t *cntp;
  1799. int i;
  1800. mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
  1801. if (mp->m_sb_cnts == NULL)
  1802. return -ENOMEM;
  1803. #ifdef CONFIG_HOTPLUG_CPU
  1804. mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
  1805. mp->m_icsb_notifier.priority = 0;
  1806. register_hotcpu_notifier(&mp->m_icsb_notifier);
  1807. #endif /* CONFIG_HOTPLUG_CPU */
  1808. for_each_online_cpu(i) {
  1809. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1810. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1811. }
  1812. mutex_init(&mp->m_icsb_mutex);
  1813. /*
  1814. * start with all counters disabled so that the
  1815. * initial balance kicks us off correctly
  1816. */
  1817. mp->m_icsb_counters = -1;
  1818. return 0;
  1819. }
  1820. void
  1821. xfs_icsb_reinit_counters(
  1822. xfs_mount_t *mp)
  1823. {
  1824. xfs_icsb_lock(mp);
  1825. /*
  1826. * start with all counters disabled so that the
  1827. * initial balance kicks us off correctly
  1828. */
  1829. mp->m_icsb_counters = -1;
  1830. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0, 0);
  1831. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0, 0);
  1832. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0, 0);
  1833. xfs_icsb_unlock(mp);
  1834. }
  1835. STATIC void
  1836. xfs_icsb_destroy_counters(
  1837. xfs_mount_t *mp)
  1838. {
  1839. if (mp->m_sb_cnts) {
  1840. unregister_hotcpu_notifier(&mp->m_icsb_notifier);
  1841. free_percpu(mp->m_sb_cnts);
  1842. }
  1843. mutex_destroy(&mp->m_icsb_mutex);
  1844. }
  1845. STATIC_INLINE void
  1846. xfs_icsb_lock_cntr(
  1847. xfs_icsb_cnts_t *icsbp)
  1848. {
  1849. while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
  1850. ndelay(1000);
  1851. }
  1852. }
  1853. STATIC_INLINE void
  1854. xfs_icsb_unlock_cntr(
  1855. xfs_icsb_cnts_t *icsbp)
  1856. {
  1857. clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
  1858. }
  1859. STATIC_INLINE void
  1860. xfs_icsb_lock_all_counters(
  1861. xfs_mount_t *mp)
  1862. {
  1863. xfs_icsb_cnts_t *cntp;
  1864. int i;
  1865. for_each_online_cpu(i) {
  1866. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1867. xfs_icsb_lock_cntr(cntp);
  1868. }
  1869. }
  1870. STATIC_INLINE void
  1871. xfs_icsb_unlock_all_counters(
  1872. xfs_mount_t *mp)
  1873. {
  1874. xfs_icsb_cnts_t *cntp;
  1875. int i;
  1876. for_each_online_cpu(i) {
  1877. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1878. xfs_icsb_unlock_cntr(cntp);
  1879. }
  1880. }
  1881. STATIC void
  1882. xfs_icsb_count(
  1883. xfs_mount_t *mp,
  1884. xfs_icsb_cnts_t *cnt,
  1885. int flags)
  1886. {
  1887. xfs_icsb_cnts_t *cntp;
  1888. int i;
  1889. memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
  1890. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1891. xfs_icsb_lock_all_counters(mp);
  1892. for_each_online_cpu(i) {
  1893. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1894. cnt->icsb_icount += cntp->icsb_icount;
  1895. cnt->icsb_ifree += cntp->icsb_ifree;
  1896. cnt->icsb_fdblocks += cntp->icsb_fdblocks;
  1897. }
  1898. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1899. xfs_icsb_unlock_all_counters(mp);
  1900. }
  1901. STATIC int
  1902. xfs_icsb_counter_disabled(
  1903. xfs_mount_t *mp,
  1904. xfs_sb_field_t field)
  1905. {
  1906. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1907. return test_bit(field, &mp->m_icsb_counters);
  1908. }
  1909. STATIC int
  1910. xfs_icsb_disable_counter(
  1911. xfs_mount_t *mp,
  1912. xfs_sb_field_t field)
  1913. {
  1914. xfs_icsb_cnts_t cnt;
  1915. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1916. /*
  1917. * If we are already disabled, then there is nothing to do
  1918. * here. We check before locking all the counters to avoid
  1919. * the expensive lock operation when being called in the
  1920. * slow path and the counter is already disabled. This is
  1921. * safe because the only time we set or clear this state is under
  1922. * the m_icsb_mutex.
  1923. */
  1924. if (xfs_icsb_counter_disabled(mp, field))
  1925. return 0;
  1926. xfs_icsb_lock_all_counters(mp);
  1927. if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
  1928. /* drain back to superblock */
  1929. xfs_icsb_count(mp, &cnt, XFS_ICSB_SB_LOCKED|XFS_ICSB_LAZY_COUNT);
  1930. switch(field) {
  1931. case XFS_SBS_ICOUNT:
  1932. mp->m_sb.sb_icount = cnt.icsb_icount;
  1933. break;
  1934. case XFS_SBS_IFREE:
  1935. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1936. break;
  1937. case XFS_SBS_FDBLOCKS:
  1938. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1939. break;
  1940. default:
  1941. BUG();
  1942. }
  1943. }
  1944. xfs_icsb_unlock_all_counters(mp);
  1945. return 0;
  1946. }
  1947. STATIC void
  1948. xfs_icsb_enable_counter(
  1949. xfs_mount_t *mp,
  1950. xfs_sb_field_t field,
  1951. uint64_t count,
  1952. uint64_t resid)
  1953. {
  1954. xfs_icsb_cnts_t *cntp;
  1955. int i;
  1956. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1957. xfs_icsb_lock_all_counters(mp);
  1958. for_each_online_cpu(i) {
  1959. cntp = per_cpu_ptr(mp->m_sb_cnts, i);
  1960. switch (field) {
  1961. case XFS_SBS_ICOUNT:
  1962. cntp->icsb_icount = count + resid;
  1963. break;
  1964. case XFS_SBS_IFREE:
  1965. cntp->icsb_ifree = count + resid;
  1966. break;
  1967. case XFS_SBS_FDBLOCKS:
  1968. cntp->icsb_fdblocks = count + resid;
  1969. break;
  1970. default:
  1971. BUG();
  1972. break;
  1973. }
  1974. resid = 0;
  1975. }
  1976. clear_bit(field, &mp->m_icsb_counters);
  1977. xfs_icsb_unlock_all_counters(mp);
  1978. }
  1979. void
  1980. xfs_icsb_sync_counters_flags(
  1981. xfs_mount_t *mp,
  1982. int flags)
  1983. {
  1984. xfs_icsb_cnts_t cnt;
  1985. int s;
  1986. /* Pass 1: lock all counters */
  1987. if ((flags & XFS_ICSB_SB_LOCKED) == 0)
  1988. s = XFS_SB_LOCK(mp);
  1989. xfs_icsb_count(mp, &cnt, flags);
  1990. /* Step 3: update mp->m_sb fields */
  1991. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
  1992. mp->m_sb.sb_icount = cnt.icsb_icount;
  1993. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
  1994. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1995. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
  1996. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1997. if ((flags & XFS_ICSB_SB_LOCKED) == 0)
  1998. XFS_SB_UNLOCK(mp, s);
  1999. }
  2000. /*
  2001. * Accurate update of per-cpu counters to incore superblock
  2002. */
  2003. STATIC void
  2004. xfs_icsb_sync_counters(
  2005. xfs_mount_t *mp)
  2006. {
  2007. xfs_icsb_sync_counters_flags(mp, 0);
  2008. }
  2009. /*
  2010. * Balance and enable/disable counters as necessary.
  2011. *
  2012. * Thresholds for re-enabling counters are somewhat magic. inode counts are
  2013. * chosen to be the same number as single on disk allocation chunk per CPU, and
  2014. * free blocks is something far enough zero that we aren't going thrash when we
  2015. * get near ENOSPC. We also need to supply a minimum we require per cpu to
  2016. * prevent looping endlessly when xfs_alloc_space asks for more than will
  2017. * be distributed to a single CPU but each CPU has enough blocks to be
  2018. * reenabled.
  2019. *
  2020. * Note that we can be called when counters are already disabled.
  2021. * xfs_icsb_disable_counter() optimises the counter locking in this case to
  2022. * prevent locking every per-cpu counter needlessly.
  2023. */
  2024. #define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
  2025. #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
  2026. (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
  2027. STATIC void
  2028. xfs_icsb_balance_counter(
  2029. xfs_mount_t *mp,
  2030. xfs_sb_field_t field,
  2031. int flags,
  2032. int min_per_cpu)
  2033. {
  2034. uint64_t count, resid;
  2035. int weight = num_online_cpus();
  2036. int s;
  2037. uint64_t min = (uint64_t)min_per_cpu;
  2038. if (!(flags & XFS_ICSB_SB_LOCKED))
  2039. s = XFS_SB_LOCK(mp);
  2040. /* disable counter and sync counter */
  2041. xfs_icsb_disable_counter(mp, field);
  2042. /* update counters - first CPU gets residual*/
  2043. switch (field) {
  2044. case XFS_SBS_ICOUNT:
  2045. count = mp->m_sb.sb_icount;
  2046. resid = do_div(count, weight);
  2047. if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
  2048. goto out;
  2049. break;
  2050. case XFS_SBS_IFREE:
  2051. count = mp->m_sb.sb_ifree;
  2052. resid = do_div(count, weight);
  2053. if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
  2054. goto out;
  2055. break;
  2056. case XFS_SBS_FDBLOCKS:
  2057. count = mp->m_sb.sb_fdblocks;
  2058. resid = do_div(count, weight);
  2059. if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
  2060. goto out;
  2061. break;
  2062. default:
  2063. BUG();
  2064. count = resid = 0; /* quiet, gcc */
  2065. break;
  2066. }
  2067. xfs_icsb_enable_counter(mp, field, count, resid);
  2068. out:
  2069. if (!(flags & XFS_ICSB_SB_LOCKED))
  2070. XFS_SB_UNLOCK(mp, s);
  2071. }
  2072. int
  2073. xfs_icsb_modify_counters(
  2074. xfs_mount_t *mp,
  2075. xfs_sb_field_t field,
  2076. int64_t delta,
  2077. int rsvd)
  2078. {
  2079. xfs_icsb_cnts_t *icsbp;
  2080. long long lcounter; /* long counter for 64 bit fields */
  2081. int cpu, ret = 0, s;
  2082. might_sleep();
  2083. again:
  2084. cpu = get_cpu();
  2085. icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu);
  2086. /*
  2087. * if the counter is disabled, go to slow path
  2088. */
  2089. if (unlikely(xfs_icsb_counter_disabled(mp, field)))
  2090. goto slow_path;
  2091. xfs_icsb_lock_cntr(icsbp);
  2092. if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
  2093. xfs_icsb_unlock_cntr(icsbp);
  2094. goto slow_path;
  2095. }
  2096. switch (field) {
  2097. case XFS_SBS_ICOUNT:
  2098. lcounter = icsbp->icsb_icount;
  2099. lcounter += delta;
  2100. if (unlikely(lcounter < 0))
  2101. goto balance_counter;
  2102. icsbp->icsb_icount = lcounter;
  2103. break;
  2104. case XFS_SBS_IFREE:
  2105. lcounter = icsbp->icsb_ifree;
  2106. lcounter += delta;
  2107. if (unlikely(lcounter < 0))
  2108. goto balance_counter;
  2109. icsbp->icsb_ifree = lcounter;
  2110. break;
  2111. case XFS_SBS_FDBLOCKS:
  2112. BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
  2113. lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  2114. lcounter += delta;
  2115. if (unlikely(lcounter < 0))
  2116. goto balance_counter;
  2117. icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
  2118. break;
  2119. default:
  2120. BUG();
  2121. break;
  2122. }
  2123. xfs_icsb_unlock_cntr(icsbp);
  2124. put_cpu();
  2125. return 0;
  2126. slow_path:
  2127. put_cpu();
  2128. /*
  2129. * serialise with a mutex so we don't burn lots of cpu on
  2130. * the superblock lock. We still need to hold the superblock
  2131. * lock, however, when we modify the global structures.
  2132. */
  2133. xfs_icsb_lock(mp);
  2134. /*
  2135. * Now running atomically.
  2136. *
  2137. * If the counter is enabled, someone has beaten us to rebalancing.
  2138. * Drop the lock and try again in the fast path....
  2139. */
  2140. if (!(xfs_icsb_counter_disabled(mp, field))) {
  2141. xfs_icsb_unlock(mp);
  2142. goto again;
  2143. }
  2144. /*
  2145. * The counter is currently disabled. Because we are
  2146. * running atomically here, we know a rebalance cannot
  2147. * be in progress. Hence we can go straight to operating
  2148. * on the global superblock. We do not call xfs_mod_incore_sb()
  2149. * here even though we need to get the SB_LOCK. Doing so
  2150. * will cause us to re-enter this function and deadlock.
  2151. * Hence we get the SB_LOCK ourselves and then call
  2152. * xfs_mod_incore_sb_unlocked() as the unlocked path operates
  2153. * directly on the global counters.
  2154. */
  2155. s = XFS_SB_LOCK(mp);
  2156. ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  2157. XFS_SB_UNLOCK(mp, s);
  2158. /*
  2159. * Now that we've modified the global superblock, we
  2160. * may be able to re-enable the distributed counters
  2161. * (e.g. lots of space just got freed). After that
  2162. * we are done.
  2163. */
  2164. if (ret != ENOSPC)
  2165. xfs_icsb_balance_counter(mp, field, 0, 0);
  2166. xfs_icsb_unlock(mp);
  2167. return ret;
  2168. balance_counter:
  2169. xfs_icsb_unlock_cntr(icsbp);
  2170. put_cpu();
  2171. /*
  2172. * We may have multiple threads here if multiple per-cpu
  2173. * counters run dry at the same time. This will mean we can
  2174. * do more balances than strictly necessary but it is not
  2175. * the common slowpath case.
  2176. */
  2177. xfs_icsb_lock(mp);
  2178. /*
  2179. * running atomically.
  2180. *
  2181. * This will leave the counter in the correct state for future
  2182. * accesses. After the rebalance, we simply try again and our retry
  2183. * will either succeed through the fast path or slow path without
  2184. * another balance operation being required.
  2185. */
  2186. xfs_icsb_balance_counter(mp, field, 0, delta);
  2187. xfs_icsb_unlock(mp);
  2188. goto again;
  2189. }
  2190. #endif