xfs_qm.c 59 KB

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