xfs_qm_syscalls.c 21 KB

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