xfs_mount.c 66 KB

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