xfs_mount.c 67 KB

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