xfs_mount.c 62 KB

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