xfs_qm.c 71 KB

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