xfs_qm_syscalls.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include <linux/capability.h>
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_bit.h"
  14. #include "xfs_sb.h"
  15. #include "xfs_mount.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_trans.h"
  18. #include "xfs_error.h"
  19. #include "xfs_quota.h"
  20. #include "xfs_qm.h"
  21. #include "xfs_trace.h"
  22. #include "xfs_icache.h"
  23. #include "xfs_defer.h"
  24. STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
  25. STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
  26. uint);
  27. /*
  28. * Turn off quota accounting and/or enforcement for all udquots and/or
  29. * gdquots. Called only at unmount time.
  30. *
  31. * This assumes that there are no dquots of this file system cached
  32. * incore, and modifies the ondisk dquot directly. Therefore, for example,
  33. * it is an error to call this twice, without purging the cache.
  34. */
  35. int
  36. xfs_qm_scall_quotaoff(
  37. xfs_mount_t *mp,
  38. uint flags)
  39. {
  40. struct xfs_quotainfo *q = mp->m_quotainfo;
  41. uint dqtype;
  42. int error;
  43. uint inactivate_flags;
  44. xfs_qoff_logitem_t *qoffstart;
  45. /*
  46. * No file system can have quotas enabled on disk but not in core.
  47. * Note that quota utilities (like quotaoff) _expect_
  48. * errno == -EEXIST here.
  49. */
  50. if ((mp->m_qflags & flags) == 0)
  51. return -EEXIST;
  52. error = 0;
  53. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  54. /*
  55. * We don't want to deal with two quotaoffs messing up each other,
  56. * so we're going to serialize it. quotaoff isn't exactly a performance
  57. * critical thing.
  58. * If quotaoff, then we must be dealing with the root filesystem.
  59. */
  60. ASSERT(q);
  61. mutex_lock(&q->qi_quotaofflock);
  62. /*
  63. * If we're just turning off quota enforcement, change mp and go.
  64. */
  65. if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
  66. mp->m_qflags &= ~(flags);
  67. spin_lock(&mp->m_sb_lock);
  68. mp->m_sb.sb_qflags = mp->m_qflags;
  69. spin_unlock(&mp->m_sb_lock);
  70. mutex_unlock(&q->qi_quotaofflock);
  71. /* XXX what to do if error ? Revert back to old vals incore ? */
  72. return xfs_sync_sb(mp, false);
  73. }
  74. dqtype = 0;
  75. inactivate_flags = 0;
  76. /*
  77. * If accounting is off, we must turn enforcement off, clear the
  78. * quota 'CHKD' certificate to make it known that we have to
  79. * do a quotacheck the next time this quota is turned on.
  80. */
  81. if (flags & XFS_UQUOTA_ACCT) {
  82. dqtype |= XFS_QMOPT_UQUOTA;
  83. flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
  84. inactivate_flags |= XFS_UQUOTA_ACTIVE;
  85. }
  86. if (flags & XFS_GQUOTA_ACCT) {
  87. dqtype |= XFS_QMOPT_GQUOTA;
  88. flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD);
  89. inactivate_flags |= XFS_GQUOTA_ACTIVE;
  90. }
  91. if (flags & XFS_PQUOTA_ACCT) {
  92. dqtype |= XFS_QMOPT_PQUOTA;
  93. flags |= (XFS_PQUOTA_CHKD | XFS_PQUOTA_ENFD);
  94. inactivate_flags |= XFS_PQUOTA_ACTIVE;
  95. }
  96. /*
  97. * Nothing to do? Don't complain. This happens when we're just
  98. * turning off quota enforcement.
  99. */
  100. if ((mp->m_qflags & flags) == 0)
  101. goto out_unlock;
  102. /*
  103. * Write the LI_QUOTAOFF log record, and do SB changes atomically,
  104. * and synchronously. If we fail to write, we should abort the
  105. * operation as it cannot be recovered safely if we crash.
  106. */
  107. error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
  108. if (error)
  109. goto out_unlock;
  110. /*
  111. * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
  112. * to take care of the race between dqget and quotaoff. We don't take
  113. * any special locks to reset these bits. All processes need to check
  114. * these bits *after* taking inode lock(s) to see if the particular
  115. * quota type is in the process of being turned off. If *ACTIVE, it is
  116. * guaranteed that all dquot structures and all quotainode ptrs will all
  117. * stay valid as long as that inode is kept locked.
  118. *
  119. * There is no turning back after this.
  120. */
  121. mp->m_qflags &= ~inactivate_flags;
  122. /*
  123. * Give back all the dquot reference(s) held by inodes.
  124. * Here we go thru every single incore inode in this file system, and
  125. * do a dqrele on the i_udquot/i_gdquot that it may have.
  126. * Essentially, as long as somebody has an inode locked, this guarantees
  127. * that quotas will not be turned off. This is handy because in a
  128. * transaction once we lock the inode(s) and check for quotaon, we can
  129. * depend on the quota inodes (and other things) being valid as long as
  130. * we keep the lock(s).
  131. */
  132. xfs_qm_dqrele_all_inodes(mp, flags);
  133. /*
  134. * Next we make the changes in the quota flag in the mount struct.
  135. * This isn't protected by a particular lock directly, because we
  136. * don't want to take a mrlock every time we depend on quotas being on.
  137. */
  138. mp->m_qflags &= ~flags;
  139. /*
  140. * Go through all the dquots of this file system and purge them,
  141. * according to what was turned off.
  142. */
  143. xfs_qm_dqpurge_all(mp, dqtype);
  144. /*
  145. * Transactions that had started before ACTIVE state bit was cleared
  146. * could have logged many dquots, so they'd have higher LSNs than
  147. * the first QUOTAOFF log record does. If we happen to crash when
  148. * the tail of the log has gone past the QUOTAOFF record, but
  149. * before the last dquot modification, those dquots __will__
  150. * recover, and that's not good.
  151. *
  152. * So, we have QUOTAOFF start and end logitems; the start
  153. * logitem won't get overwritten until the end logitem appears...
  154. */
  155. error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
  156. if (error) {
  157. /* We're screwed now. Shutdown is the only option. */
  158. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  159. goto out_unlock;
  160. }
  161. /*
  162. * If all quotas are completely turned off, close shop.
  163. */
  164. if (mp->m_qflags == 0) {
  165. mutex_unlock(&q->qi_quotaofflock);
  166. xfs_qm_destroy_quotainfo(mp);
  167. return 0;
  168. }
  169. /*
  170. * Release our quotainode references if we don't need them anymore.
  171. */
  172. if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
  173. xfs_irele(q->qi_uquotaip);
  174. q->qi_uquotaip = NULL;
  175. }
  176. if ((dqtype & XFS_QMOPT_GQUOTA) && q->qi_gquotaip) {
  177. xfs_irele(q->qi_gquotaip);
  178. q->qi_gquotaip = NULL;
  179. }
  180. if ((dqtype & XFS_QMOPT_PQUOTA) && q->qi_pquotaip) {
  181. xfs_irele(q->qi_pquotaip);
  182. q->qi_pquotaip = NULL;
  183. }
  184. out_unlock:
  185. mutex_unlock(&q->qi_quotaofflock);
  186. return error;
  187. }
  188. STATIC int
  189. xfs_qm_scall_trunc_qfile(
  190. struct xfs_mount *mp,
  191. xfs_ino_t ino)
  192. {
  193. struct xfs_inode *ip;
  194. struct xfs_trans *tp;
  195. int error;
  196. if (ino == NULLFSINO)
  197. return 0;
  198. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  199. if (error)
  200. return error;
  201. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  202. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
  203. if (error) {
  204. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  205. goto out_put;
  206. }
  207. xfs_ilock(ip, XFS_ILOCK_EXCL);
  208. xfs_trans_ijoin(tp, ip, 0);
  209. ip->i_d.di_size = 0;
  210. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  211. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  212. if (error) {
  213. xfs_trans_cancel(tp);
  214. goto out_unlock;
  215. }
  216. ASSERT(ip->i_d.di_nextents == 0);
  217. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  218. error = xfs_trans_commit(tp);
  219. out_unlock:
  220. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  221. out_put:
  222. xfs_irele(ip);
  223. return error;
  224. }
  225. int
  226. xfs_qm_scall_trunc_qfiles(
  227. xfs_mount_t *mp,
  228. uint flags)
  229. {
  230. int error = -EINVAL;
  231. if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0 ||
  232. (flags & ~XFS_DQ_ALLTYPES)) {
  233. xfs_debug(mp, "%s: flags=%x m_qflags=%x",
  234. __func__, flags, mp->m_qflags);
  235. return -EINVAL;
  236. }
  237. if (flags & XFS_DQ_USER) {
  238. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
  239. if (error)
  240. return error;
  241. }
  242. if (flags & XFS_DQ_GROUP) {
  243. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
  244. if (error)
  245. return error;
  246. }
  247. if (flags & XFS_DQ_PROJ)
  248. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
  249. return error;
  250. }
  251. /*
  252. * Switch on (a given) quota enforcement for a filesystem. This takes
  253. * effect immediately.
  254. * (Switching on quota accounting must be done at mount time.)
  255. */
  256. int
  257. xfs_qm_scall_quotaon(
  258. xfs_mount_t *mp,
  259. uint flags)
  260. {
  261. int error;
  262. uint qf;
  263. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  264. /*
  265. * Switching on quota accounting must be done at mount time.
  266. */
  267. flags &= ~(XFS_ALL_QUOTA_ACCT);
  268. if (flags == 0) {
  269. xfs_debug(mp, "%s: zero flags, m_qflags=%x",
  270. __func__, mp->m_qflags);
  271. return -EINVAL;
  272. }
  273. /*
  274. * Can't enforce without accounting. We check the superblock
  275. * qflags here instead of m_qflags because rootfs can have
  276. * quota acct on ondisk without m_qflags' knowing.
  277. */
  278. if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
  279. (flags & XFS_UQUOTA_ENFD)) ||
  280. ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
  281. (flags & XFS_GQUOTA_ENFD)) ||
  282. ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
  283. (flags & XFS_PQUOTA_ENFD))) {
  284. xfs_debug(mp,
  285. "%s: Can't enforce without acct, flags=%x sbflags=%x",
  286. __func__, flags, mp->m_sb.sb_qflags);
  287. return -EINVAL;
  288. }
  289. /*
  290. * If everything's up to-date incore, then don't waste time.
  291. */
  292. if ((mp->m_qflags & flags) == flags)
  293. return -EEXIST;
  294. /*
  295. * Change sb_qflags on disk but not incore mp->qflags
  296. * if this is the root filesystem.
  297. */
  298. spin_lock(&mp->m_sb_lock);
  299. qf = mp->m_sb.sb_qflags;
  300. mp->m_sb.sb_qflags = qf | flags;
  301. spin_unlock(&mp->m_sb_lock);
  302. /*
  303. * There's nothing to change if it's the same.
  304. */
  305. if ((qf & flags) == flags)
  306. return -EEXIST;
  307. error = xfs_sync_sb(mp, false);
  308. if (error)
  309. return error;
  310. /*
  311. * If we aren't trying to switch on quota enforcement, we are done.
  312. */
  313. if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
  314. (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
  315. ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
  316. (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
  317. ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
  318. (mp->m_qflags & XFS_GQUOTA_ACCT)))
  319. return 0;
  320. if (! XFS_IS_QUOTA_RUNNING(mp))
  321. return -ESRCH;
  322. /*
  323. * Switch on quota enforcement in core.
  324. */
  325. mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
  326. mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
  327. mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
  328. return 0;
  329. }
  330. #define XFS_QC_MASK \
  331. (QC_LIMIT_MASK | QC_TIMER_MASK | QC_WARNS_MASK)
  332. /*
  333. * Adjust quota limits, and start/stop timers accordingly.
  334. */
  335. int
  336. xfs_qm_scall_setqlim(
  337. struct xfs_mount *mp,
  338. xfs_dqid_t id,
  339. uint type,
  340. struct qc_dqblk *newlim)
  341. {
  342. struct xfs_quotainfo *q = mp->m_quotainfo;
  343. struct xfs_disk_dquot *ddq;
  344. struct xfs_dquot *dqp;
  345. struct xfs_trans *tp;
  346. struct xfs_def_quota *defq;
  347. int error;
  348. xfs_qcnt_t hard, soft;
  349. if (newlim->d_fieldmask & ~XFS_QC_MASK)
  350. return -EINVAL;
  351. if ((newlim->d_fieldmask & XFS_QC_MASK) == 0)
  352. return 0;
  353. /*
  354. * We don't want to race with a quotaoff so take the quotaoff lock.
  355. * We don't hold an inode lock, so there's nothing else to stop
  356. * a quotaoff from happening.
  357. */
  358. mutex_lock(&q->qi_quotaofflock);
  359. /*
  360. * Get the dquot (locked) before we start, as we need to do a
  361. * transaction to allocate it if it doesn't exist. Once we have the
  362. * dquot, unlock it so we can start the next transaction safely. We hold
  363. * a reference to the dquot, so it's safe to do this unlock/lock without
  364. * it being reclaimed in the mean time.
  365. */
  366. error = xfs_qm_dqget(mp, id, type, true, &dqp);
  367. if (error) {
  368. ASSERT(error != -ENOENT);
  369. goto out_unlock;
  370. }
  371. defq = xfs_get_defquota(dqp, q);
  372. xfs_dqunlock(dqp);
  373. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_setqlim, 0, 0, 0, &tp);
  374. if (error)
  375. goto out_rele;
  376. xfs_dqlock(dqp);
  377. xfs_trans_dqjoin(tp, dqp);
  378. ddq = &dqp->q_core;
  379. /*
  380. * Make sure that hardlimits are >= soft limits before changing.
  381. */
  382. hard = (newlim->d_fieldmask & QC_SPC_HARD) ?
  383. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_hardlimit) :
  384. be64_to_cpu(ddq->d_blk_hardlimit);
  385. soft = (newlim->d_fieldmask & QC_SPC_SOFT) ?
  386. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_softlimit) :
  387. be64_to_cpu(ddq->d_blk_softlimit);
  388. if (hard == 0 || hard >= soft) {
  389. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  390. ddq->d_blk_softlimit = cpu_to_be64(soft);
  391. xfs_dquot_set_prealloc_limits(dqp);
  392. if (id == 0) {
  393. defq->bhardlimit = hard;
  394. defq->bsoftlimit = soft;
  395. }
  396. } else {
  397. xfs_debug(mp, "blkhard %Ld < blksoft %Ld", hard, soft);
  398. }
  399. hard = (newlim->d_fieldmask & QC_RT_SPC_HARD) ?
  400. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_hardlimit) :
  401. be64_to_cpu(ddq->d_rtb_hardlimit);
  402. soft = (newlim->d_fieldmask & QC_RT_SPC_SOFT) ?
  403. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_softlimit) :
  404. be64_to_cpu(ddq->d_rtb_softlimit);
  405. if (hard == 0 || hard >= soft) {
  406. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  407. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  408. if (id == 0) {
  409. defq->rtbhardlimit = hard;
  410. defq->rtbsoftlimit = soft;
  411. }
  412. } else {
  413. xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld", hard, soft);
  414. }
  415. hard = (newlim->d_fieldmask & QC_INO_HARD) ?
  416. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  417. be64_to_cpu(ddq->d_ino_hardlimit);
  418. soft = (newlim->d_fieldmask & QC_INO_SOFT) ?
  419. (xfs_qcnt_t) newlim->d_ino_softlimit :
  420. be64_to_cpu(ddq->d_ino_softlimit);
  421. if (hard == 0 || hard >= soft) {
  422. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  423. ddq->d_ino_softlimit = cpu_to_be64(soft);
  424. if (id == 0) {
  425. defq->ihardlimit = hard;
  426. defq->isoftlimit = soft;
  427. }
  428. } else {
  429. xfs_debug(mp, "ihard %Ld < isoft %Ld", hard, soft);
  430. }
  431. /*
  432. * Update warnings counter(s) if requested
  433. */
  434. if (newlim->d_fieldmask & QC_SPC_WARNS)
  435. ddq->d_bwarns = cpu_to_be16(newlim->d_spc_warns);
  436. if (newlim->d_fieldmask & QC_INO_WARNS)
  437. ddq->d_iwarns = cpu_to_be16(newlim->d_ino_warns);
  438. if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
  439. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rt_spc_warns);
  440. if (id == 0) {
  441. /*
  442. * Timelimits for the super user set the relative time
  443. * the other users can be over quota for this file system.
  444. * If it is zero a default is used. Ditto for the default
  445. * soft and hard limit values (already done, above), and
  446. * for warnings.
  447. */
  448. if (newlim->d_fieldmask & QC_SPC_TIMER) {
  449. q->qi_btimelimit = newlim->d_spc_timer;
  450. ddq->d_btimer = cpu_to_be32(newlim->d_spc_timer);
  451. }
  452. if (newlim->d_fieldmask & QC_INO_TIMER) {
  453. q->qi_itimelimit = newlim->d_ino_timer;
  454. ddq->d_itimer = cpu_to_be32(newlim->d_ino_timer);
  455. }
  456. if (newlim->d_fieldmask & QC_RT_SPC_TIMER) {
  457. q->qi_rtbtimelimit = newlim->d_rt_spc_timer;
  458. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rt_spc_timer);
  459. }
  460. if (newlim->d_fieldmask & QC_SPC_WARNS)
  461. q->qi_bwarnlimit = newlim->d_spc_warns;
  462. if (newlim->d_fieldmask & QC_INO_WARNS)
  463. q->qi_iwarnlimit = newlim->d_ino_warns;
  464. if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
  465. q->qi_rtbwarnlimit = newlim->d_rt_spc_warns;
  466. } else {
  467. /*
  468. * If the user is now over quota, start the timelimit.
  469. * The user will not be 'warned'.
  470. * Note that we keep the timers ticking, whether enforcement
  471. * is on or off. We don't really want to bother with iterating
  472. * over all ondisk dquots and turning the timers on/off.
  473. */
  474. xfs_qm_adjust_dqtimers(mp, ddq);
  475. }
  476. dqp->dq_flags |= XFS_DQ_DIRTY;
  477. xfs_trans_log_dquot(tp, dqp);
  478. error = xfs_trans_commit(tp);
  479. out_rele:
  480. xfs_qm_dqrele(dqp);
  481. out_unlock:
  482. mutex_unlock(&q->qi_quotaofflock);
  483. return error;
  484. }
  485. STATIC int
  486. xfs_qm_log_quotaoff_end(
  487. xfs_mount_t *mp,
  488. xfs_qoff_logitem_t *startqoff,
  489. uint flags)
  490. {
  491. xfs_trans_t *tp;
  492. int error;
  493. xfs_qoff_logitem_t *qoffi;
  494. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_equotaoff, 0, 0, 0, &tp);
  495. if (error)
  496. return error;
  497. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  498. flags & XFS_ALL_QUOTA_ACCT);
  499. xfs_trans_log_quotaoff_item(tp, qoffi);
  500. /*
  501. * We have to make sure that the transaction is secure on disk before we
  502. * return and actually stop quota accounting. So, make it synchronous.
  503. * We don't care about quotoff's performance.
  504. */
  505. xfs_trans_set_sync(tp);
  506. return xfs_trans_commit(tp);
  507. }
  508. STATIC int
  509. xfs_qm_log_quotaoff(
  510. xfs_mount_t *mp,
  511. xfs_qoff_logitem_t **qoffstartp,
  512. uint flags)
  513. {
  514. xfs_trans_t *tp;
  515. int error;
  516. xfs_qoff_logitem_t *qoffi;
  517. *qoffstartp = NULL;
  518. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_quotaoff, 0, 0, 0, &tp);
  519. if (error)
  520. goto out;
  521. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  522. xfs_trans_log_quotaoff_item(tp, qoffi);
  523. spin_lock(&mp->m_sb_lock);
  524. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  525. spin_unlock(&mp->m_sb_lock);
  526. xfs_log_sb(tp);
  527. /*
  528. * We have to make sure that the transaction is secure on disk before we
  529. * return and actually stop quota accounting. So, make it synchronous.
  530. * We don't care about quotoff's performance.
  531. */
  532. xfs_trans_set_sync(tp);
  533. error = xfs_trans_commit(tp);
  534. if (error)
  535. goto out;
  536. *qoffstartp = qoffi;
  537. out:
  538. return error;
  539. }
  540. /* Fill out the quota context. */
  541. static void
  542. xfs_qm_scall_getquota_fill_qc(
  543. struct xfs_mount *mp,
  544. uint type,
  545. const struct xfs_dquot *dqp,
  546. struct qc_dqblk *dst)
  547. {
  548. memset(dst, 0, sizeof(*dst));
  549. dst->d_spc_hardlimit =
  550. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
  551. dst->d_spc_softlimit =
  552. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
  553. dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  554. dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  555. dst->d_space = XFS_FSB_TO_B(mp, dqp->q_res_bcount);
  556. dst->d_ino_count = dqp->q_res_icount;
  557. dst->d_spc_timer = be32_to_cpu(dqp->q_core.d_btimer);
  558. dst->d_ino_timer = be32_to_cpu(dqp->q_core.d_itimer);
  559. dst->d_ino_warns = be16_to_cpu(dqp->q_core.d_iwarns);
  560. dst->d_spc_warns = be16_to_cpu(dqp->q_core.d_bwarns);
  561. dst->d_rt_spc_hardlimit =
  562. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
  563. dst->d_rt_spc_softlimit =
  564. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
  565. dst->d_rt_space = XFS_FSB_TO_B(mp, dqp->q_res_rtbcount);
  566. dst->d_rt_spc_timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  567. dst->d_rt_spc_warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  568. /*
  569. * Internally, we don't reset all the timers when quota enforcement
  570. * gets turned off. No need to confuse the user level code,
  571. * so return zeroes in that case.
  572. */
  573. if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
  574. dqp->q_core.d_flags == XFS_DQ_USER) ||
  575. (!XFS_IS_GQUOTA_ENFORCED(mp) &&
  576. dqp->q_core.d_flags == XFS_DQ_GROUP) ||
  577. (!XFS_IS_PQUOTA_ENFORCED(mp) &&
  578. dqp->q_core.d_flags == XFS_DQ_PROJ)) {
  579. dst->d_spc_timer = 0;
  580. dst->d_ino_timer = 0;
  581. dst->d_rt_spc_timer = 0;
  582. }
  583. #ifdef DEBUG
  584. if (((XFS_IS_UQUOTA_ENFORCED(mp) && type == XFS_DQ_USER) ||
  585. (XFS_IS_GQUOTA_ENFORCED(mp) && type == XFS_DQ_GROUP) ||
  586. (XFS_IS_PQUOTA_ENFORCED(mp) && type == XFS_DQ_PROJ)) &&
  587. dqp->q_core.d_id != 0) {
  588. if ((dst->d_space > dst->d_spc_softlimit) &&
  589. (dst->d_spc_softlimit > 0)) {
  590. ASSERT(dst->d_spc_timer != 0);
  591. }
  592. if ((dst->d_ino_count > dst->d_ino_softlimit) &&
  593. (dst->d_ino_softlimit > 0)) {
  594. ASSERT(dst->d_ino_timer != 0);
  595. }
  596. }
  597. #endif
  598. }
  599. /* Return the quota information for the dquot matching id. */
  600. int
  601. xfs_qm_scall_getquota(
  602. struct xfs_mount *mp,
  603. xfs_dqid_t id,
  604. uint type,
  605. struct qc_dqblk *dst)
  606. {
  607. struct xfs_dquot *dqp;
  608. int error;
  609. /*
  610. * Try to get the dquot. We don't want it allocated on disk, so don't
  611. * set doalloc. If it doesn't exist, we'll get ENOENT back.
  612. */
  613. error = xfs_qm_dqget(mp, id, type, false, &dqp);
  614. if (error)
  615. return error;
  616. /*
  617. * If everything's NULL, this dquot doesn't quite exist as far as
  618. * our utility programs are concerned.
  619. */
  620. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  621. error = -ENOENT;
  622. goto out_put;
  623. }
  624. xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
  625. out_put:
  626. xfs_qm_dqput(dqp);
  627. return error;
  628. }
  629. /*
  630. * Return the quota information for the first initialized dquot whose id
  631. * is at least as high as id.
  632. */
  633. int
  634. xfs_qm_scall_getquota_next(
  635. struct xfs_mount *mp,
  636. xfs_dqid_t *id,
  637. uint type,
  638. struct qc_dqblk *dst)
  639. {
  640. struct xfs_dquot *dqp;
  641. int error;
  642. error = xfs_qm_dqget_next(mp, *id, type, &dqp);
  643. if (error)
  644. return error;
  645. /* Fill in the ID we actually read from disk */
  646. *id = be32_to_cpu(dqp->q_core.d_id);
  647. xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
  648. xfs_qm_dqput(dqp);
  649. return error;
  650. }
  651. STATIC int
  652. xfs_dqrele_inode(
  653. struct xfs_inode *ip,
  654. int flags,
  655. void *args)
  656. {
  657. /* skip quota inodes */
  658. if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
  659. ip == ip->i_mount->m_quotainfo->qi_gquotaip ||
  660. ip == ip->i_mount->m_quotainfo->qi_pquotaip) {
  661. ASSERT(ip->i_udquot == NULL);
  662. ASSERT(ip->i_gdquot == NULL);
  663. ASSERT(ip->i_pdquot == NULL);
  664. return 0;
  665. }
  666. xfs_ilock(ip, XFS_ILOCK_EXCL);
  667. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  668. xfs_qm_dqrele(ip->i_udquot);
  669. ip->i_udquot = NULL;
  670. }
  671. if ((flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  672. xfs_qm_dqrele(ip->i_gdquot);
  673. ip->i_gdquot = NULL;
  674. }
  675. if ((flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) {
  676. xfs_qm_dqrele(ip->i_pdquot);
  677. ip->i_pdquot = NULL;
  678. }
  679. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  680. return 0;
  681. }
  682. /*
  683. * Go thru all the inodes in the file system, releasing their dquots.
  684. *
  685. * Note that the mount structure gets modified to indicate that quotas are off
  686. * AFTER this, in the case of quotaoff.
  687. */
  688. void
  689. xfs_qm_dqrele_all_inodes(
  690. struct xfs_mount *mp,
  691. uint flags)
  692. {
  693. ASSERT(mp->m_quotainfo);
  694. xfs_inode_ag_iterator_flags(mp, xfs_dqrele_inode, flags, NULL,
  695. XFS_AGITER_INEW_WAIT);
  696. }