xfs_qm.c 52 KB

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