xfs_qm.c 68 KB

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