xfs_qm.c 70 KB

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