xfs_qm.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798
  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_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_btree.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_itable.h"
  41. #include "xfs_rtalloc.h"
  42. #include "xfs_error.h"
  43. #include "xfs_bmap.h"
  44. #include "xfs_rw.h"
  45. #include "xfs_acl.h"
  46. #include "xfs_attr.h"
  47. #include "xfs_buf_item.h"
  48. #include "xfs_trans_space.h"
  49. #include "xfs_utils.h"
  50. #include "xfs_qm.h"
  51. /*
  52. * The global quota manager. There is only one of these for the entire
  53. * system, _not_ one per file system. XQM keeps track of the overall
  54. * quota functionality, including maintaining the freelist and hash
  55. * tables of dquots.
  56. */
  57. mutex_t xfs_Gqm_lock;
  58. struct xfs_qm *xfs_Gqm;
  59. uint ndquot;
  60. kmem_zone_t *qm_dqzone;
  61. kmem_zone_t *qm_dqtrxzone;
  62. static cred_t xfs_zerocr;
  63. STATIC void xfs_qm_list_init(xfs_dqlist_t *, char *, int);
  64. STATIC void xfs_qm_list_destroy(xfs_dqlist_t *);
  65. STATIC void xfs_qm_freelist_init(xfs_frlist_t *);
  66. STATIC void xfs_qm_freelist_destroy(xfs_frlist_t *);
  67. STATIC int xfs_qm_mplist_nowait(xfs_mount_t *);
  68. STATIC int xfs_qm_dqhashlock_nowait(xfs_dquot_t *);
  69. STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
  70. STATIC int xfs_qm_init_quotainfo(xfs_mount_t *);
  71. STATIC int xfs_qm_shake(int, gfp_t);
  72. static struct shrinker xfs_qm_shaker = {
  73. .shrink = xfs_qm_shake,
  74. .seeks = DEFAULT_SEEKS,
  75. };
  76. #ifdef DEBUG
  77. extern mutex_t qcheck_lock;
  78. #endif
  79. #ifdef QUOTADEBUG
  80. #define XQM_LIST_PRINT(l, NXT, title) \
  81. { \
  82. xfs_dquot_t *dqp; int i = 0; \
  83. cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
  84. for (dqp = (l)->qh_next; dqp != NULL; dqp = dqp->NXT) { \
  85. cmn_err(CE_DEBUG, " %d. \"%d (%s)\" " \
  86. "bcnt = %d, icnt = %d, refs = %d", \
  87. ++i, (int) be32_to_cpu(dqp->q_core.d_id), \
  88. DQFLAGTO_TYPESTR(dqp), \
  89. (int) be64_to_cpu(dqp->q_core.d_bcount), \
  90. (int) be64_to_cpu(dqp->q_core.d_icount), \
  91. (int) dqp->q_nrefs); } \
  92. }
  93. #else
  94. #define XQM_LIST_PRINT(l, NXT, title) do { } while (0)
  95. #endif
  96. /*
  97. * Initialize the XQM structure.
  98. * Note that there is not one quota manager per file system.
  99. */
  100. STATIC struct xfs_qm *
  101. xfs_Gqm_init(void)
  102. {
  103. xfs_dqhash_t *udqhash, *gdqhash;
  104. xfs_qm_t *xqm;
  105. size_t hsize;
  106. uint i;
  107. /*
  108. * Initialize the dquot hash tables.
  109. */
  110. udqhash = kmem_zalloc_greedy(&hsize,
  111. XFS_QM_HASHSIZE_LOW * sizeof(xfs_dqhash_t),
  112. XFS_QM_HASHSIZE_HIGH * sizeof(xfs_dqhash_t),
  113. KM_SLEEP | KM_MAYFAIL | KM_LARGE);
  114. gdqhash = kmem_zalloc(hsize, KM_SLEEP | KM_LARGE);
  115. hsize /= sizeof(xfs_dqhash_t);
  116. ndquot = hsize << 8;
  117. xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
  118. xqm->qm_dqhashmask = hsize - 1;
  119. xqm->qm_usr_dqhtable = udqhash;
  120. xqm->qm_grp_dqhtable = gdqhash;
  121. ASSERT(xqm->qm_usr_dqhtable != NULL);
  122. ASSERT(xqm->qm_grp_dqhtable != NULL);
  123. for (i = 0; i < hsize; i++) {
  124. xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i);
  125. xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i);
  126. }
  127. /*
  128. * Freelist of all dquots of all file systems
  129. */
  130. xfs_qm_freelist_init(&(xqm->qm_dqfreelist));
  131. /*
  132. * dquot zone. we register our own low-memory callback.
  133. */
  134. if (!qm_dqzone) {
  135. xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t),
  136. "xfs_dquots");
  137. qm_dqzone = xqm->qm_dqzone;
  138. } else
  139. xqm->qm_dqzone = qm_dqzone;
  140. register_shrinker(&xfs_qm_shaker);
  141. /*
  142. * The t_dqinfo portion of transactions.
  143. */
  144. if (!qm_dqtrxzone) {
  145. xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t),
  146. "xfs_dqtrx");
  147. qm_dqtrxzone = xqm->qm_dqtrxzone;
  148. } else
  149. xqm->qm_dqtrxzone = qm_dqtrxzone;
  150. atomic_set(&xqm->qm_totaldquots, 0);
  151. xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO;
  152. xqm->qm_nrefs = 0;
  153. #ifdef DEBUG
  154. mutex_init(&qcheck_lock);
  155. #endif
  156. return xqm;
  157. }
  158. /*
  159. * Destroy the global quota manager when its reference count goes to zero.
  160. */
  161. STATIC void
  162. xfs_qm_destroy(
  163. struct xfs_qm *xqm)
  164. {
  165. int hsize, i;
  166. ASSERT(xqm != NULL);
  167. ASSERT(xqm->qm_nrefs == 0);
  168. unregister_shrinker(&xfs_qm_shaker);
  169. hsize = xqm->qm_dqhashmask + 1;
  170. for (i = 0; i < hsize; i++) {
  171. xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i]));
  172. xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i]));
  173. }
  174. kmem_free(xqm->qm_usr_dqhtable);
  175. kmem_free(xqm->qm_grp_dqhtable);
  176. xqm->qm_usr_dqhtable = NULL;
  177. xqm->qm_grp_dqhtable = NULL;
  178. xqm->qm_dqhashmask = 0;
  179. xfs_qm_freelist_destroy(&(xqm->qm_dqfreelist));
  180. #ifdef DEBUG
  181. mutex_destroy(&qcheck_lock);
  182. #endif
  183. kmem_free(xqm);
  184. }
  185. /*
  186. * Called at mount time to let XQM know that another file system is
  187. * starting quotas. This isn't crucial information as the individual mount
  188. * structures are pretty independent, but it helps the XQM keep a
  189. * global view of what's going on.
  190. */
  191. /* ARGSUSED */
  192. STATIC int
  193. xfs_qm_hold_quotafs_ref(
  194. struct xfs_mount *mp)
  195. {
  196. /*
  197. * Need to lock the xfs_Gqm structure for things like this. For example,
  198. * the structure could disappear between the entry to this routine and
  199. * a HOLD operation if not locked.
  200. */
  201. XFS_QM_LOCK(xfs_Gqm);
  202. if (xfs_Gqm == NULL)
  203. xfs_Gqm = xfs_Gqm_init();
  204. /*
  205. * We can keep a list of all filesystems with quotas mounted for
  206. * debugging and statistical purposes, but ...
  207. * Just take a reference and get out.
  208. */
  209. XFS_QM_HOLD(xfs_Gqm);
  210. XFS_QM_UNLOCK(xfs_Gqm);
  211. return 0;
  212. }
  213. /*
  214. * Release the reference that a filesystem took at mount time,
  215. * so that we know when we need to destroy the entire quota manager.
  216. */
  217. /* ARGSUSED */
  218. STATIC void
  219. xfs_qm_rele_quotafs_ref(
  220. struct xfs_mount *mp)
  221. {
  222. xfs_dquot_t *dqp, *nextdqp;
  223. ASSERT(xfs_Gqm);
  224. ASSERT(xfs_Gqm->qm_nrefs > 0);
  225. /*
  226. * Go thru the freelist and destroy all inactive dquots.
  227. */
  228. xfs_qm_freelist_lock(xfs_Gqm);
  229. for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
  230. dqp != (xfs_dquot_t *)&(xfs_Gqm->qm_dqfreelist); ) {
  231. xfs_dqlock(dqp);
  232. nextdqp = dqp->dq_flnext;
  233. if (dqp->dq_flags & XFS_DQ_INACTIVE) {
  234. ASSERT(dqp->q_mount == NULL);
  235. ASSERT(! XFS_DQ_IS_DIRTY(dqp));
  236. ASSERT(dqp->HL_PREVP == NULL);
  237. ASSERT(dqp->MPL_PREVP == NULL);
  238. XQM_FREELIST_REMOVE(dqp);
  239. xfs_dqunlock(dqp);
  240. xfs_qm_dqdestroy(dqp);
  241. } else {
  242. xfs_dqunlock(dqp);
  243. }
  244. dqp = nextdqp;
  245. }
  246. xfs_qm_freelist_unlock(xfs_Gqm);
  247. /*
  248. * Destroy the entire XQM. If somebody mounts with quotaon, this'll
  249. * be restarted.
  250. */
  251. XFS_QM_LOCK(xfs_Gqm);
  252. XFS_QM_RELE(xfs_Gqm);
  253. if (xfs_Gqm->qm_nrefs == 0) {
  254. xfs_qm_destroy(xfs_Gqm);
  255. xfs_Gqm = NULL;
  256. }
  257. XFS_QM_UNLOCK(xfs_Gqm);
  258. }
  259. /*
  260. * Just destroy the quotainfo structure.
  261. */
  262. void
  263. xfs_qm_unmount_quotadestroy(
  264. xfs_mount_t *mp)
  265. {
  266. if (mp->m_quotainfo)
  267. xfs_qm_destroy_quotainfo(mp);
  268. }
  269. /*
  270. * This is called from xfs_mountfs to start quotas and initialize all
  271. * necessary data structures like quotainfo. This is also responsible for
  272. * running a quotacheck as necessary. We are guaranteed that the superblock
  273. * is consistently read in at this point.
  274. *
  275. * If we fail here, the mount will continue with quota turned off. We don't
  276. * need to inidicate success or failure at all.
  277. */
  278. void
  279. xfs_qm_mount_quotas(
  280. xfs_mount_t *mp)
  281. {
  282. int error = 0;
  283. uint sbf;
  284. /*
  285. * If quotas on realtime volumes is not supported, we disable
  286. * quotas immediately.
  287. */
  288. if (mp->m_sb.sb_rextents) {
  289. cmn_err(CE_NOTE,
  290. "Cannot turn on quotas for realtime filesystem %s",
  291. mp->m_fsname);
  292. mp->m_qflags = 0;
  293. goto write_changes;
  294. }
  295. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  296. /*
  297. * Allocate the quotainfo structure inside the mount struct, and
  298. * create quotainode(s), and change/rev superblock if necessary.
  299. */
  300. error = xfs_qm_init_quotainfo(mp);
  301. if (error) {
  302. /*
  303. * We must turn off quotas.
  304. */
  305. ASSERT(mp->m_quotainfo == NULL);
  306. mp->m_qflags = 0;
  307. goto write_changes;
  308. }
  309. /*
  310. * If any of the quotas are not consistent, do a quotacheck.
  311. */
  312. if (XFS_QM_NEED_QUOTACHECK(mp)) {
  313. error = xfs_qm_quotacheck(mp);
  314. if (error) {
  315. /* Quotacheck failed and disabled quotas. */
  316. return;
  317. }
  318. }
  319. /*
  320. * If one type of quotas is off, then it will lose its
  321. * quotachecked status, since we won't be doing accounting for
  322. * that type anymore.
  323. */
  324. if (!XFS_IS_UQUOTA_ON(mp))
  325. mp->m_qflags &= ~XFS_UQUOTA_CHKD;
  326. if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
  327. mp->m_qflags &= ~XFS_OQUOTA_CHKD;
  328. write_changes:
  329. /*
  330. * We actually don't have to acquire the m_sb_lock at all.
  331. * This can only be called from mount, and that's single threaded. XXX
  332. */
  333. spin_lock(&mp->m_sb_lock);
  334. sbf = mp->m_sb.sb_qflags;
  335. mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
  336. spin_unlock(&mp->m_sb_lock);
  337. if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
  338. if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
  339. /*
  340. * We could only have been turning quotas off.
  341. * We aren't in very good shape actually because
  342. * the incore structures are convinced that quotas are
  343. * off, but the on disk superblock doesn't know that !
  344. */
  345. ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
  346. xfs_fs_cmn_err(CE_ALERT, mp,
  347. "XFS mount_quotas: Superblock update failed!");
  348. }
  349. }
  350. if (error) {
  351. xfs_fs_cmn_err(CE_WARN, mp,
  352. "Failed to initialize disk quotas.");
  353. }
  354. return;
  355. }
  356. /*
  357. * Called from the vfsops layer.
  358. */
  359. void
  360. xfs_qm_unmount_quotas(
  361. xfs_mount_t *mp)
  362. {
  363. /*
  364. * Release the dquots that root inode, et al might be holding,
  365. * before we flush quotas and blow away the quotainfo structure.
  366. */
  367. ASSERT(mp->m_rootip);
  368. xfs_qm_dqdetach(mp->m_rootip);
  369. if (mp->m_rbmip)
  370. xfs_qm_dqdetach(mp->m_rbmip);
  371. if (mp->m_rsumip)
  372. xfs_qm_dqdetach(mp->m_rsumip);
  373. /*
  374. * Release the quota inodes.
  375. */
  376. if (mp->m_quotainfo) {
  377. if (mp->m_quotainfo->qi_uquotaip) {
  378. IRELE(mp->m_quotainfo->qi_uquotaip);
  379. mp->m_quotainfo->qi_uquotaip = NULL;
  380. }
  381. if (mp->m_quotainfo->qi_gquotaip) {
  382. IRELE(mp->m_quotainfo->qi_gquotaip);
  383. mp->m_quotainfo->qi_gquotaip = NULL;
  384. }
  385. }
  386. }
  387. /*
  388. * Flush all dquots of the given file system to disk. The dquots are
  389. * _not_ purged from memory here, just their data written to disk.
  390. */
  391. STATIC int
  392. xfs_qm_dqflush_all(
  393. xfs_mount_t *mp,
  394. int flags)
  395. {
  396. int recl;
  397. xfs_dquot_t *dqp;
  398. int niters;
  399. int error;
  400. if (mp->m_quotainfo == NULL)
  401. return 0;
  402. niters = 0;
  403. again:
  404. xfs_qm_mplist_lock(mp);
  405. FOREACH_DQUOT_IN_MP(dqp, mp) {
  406. xfs_dqlock(dqp);
  407. if (! XFS_DQ_IS_DIRTY(dqp)) {
  408. xfs_dqunlock(dqp);
  409. continue;
  410. }
  411. xfs_dqtrace_entry(dqp, "FLUSHALL: DQDIRTY");
  412. /* XXX a sentinel would be better */
  413. recl = XFS_QI_MPLRECLAIMS(mp);
  414. if (!xfs_dqflock_nowait(dqp)) {
  415. /*
  416. * If we can't grab the flush lock then check
  417. * to see if the dquot has been flushed delayed
  418. * write. If so, grab its buffer and send it
  419. * out immediately. We'll be able to acquire
  420. * the flush lock when the I/O completes.
  421. */
  422. xfs_qm_dqflock_pushbuf_wait(dqp);
  423. }
  424. /*
  425. * Let go of the mplist lock. We don't want to hold it
  426. * across a disk write.
  427. */
  428. xfs_qm_mplist_unlock(mp);
  429. error = xfs_qm_dqflush(dqp, flags);
  430. xfs_dqunlock(dqp);
  431. if (error)
  432. return error;
  433. xfs_qm_mplist_lock(mp);
  434. if (recl != XFS_QI_MPLRECLAIMS(mp)) {
  435. xfs_qm_mplist_unlock(mp);
  436. /* XXX restart limit */
  437. goto again;
  438. }
  439. }
  440. xfs_qm_mplist_unlock(mp);
  441. /* return ! busy */
  442. return 0;
  443. }
  444. /*
  445. * Release the group dquot pointers the user dquots may be
  446. * carrying around as a hint. mplist is locked on entry and exit.
  447. */
  448. STATIC void
  449. xfs_qm_detach_gdquots(
  450. xfs_mount_t *mp)
  451. {
  452. xfs_dquot_t *dqp, *gdqp;
  453. int nrecl;
  454. again:
  455. ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
  456. dqp = XFS_QI_MPLNEXT(mp);
  457. while (dqp) {
  458. xfs_dqlock(dqp);
  459. if ((gdqp = dqp->q_gdquot)) {
  460. xfs_dqlock(gdqp);
  461. dqp->q_gdquot = NULL;
  462. }
  463. xfs_dqunlock(dqp);
  464. if (gdqp) {
  465. /*
  466. * Can't hold the mplist lock across a dqput.
  467. * XXXmust convert to marker based iterations here.
  468. */
  469. nrecl = XFS_QI_MPLRECLAIMS(mp);
  470. xfs_qm_mplist_unlock(mp);
  471. xfs_qm_dqput(gdqp);
  472. xfs_qm_mplist_lock(mp);
  473. if (nrecl != XFS_QI_MPLRECLAIMS(mp))
  474. goto again;
  475. }
  476. dqp = dqp->MPL_NEXT;
  477. }
  478. }
  479. /*
  480. * Go through all the incore dquots of this file system and take them
  481. * off the mplist and hashlist, if the dquot type matches the dqtype
  482. * parameter. This is used when turning off quota accounting for
  483. * users and/or groups, as well as when the filesystem is unmounting.
  484. */
  485. STATIC int
  486. xfs_qm_dqpurge_int(
  487. xfs_mount_t *mp,
  488. uint flags) /* QUOTAOFF/UMOUNTING/UQUOTA/PQUOTA/GQUOTA */
  489. {
  490. xfs_dquot_t *dqp;
  491. uint dqtype;
  492. int nrecl;
  493. xfs_dquot_t *nextdqp;
  494. int nmisses;
  495. if (mp->m_quotainfo == NULL)
  496. return 0;
  497. dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
  498. dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0;
  499. dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;
  500. xfs_qm_mplist_lock(mp);
  501. /*
  502. * In the first pass through all incore dquots of this filesystem,
  503. * we release the group dquot pointers the user dquots may be
  504. * carrying around as a hint. We need to do this irrespective of
  505. * what's being turned off.
  506. */
  507. xfs_qm_detach_gdquots(mp);
  508. again:
  509. nmisses = 0;
  510. ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
  511. /*
  512. * Try to get rid of all of the unwanted dquots. The idea is to
  513. * get them off mplist and hashlist, but leave them on freelist.
  514. */
  515. dqp = XFS_QI_MPLNEXT(mp);
  516. while (dqp) {
  517. /*
  518. * It's OK to look at the type without taking dqlock here.
  519. * We're holding the mplist lock here, and that's needed for
  520. * a dqreclaim.
  521. */
  522. if ((dqp->dq_flags & dqtype) == 0) {
  523. dqp = dqp->MPL_NEXT;
  524. continue;
  525. }
  526. if (! xfs_qm_dqhashlock_nowait(dqp)) {
  527. nrecl = XFS_QI_MPLRECLAIMS(mp);
  528. xfs_qm_mplist_unlock(mp);
  529. XFS_DQ_HASH_LOCK(dqp->q_hash);
  530. xfs_qm_mplist_lock(mp);
  531. /*
  532. * XXXTheoretically, we can get into a very long
  533. * ping pong game here.
  534. * No one can be adding dquots to the mplist at
  535. * this point, but somebody might be taking things off.
  536. */
  537. if (nrecl != XFS_QI_MPLRECLAIMS(mp)) {
  538. XFS_DQ_HASH_UNLOCK(dqp->q_hash);
  539. goto again;
  540. }
  541. }
  542. /*
  543. * Take the dquot off the mplist and hashlist. It may remain on
  544. * freelist in INACTIVE state.
  545. */
  546. nextdqp = dqp->MPL_NEXT;
  547. nmisses += xfs_qm_dqpurge(dqp);
  548. dqp = nextdqp;
  549. }
  550. xfs_qm_mplist_unlock(mp);
  551. return nmisses;
  552. }
  553. int
  554. xfs_qm_dqpurge_all(
  555. xfs_mount_t *mp,
  556. uint flags)
  557. {
  558. int ndquots;
  559. /*
  560. * Purge the dquot cache.
  561. * None of the dquots should really be busy at this point.
  562. */
  563. if (mp->m_quotainfo) {
  564. while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) {
  565. delay(ndquots * 10);
  566. }
  567. }
  568. return 0;
  569. }
  570. STATIC int
  571. xfs_qm_dqattach_one(
  572. xfs_inode_t *ip,
  573. xfs_dqid_t id,
  574. uint type,
  575. uint doalloc,
  576. uint dolock,
  577. xfs_dquot_t *udqhint, /* hint */
  578. xfs_dquot_t **IO_idqpp)
  579. {
  580. xfs_dquot_t *dqp;
  581. int error;
  582. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  583. error = 0;
  584. /*
  585. * See if we already have it in the inode itself. IO_idqpp is
  586. * &i_udquot or &i_gdquot. This made the code look weird, but
  587. * made the logic a lot simpler.
  588. */
  589. if ((dqp = *IO_idqpp)) {
  590. if (dolock)
  591. xfs_dqlock(dqp);
  592. xfs_dqtrace_entry(dqp, "DQATTACH: found in ip");
  593. goto done;
  594. }
  595. /*
  596. * udqhint is the i_udquot field in inode, and is non-NULL only
  597. * when the type arg is group/project. Its purpose is to save a
  598. * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
  599. * the user dquot.
  600. */
  601. ASSERT(!udqhint || type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
  602. if (udqhint && !dolock)
  603. xfs_dqlock(udqhint);
  604. /*
  605. * No need to take dqlock to look at the id.
  606. * The ID can't change until it gets reclaimed, and it won't
  607. * be reclaimed as long as we have a ref from inode and we hold
  608. * the ilock.
  609. */
  610. if (udqhint &&
  611. (dqp = udqhint->q_gdquot) &&
  612. (be32_to_cpu(dqp->q_core.d_id) == id)) {
  613. ASSERT(XFS_DQ_IS_LOCKED(udqhint));
  614. xfs_dqlock(dqp);
  615. XFS_DQHOLD(dqp);
  616. ASSERT(*IO_idqpp == NULL);
  617. *IO_idqpp = dqp;
  618. if (!dolock) {
  619. xfs_dqunlock(dqp);
  620. xfs_dqunlock(udqhint);
  621. }
  622. goto done;
  623. }
  624. /*
  625. * We can't hold a dquot lock when we call the dqget code.
  626. * We'll deadlock in no time, because of (not conforming to)
  627. * lock ordering - the inodelock comes before any dquot lock,
  628. * and we may drop and reacquire the ilock in xfs_qm_dqget().
  629. */
  630. if (udqhint)
  631. xfs_dqunlock(udqhint);
  632. /*
  633. * Find the dquot from somewhere. This bumps the
  634. * reference count of dquot and returns it locked.
  635. * This can return ENOENT if dquot didn't exist on
  636. * disk and we didn't ask it to allocate;
  637. * ESRCH if quotas got turned off suddenly.
  638. */
  639. if ((error = xfs_qm_dqget(ip->i_mount, ip, id, type,
  640. doalloc|XFS_QMOPT_DOWARN, &dqp))) {
  641. if (udqhint && dolock)
  642. xfs_dqlock(udqhint);
  643. goto done;
  644. }
  645. xfs_dqtrace_entry(dqp, "DQATTACH: found by dqget");
  646. /*
  647. * dqget may have dropped and re-acquired the ilock, but it guarantees
  648. * that the dquot returned is the one that should go in the inode.
  649. */
  650. *IO_idqpp = dqp;
  651. ASSERT(dqp);
  652. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  653. if (! dolock) {
  654. xfs_dqunlock(dqp);
  655. goto done;
  656. }
  657. if (! udqhint)
  658. goto done;
  659. ASSERT(udqhint);
  660. ASSERT(dolock);
  661. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  662. if (! xfs_qm_dqlock_nowait(udqhint)) {
  663. xfs_dqunlock(dqp);
  664. xfs_dqlock(udqhint);
  665. xfs_dqlock(dqp);
  666. }
  667. done:
  668. #ifdef QUOTADEBUG
  669. if (udqhint) {
  670. if (dolock)
  671. ASSERT(XFS_DQ_IS_LOCKED(udqhint));
  672. }
  673. if (! error) {
  674. if (dolock)
  675. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  676. }
  677. #endif
  678. return error;
  679. }
  680. /*
  681. * Given a udquot and gdquot, attach a ptr to the group dquot in the
  682. * udquot as a hint for future lookups. The idea sounds simple, but the
  683. * execution isn't, because the udquot might have a group dquot attached
  684. * already and getting rid of that gets us into lock ordering constraints.
  685. * The process is complicated more by the fact that the dquots may or may not
  686. * be locked on entry.
  687. */
  688. STATIC void
  689. xfs_qm_dqattach_grouphint(
  690. xfs_dquot_t *udq,
  691. xfs_dquot_t *gdq,
  692. uint locked)
  693. {
  694. xfs_dquot_t *tmp;
  695. #ifdef QUOTADEBUG
  696. if (locked) {
  697. ASSERT(XFS_DQ_IS_LOCKED(udq));
  698. ASSERT(XFS_DQ_IS_LOCKED(gdq));
  699. }
  700. #endif
  701. if (! locked)
  702. xfs_dqlock(udq);
  703. if ((tmp = udq->q_gdquot)) {
  704. if (tmp == gdq) {
  705. if (! locked)
  706. xfs_dqunlock(udq);
  707. return;
  708. }
  709. udq->q_gdquot = NULL;
  710. /*
  711. * We can't keep any dqlocks when calling dqrele,
  712. * because the freelist lock comes before dqlocks.
  713. */
  714. xfs_dqunlock(udq);
  715. if (locked)
  716. xfs_dqunlock(gdq);
  717. /*
  718. * we took a hard reference once upon a time in dqget,
  719. * so give it back when the udquot no longer points at it
  720. * dqput() does the unlocking of the dquot.
  721. */
  722. xfs_qm_dqrele(tmp);
  723. xfs_dqlock(udq);
  724. xfs_dqlock(gdq);
  725. } else {
  726. ASSERT(XFS_DQ_IS_LOCKED(udq));
  727. if (! locked) {
  728. xfs_dqlock(gdq);
  729. }
  730. }
  731. ASSERT(XFS_DQ_IS_LOCKED(udq));
  732. ASSERT(XFS_DQ_IS_LOCKED(gdq));
  733. /*
  734. * Somebody could have attached a gdquot here,
  735. * when we dropped the uqlock. If so, just do nothing.
  736. */
  737. if (udq->q_gdquot == NULL) {
  738. XFS_DQHOLD(gdq);
  739. udq->q_gdquot = gdq;
  740. }
  741. if (! locked) {
  742. xfs_dqunlock(gdq);
  743. xfs_dqunlock(udq);
  744. }
  745. }
  746. /*
  747. * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
  748. * into account.
  749. * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
  750. * If XFS_QMOPT_DQLOCK, the dquot(s) will be returned locked. This option pretty
  751. * much made this code a complete mess, but it has been pretty useful.
  752. * If XFS_QMOPT_ILOCKED, then inode sent is already locked EXCL.
  753. * Inode may get unlocked and relocked in here, and the caller must deal with
  754. * the consequences.
  755. */
  756. int
  757. xfs_qm_dqattach(
  758. xfs_inode_t *ip,
  759. uint flags)
  760. {
  761. xfs_mount_t *mp = ip->i_mount;
  762. uint nquotas = 0;
  763. int error = 0;
  764. if ((! XFS_IS_QUOTA_ON(mp)) ||
  765. (! XFS_NOT_DQATTACHED(mp, ip)) ||
  766. (ip->i_ino == mp->m_sb.sb_uquotino) ||
  767. (ip->i_ino == mp->m_sb.sb_gquotino))
  768. return 0;
  769. ASSERT((flags & XFS_QMOPT_ILOCKED) == 0 ||
  770. xfs_isilocked(ip, XFS_ILOCK_EXCL));
  771. if (! (flags & XFS_QMOPT_ILOCKED))
  772. xfs_ilock(ip, XFS_ILOCK_EXCL);
  773. if (XFS_IS_UQUOTA_ON(mp)) {
  774. error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
  775. flags & XFS_QMOPT_DQALLOC,
  776. flags & XFS_QMOPT_DQLOCK,
  777. NULL, &ip->i_udquot);
  778. if (error)
  779. goto done;
  780. nquotas++;
  781. }
  782. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  783. if (XFS_IS_OQUOTA_ON(mp)) {
  784. error = XFS_IS_GQUOTA_ON(mp) ?
  785. xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
  786. flags & XFS_QMOPT_DQALLOC,
  787. flags & XFS_QMOPT_DQLOCK,
  788. ip->i_udquot, &ip->i_gdquot) :
  789. xfs_qm_dqattach_one(ip, ip->i_d.di_projid, XFS_DQ_PROJ,
  790. flags & XFS_QMOPT_DQALLOC,
  791. flags & XFS_QMOPT_DQLOCK,
  792. ip->i_udquot, &ip->i_gdquot);
  793. /*
  794. * Don't worry about the udquot that we may have
  795. * attached above. It'll get detached, if not already.
  796. */
  797. if (error)
  798. goto done;
  799. nquotas++;
  800. }
  801. /*
  802. * Attach this group quota to the user quota as a hint.
  803. * This WON'T, in general, result in a thrash.
  804. */
  805. if (nquotas == 2) {
  806. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  807. ASSERT(ip->i_udquot);
  808. ASSERT(ip->i_gdquot);
  809. /*
  810. * We may or may not have the i_udquot locked at this point,
  811. * but this check is OK since we don't depend on the i_gdquot to
  812. * be accurate 100% all the time. It is just a hint, and this
  813. * will succeed in general.
  814. */
  815. if (ip->i_udquot->q_gdquot == ip->i_gdquot)
  816. goto done;
  817. /*
  818. * Attach i_gdquot to the gdquot hint inside the i_udquot.
  819. */
  820. xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot,
  821. flags & XFS_QMOPT_DQLOCK);
  822. }
  823. done:
  824. #ifdef QUOTADEBUG
  825. if (! error) {
  826. if (ip->i_udquot) {
  827. if (flags & XFS_QMOPT_DQLOCK)
  828. ASSERT(XFS_DQ_IS_LOCKED(ip->i_udquot));
  829. }
  830. if (ip->i_gdquot) {
  831. if (flags & XFS_QMOPT_DQLOCK)
  832. ASSERT(XFS_DQ_IS_LOCKED(ip->i_gdquot));
  833. }
  834. if (XFS_IS_UQUOTA_ON(mp))
  835. ASSERT(ip->i_udquot);
  836. if (XFS_IS_OQUOTA_ON(mp))
  837. ASSERT(ip->i_gdquot);
  838. }
  839. #endif
  840. if (! (flags & XFS_QMOPT_ILOCKED))
  841. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  842. #ifdef QUOTADEBUG
  843. else
  844. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  845. #endif
  846. return error;
  847. }
  848. /*
  849. * Release dquots (and their references) if any.
  850. * The inode should be locked EXCL except when this's called by
  851. * xfs_ireclaim.
  852. */
  853. void
  854. xfs_qm_dqdetach(
  855. xfs_inode_t *ip)
  856. {
  857. if (!(ip->i_udquot || ip->i_gdquot))
  858. return;
  859. ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
  860. ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
  861. if (ip->i_udquot) {
  862. xfs_dqtrace_entry_ino(ip->i_udquot, "DQDETTACH", ip);
  863. xfs_qm_dqrele(ip->i_udquot);
  864. ip->i_udquot = NULL;
  865. }
  866. if (ip->i_gdquot) {
  867. xfs_dqtrace_entry_ino(ip->i_gdquot, "DQDETTACH", ip);
  868. xfs_qm_dqrele(ip->i_gdquot);
  869. ip->i_gdquot = NULL;
  870. }
  871. }
  872. /*
  873. * This is called to sync quotas. We can be told to use non-blocking
  874. * semantics by either the SYNC_BDFLUSH flag or the absence of the
  875. * SYNC_WAIT flag.
  876. */
  877. int
  878. xfs_qm_sync(
  879. xfs_mount_t *mp,
  880. int flags)
  881. {
  882. int recl, restarts;
  883. xfs_dquot_t *dqp;
  884. uint flush_flags;
  885. boolean_t nowait;
  886. int error;
  887. if (! XFS_IS_QUOTA_ON(mp))
  888. return 0;
  889. restarts = 0;
  890. /*
  891. * We won't block unless we are asked to.
  892. */
  893. nowait = (boolean_t)(flags & SYNC_BDFLUSH || (flags & SYNC_WAIT) == 0);
  894. again:
  895. xfs_qm_mplist_lock(mp);
  896. /*
  897. * dqpurge_all() also takes the mplist lock and iterate thru all dquots
  898. * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared
  899. * when we have the mplist lock, we know that dquots will be consistent
  900. * as long as we have it locked.
  901. */
  902. if (! XFS_IS_QUOTA_ON(mp)) {
  903. xfs_qm_mplist_unlock(mp);
  904. return 0;
  905. }
  906. FOREACH_DQUOT_IN_MP(dqp, mp) {
  907. /*
  908. * If this is vfs_sync calling, then skip the dquots that
  909. * don't 'seem' to be dirty. ie. don't acquire dqlock.
  910. * This is very similar to what xfs_sync does with inodes.
  911. */
  912. if (flags & SYNC_BDFLUSH) {
  913. if (! XFS_DQ_IS_DIRTY(dqp))
  914. continue;
  915. }
  916. if (nowait) {
  917. /*
  918. * Try to acquire the dquot lock. We are NOT out of
  919. * lock order, but we just don't want to wait for this
  920. * lock, unless somebody wanted us to.
  921. */
  922. if (! xfs_qm_dqlock_nowait(dqp))
  923. continue;
  924. } else {
  925. xfs_dqlock(dqp);
  926. }
  927. /*
  928. * Now, find out for sure if this dquot is dirty or not.
  929. */
  930. if (! XFS_DQ_IS_DIRTY(dqp)) {
  931. xfs_dqunlock(dqp);
  932. continue;
  933. }
  934. /* XXX a sentinel would be better */
  935. recl = XFS_QI_MPLRECLAIMS(mp);
  936. if (!xfs_dqflock_nowait(dqp)) {
  937. if (nowait) {
  938. xfs_dqunlock(dqp);
  939. continue;
  940. }
  941. /*
  942. * If we can't grab the flush lock then if the caller
  943. * really wanted us to give this our best shot, so
  944. * see if we can give a push to the buffer before we wait
  945. * on the flush lock. At this point, we know that
  946. * even though the dquot is being flushed,
  947. * it has (new) dirty data.
  948. */
  949. xfs_qm_dqflock_pushbuf_wait(dqp);
  950. }
  951. /*
  952. * Let go of the mplist lock. We don't want to hold it
  953. * across a disk write
  954. */
  955. flush_flags = (nowait) ? XFS_QMOPT_DELWRI : XFS_QMOPT_SYNC;
  956. xfs_qm_mplist_unlock(mp);
  957. xfs_dqtrace_entry(dqp, "XQM_SYNC: DQFLUSH");
  958. error = xfs_qm_dqflush(dqp, flush_flags);
  959. xfs_dqunlock(dqp);
  960. if (error && XFS_FORCED_SHUTDOWN(mp))
  961. return 0; /* Need to prevent umount failure */
  962. else if (error)
  963. return error;
  964. xfs_qm_mplist_lock(mp);
  965. if (recl != XFS_QI_MPLRECLAIMS(mp)) {
  966. if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS)
  967. break;
  968. xfs_qm_mplist_unlock(mp);
  969. goto again;
  970. }
  971. }
  972. xfs_qm_mplist_unlock(mp);
  973. return 0;
  974. }
  975. /*
  976. * This initializes all the quota information that's kept in the
  977. * mount structure
  978. */
  979. STATIC int
  980. xfs_qm_init_quotainfo(
  981. xfs_mount_t *mp)
  982. {
  983. xfs_quotainfo_t *qinf;
  984. int error;
  985. xfs_dquot_t *dqp;
  986. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  987. /*
  988. * Tell XQM that we exist as soon as possible.
  989. */
  990. if ((error = xfs_qm_hold_quotafs_ref(mp))) {
  991. return error;
  992. }
  993. qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
  994. /*
  995. * See if quotainodes are setup, and if not, allocate them,
  996. * and change the superblock accordingly.
  997. */
  998. if ((error = xfs_qm_init_quotainos(mp))) {
  999. kmem_free(qinf);
  1000. mp->m_quotainfo = NULL;
  1001. return error;
  1002. }
  1003. xfs_qm_list_init(&qinf->qi_dqlist, "mpdqlist", 0);
  1004. qinf->qi_dqreclaims = 0;
  1005. /* mutex used to serialize quotaoffs */
  1006. mutex_init(&qinf->qi_quotaofflock);
  1007. /* Precalc some constants */
  1008. qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  1009. ASSERT(qinf->qi_dqchunklen);
  1010. qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
  1011. do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));
  1012. mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
  1013. /*
  1014. * We try to get the limits from the superuser's limits fields.
  1015. * This is quite hacky, but it is standard quota practice.
  1016. * We look at the USR dquot with id == 0 first, but if user quotas
  1017. * are not enabled we goto the GRP dquot with id == 0.
  1018. * We don't really care to keep separate default limits for user
  1019. * and group quotas, at least not at this point.
  1020. */
  1021. error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0,
  1022. XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER :
  1023. (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
  1024. XFS_DQ_PROJ),
  1025. XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN,
  1026. &dqp);
  1027. if (! error) {
  1028. xfs_disk_dquot_t *ddqp = &dqp->q_core;
  1029. /*
  1030. * The warnings and timers set the grace period given to
  1031. * a user or group before he or she can not perform any
  1032. * more writing. If it is zero, a default is used.
  1033. */
  1034. qinf->qi_btimelimit = ddqp->d_btimer ?
  1035. be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
  1036. qinf->qi_itimelimit = ddqp->d_itimer ?
  1037. be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
  1038. qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
  1039. be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
  1040. qinf->qi_bwarnlimit = ddqp->d_bwarns ?
  1041. be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
  1042. qinf->qi_iwarnlimit = ddqp->d_iwarns ?
  1043. be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
  1044. qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
  1045. be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
  1046. qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
  1047. qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
  1048. qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
  1049. qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
  1050. qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
  1051. qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
  1052. /*
  1053. * We sent the XFS_QMOPT_DQSUSER flag to dqget because
  1054. * we don't want this dquot cached. We haven't done a
  1055. * quotacheck yet, and quotacheck doesn't like incore dquots.
  1056. */
  1057. xfs_qm_dqdestroy(dqp);
  1058. } else {
  1059. qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
  1060. qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
  1061. qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
  1062. qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
  1063. qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
  1064. qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
  1065. }
  1066. return 0;
  1067. }
  1068. /*
  1069. * Gets called when unmounting a filesystem or when all quotas get
  1070. * turned off.
  1071. * This purges the quota inodes, destroys locks and frees itself.
  1072. */
  1073. void
  1074. xfs_qm_destroy_quotainfo(
  1075. xfs_mount_t *mp)
  1076. {
  1077. xfs_quotainfo_t *qi;
  1078. qi = mp->m_quotainfo;
  1079. ASSERT(qi != NULL);
  1080. ASSERT(xfs_Gqm != NULL);
  1081. /*
  1082. * Release the reference that XQM kept, so that we know
  1083. * when the XQM structure should be freed. We cannot assume
  1084. * that xfs_Gqm is non-null after this point.
  1085. */
  1086. xfs_qm_rele_quotafs_ref(mp);
  1087. xfs_qm_list_destroy(&qi->qi_dqlist);
  1088. if (qi->qi_uquotaip) {
  1089. IRELE(qi->qi_uquotaip);
  1090. qi->qi_uquotaip = NULL; /* paranoia */
  1091. }
  1092. if (qi->qi_gquotaip) {
  1093. IRELE(qi->qi_gquotaip);
  1094. qi->qi_gquotaip = NULL;
  1095. }
  1096. mutex_destroy(&qi->qi_quotaofflock);
  1097. kmem_free(qi);
  1098. mp->m_quotainfo = NULL;
  1099. }
  1100. /* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */
  1101. /* ARGSUSED */
  1102. STATIC void
  1103. xfs_qm_list_init(
  1104. xfs_dqlist_t *list,
  1105. char *str,
  1106. int n)
  1107. {
  1108. mutex_init(&list->qh_lock);
  1109. list->qh_next = NULL;
  1110. list->qh_version = 0;
  1111. list->qh_nelems = 0;
  1112. }
  1113. STATIC void
  1114. xfs_qm_list_destroy(
  1115. xfs_dqlist_t *list)
  1116. {
  1117. mutex_destroy(&(list->qh_lock));
  1118. }
  1119. /*
  1120. * Stripped down version of dqattach. This doesn't attach, or even look at the
  1121. * dquots attached to the inode. The rationale is that there won't be any
  1122. * attached at the time this is called from quotacheck.
  1123. */
  1124. STATIC int
  1125. xfs_qm_dqget_noattach(
  1126. xfs_inode_t *ip,
  1127. xfs_dquot_t **O_udqpp,
  1128. xfs_dquot_t **O_gdqpp)
  1129. {
  1130. int error;
  1131. xfs_mount_t *mp;
  1132. xfs_dquot_t *udqp, *gdqp;
  1133. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1134. mp = ip->i_mount;
  1135. udqp = NULL;
  1136. gdqp = NULL;
  1137. if (XFS_IS_UQUOTA_ON(mp)) {
  1138. ASSERT(ip->i_udquot == NULL);
  1139. /*
  1140. * We want the dquot allocated if it doesn't exist.
  1141. */
  1142. if ((error = xfs_qm_dqget(mp, ip, ip->i_d.di_uid, XFS_DQ_USER,
  1143. XFS_QMOPT_DQALLOC | XFS_QMOPT_DOWARN,
  1144. &udqp))) {
  1145. /*
  1146. * Shouldn't be able to turn off quotas here.
  1147. */
  1148. ASSERT(error != ESRCH);
  1149. ASSERT(error != ENOENT);
  1150. return error;
  1151. }
  1152. ASSERT(udqp);
  1153. }
  1154. if (XFS_IS_OQUOTA_ON(mp)) {
  1155. ASSERT(ip->i_gdquot == NULL);
  1156. if (udqp)
  1157. xfs_dqunlock(udqp);
  1158. error = XFS_IS_GQUOTA_ON(mp) ?
  1159. xfs_qm_dqget(mp, ip,
  1160. ip->i_d.di_gid, XFS_DQ_GROUP,
  1161. XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
  1162. &gdqp) :
  1163. xfs_qm_dqget(mp, ip,
  1164. ip->i_d.di_projid, XFS_DQ_PROJ,
  1165. XFS_QMOPT_DQALLOC|XFS_QMOPT_DOWARN,
  1166. &gdqp);
  1167. if (error) {
  1168. if (udqp)
  1169. xfs_qm_dqrele(udqp);
  1170. ASSERT(error != ESRCH);
  1171. ASSERT(error != ENOENT);
  1172. return error;
  1173. }
  1174. ASSERT(gdqp);
  1175. /* Reacquire the locks in the right order */
  1176. if (udqp) {
  1177. if (! xfs_qm_dqlock_nowait(udqp)) {
  1178. xfs_dqunlock(gdqp);
  1179. xfs_dqlock(udqp);
  1180. xfs_dqlock(gdqp);
  1181. }
  1182. }
  1183. }
  1184. *O_udqpp = udqp;
  1185. *O_gdqpp = gdqp;
  1186. #ifdef QUOTADEBUG
  1187. if (udqp) ASSERT(XFS_DQ_IS_LOCKED(udqp));
  1188. if (gdqp) ASSERT(XFS_DQ_IS_LOCKED(gdqp));
  1189. #endif
  1190. return 0;
  1191. }
  1192. /*
  1193. * Create an inode and return with a reference already taken, but unlocked
  1194. * This is how we create quota inodes
  1195. */
  1196. STATIC int
  1197. xfs_qm_qino_alloc(
  1198. xfs_mount_t *mp,
  1199. xfs_inode_t **ip,
  1200. __int64_t sbfields,
  1201. uint flags)
  1202. {
  1203. xfs_trans_t *tp;
  1204. int error;
  1205. int committed;
  1206. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QINOCREATE);
  1207. if ((error = xfs_trans_reserve(tp,
  1208. XFS_QM_QINOCREATE_SPACE_RES(mp),
  1209. XFS_CREATE_LOG_RES(mp), 0,
  1210. XFS_TRANS_PERM_LOG_RES,
  1211. XFS_CREATE_LOG_COUNT))) {
  1212. xfs_trans_cancel(tp, 0);
  1213. return error;
  1214. }
  1215. if ((error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0,
  1216. &xfs_zerocr, 0, 1, ip, &committed))) {
  1217. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
  1218. XFS_TRANS_ABORT);
  1219. return error;
  1220. }
  1221. /*
  1222. * Keep an extra reference to this quota inode. This inode is
  1223. * locked exclusively and joined to the transaction already.
  1224. */
  1225. ASSERT(xfs_isilocked(*ip, XFS_ILOCK_EXCL));
  1226. IHOLD(*ip);
  1227. /*
  1228. * Make the changes in the superblock, and log those too.
  1229. * sbfields arg may contain fields other than *QUOTINO;
  1230. * VERSIONNUM for example.
  1231. */
  1232. spin_lock(&mp->m_sb_lock);
  1233. if (flags & XFS_QMOPT_SBVERSION) {
  1234. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  1235. unsigned oldv = mp->m_sb.sb_versionnum;
  1236. #endif
  1237. ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
  1238. ASSERT((sbfields & (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
  1239. XFS_SB_GQUOTINO | XFS_SB_QFLAGS)) ==
  1240. (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
  1241. XFS_SB_GQUOTINO | XFS_SB_QFLAGS));
  1242. xfs_sb_version_addquota(&mp->m_sb);
  1243. mp->m_sb.sb_uquotino = NULLFSINO;
  1244. mp->m_sb.sb_gquotino = NULLFSINO;
  1245. /* qflags will get updated _after_ quotacheck */
  1246. mp->m_sb.sb_qflags = 0;
  1247. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  1248. cmn_err(CE_NOTE,
  1249. "Old superblock version %x, converting to %x.",
  1250. oldv, mp->m_sb.sb_versionnum);
  1251. #endif
  1252. }
  1253. if (flags & XFS_QMOPT_UQUOTA)
  1254. mp->m_sb.sb_uquotino = (*ip)->i_ino;
  1255. else
  1256. mp->m_sb.sb_gquotino = (*ip)->i_ino;
  1257. spin_unlock(&mp->m_sb_lock);
  1258. xfs_mod_sb(tp, sbfields);
  1259. if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES))) {
  1260. xfs_fs_cmn_err(CE_ALERT, mp, "XFS qino_alloc failed!");
  1261. return error;
  1262. }
  1263. return 0;
  1264. }
  1265. STATIC void
  1266. xfs_qm_reset_dqcounts(
  1267. xfs_mount_t *mp,
  1268. xfs_buf_t *bp,
  1269. xfs_dqid_t id,
  1270. uint type)
  1271. {
  1272. xfs_disk_dquot_t *ddq;
  1273. int j;
  1274. xfs_buftrace("RESET DQUOTS", bp);
  1275. /*
  1276. * Reset all counters and timers. They'll be
  1277. * started afresh by xfs_qm_quotacheck.
  1278. */
  1279. #ifdef DEBUG
  1280. j = XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  1281. do_div(j, sizeof(xfs_dqblk_t));
  1282. ASSERT(XFS_QM_DQPERBLK(mp) == j);
  1283. #endif
  1284. ddq = (xfs_disk_dquot_t *)XFS_BUF_PTR(bp);
  1285. for (j = 0; j < XFS_QM_DQPERBLK(mp); j++) {
  1286. /*
  1287. * Do a sanity check, and if needed, repair the dqblk. Don't
  1288. * output any warnings because it's perfectly possible to
  1289. * find uninitialised dquot blks. See comment in xfs_qm_dqcheck.
  1290. */
  1291. (void) xfs_qm_dqcheck(ddq, id+j, type, XFS_QMOPT_DQREPAIR,
  1292. "xfs_quotacheck");
  1293. ddq->d_bcount = 0;
  1294. ddq->d_icount = 0;
  1295. ddq->d_rtbcount = 0;
  1296. ddq->d_btimer = 0;
  1297. ddq->d_itimer = 0;
  1298. ddq->d_rtbtimer = 0;
  1299. ddq->d_bwarns = 0;
  1300. ddq->d_iwarns = 0;
  1301. ddq->d_rtbwarns = 0;
  1302. ddq = (xfs_disk_dquot_t *) ((xfs_dqblk_t *)ddq + 1);
  1303. }
  1304. }
  1305. STATIC int
  1306. xfs_qm_dqiter_bufs(
  1307. xfs_mount_t *mp,
  1308. xfs_dqid_t firstid,
  1309. xfs_fsblock_t bno,
  1310. xfs_filblks_t blkcnt,
  1311. uint flags)
  1312. {
  1313. xfs_buf_t *bp;
  1314. int error;
  1315. int notcommitted;
  1316. int incr;
  1317. int type;
  1318. ASSERT(blkcnt > 0);
  1319. notcommitted = 0;
  1320. incr = (blkcnt > XFS_QM_MAX_DQCLUSTER_LOGSZ) ?
  1321. XFS_QM_MAX_DQCLUSTER_LOGSZ : blkcnt;
  1322. type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
  1323. (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
  1324. error = 0;
  1325. /*
  1326. * Blkcnt arg can be a very big number, and might even be
  1327. * larger than the log itself. So, we have to break it up into
  1328. * manageable-sized transactions.
  1329. * Note that we don't start a permanent transaction here; we might
  1330. * not be able to get a log reservation for the whole thing up front,
  1331. * and we don't really care to either, because we just discard
  1332. * everything if we were to crash in the middle of this loop.
  1333. */
  1334. while (blkcnt--) {
  1335. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  1336. XFS_FSB_TO_DADDR(mp, bno),
  1337. (int)XFS_QI_DQCHUNKLEN(mp), 0, &bp);
  1338. if (error)
  1339. break;
  1340. xfs_qm_reset_dqcounts(mp, bp, firstid, type);
  1341. xfs_bdwrite(mp, bp);
  1342. /*
  1343. * goto the next block.
  1344. */
  1345. bno++;
  1346. firstid += XFS_QM_DQPERBLK(mp);
  1347. }
  1348. return error;
  1349. }
  1350. /*
  1351. * Iterate over all allocated USR/GRP/PRJ dquots in the system, calling a
  1352. * caller supplied function for every chunk of dquots that we find.
  1353. */
  1354. STATIC int
  1355. xfs_qm_dqiterate(
  1356. xfs_mount_t *mp,
  1357. xfs_inode_t *qip,
  1358. uint flags)
  1359. {
  1360. xfs_bmbt_irec_t *map;
  1361. int i, nmaps; /* number of map entries */
  1362. int error; /* return value */
  1363. xfs_fileoff_t lblkno;
  1364. xfs_filblks_t maxlblkcnt;
  1365. xfs_dqid_t firstid;
  1366. xfs_fsblock_t rablkno;
  1367. xfs_filblks_t rablkcnt;
  1368. error = 0;
  1369. /*
  1370. * This looks racy, but we can't keep an inode lock across a
  1371. * trans_reserve. But, this gets called during quotacheck, and that
  1372. * happens only at mount time which is single threaded.
  1373. */
  1374. if (qip->i_d.di_nblocks == 0)
  1375. return 0;
  1376. map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
  1377. lblkno = 0;
  1378. maxlblkcnt = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1379. do {
  1380. nmaps = XFS_DQITER_MAP_SIZE;
  1381. /*
  1382. * We aren't changing the inode itself. Just changing
  1383. * some of its data. No new blocks are added here, and
  1384. * the inode is never added to the transaction.
  1385. */
  1386. xfs_ilock(qip, XFS_ILOCK_SHARED);
  1387. error = xfs_bmapi(NULL, qip, lblkno,
  1388. maxlblkcnt - lblkno,
  1389. XFS_BMAPI_METADATA,
  1390. NULL,
  1391. 0, map, &nmaps, NULL, NULL);
  1392. xfs_iunlock(qip, XFS_ILOCK_SHARED);
  1393. if (error)
  1394. break;
  1395. ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
  1396. for (i = 0; i < nmaps; i++) {
  1397. ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
  1398. ASSERT(map[i].br_blockcount);
  1399. lblkno += map[i].br_blockcount;
  1400. if (map[i].br_startblock == HOLESTARTBLOCK)
  1401. continue;
  1402. firstid = (xfs_dqid_t) map[i].br_startoff *
  1403. XFS_QM_DQPERBLK(mp);
  1404. /*
  1405. * Do a read-ahead on the next extent.
  1406. */
  1407. if ((i+1 < nmaps) &&
  1408. (map[i+1].br_startblock != HOLESTARTBLOCK)) {
  1409. rablkcnt = map[i+1].br_blockcount;
  1410. rablkno = map[i+1].br_startblock;
  1411. while (rablkcnt--) {
  1412. xfs_baread(mp->m_ddev_targp,
  1413. XFS_FSB_TO_DADDR(mp, rablkno),
  1414. (int)XFS_QI_DQCHUNKLEN(mp));
  1415. rablkno++;
  1416. }
  1417. }
  1418. /*
  1419. * Iterate thru all the blks in the extent and
  1420. * reset the counters of all the dquots inside them.
  1421. */
  1422. if ((error = xfs_qm_dqiter_bufs(mp,
  1423. firstid,
  1424. map[i].br_startblock,
  1425. map[i].br_blockcount,
  1426. flags))) {
  1427. break;
  1428. }
  1429. }
  1430. if (error)
  1431. break;
  1432. } while (nmaps > 0);
  1433. kmem_free(map);
  1434. return error;
  1435. }
  1436. /*
  1437. * Called by dqusage_adjust in doing a quotacheck.
  1438. * Given the inode, and a dquot (either USR or GRP, doesn't matter),
  1439. * this updates its incore copy as well as the buffer copy. This is
  1440. * so that once the quotacheck is done, we can just log all the buffers,
  1441. * as opposed to logging numerous updates to individual dquots.
  1442. */
  1443. STATIC void
  1444. xfs_qm_quotacheck_dqadjust(
  1445. xfs_dquot_t *dqp,
  1446. xfs_qcnt_t nblks,
  1447. xfs_qcnt_t rtblks)
  1448. {
  1449. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  1450. xfs_dqtrace_entry(dqp, "QCHECK DQADJUST");
  1451. /*
  1452. * Adjust the inode count and the block count to reflect this inode's
  1453. * resource usage.
  1454. */
  1455. be64_add_cpu(&dqp->q_core.d_icount, 1);
  1456. dqp->q_res_icount++;
  1457. if (nblks) {
  1458. be64_add_cpu(&dqp->q_core.d_bcount, nblks);
  1459. dqp->q_res_bcount += nblks;
  1460. }
  1461. if (rtblks) {
  1462. be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
  1463. dqp->q_res_rtbcount += rtblks;
  1464. }
  1465. /*
  1466. * Set default limits, adjust timers (since we changed usages)
  1467. */
  1468. if (! XFS_IS_SUSER_DQUOT(dqp)) {
  1469. xfs_qm_adjust_dqlimits(dqp->q_mount, &dqp->q_core);
  1470. xfs_qm_adjust_dqtimers(dqp->q_mount, &dqp->q_core);
  1471. }
  1472. dqp->dq_flags |= XFS_DQ_DIRTY;
  1473. }
  1474. STATIC int
  1475. xfs_qm_get_rtblks(
  1476. xfs_inode_t *ip,
  1477. xfs_qcnt_t *O_rtblks)
  1478. {
  1479. xfs_filblks_t rtblks; /* total rt blks */
  1480. xfs_extnum_t idx; /* extent record index */
  1481. xfs_ifork_t *ifp; /* inode fork pointer */
  1482. xfs_extnum_t nextents; /* number of extent entries */
  1483. int error;
  1484. ASSERT(XFS_IS_REALTIME_INODE(ip));
  1485. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1486. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1487. if ((error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
  1488. return error;
  1489. }
  1490. rtblks = 0;
  1491. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1492. for (idx = 0; idx < nextents; idx++)
  1493. rtblks += xfs_bmbt_get_blockcount(xfs_iext_get_ext(ifp, idx));
  1494. *O_rtblks = (xfs_qcnt_t)rtblks;
  1495. return 0;
  1496. }
  1497. /*
  1498. * callback routine supplied to bulkstat(). Given an inumber, find its
  1499. * dquots and update them to account for resources taken by that inode.
  1500. */
  1501. /* ARGSUSED */
  1502. STATIC int
  1503. xfs_qm_dqusage_adjust(
  1504. xfs_mount_t *mp, /* mount point for filesystem */
  1505. xfs_ino_t ino, /* inode number to get data for */
  1506. void __user *buffer, /* not used */
  1507. int ubsize, /* not used */
  1508. void *private_data, /* not used */
  1509. xfs_daddr_t bno, /* starting block of inode cluster */
  1510. int *ubused, /* not used */
  1511. void *dip, /* on-disk inode pointer (not used) */
  1512. int *res) /* result code value */
  1513. {
  1514. xfs_inode_t *ip;
  1515. xfs_dquot_t *udqp, *gdqp;
  1516. xfs_qcnt_t nblks, rtblks;
  1517. int error;
  1518. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1519. /*
  1520. * rootino must have its resources accounted for, not so with the quota
  1521. * inodes.
  1522. */
  1523. if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
  1524. *res = BULKSTAT_RV_NOTHING;
  1525. return XFS_ERROR(EINVAL);
  1526. }
  1527. /*
  1528. * We don't _need_ to take the ilock EXCL. However, the xfs_qm_dqget
  1529. * interface expects the inode to be exclusively locked because that's
  1530. * the case in all other instances. It's OK that we do this because
  1531. * quotacheck is done only at mount time.
  1532. */
  1533. if ((error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_EXCL, &ip, bno))) {
  1534. *res = BULKSTAT_RV_NOTHING;
  1535. return error;
  1536. }
  1537. /*
  1538. * Obtain the locked dquots. In case of an error (eg. allocation
  1539. * fails for ENOSPC), we return the negative of the error number
  1540. * to bulkstat, so that it can get propagated to quotacheck() and
  1541. * making us disable quotas for the file system.
  1542. */
  1543. if ((error = xfs_qm_dqget_noattach(ip, &udqp, &gdqp))) {
  1544. xfs_iput(ip, XFS_ILOCK_EXCL);
  1545. *res = BULKSTAT_RV_GIVEUP;
  1546. return error;
  1547. }
  1548. rtblks = 0;
  1549. if (! XFS_IS_REALTIME_INODE(ip)) {
  1550. nblks = (xfs_qcnt_t)ip->i_d.di_nblocks;
  1551. } else {
  1552. /*
  1553. * Walk thru the extent list and count the realtime blocks.
  1554. */
  1555. if ((error = xfs_qm_get_rtblks(ip, &rtblks))) {
  1556. xfs_iput(ip, XFS_ILOCK_EXCL);
  1557. if (udqp)
  1558. xfs_qm_dqput(udqp);
  1559. if (gdqp)
  1560. xfs_qm_dqput(gdqp);
  1561. *res = BULKSTAT_RV_GIVEUP;
  1562. return error;
  1563. }
  1564. nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
  1565. }
  1566. ASSERT(ip->i_delayed_blks == 0);
  1567. /*
  1568. * We can't release the inode while holding its dquot locks.
  1569. * The inode can go into inactive and might try to acquire the dquotlocks.
  1570. * So, just unlock here and do a vn_rele at the end.
  1571. */
  1572. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1573. /*
  1574. * Add the (disk blocks and inode) resources occupied by this
  1575. * inode to its dquots. We do this adjustment in the incore dquot,
  1576. * and also copy the changes to its buffer.
  1577. * We don't care about putting these changes in a transaction
  1578. * envelope because if we crash in the middle of a 'quotacheck'
  1579. * we have to start from the beginning anyway.
  1580. * Once we're done, we'll log all the dquot bufs.
  1581. *
  1582. * The *QUOTA_ON checks below may look pretty racy, but quotachecks
  1583. * and quotaoffs don't race. (Quotachecks happen at mount time only).
  1584. */
  1585. if (XFS_IS_UQUOTA_ON(mp)) {
  1586. ASSERT(udqp);
  1587. xfs_qm_quotacheck_dqadjust(udqp, nblks, rtblks);
  1588. xfs_qm_dqput(udqp);
  1589. }
  1590. if (XFS_IS_OQUOTA_ON(mp)) {
  1591. ASSERT(gdqp);
  1592. xfs_qm_quotacheck_dqadjust(gdqp, nblks, rtblks);
  1593. xfs_qm_dqput(gdqp);
  1594. }
  1595. /*
  1596. * Now release the inode. This will send it to 'inactive', and
  1597. * possibly even free blocks.
  1598. */
  1599. IRELE(ip);
  1600. /*
  1601. * Goto next inode.
  1602. */
  1603. *res = BULKSTAT_RV_DIDONE;
  1604. return 0;
  1605. }
  1606. /*
  1607. * Walk thru all the filesystem inodes and construct a consistent view
  1608. * of the disk quota world. If the quotacheck fails, disable quotas.
  1609. */
  1610. int
  1611. xfs_qm_quotacheck(
  1612. xfs_mount_t *mp)
  1613. {
  1614. int done, count, error;
  1615. xfs_ino_t lastino;
  1616. size_t structsz;
  1617. xfs_inode_t *uip, *gip;
  1618. uint flags;
  1619. count = INT_MAX;
  1620. structsz = 1;
  1621. lastino = 0;
  1622. flags = 0;
  1623. ASSERT(XFS_QI_UQIP(mp) || XFS_QI_GQIP(mp));
  1624. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1625. /*
  1626. * There should be no cached dquots. The (simplistic) quotacheck
  1627. * algorithm doesn't like that.
  1628. */
  1629. ASSERT(XFS_QI_MPLNDQUOTS(mp) == 0);
  1630. cmn_err(CE_NOTE, "XFS quotacheck %s: Please wait.", mp->m_fsname);
  1631. /*
  1632. * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
  1633. * their counters to zero. We need a clean slate.
  1634. * We don't log our changes till later.
  1635. */
  1636. if ((uip = XFS_QI_UQIP(mp))) {
  1637. if ((error = xfs_qm_dqiterate(mp, uip, XFS_QMOPT_UQUOTA)))
  1638. goto error_return;
  1639. flags |= XFS_UQUOTA_CHKD;
  1640. }
  1641. if ((gip = XFS_QI_GQIP(mp))) {
  1642. if ((error = xfs_qm_dqiterate(mp, gip, XFS_IS_GQUOTA_ON(mp) ?
  1643. XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA)))
  1644. goto error_return;
  1645. flags |= XFS_OQUOTA_CHKD;
  1646. }
  1647. do {
  1648. /*
  1649. * Iterate thru all the inodes in the file system,
  1650. * adjusting the corresponding dquot counters in core.
  1651. */
  1652. if ((error = xfs_bulkstat(mp, &lastino, &count,
  1653. xfs_qm_dqusage_adjust, NULL,
  1654. structsz, NULL, BULKSTAT_FG_IGET, &done)))
  1655. break;
  1656. } while (! done);
  1657. /*
  1658. * We've made all the changes that we need to make incore.
  1659. * Flush them down to disk buffers if everything was updated
  1660. * successfully.
  1661. */
  1662. if (!error)
  1663. error = xfs_qm_dqflush_all(mp, XFS_QMOPT_DELWRI);
  1664. /*
  1665. * We can get this error if we couldn't do a dquot allocation inside
  1666. * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
  1667. * dirty dquots that might be cached, we just want to get rid of them
  1668. * and turn quotaoff. The dquots won't be attached to any of the inodes
  1669. * at this point (because we intentionally didn't in dqget_noattach).
  1670. */
  1671. if (error) {
  1672. xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_QUOTAOFF);
  1673. goto error_return;
  1674. }
  1675. /*
  1676. * We didn't log anything, because if we crashed, we'll have to
  1677. * start the quotacheck from scratch anyway. However, we must make
  1678. * sure that our dquot changes are secure before we put the
  1679. * quotacheck'd stamp on the superblock. So, here we do a synchronous
  1680. * flush.
  1681. */
  1682. XFS_bflush(mp->m_ddev_targp);
  1683. /*
  1684. * If one type of quotas is off, then it will lose its
  1685. * quotachecked status, since we won't be doing accounting for
  1686. * that type anymore.
  1687. */
  1688. mp->m_qflags &= ~(XFS_OQUOTA_CHKD | XFS_UQUOTA_CHKD);
  1689. mp->m_qflags |= flags;
  1690. XQM_LIST_PRINT(&(XFS_QI_MPL_LIST(mp)), MPL_NEXT, "++++ Mp list +++");
  1691. error_return:
  1692. if (error) {
  1693. cmn_err(CE_WARN, "XFS quotacheck %s: Unsuccessful (Error %d): "
  1694. "Disabling quotas.",
  1695. mp->m_fsname, error);
  1696. /*
  1697. * We must turn off quotas.
  1698. */
  1699. ASSERT(mp->m_quotainfo != NULL);
  1700. ASSERT(xfs_Gqm != NULL);
  1701. xfs_qm_destroy_quotainfo(mp);
  1702. if (xfs_mount_reset_sbqflags(mp)) {
  1703. cmn_err(CE_WARN, "XFS quotacheck %s: "
  1704. "Failed to reset quota flags.", mp->m_fsname);
  1705. }
  1706. } else {
  1707. cmn_err(CE_NOTE, "XFS quotacheck %s: Done.", mp->m_fsname);
  1708. }
  1709. return (error);
  1710. }
  1711. /*
  1712. * This is called after the superblock has been read in and we're ready to
  1713. * iget the quota inodes.
  1714. */
  1715. STATIC int
  1716. xfs_qm_init_quotainos(
  1717. xfs_mount_t *mp)
  1718. {
  1719. xfs_inode_t *uip, *gip;
  1720. int error;
  1721. __int64_t sbflags;
  1722. uint flags;
  1723. ASSERT(mp->m_quotainfo);
  1724. uip = gip = NULL;
  1725. sbflags = 0;
  1726. flags = 0;
  1727. /*
  1728. * Get the uquota and gquota inodes
  1729. */
  1730. if (xfs_sb_version_hasquota(&mp->m_sb)) {
  1731. if (XFS_IS_UQUOTA_ON(mp) &&
  1732. mp->m_sb.sb_uquotino != NULLFSINO) {
  1733. ASSERT(mp->m_sb.sb_uquotino > 0);
  1734. if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  1735. 0, 0, &uip, 0)))
  1736. return XFS_ERROR(error);
  1737. }
  1738. if (XFS_IS_OQUOTA_ON(mp) &&
  1739. mp->m_sb.sb_gquotino != NULLFSINO) {
  1740. ASSERT(mp->m_sb.sb_gquotino > 0);
  1741. if ((error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  1742. 0, 0, &gip, 0))) {
  1743. if (uip)
  1744. IRELE(uip);
  1745. return XFS_ERROR(error);
  1746. }
  1747. }
  1748. } else {
  1749. flags |= XFS_QMOPT_SBVERSION;
  1750. sbflags |= (XFS_SB_VERSIONNUM | XFS_SB_UQUOTINO |
  1751. XFS_SB_GQUOTINO | XFS_SB_QFLAGS);
  1752. }
  1753. /*
  1754. * Create the two inodes, if they don't exist already. The changes
  1755. * made above will get added to a transaction and logged in one of
  1756. * the qino_alloc calls below. If the device is readonly,
  1757. * temporarily switch to read-write to do this.
  1758. */
  1759. if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
  1760. if ((error = xfs_qm_qino_alloc(mp, &uip,
  1761. sbflags | XFS_SB_UQUOTINO,
  1762. flags | XFS_QMOPT_UQUOTA)))
  1763. return XFS_ERROR(error);
  1764. flags &= ~XFS_QMOPT_SBVERSION;
  1765. }
  1766. if (XFS_IS_OQUOTA_ON(mp) && gip == NULL) {
  1767. flags |= (XFS_IS_GQUOTA_ON(mp) ?
  1768. XFS_QMOPT_GQUOTA : XFS_QMOPT_PQUOTA);
  1769. error = xfs_qm_qino_alloc(mp, &gip,
  1770. sbflags | XFS_SB_GQUOTINO, flags);
  1771. if (error) {
  1772. if (uip)
  1773. IRELE(uip);
  1774. return XFS_ERROR(error);
  1775. }
  1776. }
  1777. XFS_QI_UQIP(mp) = uip;
  1778. XFS_QI_GQIP(mp) = gip;
  1779. return 0;
  1780. }
  1781. /*
  1782. * Traverse the freelist of dquots and attempt to reclaim a maximum of
  1783. * 'howmany' dquots. This operation races with dqlookup(), and attempts to
  1784. * favor the lookup function ...
  1785. * XXXsup merge this with qm_reclaim_one().
  1786. */
  1787. STATIC int
  1788. xfs_qm_shake_freelist(
  1789. int howmany)
  1790. {
  1791. int nreclaimed;
  1792. xfs_dqhash_t *hash;
  1793. xfs_dquot_t *dqp, *nextdqp;
  1794. int restarts;
  1795. int nflushes;
  1796. if (howmany <= 0)
  1797. return 0;
  1798. nreclaimed = 0;
  1799. restarts = 0;
  1800. nflushes = 0;
  1801. #ifdef QUOTADEBUG
  1802. cmn_err(CE_DEBUG, "Shake free 0x%x", howmany);
  1803. #endif
  1804. /* lock order is : hashchainlock, freelistlock, mplistlock */
  1805. tryagain:
  1806. xfs_qm_freelist_lock(xfs_Gqm);
  1807. for (dqp = xfs_Gqm->qm_dqfreelist.qh_next;
  1808. ((dqp != (xfs_dquot_t *) &xfs_Gqm->qm_dqfreelist) &&
  1809. nreclaimed < howmany); ) {
  1810. xfs_dqlock(dqp);
  1811. /*
  1812. * We are racing with dqlookup here. Naturally we don't
  1813. * want to reclaim a dquot that lookup wants.
  1814. */
  1815. if (dqp->dq_flags & XFS_DQ_WANT) {
  1816. xfs_dqunlock(dqp);
  1817. xfs_qm_freelist_unlock(xfs_Gqm);
  1818. if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
  1819. return nreclaimed;
  1820. XQM_STATS_INC(xqmstats.xs_qm_dqwants);
  1821. goto tryagain;
  1822. }
  1823. /*
  1824. * If the dquot is inactive, we are assured that it is
  1825. * not on the mplist or the hashlist, and that makes our
  1826. * life easier.
  1827. */
  1828. if (dqp->dq_flags & XFS_DQ_INACTIVE) {
  1829. ASSERT(dqp->q_mount == NULL);
  1830. ASSERT(! XFS_DQ_IS_DIRTY(dqp));
  1831. ASSERT(dqp->HL_PREVP == NULL);
  1832. ASSERT(dqp->MPL_PREVP == NULL);
  1833. XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
  1834. nextdqp = dqp->dq_flnext;
  1835. goto off_freelist;
  1836. }
  1837. ASSERT(dqp->MPL_PREVP);
  1838. /*
  1839. * Try to grab the flush lock. If this dquot is in the process of
  1840. * getting flushed to disk, we don't want to reclaim it.
  1841. */
  1842. if (!xfs_dqflock_nowait(dqp)) {
  1843. xfs_dqunlock(dqp);
  1844. dqp = dqp->dq_flnext;
  1845. continue;
  1846. }
  1847. /*
  1848. * We have the flush lock so we know that this is not in the
  1849. * process of being flushed. So, if this is dirty, flush it
  1850. * DELWRI so that we don't get a freelist infested with
  1851. * dirty dquots.
  1852. */
  1853. if (XFS_DQ_IS_DIRTY(dqp)) {
  1854. int error;
  1855. xfs_dqtrace_entry(dqp, "DQSHAKE: DQDIRTY");
  1856. /*
  1857. * We flush it delayed write, so don't bother
  1858. * releasing the mplock.
  1859. */
  1860. error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
  1861. if (error) {
  1862. xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
  1863. "xfs_qm_dqflush_all: dquot %p flush failed", dqp);
  1864. }
  1865. xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
  1866. dqp = dqp->dq_flnext;
  1867. continue;
  1868. }
  1869. /*
  1870. * We're trying to get the hashlock out of order. This races
  1871. * with dqlookup; so, we giveup and goto the next dquot if
  1872. * we couldn't get the hashlock. This way, we won't starve
  1873. * a dqlookup process that holds the hashlock that is
  1874. * waiting for the freelist lock.
  1875. */
  1876. if (! xfs_qm_dqhashlock_nowait(dqp)) {
  1877. xfs_dqfunlock(dqp);
  1878. xfs_dqunlock(dqp);
  1879. dqp = dqp->dq_flnext;
  1880. continue;
  1881. }
  1882. /*
  1883. * This races with dquot allocation code as well as dqflush_all
  1884. * and reclaim code. So, if we failed to grab the mplist lock,
  1885. * giveup everything and start over.
  1886. */
  1887. hash = dqp->q_hash;
  1888. ASSERT(hash);
  1889. if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
  1890. /* XXX put a sentinel so that we can come back here */
  1891. xfs_dqfunlock(dqp);
  1892. xfs_dqunlock(dqp);
  1893. XFS_DQ_HASH_UNLOCK(hash);
  1894. xfs_qm_freelist_unlock(xfs_Gqm);
  1895. if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
  1896. return nreclaimed;
  1897. goto tryagain;
  1898. }
  1899. xfs_dqtrace_entry(dqp, "DQSHAKE: UNLINKING");
  1900. #ifdef QUOTADEBUG
  1901. cmn_err(CE_DEBUG, "Shake 0x%p, ID 0x%x\n",
  1902. dqp, be32_to_cpu(dqp->q_core.d_id));
  1903. #endif
  1904. ASSERT(dqp->q_nrefs == 0);
  1905. nextdqp = dqp->dq_flnext;
  1906. XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
  1907. XQM_HASHLIST_REMOVE(hash, dqp);
  1908. xfs_dqfunlock(dqp);
  1909. xfs_qm_mplist_unlock(dqp->q_mount);
  1910. XFS_DQ_HASH_UNLOCK(hash);
  1911. off_freelist:
  1912. XQM_FREELIST_REMOVE(dqp);
  1913. xfs_dqunlock(dqp);
  1914. nreclaimed++;
  1915. XQM_STATS_INC(xqmstats.xs_qm_dqshake_reclaims);
  1916. xfs_qm_dqdestroy(dqp);
  1917. dqp = nextdqp;
  1918. }
  1919. xfs_qm_freelist_unlock(xfs_Gqm);
  1920. return nreclaimed;
  1921. }
  1922. /*
  1923. * The kmem_shake interface is invoked when memory is running low.
  1924. */
  1925. /* ARGSUSED */
  1926. STATIC int
  1927. xfs_qm_shake(int nr_to_scan, gfp_t gfp_mask)
  1928. {
  1929. int ndqused, nfree, n;
  1930. if (!kmem_shake_allow(gfp_mask))
  1931. return 0;
  1932. if (!xfs_Gqm)
  1933. return 0;
  1934. nfree = xfs_Gqm->qm_dqfreelist.qh_nelems; /* free dquots */
  1935. /* incore dquots in all f/s's */
  1936. ndqused = atomic_read(&xfs_Gqm->qm_totaldquots) - nfree;
  1937. ASSERT(ndqused >= 0);
  1938. if (nfree <= ndqused && nfree < ndquot)
  1939. return 0;
  1940. ndqused *= xfs_Gqm->qm_dqfree_ratio; /* target # of free dquots */
  1941. n = nfree - ndqused - ndquot; /* # over target */
  1942. return xfs_qm_shake_freelist(MAX(nfree, n));
  1943. }
  1944. /*
  1945. * Just pop the least recently used dquot off the freelist and
  1946. * recycle it. The returned dquot is locked.
  1947. */
  1948. STATIC xfs_dquot_t *
  1949. xfs_qm_dqreclaim_one(void)
  1950. {
  1951. xfs_dquot_t *dqpout;
  1952. xfs_dquot_t *dqp;
  1953. int restarts;
  1954. int nflushes;
  1955. restarts = 0;
  1956. dqpout = NULL;
  1957. nflushes = 0;
  1958. /* lockorder: hashchainlock, freelistlock, mplistlock, dqlock, dqflock */
  1959. startagain:
  1960. xfs_qm_freelist_lock(xfs_Gqm);
  1961. FOREACH_DQUOT_IN_FREELIST(dqp, &(xfs_Gqm->qm_dqfreelist)) {
  1962. xfs_dqlock(dqp);
  1963. /*
  1964. * We are racing with dqlookup here. Naturally we don't
  1965. * want to reclaim a dquot that lookup wants. We release the
  1966. * freelist lock and start over, so that lookup will grab
  1967. * both the dquot and the freelistlock.
  1968. */
  1969. if (dqp->dq_flags & XFS_DQ_WANT) {
  1970. ASSERT(! (dqp->dq_flags & XFS_DQ_INACTIVE));
  1971. xfs_dqtrace_entry(dqp, "DQRECLAIM: DQWANT");
  1972. xfs_dqunlock(dqp);
  1973. xfs_qm_freelist_unlock(xfs_Gqm);
  1974. if (++restarts >= XFS_QM_RECLAIM_MAX_RESTARTS)
  1975. return NULL;
  1976. XQM_STATS_INC(xqmstats.xs_qm_dqwants);
  1977. goto startagain;
  1978. }
  1979. /*
  1980. * If the dquot is inactive, we are assured that it is
  1981. * not on the mplist or the hashlist, and that makes our
  1982. * life easier.
  1983. */
  1984. if (dqp->dq_flags & XFS_DQ_INACTIVE) {
  1985. ASSERT(dqp->q_mount == NULL);
  1986. ASSERT(! XFS_DQ_IS_DIRTY(dqp));
  1987. ASSERT(dqp->HL_PREVP == NULL);
  1988. ASSERT(dqp->MPL_PREVP == NULL);
  1989. XQM_FREELIST_REMOVE(dqp);
  1990. xfs_dqunlock(dqp);
  1991. dqpout = dqp;
  1992. XQM_STATS_INC(xqmstats.xs_qm_dqinact_reclaims);
  1993. break;
  1994. }
  1995. ASSERT(dqp->q_hash);
  1996. ASSERT(dqp->MPL_PREVP);
  1997. /*
  1998. * Try to grab the flush lock. If this dquot is in the process of
  1999. * getting flushed to disk, we don't want to reclaim it.
  2000. */
  2001. if (!xfs_dqflock_nowait(dqp)) {
  2002. xfs_dqunlock(dqp);
  2003. continue;
  2004. }
  2005. /*
  2006. * We have the flush lock so we know that this is not in the
  2007. * process of being flushed. So, if this is dirty, flush it
  2008. * DELWRI so that we don't get a freelist infested with
  2009. * dirty dquots.
  2010. */
  2011. if (XFS_DQ_IS_DIRTY(dqp)) {
  2012. int error;
  2013. xfs_dqtrace_entry(dqp, "DQRECLAIM: DQDIRTY");
  2014. /*
  2015. * We flush it delayed write, so don't bother
  2016. * releasing the freelist lock.
  2017. */
  2018. error = xfs_qm_dqflush(dqp, XFS_QMOPT_DELWRI);
  2019. if (error) {
  2020. xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
  2021. "xfs_qm_dqreclaim: dquot %p flush failed", dqp);
  2022. }
  2023. xfs_dqunlock(dqp); /* dqflush unlocks dqflock */
  2024. continue;
  2025. }
  2026. if (! xfs_qm_mplist_nowait(dqp->q_mount)) {
  2027. xfs_dqfunlock(dqp);
  2028. xfs_dqunlock(dqp);
  2029. continue;
  2030. }
  2031. if (! xfs_qm_dqhashlock_nowait(dqp))
  2032. goto mplistunlock;
  2033. ASSERT(dqp->q_nrefs == 0);
  2034. xfs_dqtrace_entry(dqp, "DQRECLAIM: UNLINKING");
  2035. XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(dqp->q_mount)), dqp);
  2036. XQM_HASHLIST_REMOVE(dqp->q_hash, dqp);
  2037. XQM_FREELIST_REMOVE(dqp);
  2038. dqpout = dqp;
  2039. XFS_DQ_HASH_UNLOCK(dqp->q_hash);
  2040. mplistunlock:
  2041. xfs_qm_mplist_unlock(dqp->q_mount);
  2042. xfs_dqfunlock(dqp);
  2043. xfs_dqunlock(dqp);
  2044. if (dqpout)
  2045. break;
  2046. }
  2047. xfs_qm_freelist_unlock(xfs_Gqm);
  2048. return dqpout;
  2049. }
  2050. /*------------------------------------------------------------------*/
  2051. /*
  2052. * Return a new incore dquot. Depending on the number of
  2053. * dquots in the system, we either allocate a new one on the kernel heap,
  2054. * or reclaim a free one.
  2055. * Return value is B_TRUE if we allocated a new dquot, B_FALSE if we managed
  2056. * to reclaim an existing one from the freelist.
  2057. */
  2058. boolean_t
  2059. xfs_qm_dqalloc_incore(
  2060. xfs_dquot_t **O_dqpp)
  2061. {
  2062. xfs_dquot_t *dqp;
  2063. /*
  2064. * Check against high water mark to see if we want to pop
  2065. * a nincompoop dquot off the freelist.
  2066. */
  2067. if (atomic_read(&xfs_Gqm->qm_totaldquots) >= ndquot) {
  2068. /*
  2069. * Try to recycle a dquot from the freelist.
  2070. */
  2071. if ((dqp = xfs_qm_dqreclaim_one())) {
  2072. XQM_STATS_INC(xqmstats.xs_qm_dqreclaims);
  2073. /*
  2074. * Just zero the core here. The rest will get
  2075. * reinitialized by caller. XXX we shouldn't even
  2076. * do this zero ...
  2077. */
  2078. memset(&dqp->q_core, 0, sizeof(dqp->q_core));
  2079. *O_dqpp = dqp;
  2080. return B_FALSE;
  2081. }
  2082. XQM_STATS_INC(xqmstats.xs_qm_dqreclaim_misses);
  2083. }
  2084. /*
  2085. * Allocate a brand new dquot on the kernel heap and return it
  2086. * to the caller to initialize.
  2087. */
  2088. ASSERT(xfs_Gqm->qm_dqzone != NULL);
  2089. *O_dqpp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
  2090. atomic_inc(&xfs_Gqm->qm_totaldquots);
  2091. return B_TRUE;
  2092. }
  2093. /*
  2094. * Start a transaction and write the incore superblock changes to
  2095. * disk. flags parameter indicates which fields have changed.
  2096. */
  2097. int
  2098. xfs_qm_write_sb_changes(
  2099. xfs_mount_t *mp,
  2100. __int64_t flags)
  2101. {
  2102. xfs_trans_t *tp;
  2103. int error;
  2104. #ifdef QUOTADEBUG
  2105. cmn_err(CE_NOTE, "Writing superblock quota changes :%s", mp->m_fsname);
  2106. #endif
  2107. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
  2108. if ((error = xfs_trans_reserve(tp, 0,
  2109. mp->m_sb.sb_sectsize + 128, 0,
  2110. 0,
  2111. XFS_DEFAULT_LOG_COUNT))) {
  2112. xfs_trans_cancel(tp, 0);
  2113. return error;
  2114. }
  2115. xfs_mod_sb(tp, flags);
  2116. error = xfs_trans_commit(tp, 0);
  2117. return error;
  2118. }
  2119. /* --------------- utility functions for vnodeops ---------------- */
  2120. /*
  2121. * Given an inode, a uid and gid (from cred_t) make sure that we have
  2122. * allocated relevant dquot(s) on disk, and that we won't exceed inode
  2123. * quotas by creating this file.
  2124. * This also attaches dquot(s) to the given inode after locking it,
  2125. * and returns the dquots corresponding to the uid and/or gid.
  2126. *
  2127. * in : inode (unlocked)
  2128. * out : udquot, gdquot with references taken and unlocked
  2129. */
  2130. int
  2131. xfs_qm_vop_dqalloc(
  2132. xfs_mount_t *mp,
  2133. xfs_inode_t *ip,
  2134. uid_t uid,
  2135. gid_t gid,
  2136. prid_t prid,
  2137. uint flags,
  2138. xfs_dquot_t **O_udqpp,
  2139. xfs_dquot_t **O_gdqpp)
  2140. {
  2141. int error;
  2142. xfs_dquot_t *uq, *gq;
  2143. uint lockflags;
  2144. if (!XFS_IS_QUOTA_ON(mp))
  2145. return 0;
  2146. lockflags = XFS_ILOCK_EXCL;
  2147. xfs_ilock(ip, lockflags);
  2148. if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
  2149. gid = ip->i_d.di_gid;
  2150. /*
  2151. * Attach the dquot(s) to this inode, doing a dquot allocation
  2152. * if necessary. The dquot(s) will not be locked.
  2153. */
  2154. if (XFS_NOT_DQATTACHED(mp, ip)) {
  2155. if ((error = xfs_qm_dqattach(ip, XFS_QMOPT_DQALLOC |
  2156. XFS_QMOPT_ILOCKED))) {
  2157. xfs_iunlock(ip, lockflags);
  2158. return error;
  2159. }
  2160. }
  2161. uq = gq = NULL;
  2162. if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
  2163. if (ip->i_d.di_uid != uid) {
  2164. /*
  2165. * What we need is the dquot that has this uid, and
  2166. * if we send the inode to dqget, the uid of the inode
  2167. * takes priority over what's sent in the uid argument.
  2168. * We must unlock inode here before calling dqget if
  2169. * we're not sending the inode, because otherwise
  2170. * we'll deadlock by doing trans_reserve while
  2171. * holding ilock.
  2172. */
  2173. xfs_iunlock(ip, lockflags);
  2174. if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t) uid,
  2175. XFS_DQ_USER,
  2176. XFS_QMOPT_DQALLOC |
  2177. XFS_QMOPT_DOWARN,
  2178. &uq))) {
  2179. ASSERT(error != ENOENT);
  2180. return error;
  2181. }
  2182. /*
  2183. * Get the ilock in the right order.
  2184. */
  2185. xfs_dqunlock(uq);
  2186. lockflags = XFS_ILOCK_SHARED;
  2187. xfs_ilock(ip, lockflags);
  2188. } else {
  2189. /*
  2190. * Take an extra reference, because we'll return
  2191. * this to caller
  2192. */
  2193. ASSERT(ip->i_udquot);
  2194. uq = ip->i_udquot;
  2195. xfs_dqlock(uq);
  2196. XFS_DQHOLD(uq);
  2197. xfs_dqunlock(uq);
  2198. }
  2199. }
  2200. if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
  2201. if (ip->i_d.di_gid != gid) {
  2202. xfs_iunlock(ip, lockflags);
  2203. if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)gid,
  2204. XFS_DQ_GROUP,
  2205. XFS_QMOPT_DQALLOC |
  2206. XFS_QMOPT_DOWARN,
  2207. &gq))) {
  2208. if (uq)
  2209. xfs_qm_dqrele(uq);
  2210. ASSERT(error != ENOENT);
  2211. return error;
  2212. }
  2213. xfs_dqunlock(gq);
  2214. lockflags = XFS_ILOCK_SHARED;
  2215. xfs_ilock(ip, lockflags);
  2216. } else {
  2217. ASSERT(ip->i_gdquot);
  2218. gq = ip->i_gdquot;
  2219. xfs_dqlock(gq);
  2220. XFS_DQHOLD(gq);
  2221. xfs_dqunlock(gq);
  2222. }
  2223. } else if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
  2224. if (ip->i_d.di_projid != prid) {
  2225. xfs_iunlock(ip, lockflags);
  2226. if ((error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)prid,
  2227. XFS_DQ_PROJ,
  2228. XFS_QMOPT_DQALLOC |
  2229. XFS_QMOPT_DOWARN,
  2230. &gq))) {
  2231. if (uq)
  2232. xfs_qm_dqrele(uq);
  2233. ASSERT(error != ENOENT);
  2234. return (error);
  2235. }
  2236. xfs_dqunlock(gq);
  2237. lockflags = XFS_ILOCK_SHARED;
  2238. xfs_ilock(ip, lockflags);
  2239. } else {
  2240. ASSERT(ip->i_gdquot);
  2241. gq = ip->i_gdquot;
  2242. xfs_dqlock(gq);
  2243. XFS_DQHOLD(gq);
  2244. xfs_dqunlock(gq);
  2245. }
  2246. }
  2247. if (uq)
  2248. xfs_dqtrace_entry_ino(uq, "DQALLOC", ip);
  2249. xfs_iunlock(ip, lockflags);
  2250. if (O_udqpp)
  2251. *O_udqpp = uq;
  2252. else if (uq)
  2253. xfs_qm_dqrele(uq);
  2254. if (O_gdqpp)
  2255. *O_gdqpp = gq;
  2256. else if (gq)
  2257. xfs_qm_dqrele(gq);
  2258. return 0;
  2259. }
  2260. /*
  2261. * Actually transfer ownership, and do dquot modifications.
  2262. * These were already reserved.
  2263. */
  2264. xfs_dquot_t *
  2265. xfs_qm_vop_chown(
  2266. xfs_trans_t *tp,
  2267. xfs_inode_t *ip,
  2268. xfs_dquot_t **IO_olddq,
  2269. xfs_dquot_t *newdq)
  2270. {
  2271. xfs_dquot_t *prevdq;
  2272. uint bfield = XFS_IS_REALTIME_INODE(ip) ?
  2273. XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
  2274. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2275. ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
  2276. /* old dquot */
  2277. prevdq = *IO_olddq;
  2278. ASSERT(prevdq);
  2279. ASSERT(prevdq != newdq);
  2280. xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
  2281. xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
  2282. /* the sparkling new dquot */
  2283. xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
  2284. xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
  2285. /*
  2286. * Take an extra reference, because the inode
  2287. * is going to keep this dquot pointer even
  2288. * after the trans_commit.
  2289. */
  2290. xfs_dqlock(newdq);
  2291. XFS_DQHOLD(newdq);
  2292. xfs_dqunlock(newdq);
  2293. *IO_olddq = newdq;
  2294. return prevdq;
  2295. }
  2296. /*
  2297. * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
  2298. */
  2299. int
  2300. xfs_qm_vop_chown_reserve(
  2301. xfs_trans_t *tp,
  2302. xfs_inode_t *ip,
  2303. xfs_dquot_t *udqp,
  2304. xfs_dquot_t *gdqp,
  2305. uint flags)
  2306. {
  2307. int error;
  2308. xfs_mount_t *mp;
  2309. uint delblks, blkflags, prjflags = 0;
  2310. xfs_dquot_t *unresudq, *unresgdq, *delblksudq, *delblksgdq;
  2311. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2312. mp = ip->i_mount;
  2313. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  2314. delblks = ip->i_delayed_blks;
  2315. delblksudq = delblksgdq = unresudq = unresgdq = NULL;
  2316. blkflags = XFS_IS_REALTIME_INODE(ip) ?
  2317. XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
  2318. if (XFS_IS_UQUOTA_ON(mp) && udqp &&
  2319. ip->i_d.di_uid != (uid_t)be32_to_cpu(udqp->q_core.d_id)) {
  2320. delblksudq = udqp;
  2321. /*
  2322. * If there are delayed allocation blocks, then we have to
  2323. * unreserve those from the old dquot, and add them to the
  2324. * new dquot.
  2325. */
  2326. if (delblks) {
  2327. ASSERT(ip->i_udquot);
  2328. unresudq = ip->i_udquot;
  2329. }
  2330. }
  2331. if (XFS_IS_OQUOTA_ON(ip->i_mount) && gdqp) {
  2332. if (XFS_IS_PQUOTA_ON(ip->i_mount) &&
  2333. ip->i_d.di_projid != be32_to_cpu(gdqp->q_core.d_id))
  2334. prjflags = XFS_QMOPT_ENOSPC;
  2335. if (prjflags ||
  2336. (XFS_IS_GQUOTA_ON(ip->i_mount) &&
  2337. ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id))) {
  2338. delblksgdq = gdqp;
  2339. if (delblks) {
  2340. ASSERT(ip->i_gdquot);
  2341. unresgdq = ip->i_gdquot;
  2342. }
  2343. }
  2344. }
  2345. if ((error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
  2346. delblksudq, delblksgdq, ip->i_d.di_nblocks, 1,
  2347. flags | blkflags | prjflags)))
  2348. return (error);
  2349. /*
  2350. * Do the delayed blks reservations/unreservations now. Since, these
  2351. * are done without the help of a transaction, if a reservation fails
  2352. * its previous reservations won't be automatically undone by trans
  2353. * code. So, we have to do it manually here.
  2354. */
  2355. if (delblks) {
  2356. /*
  2357. * Do the reservations first. Unreservation can't fail.
  2358. */
  2359. ASSERT(delblksudq || delblksgdq);
  2360. ASSERT(unresudq || unresgdq);
  2361. if ((error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  2362. delblksudq, delblksgdq, (xfs_qcnt_t)delblks, 0,
  2363. flags | blkflags | prjflags)))
  2364. return (error);
  2365. xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  2366. unresudq, unresgdq, -((xfs_qcnt_t)delblks), 0,
  2367. blkflags);
  2368. }
  2369. return (0);
  2370. }
  2371. int
  2372. xfs_qm_vop_rename_dqattach(
  2373. xfs_inode_t **i_tab)
  2374. {
  2375. xfs_inode_t *ip;
  2376. int i;
  2377. int error;
  2378. ip = i_tab[0];
  2379. if (! XFS_IS_QUOTA_ON(ip->i_mount))
  2380. return 0;
  2381. if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
  2382. error = xfs_qm_dqattach(ip, 0);
  2383. if (error)
  2384. return error;
  2385. }
  2386. for (i = 1; (i < 4 && i_tab[i]); i++) {
  2387. /*
  2388. * Watch out for duplicate entries in the table.
  2389. */
  2390. if ((ip = i_tab[i]) != i_tab[i-1]) {
  2391. if (XFS_NOT_DQATTACHED(ip->i_mount, ip)) {
  2392. error = xfs_qm_dqattach(ip, 0);
  2393. if (error)
  2394. return error;
  2395. }
  2396. }
  2397. }
  2398. return 0;
  2399. }
  2400. void
  2401. xfs_qm_vop_dqattach_and_dqmod_newinode(
  2402. xfs_trans_t *tp,
  2403. xfs_inode_t *ip,
  2404. xfs_dquot_t *udqp,
  2405. xfs_dquot_t *gdqp)
  2406. {
  2407. if (!XFS_IS_QUOTA_ON(tp->t_mountp))
  2408. return;
  2409. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2410. ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
  2411. if (udqp) {
  2412. xfs_dqlock(udqp);
  2413. XFS_DQHOLD(udqp);
  2414. xfs_dqunlock(udqp);
  2415. ASSERT(ip->i_udquot == NULL);
  2416. ip->i_udquot = udqp;
  2417. ASSERT(XFS_IS_UQUOTA_ON(tp->t_mountp));
  2418. ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
  2419. xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
  2420. }
  2421. if (gdqp) {
  2422. xfs_dqlock(gdqp);
  2423. XFS_DQHOLD(gdqp);
  2424. xfs_dqunlock(gdqp);
  2425. ASSERT(ip->i_gdquot == NULL);
  2426. ip->i_gdquot = gdqp;
  2427. ASSERT(XFS_IS_OQUOTA_ON(tp->t_mountp));
  2428. ASSERT((XFS_IS_GQUOTA_ON(tp->t_mountp) ?
  2429. ip->i_d.di_gid : ip->i_d.di_projid) ==
  2430. be32_to_cpu(gdqp->q_core.d_id));
  2431. xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
  2432. }
  2433. }
  2434. /* ------------- list stuff -----------------*/
  2435. STATIC void
  2436. xfs_qm_freelist_init(xfs_frlist_t *ql)
  2437. {
  2438. ql->qh_next = ql->qh_prev = (xfs_dquot_t *) ql;
  2439. mutex_init(&ql->qh_lock);
  2440. ql->qh_version = 0;
  2441. ql->qh_nelems = 0;
  2442. }
  2443. STATIC void
  2444. xfs_qm_freelist_destroy(xfs_frlist_t *ql)
  2445. {
  2446. xfs_dquot_t *dqp, *nextdqp;
  2447. mutex_lock(&ql->qh_lock);
  2448. for (dqp = ql->qh_next;
  2449. dqp != (xfs_dquot_t *)ql; ) {
  2450. xfs_dqlock(dqp);
  2451. nextdqp = dqp->dq_flnext;
  2452. #ifdef QUOTADEBUG
  2453. cmn_err(CE_DEBUG, "FREELIST destroy 0x%p", dqp);
  2454. #endif
  2455. XQM_FREELIST_REMOVE(dqp);
  2456. xfs_dqunlock(dqp);
  2457. xfs_qm_dqdestroy(dqp);
  2458. dqp = nextdqp;
  2459. }
  2460. mutex_unlock(&ql->qh_lock);
  2461. mutex_destroy(&ql->qh_lock);
  2462. ASSERT(ql->qh_nelems == 0);
  2463. }
  2464. STATIC void
  2465. xfs_qm_freelist_insert(xfs_frlist_t *ql, xfs_dquot_t *dq)
  2466. {
  2467. dq->dq_flnext = ql->qh_next;
  2468. dq->dq_flprev = (xfs_dquot_t *)ql;
  2469. ql->qh_next = dq;
  2470. dq->dq_flnext->dq_flprev = dq;
  2471. xfs_Gqm->qm_dqfreelist.qh_nelems++;
  2472. xfs_Gqm->qm_dqfreelist.qh_version++;
  2473. }
  2474. void
  2475. xfs_qm_freelist_unlink(xfs_dquot_t *dq)
  2476. {
  2477. xfs_dquot_t *next = dq->dq_flnext;
  2478. xfs_dquot_t *prev = dq->dq_flprev;
  2479. next->dq_flprev = prev;
  2480. prev->dq_flnext = next;
  2481. dq->dq_flnext = dq->dq_flprev = dq;
  2482. xfs_Gqm->qm_dqfreelist.qh_nelems--;
  2483. xfs_Gqm->qm_dqfreelist.qh_version++;
  2484. }
  2485. void
  2486. xfs_qm_freelist_append(xfs_frlist_t *ql, xfs_dquot_t *dq)
  2487. {
  2488. xfs_qm_freelist_insert((xfs_frlist_t *)ql->qh_prev, dq);
  2489. }
  2490. STATIC int
  2491. xfs_qm_dqhashlock_nowait(
  2492. xfs_dquot_t *dqp)
  2493. {
  2494. int locked;
  2495. locked = mutex_trylock(&((dqp)->q_hash->qh_lock));
  2496. return locked;
  2497. }
  2498. int
  2499. xfs_qm_freelist_lock_nowait(
  2500. xfs_qm_t *xqm)
  2501. {
  2502. int locked;
  2503. locked = mutex_trylock(&(xqm->qm_dqfreelist.qh_lock));
  2504. return locked;
  2505. }
  2506. STATIC int
  2507. xfs_qm_mplist_nowait(
  2508. xfs_mount_t *mp)
  2509. {
  2510. int locked;
  2511. ASSERT(mp->m_quotainfo);
  2512. locked = mutex_trylock(&(XFS_QI_MPLLOCK(mp)));
  2513. return locked;
  2514. }