xfs_mount.c 56 KB

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