xfs_mount.c 66 KB

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