xfs_mount.c 73 KB

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