xfs_mount.c 65 KB

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