xfs_qm.c 66 KB

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