xfs_mount.c 66 KB

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