xfs_qm.c 71 KB

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