xfs_qm.c 70 KB

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