xfs_mount.c 67 KB

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