xfs_mount.c 56 KB

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