xfs_mount.c 68 KB

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