xfs_mount.c 72 KB

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