xfs_qm.c 66 KB

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