xfs_mount.c 68 KB

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