xfs_mount.c 68 KB

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