xfs_mount.c 64 KB

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