xfs_mount.c 66 KB

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