xfs_mount.c 65 KB

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