xfs_qm.c 70 KB

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