xfs_mount.c 65 KB

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