xfs_mount.c 66 KB

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