xfs_qm_syscalls.c 21 KB

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