xfs_mount.c 66 KB

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