xfs_mount.c 69 KB

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