xfs_qm_syscalls.c 21 KB

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