xfs_mount.c 68 KB

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