xfs_qm.c 69 KB

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