xfs_mount.c 66 KB

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