xfs_mount.c 67 KB

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