xfs_mount.c 64 KB

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