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