xfs_qm.c 71 KB

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