xfs_mount.c 70 KB

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