xfs_mount.c 73 KB

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