xfs_mount.c 67 KB

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