xfs_mount.c 64 KB

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