xfs_mount.c 66 KB

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