xfs_mount.c 71 KB

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