xfs_qm.c 66 KB

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