xfs_mount.c 57 KB

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