xfs_qm.c 69 KB

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