xfs_mount.c 70 KB

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