xfs_mount.c 67 KB

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