xfs_qm.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_ialloc.h"
  17. #include "xfs_itable.h"
  18. #include "xfs_quota.h"
  19. #include "xfs_error.h"
  20. #include "xfs_bmap.h"
  21. #include "xfs_bmap_btree.h"
  22. #include "xfs_bmap_util.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_trans_space.h"
  25. #include "xfs_qm.h"
  26. #include "xfs_trace.h"
  27. #include "xfs_icache.h"
  28. #include "xfs_cksum.h"
  29. /*
  30. * The global quota manager. There is only one of these for the entire
  31. * system, _not_ one per file system. XQM keeps track of the overall
  32. * quota functionality, including maintaining the freelist and hash
  33. * tables of dquots.
  34. */
  35. STATIC int xfs_qm_init_quotainos(xfs_mount_t *);
  36. STATIC int xfs_qm_init_quotainfo(xfs_mount_t *);
  37. STATIC void xfs_qm_destroy_quotainos(xfs_quotainfo_t *qi);
  38. STATIC void xfs_qm_dqfree_one(struct xfs_dquot *dqp);
  39. /*
  40. * We use the batch lookup interface to iterate over the dquots as it
  41. * currently is the only interface into the radix tree code that allows
  42. * fuzzy lookups instead of exact matches. Holding the lock over multiple
  43. * operations is fine as all callers are used either during mount/umount
  44. * or quotaoff.
  45. */
  46. #define XFS_DQ_LOOKUP_BATCH 32
  47. STATIC int
  48. xfs_qm_dquot_walk(
  49. struct xfs_mount *mp,
  50. int type,
  51. int (*execute)(struct xfs_dquot *dqp, void *data),
  52. void *data)
  53. {
  54. struct xfs_quotainfo *qi = mp->m_quotainfo;
  55. struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
  56. uint32_t next_index;
  57. int last_error = 0;
  58. int skipped;
  59. int nr_found;
  60. restart:
  61. skipped = 0;
  62. next_index = 0;
  63. nr_found = 0;
  64. while (1) {
  65. struct xfs_dquot *batch[XFS_DQ_LOOKUP_BATCH];
  66. int error = 0;
  67. int i;
  68. mutex_lock(&qi->qi_tree_lock);
  69. nr_found = radix_tree_gang_lookup(tree, (void **)batch,
  70. next_index, XFS_DQ_LOOKUP_BATCH);
  71. if (!nr_found) {
  72. mutex_unlock(&qi->qi_tree_lock);
  73. break;
  74. }
  75. for (i = 0; i < nr_found; i++) {
  76. struct xfs_dquot *dqp = batch[i];
  77. next_index = be32_to_cpu(dqp->q_core.d_id) + 1;
  78. error = execute(batch[i], data);
  79. if (error == -EAGAIN) {
  80. skipped++;
  81. continue;
  82. }
  83. if (error && last_error != -EFSCORRUPTED)
  84. last_error = error;
  85. }
  86. mutex_unlock(&qi->qi_tree_lock);
  87. /* bail out if the filesystem is corrupted. */
  88. if (last_error == -EFSCORRUPTED) {
  89. skipped = 0;
  90. break;
  91. }
  92. /* we're done if id overflows back to zero */
  93. if (!next_index)
  94. break;
  95. }
  96. if (skipped) {
  97. delay(1);
  98. goto restart;
  99. }
  100. return last_error;
  101. }
  102. /*
  103. * Purge a dquot from all tracking data structures and free it.
  104. */
  105. STATIC int
  106. xfs_qm_dqpurge(
  107. struct xfs_dquot *dqp,
  108. void *data)
  109. {
  110. struct xfs_mount *mp = dqp->q_mount;
  111. struct xfs_quotainfo *qi = mp->m_quotainfo;
  112. xfs_dqlock(dqp);
  113. if ((dqp->dq_flags & XFS_DQ_FREEING) || dqp->q_nrefs != 0) {
  114. xfs_dqunlock(dqp);
  115. return -EAGAIN;
  116. }
  117. dqp->dq_flags |= XFS_DQ_FREEING;
  118. xfs_dqflock(dqp);
  119. /*
  120. * If we are turning this type of quotas off, we don't care
  121. * about the dirty metadata sitting in this dquot. OTOH, if
  122. * we're unmounting, we do care, so we flush it and wait.
  123. */
  124. if (XFS_DQ_IS_DIRTY(dqp)) {
  125. struct xfs_buf *bp = NULL;
  126. int error;
  127. /*
  128. * We don't care about getting disk errors here. We need
  129. * to purge this dquot anyway, so we go ahead regardless.
  130. */
  131. error = xfs_qm_dqflush(dqp, &bp);
  132. if (!error) {
  133. error = xfs_bwrite(bp);
  134. xfs_buf_relse(bp);
  135. }
  136. xfs_dqflock(dqp);
  137. }
  138. ASSERT(atomic_read(&dqp->q_pincount) == 0);
  139. ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
  140. !test_bit(XFS_LI_IN_AIL, &dqp->q_logitem.qli_item.li_flags));
  141. xfs_dqfunlock(dqp);
  142. xfs_dqunlock(dqp);
  143. radix_tree_delete(xfs_dquot_tree(qi, dqp->q_core.d_flags),
  144. be32_to_cpu(dqp->q_core.d_id));
  145. qi->qi_dquots--;
  146. /*
  147. * We move dquots to the freelist as soon as their reference count
  148. * hits zero, so it really should be on the freelist here.
  149. */
  150. ASSERT(!list_empty(&dqp->q_lru));
  151. list_lru_del(&qi->qi_lru, &dqp->q_lru);
  152. XFS_STATS_DEC(mp, xs_qm_dquot_unused);
  153. xfs_qm_dqdestroy(dqp);
  154. return 0;
  155. }
  156. /*
  157. * Purge the dquot cache.
  158. */
  159. void
  160. xfs_qm_dqpurge_all(
  161. struct xfs_mount *mp,
  162. uint flags)
  163. {
  164. if (flags & XFS_QMOPT_UQUOTA)
  165. xfs_qm_dquot_walk(mp, XFS_DQ_USER, xfs_qm_dqpurge, NULL);
  166. if (flags & XFS_QMOPT_GQUOTA)
  167. xfs_qm_dquot_walk(mp, XFS_DQ_GROUP, xfs_qm_dqpurge, NULL);
  168. if (flags & XFS_QMOPT_PQUOTA)
  169. xfs_qm_dquot_walk(mp, XFS_DQ_PROJ, xfs_qm_dqpurge, NULL);
  170. }
  171. /*
  172. * Just destroy the quotainfo structure.
  173. */
  174. void
  175. xfs_qm_unmount(
  176. struct xfs_mount *mp)
  177. {
  178. if (mp->m_quotainfo) {
  179. xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
  180. xfs_qm_destroy_quotainfo(mp);
  181. }
  182. }
  183. /*
  184. * Called from the vfsops layer.
  185. */
  186. void
  187. xfs_qm_unmount_quotas(
  188. xfs_mount_t *mp)
  189. {
  190. /*
  191. * Release the dquots that root inode, et al might be holding,
  192. * before we flush quotas and blow away the quotainfo structure.
  193. */
  194. ASSERT(mp->m_rootip);
  195. xfs_qm_dqdetach(mp->m_rootip);
  196. if (mp->m_rbmip)
  197. xfs_qm_dqdetach(mp->m_rbmip);
  198. if (mp->m_rsumip)
  199. xfs_qm_dqdetach(mp->m_rsumip);
  200. /*
  201. * Release the quota inodes.
  202. */
  203. if (mp->m_quotainfo) {
  204. if (mp->m_quotainfo->qi_uquotaip) {
  205. xfs_irele(mp->m_quotainfo->qi_uquotaip);
  206. mp->m_quotainfo->qi_uquotaip = NULL;
  207. }
  208. if (mp->m_quotainfo->qi_gquotaip) {
  209. xfs_irele(mp->m_quotainfo->qi_gquotaip);
  210. mp->m_quotainfo->qi_gquotaip = NULL;
  211. }
  212. if (mp->m_quotainfo->qi_pquotaip) {
  213. xfs_irele(mp->m_quotainfo->qi_pquotaip);
  214. mp->m_quotainfo->qi_pquotaip = NULL;
  215. }
  216. }
  217. }
  218. STATIC int
  219. xfs_qm_dqattach_one(
  220. xfs_inode_t *ip,
  221. xfs_dqid_t id,
  222. uint type,
  223. bool doalloc,
  224. xfs_dquot_t **IO_idqpp)
  225. {
  226. xfs_dquot_t *dqp;
  227. int error;
  228. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  229. error = 0;
  230. /*
  231. * See if we already have it in the inode itself. IO_idqpp is &i_udquot
  232. * or &i_gdquot. This made the code look weird, but made the logic a lot
  233. * simpler.
  234. */
  235. dqp = *IO_idqpp;
  236. if (dqp) {
  237. trace_xfs_dqattach_found(dqp);
  238. return 0;
  239. }
  240. /*
  241. * Find the dquot from somewhere. This bumps the reference count of
  242. * dquot and returns it locked. This can return ENOENT if dquot didn't
  243. * exist on disk and we didn't ask it to allocate; ESRCH if quotas got
  244. * turned off suddenly.
  245. */
  246. error = xfs_qm_dqget_inode(ip, type, doalloc, &dqp);
  247. if (error)
  248. return error;
  249. trace_xfs_dqattach_get(dqp);
  250. /*
  251. * dqget may have dropped and re-acquired the ilock, but it guarantees
  252. * that the dquot returned is the one that should go in the inode.
  253. */
  254. *IO_idqpp = dqp;
  255. xfs_dqunlock(dqp);
  256. return 0;
  257. }
  258. static bool
  259. xfs_qm_need_dqattach(
  260. struct xfs_inode *ip)
  261. {
  262. struct xfs_mount *mp = ip->i_mount;
  263. if (!XFS_IS_QUOTA_RUNNING(mp))
  264. return false;
  265. if (!XFS_IS_QUOTA_ON(mp))
  266. return false;
  267. if (!XFS_NOT_DQATTACHED(mp, ip))
  268. return false;
  269. if (xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
  270. return false;
  271. return true;
  272. }
  273. /*
  274. * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
  275. * into account.
  276. * If @doalloc is true, the dquot(s) will be allocated if needed.
  277. * Inode may get unlocked and relocked in here, and the caller must deal with
  278. * the consequences.
  279. */
  280. int
  281. xfs_qm_dqattach_locked(
  282. xfs_inode_t *ip,
  283. bool doalloc)
  284. {
  285. xfs_mount_t *mp = ip->i_mount;
  286. int error = 0;
  287. if (!xfs_qm_need_dqattach(ip))
  288. return 0;
  289. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  290. if (XFS_IS_UQUOTA_ON(mp) && !ip->i_udquot) {
  291. error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
  292. doalloc, &ip->i_udquot);
  293. if (error)
  294. goto done;
  295. ASSERT(ip->i_udquot);
  296. }
  297. if (XFS_IS_GQUOTA_ON(mp) && !ip->i_gdquot) {
  298. error = xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
  299. doalloc, &ip->i_gdquot);
  300. if (error)
  301. goto done;
  302. ASSERT(ip->i_gdquot);
  303. }
  304. if (XFS_IS_PQUOTA_ON(mp) && !ip->i_pdquot) {
  305. error = xfs_qm_dqattach_one(ip, xfs_get_projid(ip), XFS_DQ_PROJ,
  306. doalloc, &ip->i_pdquot);
  307. if (error)
  308. goto done;
  309. ASSERT(ip->i_pdquot);
  310. }
  311. done:
  312. /*
  313. * Don't worry about the dquots that we may have attached before any
  314. * error - they'll get detached later if it has not already been done.
  315. */
  316. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  317. return error;
  318. }
  319. int
  320. xfs_qm_dqattach(
  321. struct xfs_inode *ip)
  322. {
  323. int error;
  324. if (!xfs_qm_need_dqattach(ip))
  325. return 0;
  326. xfs_ilock(ip, XFS_ILOCK_EXCL);
  327. error = xfs_qm_dqattach_locked(ip, false);
  328. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  329. return error;
  330. }
  331. /*
  332. * Release dquots (and their references) if any.
  333. * The inode should be locked EXCL except when this's called by
  334. * xfs_ireclaim.
  335. */
  336. void
  337. xfs_qm_dqdetach(
  338. xfs_inode_t *ip)
  339. {
  340. if (!(ip->i_udquot || ip->i_gdquot || ip->i_pdquot))
  341. return;
  342. trace_xfs_dquot_dqdetach(ip);
  343. ASSERT(!xfs_is_quota_inode(&ip->i_mount->m_sb, ip->i_ino));
  344. if (ip->i_udquot) {
  345. xfs_qm_dqrele(ip->i_udquot);
  346. ip->i_udquot = NULL;
  347. }
  348. if (ip->i_gdquot) {
  349. xfs_qm_dqrele(ip->i_gdquot);
  350. ip->i_gdquot = NULL;
  351. }
  352. if (ip->i_pdquot) {
  353. xfs_qm_dqrele(ip->i_pdquot);
  354. ip->i_pdquot = NULL;
  355. }
  356. }
  357. struct xfs_qm_isolate {
  358. struct list_head buffers;
  359. struct list_head dispose;
  360. };
  361. static enum lru_status
  362. xfs_qm_dquot_isolate(
  363. struct list_head *item,
  364. struct list_lru_one *lru,
  365. spinlock_t *lru_lock,
  366. void *arg)
  367. __releases(lru_lock) __acquires(lru_lock)
  368. {
  369. struct xfs_dquot *dqp = container_of(item,
  370. struct xfs_dquot, q_lru);
  371. struct xfs_qm_isolate *isol = arg;
  372. if (!xfs_dqlock_nowait(dqp))
  373. goto out_miss_busy;
  374. /*
  375. * This dquot has acquired a reference in the meantime remove it from
  376. * the freelist and try again.
  377. */
  378. if (dqp->q_nrefs) {
  379. xfs_dqunlock(dqp);
  380. XFS_STATS_INC(dqp->q_mount, xs_qm_dqwants);
  381. trace_xfs_dqreclaim_want(dqp);
  382. list_lru_isolate(lru, &dqp->q_lru);
  383. XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
  384. return LRU_REMOVED;
  385. }
  386. /*
  387. * If the dquot is dirty, flush it. If it's already being flushed, just
  388. * skip it so there is time for the IO to complete before we try to
  389. * reclaim it again on the next LRU pass.
  390. */
  391. if (!xfs_dqflock_nowait(dqp)) {
  392. xfs_dqunlock(dqp);
  393. goto out_miss_busy;
  394. }
  395. if (XFS_DQ_IS_DIRTY(dqp)) {
  396. struct xfs_buf *bp = NULL;
  397. int error;
  398. trace_xfs_dqreclaim_dirty(dqp);
  399. /* we have to drop the LRU lock to flush the dquot */
  400. spin_unlock(lru_lock);
  401. error = xfs_qm_dqflush(dqp, &bp);
  402. if (error)
  403. goto out_unlock_dirty;
  404. xfs_buf_delwri_queue(bp, &isol->buffers);
  405. xfs_buf_relse(bp);
  406. goto out_unlock_dirty;
  407. }
  408. xfs_dqfunlock(dqp);
  409. /*
  410. * Prevent lookups now that we are past the point of no return.
  411. */
  412. dqp->dq_flags |= XFS_DQ_FREEING;
  413. xfs_dqunlock(dqp);
  414. ASSERT(dqp->q_nrefs == 0);
  415. list_lru_isolate_move(lru, &dqp->q_lru, &isol->dispose);
  416. XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot_unused);
  417. trace_xfs_dqreclaim_done(dqp);
  418. XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaims);
  419. return LRU_REMOVED;
  420. out_miss_busy:
  421. trace_xfs_dqreclaim_busy(dqp);
  422. XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
  423. return LRU_SKIP;
  424. out_unlock_dirty:
  425. trace_xfs_dqreclaim_busy(dqp);
  426. XFS_STATS_INC(dqp->q_mount, xs_qm_dqreclaim_misses);
  427. xfs_dqunlock(dqp);
  428. spin_lock(lru_lock);
  429. return LRU_RETRY;
  430. }
  431. static unsigned long
  432. xfs_qm_shrink_scan(
  433. struct shrinker *shrink,
  434. struct shrink_control *sc)
  435. {
  436. struct xfs_quotainfo *qi = container_of(shrink,
  437. struct xfs_quotainfo, qi_shrinker);
  438. struct xfs_qm_isolate isol;
  439. unsigned long freed;
  440. int error;
  441. if ((sc->gfp_mask & (__GFP_FS|__GFP_DIRECT_RECLAIM)) != (__GFP_FS|__GFP_DIRECT_RECLAIM))
  442. return 0;
  443. INIT_LIST_HEAD(&isol.buffers);
  444. INIT_LIST_HEAD(&isol.dispose);
  445. freed = list_lru_shrink_walk(&qi->qi_lru, sc,
  446. xfs_qm_dquot_isolate, &isol);
  447. error = xfs_buf_delwri_submit(&isol.buffers);
  448. if (error)
  449. xfs_warn(NULL, "%s: dquot reclaim failed", __func__);
  450. while (!list_empty(&isol.dispose)) {
  451. struct xfs_dquot *dqp;
  452. dqp = list_first_entry(&isol.dispose, struct xfs_dquot, q_lru);
  453. list_del_init(&dqp->q_lru);
  454. xfs_qm_dqfree_one(dqp);
  455. }
  456. return freed;
  457. }
  458. static unsigned long
  459. xfs_qm_shrink_count(
  460. struct shrinker *shrink,
  461. struct shrink_control *sc)
  462. {
  463. struct xfs_quotainfo *qi = container_of(shrink,
  464. struct xfs_quotainfo, qi_shrinker);
  465. return list_lru_shrink_count(&qi->qi_lru, sc);
  466. }
  467. STATIC void
  468. xfs_qm_set_defquota(
  469. xfs_mount_t *mp,
  470. uint type,
  471. xfs_quotainfo_t *qinf)
  472. {
  473. xfs_dquot_t *dqp;
  474. struct xfs_def_quota *defq;
  475. struct xfs_disk_dquot *ddqp;
  476. int error;
  477. error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
  478. if (error)
  479. return;
  480. ddqp = &dqp->q_core;
  481. defq = xfs_get_defquota(dqp, qinf);
  482. /*
  483. * Timers and warnings have been already set, let's just set the
  484. * default limits for this quota type
  485. */
  486. defq->bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
  487. defq->bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
  488. defq->ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
  489. defq->isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
  490. defq->rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
  491. defq->rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
  492. xfs_qm_dqdestroy(dqp);
  493. }
  494. /* Initialize quota time limits from the root dquot. */
  495. static void
  496. xfs_qm_init_timelimits(
  497. struct xfs_mount *mp,
  498. struct xfs_quotainfo *qinf)
  499. {
  500. struct xfs_disk_dquot *ddqp;
  501. struct xfs_dquot *dqp;
  502. uint type;
  503. int error;
  504. qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
  505. qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
  506. qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
  507. qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
  508. qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
  509. qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
  510. /*
  511. * We try to get the limits from the superuser's limits fields.
  512. * This is quite hacky, but it is standard quota practice.
  513. *
  514. * Since we may not have done a quotacheck by this point, just read
  515. * the dquot without attaching it to any hashtables or lists.
  516. *
  517. * Timers and warnings are globally set by the first timer found in
  518. * user/group/proj quota types, otherwise a default value is used.
  519. * This should be split into different fields per quota type.
  520. */
  521. if (XFS_IS_UQUOTA_RUNNING(mp))
  522. type = XFS_DQ_USER;
  523. else if (XFS_IS_GQUOTA_RUNNING(mp))
  524. type = XFS_DQ_GROUP;
  525. else
  526. type = XFS_DQ_PROJ;
  527. error = xfs_qm_dqget_uncached(mp, 0, type, &dqp);
  528. if (error)
  529. return;
  530. ddqp = &dqp->q_core;
  531. /*
  532. * The warnings and timers set the grace period given to
  533. * a user or group before he or she can not perform any
  534. * more writing. If it is zero, a default is used.
  535. */
  536. if (ddqp->d_btimer)
  537. qinf->qi_btimelimit = be32_to_cpu(ddqp->d_btimer);
  538. if (ddqp->d_itimer)
  539. qinf->qi_itimelimit = be32_to_cpu(ddqp->d_itimer);
  540. if (ddqp->d_rtbtimer)
  541. qinf->qi_rtbtimelimit = be32_to_cpu(ddqp->d_rtbtimer);
  542. if (ddqp->d_bwarns)
  543. qinf->qi_bwarnlimit = be16_to_cpu(ddqp->d_bwarns);
  544. if (ddqp->d_iwarns)
  545. qinf->qi_iwarnlimit = be16_to_cpu(ddqp->d_iwarns);
  546. if (ddqp->d_rtbwarns)
  547. qinf->qi_rtbwarnlimit = be16_to_cpu(ddqp->d_rtbwarns);
  548. xfs_qm_dqdestroy(dqp);
  549. }
  550. /*
  551. * This initializes all the quota information that's kept in the
  552. * mount structure
  553. */
  554. STATIC int
  555. xfs_qm_init_quotainfo(
  556. struct xfs_mount *mp)
  557. {
  558. struct xfs_quotainfo *qinf;
  559. int error;
  560. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  561. qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);
  562. error = list_lru_init(&qinf->qi_lru);
  563. if (error)
  564. goto out_free_qinf;
  565. /*
  566. * See if quotainodes are setup, and if not, allocate them,
  567. * and change the superblock accordingly.
  568. */
  569. error = xfs_qm_init_quotainos(mp);
  570. if (error)
  571. goto out_free_lru;
  572. INIT_RADIX_TREE(&qinf->qi_uquota_tree, GFP_NOFS);
  573. INIT_RADIX_TREE(&qinf->qi_gquota_tree, GFP_NOFS);
  574. INIT_RADIX_TREE(&qinf->qi_pquota_tree, GFP_NOFS);
  575. mutex_init(&qinf->qi_tree_lock);
  576. /* mutex used to serialize quotaoffs */
  577. mutex_init(&qinf->qi_quotaofflock);
  578. /* Precalc some constants */
  579. qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
  580. qinf->qi_dqperchunk = xfs_calc_dquots_per_chunk(qinf->qi_dqchunklen);
  581. mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);
  582. xfs_qm_init_timelimits(mp, qinf);
  583. if (XFS_IS_UQUOTA_RUNNING(mp))
  584. xfs_qm_set_defquota(mp, XFS_DQ_USER, qinf);
  585. if (XFS_IS_GQUOTA_RUNNING(mp))
  586. xfs_qm_set_defquota(mp, XFS_DQ_GROUP, qinf);
  587. if (XFS_IS_PQUOTA_RUNNING(mp))
  588. xfs_qm_set_defquota(mp, XFS_DQ_PROJ, qinf);
  589. qinf->qi_shrinker.count_objects = xfs_qm_shrink_count;
  590. qinf->qi_shrinker.scan_objects = xfs_qm_shrink_scan;
  591. qinf->qi_shrinker.seeks = DEFAULT_SEEKS;
  592. qinf->qi_shrinker.flags = SHRINKER_NUMA_AWARE;
  593. error = register_shrinker(&qinf->qi_shrinker);
  594. if (error)
  595. goto out_free_inos;
  596. return 0;
  597. out_free_inos:
  598. mutex_destroy(&qinf->qi_quotaofflock);
  599. mutex_destroy(&qinf->qi_tree_lock);
  600. xfs_qm_destroy_quotainos(qinf);
  601. out_free_lru:
  602. list_lru_destroy(&qinf->qi_lru);
  603. out_free_qinf:
  604. kmem_free(qinf);
  605. mp->m_quotainfo = NULL;
  606. return error;
  607. }
  608. /*
  609. * Gets called when unmounting a filesystem or when all quotas get
  610. * turned off.
  611. * This purges the quota inodes, destroys locks and frees itself.
  612. */
  613. void
  614. xfs_qm_destroy_quotainfo(
  615. xfs_mount_t *mp)
  616. {
  617. xfs_quotainfo_t *qi;
  618. qi = mp->m_quotainfo;
  619. ASSERT(qi != NULL);
  620. unregister_shrinker(&qi->qi_shrinker);
  621. list_lru_destroy(&qi->qi_lru);
  622. xfs_qm_destroy_quotainos(qi);
  623. mutex_destroy(&qi->qi_tree_lock);
  624. mutex_destroy(&qi->qi_quotaofflock);
  625. kmem_free(qi);
  626. mp->m_quotainfo = NULL;
  627. }
  628. /*
  629. * Create an inode and return with a reference already taken, but unlocked
  630. * This is how we create quota inodes
  631. */
  632. STATIC int
  633. xfs_qm_qino_alloc(
  634. xfs_mount_t *mp,
  635. xfs_inode_t **ip,
  636. uint flags)
  637. {
  638. xfs_trans_t *tp;
  639. int error;
  640. bool need_alloc = true;
  641. *ip = NULL;
  642. /*
  643. * With superblock that doesn't have separate pquotino, we
  644. * share an inode between gquota and pquota. If the on-disk
  645. * superblock has GQUOTA and the filesystem is now mounted
  646. * with PQUOTA, just use sb_gquotino for sb_pquotino and
  647. * vice-versa.
  648. */
  649. if (!xfs_sb_version_has_pquotino(&mp->m_sb) &&
  650. (flags & (XFS_QMOPT_PQUOTA|XFS_QMOPT_GQUOTA))) {
  651. xfs_ino_t ino = NULLFSINO;
  652. if ((flags & XFS_QMOPT_PQUOTA) &&
  653. (mp->m_sb.sb_gquotino != NULLFSINO)) {
  654. ino = mp->m_sb.sb_gquotino;
  655. ASSERT(mp->m_sb.sb_pquotino == NULLFSINO);
  656. } else if ((flags & XFS_QMOPT_GQUOTA) &&
  657. (mp->m_sb.sb_pquotino != NULLFSINO)) {
  658. ino = mp->m_sb.sb_pquotino;
  659. ASSERT(mp->m_sb.sb_gquotino == NULLFSINO);
  660. }
  661. if (ino != NULLFSINO) {
  662. error = xfs_iget(mp, NULL, ino, 0, 0, ip);
  663. if (error)
  664. return error;
  665. mp->m_sb.sb_gquotino = NULLFSINO;
  666. mp->m_sb.sb_pquotino = NULLFSINO;
  667. need_alloc = false;
  668. }
  669. }
  670. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_create,
  671. XFS_QM_QINOCREATE_SPACE_RES(mp), 0, 0, &tp);
  672. if (error)
  673. return error;
  674. if (need_alloc) {
  675. error = xfs_dir_ialloc(&tp, NULL, S_IFREG, 1, 0, 0, ip);
  676. if (error) {
  677. xfs_trans_cancel(tp);
  678. return error;
  679. }
  680. }
  681. /*
  682. * Make the changes in the superblock, and log those too.
  683. * sbfields arg may contain fields other than *QUOTINO;
  684. * VERSIONNUM for example.
  685. */
  686. spin_lock(&mp->m_sb_lock);
  687. if (flags & XFS_QMOPT_SBVERSION) {
  688. ASSERT(!xfs_sb_version_hasquota(&mp->m_sb));
  689. xfs_sb_version_addquota(&mp->m_sb);
  690. mp->m_sb.sb_uquotino = NULLFSINO;
  691. mp->m_sb.sb_gquotino = NULLFSINO;
  692. mp->m_sb.sb_pquotino = NULLFSINO;
  693. /* qflags will get updated fully _after_ quotacheck */
  694. mp->m_sb.sb_qflags = mp->m_qflags & XFS_ALL_QUOTA_ACCT;
  695. }
  696. if (flags & XFS_QMOPT_UQUOTA)
  697. mp->m_sb.sb_uquotino = (*ip)->i_ino;
  698. else if (flags & XFS_QMOPT_GQUOTA)
  699. mp->m_sb.sb_gquotino = (*ip)->i_ino;
  700. else
  701. mp->m_sb.sb_pquotino = (*ip)->i_ino;
  702. spin_unlock(&mp->m_sb_lock);
  703. xfs_log_sb(tp);
  704. error = xfs_trans_commit(tp);
  705. if (error) {
  706. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  707. xfs_alert(mp, "%s failed (error %d)!", __func__, error);
  708. }
  709. if (need_alloc)
  710. xfs_finish_inode_setup(*ip);
  711. return error;
  712. }
  713. STATIC void
  714. xfs_qm_reset_dqcounts(
  715. xfs_mount_t *mp,
  716. xfs_buf_t *bp,
  717. xfs_dqid_t id,
  718. uint type)
  719. {
  720. struct xfs_dqblk *dqb;
  721. int j;
  722. xfs_failaddr_t fa;
  723. trace_xfs_reset_dqcounts(bp, _RET_IP_);
  724. /*
  725. * Reset all counters and timers. They'll be
  726. * started afresh by xfs_qm_quotacheck.
  727. */
  728. #ifdef DEBUG
  729. j = (int)XFS_FSB_TO_B(mp, XFS_DQUOT_CLUSTER_SIZE_FSB) /
  730. sizeof(xfs_dqblk_t);
  731. ASSERT(mp->m_quotainfo->qi_dqperchunk == j);
  732. #endif
  733. dqb = bp->b_addr;
  734. for (j = 0; j < mp->m_quotainfo->qi_dqperchunk; j++) {
  735. struct xfs_disk_dquot *ddq;
  736. ddq = (struct xfs_disk_dquot *)&dqb[j];
  737. /*
  738. * Do a sanity check, and if needed, repair the dqblk. Don't
  739. * output any warnings because it's perfectly possible to
  740. * find uninitialised dquot blks. See comment in
  741. * xfs_dquot_verify.
  742. */
  743. fa = xfs_dqblk_verify(mp, &dqb[j], id + j, type);
  744. if (fa)
  745. xfs_dqblk_repair(mp, &dqb[j], id + j, type);
  746. /*
  747. * Reset type in case we are reusing group quota file for
  748. * project quotas or vice versa
  749. */
  750. ddq->d_flags = type;
  751. ddq->d_bcount = 0;
  752. ddq->d_icount = 0;
  753. ddq->d_rtbcount = 0;
  754. ddq->d_btimer = 0;
  755. ddq->d_itimer = 0;
  756. ddq->d_rtbtimer = 0;
  757. ddq->d_bwarns = 0;
  758. ddq->d_iwarns = 0;
  759. ddq->d_rtbwarns = 0;
  760. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  761. xfs_update_cksum((char *)&dqb[j],
  762. sizeof(struct xfs_dqblk),
  763. XFS_DQUOT_CRC_OFF);
  764. }
  765. }
  766. }
  767. STATIC int
  768. xfs_qm_reset_dqcounts_all(
  769. struct xfs_mount *mp,
  770. xfs_dqid_t firstid,
  771. xfs_fsblock_t bno,
  772. xfs_filblks_t blkcnt,
  773. uint flags,
  774. struct list_head *buffer_list)
  775. {
  776. struct xfs_buf *bp;
  777. int error;
  778. int type;
  779. ASSERT(blkcnt > 0);
  780. type = flags & XFS_QMOPT_UQUOTA ? XFS_DQ_USER :
  781. (flags & XFS_QMOPT_PQUOTA ? XFS_DQ_PROJ : XFS_DQ_GROUP);
  782. error = 0;
  783. /*
  784. * Blkcnt arg can be a very big number, and might even be
  785. * larger than the log itself. So, we have to break it up into
  786. * manageable-sized transactions.
  787. * Note that we don't start a permanent transaction here; we might
  788. * not be able to get a log reservation for the whole thing up front,
  789. * and we don't really care to either, because we just discard
  790. * everything if we were to crash in the middle of this loop.
  791. */
  792. while (blkcnt--) {
  793. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  794. XFS_FSB_TO_DADDR(mp, bno),
  795. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  796. &xfs_dquot_buf_ops);
  797. /*
  798. * CRC and validation errors will return a EFSCORRUPTED here. If
  799. * this occurs, re-read without CRC validation so that we can
  800. * repair the damage via xfs_qm_reset_dqcounts(). This process
  801. * will leave a trace in the log indicating corruption has
  802. * been detected.
  803. */
  804. if (error == -EFSCORRUPTED) {
  805. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
  806. XFS_FSB_TO_DADDR(mp, bno),
  807. mp->m_quotainfo->qi_dqchunklen, 0, &bp,
  808. NULL);
  809. }
  810. if (error)
  811. break;
  812. /*
  813. * A corrupt buffer might not have a verifier attached, so
  814. * make sure we have the correct one attached before writeback
  815. * occurs.
  816. */
  817. bp->b_ops = &xfs_dquot_buf_ops;
  818. xfs_qm_reset_dqcounts(mp, bp, firstid, type);
  819. xfs_buf_delwri_queue(bp, buffer_list);
  820. xfs_buf_relse(bp);
  821. /* goto the next block. */
  822. bno++;
  823. firstid += mp->m_quotainfo->qi_dqperchunk;
  824. }
  825. return error;
  826. }
  827. /*
  828. * Iterate over all allocated dquot blocks in this quota inode, zeroing all
  829. * counters for every chunk of dquots that we find.
  830. */
  831. STATIC int
  832. xfs_qm_reset_dqcounts_buf(
  833. struct xfs_mount *mp,
  834. struct xfs_inode *qip,
  835. uint flags,
  836. struct list_head *buffer_list)
  837. {
  838. struct xfs_bmbt_irec *map;
  839. int i, nmaps; /* number of map entries */
  840. int error; /* return value */
  841. xfs_fileoff_t lblkno;
  842. xfs_filblks_t maxlblkcnt;
  843. xfs_dqid_t firstid;
  844. xfs_fsblock_t rablkno;
  845. xfs_filblks_t rablkcnt;
  846. error = 0;
  847. /*
  848. * This looks racy, but we can't keep an inode lock across a
  849. * trans_reserve. But, this gets called during quotacheck, and that
  850. * happens only at mount time which is single threaded.
  851. */
  852. if (qip->i_d.di_nblocks == 0)
  853. return 0;
  854. map = kmem_alloc(XFS_DQITER_MAP_SIZE * sizeof(*map), KM_SLEEP);
  855. lblkno = 0;
  856. maxlblkcnt = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  857. do {
  858. uint lock_mode;
  859. nmaps = XFS_DQITER_MAP_SIZE;
  860. /*
  861. * We aren't changing the inode itself. Just changing
  862. * some of its data. No new blocks are added here, and
  863. * the inode is never added to the transaction.
  864. */
  865. lock_mode = xfs_ilock_data_map_shared(qip);
  866. error = xfs_bmapi_read(qip, lblkno, maxlblkcnt - lblkno,
  867. map, &nmaps, 0);
  868. xfs_iunlock(qip, lock_mode);
  869. if (error)
  870. break;
  871. ASSERT(nmaps <= XFS_DQITER_MAP_SIZE);
  872. for (i = 0; i < nmaps; i++) {
  873. ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
  874. ASSERT(map[i].br_blockcount);
  875. lblkno += map[i].br_blockcount;
  876. if (map[i].br_startblock == HOLESTARTBLOCK)
  877. continue;
  878. firstid = (xfs_dqid_t) map[i].br_startoff *
  879. mp->m_quotainfo->qi_dqperchunk;
  880. /*
  881. * Do a read-ahead on the next extent.
  882. */
  883. if ((i+1 < nmaps) &&
  884. (map[i+1].br_startblock != HOLESTARTBLOCK)) {
  885. rablkcnt = map[i+1].br_blockcount;
  886. rablkno = map[i+1].br_startblock;
  887. while (rablkcnt--) {
  888. xfs_buf_readahead(mp->m_ddev_targp,
  889. XFS_FSB_TO_DADDR(mp, rablkno),
  890. mp->m_quotainfo->qi_dqchunklen,
  891. &xfs_dquot_buf_ops);
  892. rablkno++;
  893. }
  894. }
  895. /*
  896. * Iterate thru all the blks in the extent and
  897. * reset the counters of all the dquots inside them.
  898. */
  899. error = xfs_qm_reset_dqcounts_all(mp, firstid,
  900. map[i].br_startblock,
  901. map[i].br_blockcount,
  902. flags, buffer_list);
  903. if (error)
  904. goto out;
  905. }
  906. } while (nmaps > 0);
  907. out:
  908. kmem_free(map);
  909. return error;
  910. }
  911. /*
  912. * Called by dqusage_adjust in doing a quotacheck.
  913. *
  914. * Given the inode, and a dquot id this updates both the incore dqout as well
  915. * as the buffer copy. This is so that once the quotacheck is done, we can
  916. * just log all the buffers, as opposed to logging numerous updates to
  917. * individual dquots.
  918. */
  919. STATIC int
  920. xfs_qm_quotacheck_dqadjust(
  921. struct xfs_inode *ip,
  922. uint type,
  923. xfs_qcnt_t nblks,
  924. xfs_qcnt_t rtblks)
  925. {
  926. struct xfs_mount *mp = ip->i_mount;
  927. struct xfs_dquot *dqp;
  928. xfs_dqid_t id;
  929. int error;
  930. id = xfs_qm_id_for_quotatype(ip, type);
  931. error = xfs_qm_dqget(mp, id, type, true, &dqp);
  932. if (error) {
  933. /*
  934. * Shouldn't be able to turn off quotas here.
  935. */
  936. ASSERT(error != -ESRCH);
  937. ASSERT(error != -ENOENT);
  938. return error;
  939. }
  940. trace_xfs_dqadjust(dqp);
  941. /*
  942. * Adjust the inode count and the block count to reflect this inode's
  943. * resource usage.
  944. */
  945. be64_add_cpu(&dqp->q_core.d_icount, 1);
  946. dqp->q_res_icount++;
  947. if (nblks) {
  948. be64_add_cpu(&dqp->q_core.d_bcount, nblks);
  949. dqp->q_res_bcount += nblks;
  950. }
  951. if (rtblks) {
  952. be64_add_cpu(&dqp->q_core.d_rtbcount, rtblks);
  953. dqp->q_res_rtbcount += rtblks;
  954. }
  955. /*
  956. * Set default limits, adjust timers (since we changed usages)
  957. *
  958. * There are no timers for the default values set in the root dquot.
  959. */
  960. if (dqp->q_core.d_id) {
  961. xfs_qm_adjust_dqlimits(mp, dqp);
  962. xfs_qm_adjust_dqtimers(mp, &dqp->q_core);
  963. }
  964. dqp->dq_flags |= XFS_DQ_DIRTY;
  965. xfs_qm_dqput(dqp);
  966. return 0;
  967. }
  968. /*
  969. * callback routine supplied to bulkstat(). Given an inumber, find its
  970. * dquots and update them to account for resources taken by that inode.
  971. */
  972. /* ARGSUSED */
  973. STATIC int
  974. xfs_qm_dqusage_adjust(
  975. xfs_mount_t *mp, /* mount point for filesystem */
  976. xfs_ino_t ino, /* inode number to get data for */
  977. void __user *buffer, /* not used */
  978. int ubsize, /* not used */
  979. int *ubused, /* not used */
  980. int *res) /* result code value */
  981. {
  982. xfs_inode_t *ip;
  983. xfs_qcnt_t nblks;
  984. xfs_filblks_t rtblks = 0; /* total rt blks */
  985. int error;
  986. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  987. /*
  988. * rootino must have its resources accounted for, not so with the quota
  989. * inodes.
  990. */
  991. if (xfs_is_quota_inode(&mp->m_sb, ino)) {
  992. *res = BULKSTAT_RV_NOTHING;
  993. return -EINVAL;
  994. }
  995. /*
  996. * We don't _need_ to take the ilock EXCL here because quotacheck runs
  997. * at mount time and therefore nobody will be racing chown/chproj.
  998. */
  999. error = xfs_iget(mp, NULL, ino, XFS_IGET_DONTCACHE, 0, &ip);
  1000. if (error) {
  1001. *res = BULKSTAT_RV_NOTHING;
  1002. return error;
  1003. }
  1004. ASSERT(ip->i_delayed_blks == 0);
  1005. if (XFS_IS_REALTIME_INODE(ip)) {
  1006. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1007. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  1008. error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
  1009. if (error)
  1010. goto error0;
  1011. }
  1012. xfs_bmap_count_leaves(ifp, &rtblks);
  1013. }
  1014. nblks = (xfs_qcnt_t)ip->i_d.di_nblocks - rtblks;
  1015. /*
  1016. * Add the (disk blocks and inode) resources occupied by this
  1017. * inode to its dquots. We do this adjustment in the incore dquot,
  1018. * and also copy the changes to its buffer.
  1019. * We don't care about putting these changes in a transaction
  1020. * envelope because if we crash in the middle of a 'quotacheck'
  1021. * we have to start from the beginning anyway.
  1022. * Once we're done, we'll log all the dquot bufs.
  1023. *
  1024. * The *QUOTA_ON checks below may look pretty racy, but quotachecks
  1025. * and quotaoffs don't race. (Quotachecks happen at mount time only).
  1026. */
  1027. if (XFS_IS_UQUOTA_ON(mp)) {
  1028. error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQ_USER, nblks,
  1029. rtblks);
  1030. if (error)
  1031. goto error0;
  1032. }
  1033. if (XFS_IS_GQUOTA_ON(mp)) {
  1034. error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQ_GROUP, nblks,
  1035. rtblks);
  1036. if (error)
  1037. goto error0;
  1038. }
  1039. if (XFS_IS_PQUOTA_ON(mp)) {
  1040. error = xfs_qm_quotacheck_dqadjust(ip, XFS_DQ_PROJ, nblks,
  1041. rtblks);
  1042. if (error)
  1043. goto error0;
  1044. }
  1045. xfs_irele(ip);
  1046. *res = BULKSTAT_RV_DIDONE;
  1047. return 0;
  1048. error0:
  1049. xfs_irele(ip);
  1050. *res = BULKSTAT_RV_GIVEUP;
  1051. return error;
  1052. }
  1053. STATIC int
  1054. xfs_qm_flush_one(
  1055. struct xfs_dquot *dqp,
  1056. void *data)
  1057. {
  1058. struct xfs_mount *mp = dqp->q_mount;
  1059. struct list_head *buffer_list = data;
  1060. struct xfs_buf *bp = NULL;
  1061. int error = 0;
  1062. xfs_dqlock(dqp);
  1063. if (dqp->dq_flags & XFS_DQ_FREEING)
  1064. goto out_unlock;
  1065. if (!XFS_DQ_IS_DIRTY(dqp))
  1066. goto out_unlock;
  1067. /*
  1068. * The only way the dquot is already flush locked by the time quotacheck
  1069. * gets here is if reclaim flushed it before the dqadjust walk dirtied
  1070. * it for the final time. Quotacheck collects all dquot bufs in the
  1071. * local delwri queue before dquots are dirtied, so reclaim can't have
  1072. * possibly queued it for I/O. The only way out is to push the buffer to
  1073. * cycle the flush lock.
  1074. */
  1075. if (!xfs_dqflock_nowait(dqp)) {
  1076. /* buf is pinned in-core by delwri list */
  1077. bp = xfs_buf_incore(mp->m_ddev_targp, dqp->q_blkno,
  1078. mp->m_quotainfo->qi_dqchunklen, 0);
  1079. if (!bp) {
  1080. error = -EINVAL;
  1081. goto out_unlock;
  1082. }
  1083. xfs_buf_unlock(bp);
  1084. xfs_buf_delwri_pushbuf(bp, buffer_list);
  1085. xfs_buf_rele(bp);
  1086. error = -EAGAIN;
  1087. goto out_unlock;
  1088. }
  1089. error = xfs_qm_dqflush(dqp, &bp);
  1090. if (error)
  1091. goto out_unlock;
  1092. xfs_buf_delwri_queue(bp, buffer_list);
  1093. xfs_buf_relse(bp);
  1094. out_unlock:
  1095. xfs_dqunlock(dqp);
  1096. return error;
  1097. }
  1098. /*
  1099. * Walk thru all the filesystem inodes and construct a consistent view
  1100. * of the disk quota world. If the quotacheck fails, disable quotas.
  1101. */
  1102. STATIC int
  1103. xfs_qm_quotacheck(
  1104. xfs_mount_t *mp)
  1105. {
  1106. int done, count, error, error2;
  1107. xfs_ino_t lastino;
  1108. size_t structsz;
  1109. uint flags;
  1110. LIST_HEAD (buffer_list);
  1111. struct xfs_inode *uip = mp->m_quotainfo->qi_uquotaip;
  1112. struct xfs_inode *gip = mp->m_quotainfo->qi_gquotaip;
  1113. struct xfs_inode *pip = mp->m_quotainfo->qi_pquotaip;
  1114. count = INT_MAX;
  1115. structsz = 1;
  1116. lastino = 0;
  1117. flags = 0;
  1118. ASSERT(uip || gip || pip);
  1119. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1120. xfs_notice(mp, "Quotacheck needed: Please wait.");
  1121. /*
  1122. * First we go thru all the dquots on disk, USR and GRP/PRJ, and reset
  1123. * their counters to zero. We need a clean slate.
  1124. * We don't log our changes till later.
  1125. */
  1126. if (uip) {
  1127. error = xfs_qm_reset_dqcounts_buf(mp, uip, XFS_QMOPT_UQUOTA,
  1128. &buffer_list);
  1129. if (error)
  1130. goto error_return;
  1131. flags |= XFS_UQUOTA_CHKD;
  1132. }
  1133. if (gip) {
  1134. error = xfs_qm_reset_dqcounts_buf(mp, gip, XFS_QMOPT_GQUOTA,
  1135. &buffer_list);
  1136. if (error)
  1137. goto error_return;
  1138. flags |= XFS_GQUOTA_CHKD;
  1139. }
  1140. if (pip) {
  1141. error = xfs_qm_reset_dqcounts_buf(mp, pip, XFS_QMOPT_PQUOTA,
  1142. &buffer_list);
  1143. if (error)
  1144. goto error_return;
  1145. flags |= XFS_PQUOTA_CHKD;
  1146. }
  1147. do {
  1148. /*
  1149. * Iterate thru all the inodes in the file system,
  1150. * adjusting the corresponding dquot counters in core.
  1151. */
  1152. error = xfs_bulkstat(mp, &lastino, &count,
  1153. xfs_qm_dqusage_adjust,
  1154. structsz, NULL, &done);
  1155. if (error)
  1156. break;
  1157. } while (!done);
  1158. /*
  1159. * We've made all the changes that we need to make incore. Flush them
  1160. * down to disk buffers if everything was updated successfully.
  1161. */
  1162. if (XFS_IS_UQUOTA_ON(mp)) {
  1163. error = xfs_qm_dquot_walk(mp, XFS_DQ_USER, xfs_qm_flush_one,
  1164. &buffer_list);
  1165. }
  1166. if (XFS_IS_GQUOTA_ON(mp)) {
  1167. error2 = xfs_qm_dquot_walk(mp, XFS_DQ_GROUP, xfs_qm_flush_one,
  1168. &buffer_list);
  1169. if (!error)
  1170. error = error2;
  1171. }
  1172. if (XFS_IS_PQUOTA_ON(mp)) {
  1173. error2 = xfs_qm_dquot_walk(mp, XFS_DQ_PROJ, xfs_qm_flush_one,
  1174. &buffer_list);
  1175. if (!error)
  1176. error = error2;
  1177. }
  1178. error2 = xfs_buf_delwri_submit(&buffer_list);
  1179. if (!error)
  1180. error = error2;
  1181. /*
  1182. * We can get this error if we couldn't do a dquot allocation inside
  1183. * xfs_qm_dqusage_adjust (via bulkstat). We don't care about the
  1184. * dirty dquots that might be cached, we just want to get rid of them
  1185. * and turn quotaoff. The dquots won't be attached to any of the inodes
  1186. * at this point (because we intentionally didn't in dqget_noattach).
  1187. */
  1188. if (error) {
  1189. xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
  1190. goto error_return;
  1191. }
  1192. /*
  1193. * If one type of quotas is off, then it will lose its
  1194. * quotachecked status, since we won't be doing accounting for
  1195. * that type anymore.
  1196. */
  1197. mp->m_qflags &= ~XFS_ALL_QUOTA_CHKD;
  1198. mp->m_qflags |= flags;
  1199. error_return:
  1200. xfs_buf_delwri_cancel(&buffer_list);
  1201. if (error) {
  1202. xfs_warn(mp,
  1203. "Quotacheck: Unsuccessful (Error %d): Disabling quotas.",
  1204. error);
  1205. /*
  1206. * We must turn off quotas.
  1207. */
  1208. ASSERT(mp->m_quotainfo != NULL);
  1209. xfs_qm_destroy_quotainfo(mp);
  1210. if (xfs_mount_reset_sbqflags(mp)) {
  1211. xfs_warn(mp,
  1212. "Quotacheck: Failed to reset quota flags.");
  1213. }
  1214. } else
  1215. xfs_notice(mp, "Quotacheck: Done.");
  1216. return error;
  1217. }
  1218. /*
  1219. * This is called from xfs_mountfs to start quotas and initialize all
  1220. * necessary data structures like quotainfo. This is also responsible for
  1221. * running a quotacheck as necessary. We are guaranteed that the superblock
  1222. * is consistently read in at this point.
  1223. *
  1224. * If we fail here, the mount will continue with quota turned off. We don't
  1225. * need to inidicate success or failure at all.
  1226. */
  1227. void
  1228. xfs_qm_mount_quotas(
  1229. struct xfs_mount *mp)
  1230. {
  1231. int error = 0;
  1232. uint sbf;
  1233. /*
  1234. * If quotas on realtime volumes is not supported, we disable
  1235. * quotas immediately.
  1236. */
  1237. if (mp->m_sb.sb_rextents) {
  1238. xfs_notice(mp, "Cannot turn on quotas for realtime filesystem");
  1239. mp->m_qflags = 0;
  1240. goto write_changes;
  1241. }
  1242. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1243. /*
  1244. * Allocate the quotainfo structure inside the mount struct, and
  1245. * create quotainode(s), and change/rev superblock if necessary.
  1246. */
  1247. error = xfs_qm_init_quotainfo(mp);
  1248. if (error) {
  1249. /*
  1250. * We must turn off quotas.
  1251. */
  1252. ASSERT(mp->m_quotainfo == NULL);
  1253. mp->m_qflags = 0;
  1254. goto write_changes;
  1255. }
  1256. /*
  1257. * If any of the quotas are not consistent, do a quotacheck.
  1258. */
  1259. if (XFS_QM_NEED_QUOTACHECK(mp)) {
  1260. error = xfs_qm_quotacheck(mp);
  1261. if (error) {
  1262. /* Quotacheck failed and disabled quotas. */
  1263. return;
  1264. }
  1265. }
  1266. /*
  1267. * If one type of quotas is off, then it will lose its
  1268. * quotachecked status, since we won't be doing accounting for
  1269. * that type anymore.
  1270. */
  1271. if (!XFS_IS_UQUOTA_ON(mp))
  1272. mp->m_qflags &= ~XFS_UQUOTA_CHKD;
  1273. if (!XFS_IS_GQUOTA_ON(mp))
  1274. mp->m_qflags &= ~XFS_GQUOTA_CHKD;
  1275. if (!XFS_IS_PQUOTA_ON(mp))
  1276. mp->m_qflags &= ~XFS_PQUOTA_CHKD;
  1277. write_changes:
  1278. /*
  1279. * We actually don't have to acquire the m_sb_lock at all.
  1280. * This can only be called from mount, and that's single threaded. XXX
  1281. */
  1282. spin_lock(&mp->m_sb_lock);
  1283. sbf = mp->m_sb.sb_qflags;
  1284. mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
  1285. spin_unlock(&mp->m_sb_lock);
  1286. if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
  1287. if (xfs_sync_sb(mp, false)) {
  1288. /*
  1289. * We could only have been turning quotas off.
  1290. * We aren't in very good shape actually because
  1291. * the incore structures are convinced that quotas are
  1292. * off, but the on disk superblock doesn't know that !
  1293. */
  1294. ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
  1295. xfs_alert(mp, "%s: Superblock update failed!",
  1296. __func__);
  1297. }
  1298. }
  1299. if (error) {
  1300. xfs_warn(mp, "Failed to initialize disk quotas.");
  1301. return;
  1302. }
  1303. }
  1304. /*
  1305. * This is called after the superblock has been read in and we're ready to
  1306. * iget the quota inodes.
  1307. */
  1308. STATIC int
  1309. xfs_qm_init_quotainos(
  1310. xfs_mount_t *mp)
  1311. {
  1312. struct xfs_inode *uip = NULL;
  1313. struct xfs_inode *gip = NULL;
  1314. struct xfs_inode *pip = NULL;
  1315. int error;
  1316. uint flags = 0;
  1317. ASSERT(mp->m_quotainfo);
  1318. /*
  1319. * Get the uquota and gquota inodes
  1320. */
  1321. if (xfs_sb_version_hasquota(&mp->m_sb)) {
  1322. if (XFS_IS_UQUOTA_ON(mp) &&
  1323. mp->m_sb.sb_uquotino != NULLFSINO) {
  1324. ASSERT(mp->m_sb.sb_uquotino > 0);
  1325. error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  1326. 0, 0, &uip);
  1327. if (error)
  1328. return error;
  1329. }
  1330. if (XFS_IS_GQUOTA_ON(mp) &&
  1331. mp->m_sb.sb_gquotino != NULLFSINO) {
  1332. ASSERT(mp->m_sb.sb_gquotino > 0);
  1333. error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  1334. 0, 0, &gip);
  1335. if (error)
  1336. goto error_rele;
  1337. }
  1338. if (XFS_IS_PQUOTA_ON(mp) &&
  1339. mp->m_sb.sb_pquotino != NULLFSINO) {
  1340. ASSERT(mp->m_sb.sb_pquotino > 0);
  1341. error = xfs_iget(mp, NULL, mp->m_sb.sb_pquotino,
  1342. 0, 0, &pip);
  1343. if (error)
  1344. goto error_rele;
  1345. }
  1346. } else {
  1347. flags |= XFS_QMOPT_SBVERSION;
  1348. }
  1349. /*
  1350. * Create the three inodes, if they don't exist already. The changes
  1351. * made above will get added to a transaction and logged in one of
  1352. * the qino_alloc calls below. If the device is readonly,
  1353. * temporarily switch to read-write to do this.
  1354. */
  1355. if (XFS_IS_UQUOTA_ON(mp) && uip == NULL) {
  1356. error = xfs_qm_qino_alloc(mp, &uip,
  1357. flags | XFS_QMOPT_UQUOTA);
  1358. if (error)
  1359. goto error_rele;
  1360. flags &= ~XFS_QMOPT_SBVERSION;
  1361. }
  1362. if (XFS_IS_GQUOTA_ON(mp) && gip == NULL) {
  1363. error = xfs_qm_qino_alloc(mp, &gip,
  1364. flags | XFS_QMOPT_GQUOTA);
  1365. if (error)
  1366. goto error_rele;
  1367. flags &= ~XFS_QMOPT_SBVERSION;
  1368. }
  1369. if (XFS_IS_PQUOTA_ON(mp) && pip == NULL) {
  1370. error = xfs_qm_qino_alloc(mp, &pip,
  1371. flags | XFS_QMOPT_PQUOTA);
  1372. if (error)
  1373. goto error_rele;
  1374. }
  1375. mp->m_quotainfo->qi_uquotaip = uip;
  1376. mp->m_quotainfo->qi_gquotaip = gip;
  1377. mp->m_quotainfo->qi_pquotaip = pip;
  1378. return 0;
  1379. error_rele:
  1380. if (uip)
  1381. xfs_irele(uip);
  1382. if (gip)
  1383. xfs_irele(gip);
  1384. if (pip)
  1385. xfs_irele(pip);
  1386. return error;
  1387. }
  1388. STATIC void
  1389. xfs_qm_destroy_quotainos(
  1390. xfs_quotainfo_t *qi)
  1391. {
  1392. if (qi->qi_uquotaip) {
  1393. xfs_irele(qi->qi_uquotaip);
  1394. qi->qi_uquotaip = NULL; /* paranoia */
  1395. }
  1396. if (qi->qi_gquotaip) {
  1397. xfs_irele(qi->qi_gquotaip);
  1398. qi->qi_gquotaip = NULL;
  1399. }
  1400. if (qi->qi_pquotaip) {
  1401. xfs_irele(qi->qi_pquotaip);
  1402. qi->qi_pquotaip = NULL;
  1403. }
  1404. }
  1405. STATIC void
  1406. xfs_qm_dqfree_one(
  1407. struct xfs_dquot *dqp)
  1408. {
  1409. struct xfs_mount *mp = dqp->q_mount;
  1410. struct xfs_quotainfo *qi = mp->m_quotainfo;
  1411. mutex_lock(&qi->qi_tree_lock);
  1412. radix_tree_delete(xfs_dquot_tree(qi, dqp->q_core.d_flags),
  1413. be32_to_cpu(dqp->q_core.d_id));
  1414. qi->qi_dquots--;
  1415. mutex_unlock(&qi->qi_tree_lock);
  1416. xfs_qm_dqdestroy(dqp);
  1417. }
  1418. /* --------------- utility functions for vnodeops ---------------- */
  1419. /*
  1420. * Given an inode, a uid, gid and prid make sure that we have
  1421. * allocated relevant dquot(s) on disk, and that we won't exceed inode
  1422. * quotas by creating this file.
  1423. * This also attaches dquot(s) to the given inode after locking it,
  1424. * and returns the dquots corresponding to the uid and/or gid.
  1425. *
  1426. * in : inode (unlocked)
  1427. * out : udquot, gdquot with references taken and unlocked
  1428. */
  1429. int
  1430. xfs_qm_vop_dqalloc(
  1431. struct xfs_inode *ip,
  1432. xfs_dqid_t uid,
  1433. xfs_dqid_t gid,
  1434. prid_t prid,
  1435. uint flags,
  1436. struct xfs_dquot **O_udqpp,
  1437. struct xfs_dquot **O_gdqpp,
  1438. struct xfs_dquot **O_pdqpp)
  1439. {
  1440. struct xfs_mount *mp = ip->i_mount;
  1441. struct xfs_dquot *uq = NULL;
  1442. struct xfs_dquot *gq = NULL;
  1443. struct xfs_dquot *pq = NULL;
  1444. int error;
  1445. uint lockflags;
  1446. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  1447. return 0;
  1448. lockflags = XFS_ILOCK_EXCL;
  1449. xfs_ilock(ip, lockflags);
  1450. if ((flags & XFS_QMOPT_INHERIT) && XFS_INHERIT_GID(ip))
  1451. gid = ip->i_d.di_gid;
  1452. /*
  1453. * Attach the dquot(s) to this inode, doing a dquot allocation
  1454. * if necessary. The dquot(s) will not be locked.
  1455. */
  1456. if (XFS_NOT_DQATTACHED(mp, ip)) {
  1457. error = xfs_qm_dqattach_locked(ip, true);
  1458. if (error) {
  1459. xfs_iunlock(ip, lockflags);
  1460. return error;
  1461. }
  1462. }
  1463. if ((flags & XFS_QMOPT_UQUOTA) && XFS_IS_UQUOTA_ON(mp)) {
  1464. if (ip->i_d.di_uid != uid) {
  1465. /*
  1466. * What we need is the dquot that has this uid, and
  1467. * if we send the inode to dqget, the uid of the inode
  1468. * takes priority over what's sent in the uid argument.
  1469. * We must unlock inode here before calling dqget if
  1470. * we're not sending the inode, because otherwise
  1471. * we'll deadlock by doing trans_reserve while
  1472. * holding ilock.
  1473. */
  1474. xfs_iunlock(ip, lockflags);
  1475. error = xfs_qm_dqget(mp, uid, XFS_DQ_USER, true, &uq);
  1476. if (error) {
  1477. ASSERT(error != -ENOENT);
  1478. return error;
  1479. }
  1480. /*
  1481. * Get the ilock in the right order.
  1482. */
  1483. xfs_dqunlock(uq);
  1484. lockflags = XFS_ILOCK_SHARED;
  1485. xfs_ilock(ip, lockflags);
  1486. } else {
  1487. /*
  1488. * Take an extra reference, because we'll return
  1489. * this to caller
  1490. */
  1491. ASSERT(ip->i_udquot);
  1492. uq = xfs_qm_dqhold(ip->i_udquot);
  1493. }
  1494. }
  1495. if ((flags & XFS_QMOPT_GQUOTA) && XFS_IS_GQUOTA_ON(mp)) {
  1496. if (ip->i_d.di_gid != gid) {
  1497. xfs_iunlock(ip, lockflags);
  1498. error = xfs_qm_dqget(mp, gid, XFS_DQ_GROUP, true, &gq);
  1499. if (error) {
  1500. ASSERT(error != -ENOENT);
  1501. goto error_rele;
  1502. }
  1503. xfs_dqunlock(gq);
  1504. lockflags = XFS_ILOCK_SHARED;
  1505. xfs_ilock(ip, lockflags);
  1506. } else {
  1507. ASSERT(ip->i_gdquot);
  1508. gq = xfs_qm_dqhold(ip->i_gdquot);
  1509. }
  1510. }
  1511. if ((flags & XFS_QMOPT_PQUOTA) && XFS_IS_PQUOTA_ON(mp)) {
  1512. if (xfs_get_projid(ip) != prid) {
  1513. xfs_iunlock(ip, lockflags);
  1514. error = xfs_qm_dqget(mp, (xfs_dqid_t)prid, XFS_DQ_PROJ,
  1515. true, &pq);
  1516. if (error) {
  1517. ASSERT(error != -ENOENT);
  1518. goto error_rele;
  1519. }
  1520. xfs_dqunlock(pq);
  1521. lockflags = XFS_ILOCK_SHARED;
  1522. xfs_ilock(ip, lockflags);
  1523. } else {
  1524. ASSERT(ip->i_pdquot);
  1525. pq = xfs_qm_dqhold(ip->i_pdquot);
  1526. }
  1527. }
  1528. if (uq)
  1529. trace_xfs_dquot_dqalloc(ip);
  1530. xfs_iunlock(ip, lockflags);
  1531. if (O_udqpp)
  1532. *O_udqpp = uq;
  1533. else
  1534. xfs_qm_dqrele(uq);
  1535. if (O_gdqpp)
  1536. *O_gdqpp = gq;
  1537. else
  1538. xfs_qm_dqrele(gq);
  1539. if (O_pdqpp)
  1540. *O_pdqpp = pq;
  1541. else
  1542. xfs_qm_dqrele(pq);
  1543. return 0;
  1544. error_rele:
  1545. xfs_qm_dqrele(gq);
  1546. xfs_qm_dqrele(uq);
  1547. return error;
  1548. }
  1549. /*
  1550. * Actually transfer ownership, and do dquot modifications.
  1551. * These were already reserved.
  1552. */
  1553. xfs_dquot_t *
  1554. xfs_qm_vop_chown(
  1555. xfs_trans_t *tp,
  1556. xfs_inode_t *ip,
  1557. xfs_dquot_t **IO_olddq,
  1558. xfs_dquot_t *newdq)
  1559. {
  1560. xfs_dquot_t *prevdq;
  1561. uint bfield = XFS_IS_REALTIME_INODE(ip) ?
  1562. XFS_TRANS_DQ_RTBCOUNT : XFS_TRANS_DQ_BCOUNT;
  1563. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1564. ASSERT(XFS_IS_QUOTA_RUNNING(ip->i_mount));
  1565. /* old dquot */
  1566. prevdq = *IO_olddq;
  1567. ASSERT(prevdq);
  1568. ASSERT(prevdq != newdq);
  1569. xfs_trans_mod_dquot(tp, prevdq, bfield, -(ip->i_d.di_nblocks));
  1570. xfs_trans_mod_dquot(tp, prevdq, XFS_TRANS_DQ_ICOUNT, -1);
  1571. /* the sparkling new dquot */
  1572. xfs_trans_mod_dquot(tp, newdq, bfield, ip->i_d.di_nblocks);
  1573. xfs_trans_mod_dquot(tp, newdq, XFS_TRANS_DQ_ICOUNT, 1);
  1574. /*
  1575. * Take an extra reference, because the inode is going to keep
  1576. * this dquot pointer even after the trans_commit.
  1577. */
  1578. *IO_olddq = xfs_qm_dqhold(newdq);
  1579. return prevdq;
  1580. }
  1581. /*
  1582. * Quota reservations for setattr(AT_UID|AT_GID|AT_PROJID).
  1583. */
  1584. int
  1585. xfs_qm_vop_chown_reserve(
  1586. struct xfs_trans *tp,
  1587. struct xfs_inode *ip,
  1588. struct xfs_dquot *udqp,
  1589. struct xfs_dquot *gdqp,
  1590. struct xfs_dquot *pdqp,
  1591. uint flags)
  1592. {
  1593. struct xfs_mount *mp = ip->i_mount;
  1594. uint delblks, blkflags, prjflags = 0;
  1595. struct xfs_dquot *udq_unres = NULL;
  1596. struct xfs_dquot *gdq_unres = NULL;
  1597. struct xfs_dquot *pdq_unres = NULL;
  1598. struct xfs_dquot *udq_delblks = NULL;
  1599. struct xfs_dquot *gdq_delblks = NULL;
  1600. struct xfs_dquot *pdq_delblks = NULL;
  1601. int error;
  1602. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  1603. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1604. delblks = ip->i_delayed_blks;
  1605. blkflags = XFS_IS_REALTIME_INODE(ip) ?
  1606. XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS;
  1607. if (XFS_IS_UQUOTA_ON(mp) && udqp &&
  1608. ip->i_d.di_uid != be32_to_cpu(udqp->q_core.d_id)) {
  1609. udq_delblks = udqp;
  1610. /*
  1611. * If there are delayed allocation blocks, then we have to
  1612. * unreserve those from the old dquot, and add them to the
  1613. * new dquot.
  1614. */
  1615. if (delblks) {
  1616. ASSERT(ip->i_udquot);
  1617. udq_unres = ip->i_udquot;
  1618. }
  1619. }
  1620. if (XFS_IS_GQUOTA_ON(ip->i_mount) && gdqp &&
  1621. ip->i_d.di_gid != be32_to_cpu(gdqp->q_core.d_id)) {
  1622. gdq_delblks = gdqp;
  1623. if (delblks) {
  1624. ASSERT(ip->i_gdquot);
  1625. gdq_unres = ip->i_gdquot;
  1626. }
  1627. }
  1628. if (XFS_IS_PQUOTA_ON(ip->i_mount) && pdqp &&
  1629. xfs_get_projid(ip) != be32_to_cpu(pdqp->q_core.d_id)) {
  1630. prjflags = XFS_QMOPT_ENOSPC;
  1631. pdq_delblks = pdqp;
  1632. if (delblks) {
  1633. ASSERT(ip->i_pdquot);
  1634. pdq_unres = ip->i_pdquot;
  1635. }
  1636. }
  1637. error = xfs_trans_reserve_quota_bydquots(tp, ip->i_mount,
  1638. udq_delblks, gdq_delblks, pdq_delblks,
  1639. ip->i_d.di_nblocks, 1,
  1640. flags | blkflags | prjflags);
  1641. if (error)
  1642. return error;
  1643. /*
  1644. * Do the delayed blks reservations/unreservations now. Since, these
  1645. * are done without the help of a transaction, if a reservation fails
  1646. * its previous reservations won't be automatically undone by trans
  1647. * code. So, we have to do it manually here.
  1648. */
  1649. if (delblks) {
  1650. /*
  1651. * Do the reservations first. Unreservation can't fail.
  1652. */
  1653. ASSERT(udq_delblks || gdq_delblks || pdq_delblks);
  1654. ASSERT(udq_unres || gdq_unres || pdq_unres);
  1655. error = xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  1656. udq_delblks, gdq_delblks, pdq_delblks,
  1657. (xfs_qcnt_t)delblks, 0,
  1658. flags | blkflags | prjflags);
  1659. if (error)
  1660. return error;
  1661. xfs_trans_reserve_quota_bydquots(NULL, ip->i_mount,
  1662. udq_unres, gdq_unres, pdq_unres,
  1663. -((xfs_qcnt_t)delblks), 0, blkflags);
  1664. }
  1665. return 0;
  1666. }
  1667. int
  1668. xfs_qm_vop_rename_dqattach(
  1669. struct xfs_inode **i_tab)
  1670. {
  1671. struct xfs_mount *mp = i_tab[0]->i_mount;
  1672. int i;
  1673. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  1674. return 0;
  1675. for (i = 0; (i < 4 && i_tab[i]); i++) {
  1676. struct xfs_inode *ip = i_tab[i];
  1677. int error;
  1678. /*
  1679. * Watch out for duplicate entries in the table.
  1680. */
  1681. if (i == 0 || ip != i_tab[i-1]) {
  1682. if (XFS_NOT_DQATTACHED(mp, ip)) {
  1683. error = xfs_qm_dqattach(ip);
  1684. if (error)
  1685. return error;
  1686. }
  1687. }
  1688. }
  1689. return 0;
  1690. }
  1691. void
  1692. xfs_qm_vop_create_dqattach(
  1693. struct xfs_trans *tp,
  1694. struct xfs_inode *ip,
  1695. struct xfs_dquot *udqp,
  1696. struct xfs_dquot *gdqp,
  1697. struct xfs_dquot *pdqp)
  1698. {
  1699. struct xfs_mount *mp = tp->t_mountp;
  1700. if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
  1701. return;
  1702. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1703. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1704. if (udqp && XFS_IS_UQUOTA_ON(mp)) {
  1705. ASSERT(ip->i_udquot == NULL);
  1706. ASSERT(ip->i_d.di_uid == be32_to_cpu(udqp->q_core.d_id));
  1707. ip->i_udquot = xfs_qm_dqhold(udqp);
  1708. xfs_trans_mod_dquot(tp, udqp, XFS_TRANS_DQ_ICOUNT, 1);
  1709. }
  1710. if (gdqp && XFS_IS_GQUOTA_ON(mp)) {
  1711. ASSERT(ip->i_gdquot == NULL);
  1712. ASSERT(ip->i_d.di_gid == be32_to_cpu(gdqp->q_core.d_id));
  1713. ip->i_gdquot = xfs_qm_dqhold(gdqp);
  1714. xfs_trans_mod_dquot(tp, gdqp, XFS_TRANS_DQ_ICOUNT, 1);
  1715. }
  1716. if (pdqp && XFS_IS_PQUOTA_ON(mp)) {
  1717. ASSERT(ip->i_pdquot == NULL);
  1718. ASSERT(xfs_get_projid(ip) == be32_to_cpu(pdqp->q_core.d_id));
  1719. ip->i_pdquot = xfs_qm_dqhold(pdqp);
  1720. xfs_trans_mod_dquot(tp, pdqp, XFS_TRANS_DQ_ICOUNT, 1);
  1721. }
  1722. }