xfs_mount.c 69 KB

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