xfs_qm_syscalls.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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);
  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);
  227. goto out_unlock;
  228. }
  229. ASSERT(ip->i_d.di_nextents == 0);
  230. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  231. error = xfs_trans_commit(tp);
  232. out_unlock:
  233. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  234. out_put:
  235. IRELE(ip);
  236. return error;
  237. }
  238. int
  239. xfs_qm_scall_trunc_qfiles(
  240. xfs_mount_t *mp,
  241. uint flags)
  242. {
  243. int error = -EINVAL;
  244. if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0 ||
  245. (flags & ~XFS_DQ_ALLTYPES)) {
  246. xfs_debug(mp, "%s: flags=%x m_qflags=%x",
  247. __func__, flags, mp->m_qflags);
  248. return -EINVAL;
  249. }
  250. if (flags & XFS_DQ_USER) {
  251. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
  252. if (error)
  253. return error;
  254. }
  255. if (flags & XFS_DQ_GROUP) {
  256. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
  257. if (error)
  258. return error;
  259. }
  260. if (flags & XFS_DQ_PROJ)
  261. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
  262. return error;
  263. }
  264. /*
  265. * Switch on (a given) quota enforcement for a filesystem. This takes
  266. * effect immediately.
  267. * (Switching on quota accounting must be done at mount time.)
  268. */
  269. int
  270. xfs_qm_scall_quotaon(
  271. xfs_mount_t *mp,
  272. uint flags)
  273. {
  274. int error;
  275. uint qf;
  276. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  277. /*
  278. * Switching on quota accounting must be done at mount time.
  279. */
  280. flags &= ~(XFS_ALL_QUOTA_ACCT);
  281. if (flags == 0) {
  282. xfs_debug(mp, "%s: zero flags, m_qflags=%x",
  283. __func__, mp->m_qflags);
  284. return -EINVAL;
  285. }
  286. /*
  287. * Can't enforce without accounting. We check the superblock
  288. * qflags here instead of m_qflags because rootfs can have
  289. * quota acct on ondisk without m_qflags' knowing.
  290. */
  291. if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
  292. (flags & XFS_UQUOTA_ENFD)) ||
  293. ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
  294. (flags & XFS_GQUOTA_ENFD)) ||
  295. ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
  296. (flags & XFS_PQUOTA_ENFD))) {
  297. xfs_debug(mp,
  298. "%s: Can't enforce without acct, flags=%x sbflags=%x",
  299. __func__, flags, mp->m_sb.sb_qflags);
  300. return -EINVAL;
  301. }
  302. /*
  303. * If everything's up to-date incore, then don't waste time.
  304. */
  305. if ((mp->m_qflags & flags) == flags)
  306. return -EEXIST;
  307. /*
  308. * Change sb_qflags on disk but not incore mp->qflags
  309. * if this is the root filesystem.
  310. */
  311. spin_lock(&mp->m_sb_lock);
  312. qf = mp->m_sb.sb_qflags;
  313. mp->m_sb.sb_qflags = qf | flags;
  314. spin_unlock(&mp->m_sb_lock);
  315. /*
  316. * There's nothing to change if it's the same.
  317. */
  318. if ((qf & flags) == flags)
  319. return -EEXIST;
  320. error = xfs_sync_sb(mp, false);
  321. if (error)
  322. return error;
  323. /*
  324. * If we aren't trying to switch on quota enforcement, we are done.
  325. */
  326. if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
  327. (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
  328. ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
  329. (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
  330. ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
  331. (mp->m_qflags & XFS_GQUOTA_ACCT)))
  332. return 0;
  333. if (! XFS_IS_QUOTA_RUNNING(mp))
  334. return -ESRCH;
  335. /*
  336. * Switch on quota enforcement in core.
  337. */
  338. mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
  339. mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
  340. mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
  341. return 0;
  342. }
  343. #define XFS_QC_MASK \
  344. (QC_LIMIT_MASK | QC_TIMER_MASK | QC_WARNS_MASK)
  345. /*
  346. * Adjust quota limits, and start/stop timers accordingly.
  347. */
  348. int
  349. xfs_qm_scall_setqlim(
  350. struct xfs_mount *mp,
  351. xfs_dqid_t id,
  352. uint type,
  353. struct qc_dqblk *newlim)
  354. {
  355. struct xfs_quotainfo *q = mp->m_quotainfo;
  356. struct xfs_disk_dquot *ddq;
  357. struct xfs_dquot *dqp;
  358. struct xfs_trans *tp;
  359. int error;
  360. xfs_qcnt_t hard, soft;
  361. if (newlim->d_fieldmask & ~XFS_QC_MASK)
  362. return -EINVAL;
  363. if ((newlim->d_fieldmask & XFS_QC_MASK) == 0)
  364. return 0;
  365. /*
  366. * We don't want to race with a quotaoff so take the quotaoff lock.
  367. * We don't hold an inode lock, so there's nothing else to stop
  368. * a quotaoff from happening.
  369. */
  370. mutex_lock(&q->qi_quotaofflock);
  371. /*
  372. * Get the dquot (locked) before we start, as we need to do a
  373. * transaction to allocate it if it doesn't exist. Once we have the
  374. * dquot, unlock it so we can start the next transaction safely. We hold
  375. * a reference to the dquot, so it's safe to do this unlock/lock without
  376. * it being reclaimed in the mean time.
  377. */
  378. error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp);
  379. if (error) {
  380. ASSERT(error != -ENOENT);
  381. goto out_unlock;
  382. }
  383. xfs_dqunlock(dqp);
  384. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
  385. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_setqlim, 0, 0);
  386. if (error) {
  387. xfs_trans_cancel(tp);
  388. goto out_rele;
  389. }
  390. xfs_dqlock(dqp);
  391. xfs_trans_dqjoin(tp, dqp);
  392. ddq = &dqp->q_core;
  393. /*
  394. * Make sure that hardlimits are >= soft limits before changing.
  395. */
  396. hard = (newlim->d_fieldmask & QC_SPC_HARD) ?
  397. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_hardlimit) :
  398. be64_to_cpu(ddq->d_blk_hardlimit);
  399. soft = (newlim->d_fieldmask & QC_SPC_SOFT) ?
  400. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_softlimit) :
  401. be64_to_cpu(ddq->d_blk_softlimit);
  402. if (hard == 0 || hard >= soft) {
  403. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  404. ddq->d_blk_softlimit = cpu_to_be64(soft);
  405. xfs_dquot_set_prealloc_limits(dqp);
  406. if (id == 0) {
  407. q->qi_bhardlimit = hard;
  408. q->qi_bsoftlimit = soft;
  409. }
  410. } else {
  411. xfs_debug(mp, "blkhard %Ld < blksoft %Ld", hard, soft);
  412. }
  413. hard = (newlim->d_fieldmask & QC_RT_SPC_HARD) ?
  414. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_hardlimit) :
  415. be64_to_cpu(ddq->d_rtb_hardlimit);
  416. soft = (newlim->d_fieldmask & QC_RT_SPC_SOFT) ?
  417. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_softlimit) :
  418. be64_to_cpu(ddq->d_rtb_softlimit);
  419. if (hard == 0 || hard >= soft) {
  420. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  421. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  422. if (id == 0) {
  423. q->qi_rtbhardlimit = hard;
  424. q->qi_rtbsoftlimit = soft;
  425. }
  426. } else {
  427. xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld", hard, soft);
  428. }
  429. hard = (newlim->d_fieldmask & QC_INO_HARD) ?
  430. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  431. be64_to_cpu(ddq->d_ino_hardlimit);
  432. soft = (newlim->d_fieldmask & QC_INO_SOFT) ?
  433. (xfs_qcnt_t) newlim->d_ino_softlimit :
  434. be64_to_cpu(ddq->d_ino_softlimit);
  435. if (hard == 0 || hard >= soft) {
  436. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  437. ddq->d_ino_softlimit = cpu_to_be64(soft);
  438. if (id == 0) {
  439. q->qi_ihardlimit = hard;
  440. q->qi_isoftlimit = soft;
  441. }
  442. } else {
  443. xfs_debug(mp, "ihard %Ld < isoft %Ld", hard, soft);
  444. }
  445. /*
  446. * Update warnings counter(s) if requested
  447. */
  448. if (newlim->d_fieldmask & QC_SPC_WARNS)
  449. ddq->d_bwarns = cpu_to_be16(newlim->d_spc_warns);
  450. if (newlim->d_fieldmask & QC_INO_WARNS)
  451. ddq->d_iwarns = cpu_to_be16(newlim->d_ino_warns);
  452. if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
  453. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rt_spc_warns);
  454. if (id == 0) {
  455. /*
  456. * Timelimits for the super user set the relative time
  457. * the other users can be over quota for this file system.
  458. * If it is zero a default is used. Ditto for the default
  459. * soft and hard limit values (already done, above), and
  460. * for warnings.
  461. */
  462. if (newlim->d_fieldmask & QC_SPC_TIMER) {
  463. q->qi_btimelimit = newlim->d_spc_timer;
  464. ddq->d_btimer = cpu_to_be32(newlim->d_spc_timer);
  465. }
  466. if (newlim->d_fieldmask & QC_INO_TIMER) {
  467. q->qi_itimelimit = newlim->d_ino_timer;
  468. ddq->d_itimer = cpu_to_be32(newlim->d_ino_timer);
  469. }
  470. if (newlim->d_fieldmask & QC_RT_SPC_TIMER) {
  471. q->qi_rtbtimelimit = newlim->d_rt_spc_timer;
  472. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rt_spc_timer);
  473. }
  474. if (newlim->d_fieldmask & QC_SPC_WARNS)
  475. q->qi_bwarnlimit = newlim->d_spc_warns;
  476. if (newlim->d_fieldmask & QC_INO_WARNS)
  477. q->qi_iwarnlimit = newlim->d_ino_warns;
  478. if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
  479. q->qi_rtbwarnlimit = newlim->d_rt_spc_warns;
  480. } else {
  481. /*
  482. * If the user is now over quota, start the timelimit.
  483. * The user will not be 'warned'.
  484. * Note that we keep the timers ticking, whether enforcement
  485. * is on or off. We don't really want to bother with iterating
  486. * over all ondisk dquots and turning the timers on/off.
  487. */
  488. xfs_qm_adjust_dqtimers(mp, ddq);
  489. }
  490. dqp->dq_flags |= XFS_DQ_DIRTY;
  491. xfs_trans_log_dquot(tp, dqp);
  492. error = xfs_trans_commit(tp);
  493. out_rele:
  494. xfs_qm_dqrele(dqp);
  495. out_unlock:
  496. mutex_unlock(&q->qi_quotaofflock);
  497. return error;
  498. }
  499. STATIC int
  500. xfs_qm_log_quotaoff_end(
  501. xfs_mount_t *mp,
  502. xfs_qoff_logitem_t *startqoff,
  503. uint flags)
  504. {
  505. xfs_trans_t *tp;
  506. int error;
  507. xfs_qoff_logitem_t *qoffi;
  508. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  509. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_equotaoff, 0, 0);
  510. if (error) {
  511. xfs_trans_cancel(tp);
  512. return error;
  513. }
  514. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  515. flags & XFS_ALL_QUOTA_ACCT);
  516. xfs_trans_log_quotaoff_item(tp, qoffi);
  517. /*
  518. * We have to make sure that the transaction is secure on disk before we
  519. * return and actually stop quota accounting. So, make it synchronous.
  520. * We don't care about quotoff's performance.
  521. */
  522. xfs_trans_set_sync(tp);
  523. return xfs_trans_commit(tp);
  524. }
  525. STATIC int
  526. xfs_qm_log_quotaoff(
  527. xfs_mount_t *mp,
  528. xfs_qoff_logitem_t **qoffstartp,
  529. uint flags)
  530. {
  531. xfs_trans_t *tp;
  532. int error;
  533. xfs_qoff_logitem_t *qoffi;
  534. *qoffstartp = NULL;
  535. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  536. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_quotaoff, 0, 0);
  537. if (error) {
  538. xfs_trans_cancel(tp);
  539. goto out;
  540. }
  541. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  542. xfs_trans_log_quotaoff_item(tp, qoffi);
  543. spin_lock(&mp->m_sb_lock);
  544. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  545. spin_unlock(&mp->m_sb_lock);
  546. xfs_log_sb(tp);
  547. /*
  548. * We have to make sure that the transaction is secure on disk before we
  549. * return and actually stop quota accounting. So, make it synchronous.
  550. * We don't care about quotoff's performance.
  551. */
  552. xfs_trans_set_sync(tp);
  553. error = xfs_trans_commit(tp);
  554. if (error)
  555. goto out;
  556. *qoffstartp = qoffi;
  557. out:
  558. return error;
  559. }
  560. int
  561. xfs_qm_scall_getquota(
  562. struct xfs_mount *mp,
  563. xfs_dqid_t id,
  564. uint type,
  565. struct qc_dqblk *dst)
  566. {
  567. struct xfs_dquot *dqp;
  568. int error;
  569. /*
  570. * Try to get the dquot. We don't want it allocated on disk, so
  571. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  572. * exist, we'll get ENOENT back.
  573. */
  574. error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp);
  575. if (error)
  576. return error;
  577. /*
  578. * If everything's NULL, this dquot doesn't quite exist as far as
  579. * our utility programs are concerned.
  580. */
  581. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  582. error = -ENOENT;
  583. goto out_put;
  584. }
  585. memset(dst, 0, sizeof(*dst));
  586. dst->d_spc_hardlimit =
  587. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
  588. dst->d_spc_softlimit =
  589. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
  590. dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  591. dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  592. dst->d_space = XFS_FSB_TO_B(mp, dqp->q_res_bcount);
  593. dst->d_ino_count = dqp->q_res_icount;
  594. dst->d_spc_timer = be32_to_cpu(dqp->q_core.d_btimer);
  595. dst->d_ino_timer = be32_to_cpu(dqp->q_core.d_itimer);
  596. dst->d_ino_warns = be16_to_cpu(dqp->q_core.d_iwarns);
  597. dst->d_spc_warns = be16_to_cpu(dqp->q_core.d_bwarns);
  598. dst->d_rt_spc_hardlimit =
  599. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
  600. dst->d_rt_spc_softlimit =
  601. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
  602. dst->d_rt_space = XFS_FSB_TO_B(mp, dqp->q_res_rtbcount);
  603. dst->d_rt_spc_timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  604. dst->d_rt_spc_warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  605. /*
  606. * Internally, we don't reset all the timers when quota enforcement
  607. * gets turned off. No need to confuse the user level code,
  608. * so return zeroes in that case.
  609. */
  610. if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
  611. dqp->q_core.d_flags == XFS_DQ_USER) ||
  612. (!XFS_IS_GQUOTA_ENFORCED(mp) &&
  613. dqp->q_core.d_flags == XFS_DQ_GROUP) ||
  614. (!XFS_IS_PQUOTA_ENFORCED(mp) &&
  615. dqp->q_core.d_flags == XFS_DQ_PROJ)) {
  616. dst->d_spc_timer = 0;
  617. dst->d_ino_timer = 0;
  618. dst->d_rt_spc_timer = 0;
  619. }
  620. #ifdef DEBUG
  621. if (((XFS_IS_UQUOTA_ENFORCED(mp) && type == XFS_DQ_USER) ||
  622. (XFS_IS_GQUOTA_ENFORCED(mp) && type == XFS_DQ_GROUP) ||
  623. (XFS_IS_PQUOTA_ENFORCED(mp) && type == XFS_DQ_PROJ)) &&
  624. id != 0) {
  625. if ((dst->d_space > dst->d_spc_softlimit) &&
  626. (dst->d_spc_softlimit > 0)) {
  627. ASSERT(dst->d_spc_timer != 0);
  628. }
  629. if ((dst->d_ino_count > dst->d_ino_softlimit) &&
  630. (dst->d_ino_softlimit > 0)) {
  631. ASSERT(dst->d_ino_timer != 0);
  632. }
  633. }
  634. #endif
  635. out_put:
  636. xfs_qm_dqput(dqp);
  637. return error;
  638. }
  639. STATIC int
  640. xfs_dqrele_inode(
  641. struct xfs_inode *ip,
  642. int flags,
  643. void *args)
  644. {
  645. /* skip quota inodes */
  646. if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
  647. ip == ip->i_mount->m_quotainfo->qi_gquotaip ||
  648. ip == ip->i_mount->m_quotainfo->qi_pquotaip) {
  649. ASSERT(ip->i_udquot == NULL);
  650. ASSERT(ip->i_gdquot == NULL);
  651. ASSERT(ip->i_pdquot == NULL);
  652. return 0;
  653. }
  654. xfs_ilock(ip, XFS_ILOCK_EXCL);
  655. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  656. xfs_qm_dqrele(ip->i_udquot);
  657. ip->i_udquot = NULL;
  658. }
  659. if ((flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  660. xfs_qm_dqrele(ip->i_gdquot);
  661. ip->i_gdquot = NULL;
  662. }
  663. if ((flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) {
  664. xfs_qm_dqrele(ip->i_pdquot);
  665. ip->i_pdquot = NULL;
  666. }
  667. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  668. return 0;
  669. }
  670. /*
  671. * Go thru all the inodes in the file system, releasing their dquots.
  672. *
  673. * Note that the mount structure gets modified to indicate that quotas are off
  674. * AFTER this, in the case of quotaoff.
  675. */
  676. void
  677. xfs_qm_dqrele_all_inodes(
  678. struct xfs_mount *mp,
  679. uint flags)
  680. {
  681. ASSERT(mp->m_quotainfo);
  682. xfs_inode_ag_iterator_flags(mp, xfs_dqrele_inode, flags, NULL,
  683. XFS_AGITER_INEW_WAIT);
  684. }