xfs_mount.c 66 KB

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