xfs_qm_syscalls.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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_format.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_quota.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_inode_item.h"
  33. #include "xfs_itable.h"
  34. #include "xfs_bmap.h"
  35. #include "xfs_rtalloc.h"
  36. #include "xfs_error.h"
  37. #include "xfs_attr.h"
  38. #include "xfs_buf_item.h"
  39. #include "xfs_qm.h"
  40. #include "xfs_trace.h"
  41. #include "xfs_icache.h"
  42. STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
  43. STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
  44. uint);
  45. STATIC uint xfs_qm_export_flags(uint);
  46. STATIC uint xfs_qm_export_qtype_flags(uint);
  47. /*
  48. * Turn off quota accounting and/or enforcement for all udquots and/or
  49. * gdquots. Called only at unmount time.
  50. *
  51. * This assumes that there are no dquots of this file system cached
  52. * incore, and modifies the ondisk dquot directly. Therefore, for example,
  53. * it is an error to call this twice, without purging the cache.
  54. */
  55. int
  56. xfs_qm_scall_quotaoff(
  57. xfs_mount_t *mp,
  58. uint flags)
  59. {
  60. struct xfs_quotainfo *q = mp->m_quotainfo;
  61. uint dqtype;
  62. int error;
  63. uint inactivate_flags;
  64. xfs_qoff_logitem_t *qoffstart;
  65. /*
  66. * No file system can have quotas enabled on disk but not in core.
  67. * Note that quota utilities (like quotaoff) _expect_
  68. * errno == EEXIST here.
  69. */
  70. if ((mp->m_qflags & flags) == 0)
  71. return XFS_ERROR(EEXIST);
  72. error = 0;
  73. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  74. /*
  75. * We don't want to deal with two quotaoffs messing up each other,
  76. * so we're going to serialize it. quotaoff isn't exactly a performance
  77. * critical thing.
  78. * If quotaoff, then we must be dealing with the root filesystem.
  79. */
  80. ASSERT(q);
  81. mutex_lock(&q->qi_quotaofflock);
  82. /*
  83. * If we're just turning off quota enforcement, change mp and go.
  84. */
  85. if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
  86. mp->m_qflags &= ~(flags);
  87. spin_lock(&mp->m_sb_lock);
  88. mp->m_sb.sb_qflags = mp->m_qflags;
  89. spin_unlock(&mp->m_sb_lock);
  90. mutex_unlock(&q->qi_quotaofflock);
  91. /* XXX what to do if error ? Revert back to old vals incore ? */
  92. error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
  93. return (error);
  94. }
  95. dqtype = 0;
  96. inactivate_flags = 0;
  97. /*
  98. * If accounting is off, we must turn enforcement off, clear the
  99. * quota 'CHKD' certificate to make it known that we have to
  100. * do a quotacheck the next time this quota is turned on.
  101. */
  102. if (flags & XFS_UQUOTA_ACCT) {
  103. dqtype |= XFS_QMOPT_UQUOTA;
  104. flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
  105. inactivate_flags |= XFS_UQUOTA_ACTIVE;
  106. }
  107. if (flags & XFS_GQUOTA_ACCT) {
  108. dqtype |= XFS_QMOPT_GQUOTA;
  109. flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD);
  110. inactivate_flags |= XFS_GQUOTA_ACTIVE;
  111. }
  112. if (flags & XFS_PQUOTA_ACCT) {
  113. dqtype |= XFS_QMOPT_PQUOTA;
  114. flags |= (XFS_PQUOTA_CHKD | XFS_PQUOTA_ENFD);
  115. inactivate_flags |= XFS_PQUOTA_ACTIVE;
  116. }
  117. /*
  118. * Nothing to do? Don't complain. This happens when we're just
  119. * turning off quota enforcement.
  120. */
  121. if ((mp->m_qflags & flags) == 0)
  122. goto out_unlock;
  123. /*
  124. * Write the LI_QUOTAOFF log record, and do SB changes atomically,
  125. * and synchronously. If we fail to write, we should abort the
  126. * operation as it cannot be recovered safely if we crash.
  127. */
  128. error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
  129. if (error)
  130. goto out_unlock;
  131. /*
  132. * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
  133. * to take care of the race between dqget and quotaoff. We don't take
  134. * any special locks to reset these bits. All processes need to check
  135. * these bits *after* taking inode lock(s) to see if the particular
  136. * quota type is in the process of being turned off. If *ACTIVE, it is
  137. * guaranteed that all dquot structures and all quotainode ptrs will all
  138. * stay valid as long as that inode is kept locked.
  139. *
  140. * There is no turning back after this.
  141. */
  142. mp->m_qflags &= ~inactivate_flags;
  143. /*
  144. * Give back all the dquot reference(s) held by inodes.
  145. * Here we go thru every single incore inode in this file system, and
  146. * do a dqrele on the i_udquot/i_gdquot that it may have.
  147. * Essentially, as long as somebody has an inode locked, this guarantees
  148. * that quotas will not be turned off. This is handy because in a
  149. * transaction once we lock the inode(s) and check for quotaon, we can
  150. * depend on the quota inodes (and other things) being valid as long as
  151. * we keep the lock(s).
  152. */
  153. xfs_qm_dqrele_all_inodes(mp, flags);
  154. /*
  155. * Next we make the changes in the quota flag in the mount struct.
  156. * This isn't protected by a particular lock directly, because we
  157. * don't want to take a mrlock every time we depend on quotas being on.
  158. */
  159. mp->m_qflags &= ~flags;
  160. /*
  161. * Go through all the dquots of this file system and purge them,
  162. * according to what was turned off.
  163. */
  164. xfs_qm_dqpurge_all(mp, dqtype);
  165. /*
  166. * Transactions that had started before ACTIVE state bit was cleared
  167. * could have logged many dquots, so they'd have higher LSNs than
  168. * the first QUOTAOFF log record does. If we happen to crash when
  169. * the tail of the log has gone past the QUOTAOFF record, but
  170. * before the last dquot modification, those dquots __will__
  171. * recover, and that's not good.
  172. *
  173. * So, we have QUOTAOFF start and end logitems; the start
  174. * logitem won't get overwritten until the end logitem appears...
  175. */
  176. error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
  177. if (error) {
  178. /* We're screwed now. Shutdown is the only option. */
  179. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  180. goto out_unlock;
  181. }
  182. /*
  183. * If all quotas are completely turned off, close shop.
  184. */
  185. if (mp->m_qflags == 0) {
  186. mutex_unlock(&q->qi_quotaofflock);
  187. xfs_qm_destroy_quotainfo(mp);
  188. return (0);
  189. }
  190. /*
  191. * Release our quotainode references if we don't need them anymore.
  192. */
  193. if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
  194. IRELE(q->qi_uquotaip);
  195. q->qi_uquotaip = NULL;
  196. }
  197. if ((dqtype & XFS_QMOPT_GQUOTA) && q->qi_gquotaip) {
  198. IRELE(q->qi_gquotaip);
  199. q->qi_gquotaip = NULL;
  200. }
  201. if ((dqtype & XFS_QMOPT_PQUOTA) && q->qi_pquotaip) {
  202. IRELE(q->qi_pquotaip);
  203. q->qi_pquotaip = NULL;
  204. }
  205. out_unlock:
  206. mutex_unlock(&q->qi_quotaofflock);
  207. return error;
  208. }
  209. STATIC int
  210. xfs_qm_scall_trunc_qfile(
  211. struct xfs_mount *mp,
  212. xfs_ino_t ino)
  213. {
  214. struct xfs_inode *ip;
  215. struct xfs_trans *tp;
  216. int error;
  217. if (ino == NULLFSINO)
  218. return 0;
  219. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  220. if (error)
  221. return error;
  222. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  223. tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
  224. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  225. if (error) {
  226. xfs_trans_cancel(tp, 0);
  227. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  228. goto out_put;
  229. }
  230. xfs_ilock(ip, XFS_ILOCK_EXCL);
  231. xfs_trans_ijoin(tp, ip, 0);
  232. ip->i_d.di_size = 0;
  233. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  234. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  235. if (error) {
  236. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
  237. XFS_TRANS_ABORT);
  238. goto out_unlock;
  239. }
  240. ASSERT(ip->i_d.di_nextents == 0);
  241. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  242. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  243. out_unlock:
  244. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  245. out_put:
  246. IRELE(ip);
  247. return error;
  248. }
  249. int
  250. xfs_qm_scall_trunc_qfiles(
  251. xfs_mount_t *mp,
  252. uint flags)
  253. {
  254. int error = 0, error2 = 0;
  255. if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) {
  256. xfs_debug(mp, "%s: flags=%x m_qflags=%x\n",
  257. __func__, flags, mp->m_qflags);
  258. return XFS_ERROR(EINVAL);
  259. }
  260. if (flags & XFS_DQ_USER)
  261. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
  262. if (flags & XFS_DQ_GROUP)
  263. error2 = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
  264. if (flags & XFS_DQ_PROJ)
  265. error2 = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
  266. return error ? error : error2;
  267. }
  268. /*
  269. * Switch on (a given) quota enforcement for a filesystem. This takes
  270. * effect immediately.
  271. * (Switching on quota accounting must be done at mount time.)
  272. */
  273. int
  274. xfs_qm_scall_quotaon(
  275. xfs_mount_t *mp,
  276. uint flags)
  277. {
  278. int error;
  279. uint qf;
  280. __int64_t sbflags;
  281. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  282. /*
  283. * Switching on quota accounting must be done at mount time.
  284. */
  285. flags &= ~(XFS_ALL_QUOTA_ACCT);
  286. sbflags = 0;
  287. if (flags == 0) {
  288. xfs_debug(mp, "%s: zero flags, m_qflags=%x\n",
  289. __func__, mp->m_qflags);
  290. return XFS_ERROR(EINVAL);
  291. }
  292. /* No fs can turn on quotas with a delayed effect */
  293. ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
  294. /*
  295. * Can't enforce without accounting. We check the superblock
  296. * qflags here instead of m_qflags because rootfs can have
  297. * quota acct on ondisk without m_qflags' knowing.
  298. */
  299. if (((flags & XFS_UQUOTA_ACCT) == 0 &&
  300. (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
  301. (flags & XFS_UQUOTA_ENFD)) ||
  302. ((flags & XFS_GQUOTA_ACCT) == 0 &&
  303. (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
  304. (flags & XFS_GQUOTA_ENFD)) ||
  305. ((flags & XFS_PQUOTA_ACCT) == 0 &&
  306. (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
  307. (flags & XFS_PQUOTA_ENFD))) {
  308. xfs_debug(mp,
  309. "%s: Can't enforce without acct, flags=%x sbflags=%x\n",
  310. __func__, flags, mp->m_sb.sb_qflags);
  311. return XFS_ERROR(EINVAL);
  312. }
  313. /*
  314. * If everything's up to-date incore, then don't waste time.
  315. */
  316. if ((mp->m_qflags & flags) == flags)
  317. return XFS_ERROR(EEXIST);
  318. /*
  319. * Change sb_qflags on disk but not incore mp->qflags
  320. * if this is the root filesystem.
  321. */
  322. spin_lock(&mp->m_sb_lock);
  323. qf = mp->m_sb.sb_qflags;
  324. mp->m_sb.sb_qflags = qf | flags;
  325. spin_unlock(&mp->m_sb_lock);
  326. /*
  327. * There's nothing to change if it's the same.
  328. */
  329. if ((qf & flags) == flags && sbflags == 0)
  330. return XFS_ERROR(EEXIST);
  331. sbflags |= XFS_SB_QFLAGS;
  332. if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
  333. return (error);
  334. /*
  335. * If we aren't trying to switch on quota enforcement, we are done.
  336. */
  337. if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
  338. (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
  339. ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
  340. (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
  341. ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
  342. (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
  343. (flags & XFS_ALL_QUOTA_ENFD) == 0)
  344. return (0);
  345. if (! XFS_IS_QUOTA_RUNNING(mp))
  346. return XFS_ERROR(ESRCH);
  347. /*
  348. * Switch on quota enforcement in core.
  349. */
  350. mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
  351. mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
  352. mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
  353. return (0);
  354. }
  355. /*
  356. * Return quota status information, such as uquota-off, enforcements, etc.
  357. */
  358. int
  359. xfs_qm_scall_getqstat(
  360. struct xfs_mount *mp,
  361. struct fs_quota_stat *out)
  362. {
  363. struct xfs_quotainfo *q = mp->m_quotainfo;
  364. struct xfs_inode *uip = NULL;
  365. struct xfs_inode *gip = NULL;
  366. struct xfs_inode *pip = NULL;
  367. bool tempuqip = false;
  368. bool tempgqip = false;
  369. bool temppqip = false;
  370. memset(out, 0, sizeof(fs_quota_stat_t));
  371. out->qs_version = FS_QSTAT_VERSION;
  372. if (!xfs_sb_version_hasquota(&mp->m_sb)) {
  373. out->qs_uquota.qfs_ino = NULLFSINO;
  374. out->qs_gquota.qfs_ino = NULLFSINO;
  375. return (0);
  376. }
  377. out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
  378. (XFS_ALL_QUOTA_ACCT|
  379. XFS_ALL_QUOTA_ENFD));
  380. if (q) {
  381. uip = q->qi_uquotaip;
  382. gip = q->qi_gquotaip;
  383. pip = q->qi_pquotaip;
  384. }
  385. if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
  386. if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  387. 0, 0, &uip) == 0)
  388. tempuqip = true;
  389. }
  390. if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
  391. if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  392. 0, 0, &gip) == 0)
  393. tempgqip = true;
  394. }
  395. /*
  396. * Q_XGETQSTAT doesn't have room for both group and project quotas.
  397. * So, allow the project quota values to be copied out only if
  398. * there is no group quota information available.
  399. */
  400. if (!gip) {
  401. if (!pip && mp->m_sb.sb_pquotino != NULLFSINO) {
  402. if (xfs_iget(mp, NULL, mp->m_sb.sb_pquotino,
  403. 0, 0, &pip) == 0)
  404. temppqip = true;
  405. }
  406. } else
  407. pip = NULL;
  408. if (uip) {
  409. out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
  410. out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
  411. out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
  412. if (tempuqip)
  413. IRELE(uip);
  414. }
  415. if (gip) {
  416. out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
  417. out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
  418. out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
  419. if (tempgqip)
  420. IRELE(gip);
  421. }
  422. if (pip) {
  423. out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
  424. out->qs_gquota.qfs_nblks = pip->i_d.di_nblocks;
  425. out->qs_gquota.qfs_nextents = pip->i_d.di_nextents;
  426. if (temppqip)
  427. IRELE(pip);
  428. }
  429. if (q) {
  430. out->qs_incoredqs = q->qi_dquots;
  431. out->qs_btimelimit = q->qi_btimelimit;
  432. out->qs_itimelimit = q->qi_itimelimit;
  433. out->qs_rtbtimelimit = q->qi_rtbtimelimit;
  434. out->qs_bwarnlimit = q->qi_bwarnlimit;
  435. out->qs_iwarnlimit = q->qi_iwarnlimit;
  436. }
  437. return 0;
  438. }
  439. #define XFS_DQ_MASK \
  440. (FS_DQ_LIMIT_MASK | FS_DQ_TIMER_MASK | FS_DQ_WARNS_MASK)
  441. /*
  442. * Adjust quota limits, and start/stop timers accordingly.
  443. */
  444. int
  445. xfs_qm_scall_setqlim(
  446. struct xfs_mount *mp,
  447. xfs_dqid_t id,
  448. uint type,
  449. fs_disk_quota_t *newlim)
  450. {
  451. struct xfs_quotainfo *q = mp->m_quotainfo;
  452. struct xfs_disk_dquot *ddq;
  453. struct xfs_dquot *dqp;
  454. struct xfs_trans *tp;
  455. int error;
  456. xfs_qcnt_t hard, soft;
  457. if (newlim->d_fieldmask & ~XFS_DQ_MASK)
  458. return EINVAL;
  459. if ((newlim->d_fieldmask & XFS_DQ_MASK) == 0)
  460. return 0;
  461. /*
  462. * We don't want to race with a quotaoff so take the quotaoff lock.
  463. * We don't hold an inode lock, so there's nothing else to stop
  464. * a quotaoff from happening.
  465. */
  466. mutex_lock(&q->qi_quotaofflock);
  467. /*
  468. * Get the dquot (locked) before we start, as we need to do a
  469. * transaction to allocate it if it doesn't exist. Once we have the
  470. * dquot, unlock it so we can start the next transaction safely. We hold
  471. * a reference to the dquot, so it's safe to do this unlock/lock without
  472. * it being reclaimed in the mean time.
  473. */
  474. error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp);
  475. if (error) {
  476. ASSERT(error != ENOENT);
  477. goto out_unlock;
  478. }
  479. xfs_dqunlock(dqp);
  480. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
  481. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_setqlim, 0, 0);
  482. if (error) {
  483. xfs_trans_cancel(tp, 0);
  484. goto out_rele;
  485. }
  486. xfs_dqlock(dqp);
  487. xfs_trans_dqjoin(tp, dqp);
  488. ddq = &dqp->q_core;
  489. /*
  490. * Make sure that hardlimits are >= soft limits before changing.
  491. */
  492. hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
  493. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
  494. be64_to_cpu(ddq->d_blk_hardlimit);
  495. soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
  496. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
  497. be64_to_cpu(ddq->d_blk_softlimit);
  498. if (hard == 0 || hard >= soft) {
  499. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  500. ddq->d_blk_softlimit = cpu_to_be64(soft);
  501. xfs_dquot_set_prealloc_limits(dqp);
  502. if (id == 0) {
  503. q->qi_bhardlimit = hard;
  504. q->qi_bsoftlimit = soft;
  505. }
  506. } else {
  507. xfs_debug(mp, "blkhard %Ld < blksoft %Ld\n", hard, soft);
  508. }
  509. hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
  510. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
  511. be64_to_cpu(ddq->d_rtb_hardlimit);
  512. soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
  513. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
  514. be64_to_cpu(ddq->d_rtb_softlimit);
  515. if (hard == 0 || hard >= soft) {
  516. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  517. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  518. if (id == 0) {
  519. q->qi_rtbhardlimit = hard;
  520. q->qi_rtbsoftlimit = soft;
  521. }
  522. } else {
  523. xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
  524. }
  525. hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
  526. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  527. be64_to_cpu(ddq->d_ino_hardlimit);
  528. soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
  529. (xfs_qcnt_t) newlim->d_ino_softlimit :
  530. be64_to_cpu(ddq->d_ino_softlimit);
  531. if (hard == 0 || hard >= soft) {
  532. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  533. ddq->d_ino_softlimit = cpu_to_be64(soft);
  534. if (id == 0) {
  535. q->qi_ihardlimit = hard;
  536. q->qi_isoftlimit = soft;
  537. }
  538. } else {
  539. xfs_debug(mp, "ihard %Ld < isoft %Ld\n", hard, soft);
  540. }
  541. /*
  542. * Update warnings counter(s) if requested
  543. */
  544. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  545. ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
  546. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  547. ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
  548. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  549. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
  550. if (id == 0) {
  551. /*
  552. * Timelimits for the super user set the relative time
  553. * the other users can be over quota for this file system.
  554. * If it is zero a default is used. Ditto for the default
  555. * soft and hard limit values (already done, above), and
  556. * for warnings.
  557. */
  558. if (newlim->d_fieldmask & FS_DQ_BTIMER) {
  559. q->qi_btimelimit = newlim->d_btimer;
  560. ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
  561. }
  562. if (newlim->d_fieldmask & FS_DQ_ITIMER) {
  563. q->qi_itimelimit = newlim->d_itimer;
  564. ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
  565. }
  566. if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
  567. q->qi_rtbtimelimit = newlim->d_rtbtimer;
  568. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
  569. }
  570. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  571. q->qi_bwarnlimit = newlim->d_bwarns;
  572. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  573. q->qi_iwarnlimit = newlim->d_iwarns;
  574. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  575. q->qi_rtbwarnlimit = newlim->d_rtbwarns;
  576. } else {
  577. /*
  578. * If the user is now over quota, start the timelimit.
  579. * The user will not be 'warned'.
  580. * Note that we keep the timers ticking, whether enforcement
  581. * is on or off. We don't really want to bother with iterating
  582. * over all ondisk dquots and turning the timers on/off.
  583. */
  584. xfs_qm_adjust_dqtimers(mp, ddq);
  585. }
  586. dqp->dq_flags |= XFS_DQ_DIRTY;
  587. xfs_trans_log_dquot(tp, dqp);
  588. error = xfs_trans_commit(tp, 0);
  589. out_rele:
  590. xfs_qm_dqrele(dqp);
  591. out_unlock:
  592. mutex_unlock(&q->qi_quotaofflock);
  593. return error;
  594. }
  595. STATIC int
  596. xfs_qm_log_quotaoff_end(
  597. xfs_mount_t *mp,
  598. xfs_qoff_logitem_t *startqoff,
  599. uint flags)
  600. {
  601. xfs_trans_t *tp;
  602. int error;
  603. xfs_qoff_logitem_t *qoffi;
  604. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  605. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_equotaoff, 0, 0);
  606. if (error) {
  607. xfs_trans_cancel(tp, 0);
  608. return (error);
  609. }
  610. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  611. flags & XFS_ALL_QUOTA_ACCT);
  612. xfs_trans_log_quotaoff_item(tp, qoffi);
  613. /*
  614. * We have to make sure that the transaction is secure on disk before we
  615. * return and actually stop quota accounting. So, make it synchronous.
  616. * We don't care about quotoff's performance.
  617. */
  618. xfs_trans_set_sync(tp);
  619. error = xfs_trans_commit(tp, 0);
  620. return (error);
  621. }
  622. STATIC int
  623. xfs_qm_log_quotaoff(
  624. xfs_mount_t *mp,
  625. xfs_qoff_logitem_t **qoffstartp,
  626. uint flags)
  627. {
  628. xfs_trans_t *tp;
  629. int error;
  630. xfs_qoff_logitem_t *qoffi=NULL;
  631. uint oldsbqflag=0;
  632. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  633. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_quotaoff, 0, 0);
  634. if (error)
  635. goto error0;
  636. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  637. xfs_trans_log_quotaoff_item(tp, qoffi);
  638. spin_lock(&mp->m_sb_lock);
  639. oldsbqflag = mp->m_sb.sb_qflags;
  640. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  641. spin_unlock(&mp->m_sb_lock);
  642. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  643. /*
  644. * We have to make sure that the transaction is secure on disk before we
  645. * return and actually stop quota accounting. So, make it synchronous.
  646. * We don't care about quotoff's performance.
  647. */
  648. xfs_trans_set_sync(tp);
  649. error = xfs_trans_commit(tp, 0);
  650. error0:
  651. if (error) {
  652. xfs_trans_cancel(tp, 0);
  653. /*
  654. * No one else is modifying sb_qflags, so this is OK.
  655. * We still hold the quotaofflock.
  656. */
  657. spin_lock(&mp->m_sb_lock);
  658. mp->m_sb.sb_qflags = oldsbqflag;
  659. spin_unlock(&mp->m_sb_lock);
  660. }
  661. *qoffstartp = qoffi;
  662. return (error);
  663. }
  664. int
  665. xfs_qm_scall_getquota(
  666. struct xfs_mount *mp,
  667. xfs_dqid_t id,
  668. uint type,
  669. struct fs_disk_quota *dst)
  670. {
  671. struct xfs_dquot *dqp;
  672. int error;
  673. /*
  674. * Try to get the dquot. We don't want it allocated on disk, so
  675. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  676. * exist, we'll get ENOENT back.
  677. */
  678. error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp);
  679. if (error)
  680. return error;
  681. /*
  682. * If everything's NULL, this dquot doesn't quite exist as far as
  683. * our utility programs are concerned.
  684. */
  685. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  686. error = XFS_ERROR(ENOENT);
  687. goto out_put;
  688. }
  689. memset(dst, 0, sizeof(*dst));
  690. dst->d_version = FS_DQUOT_VERSION;
  691. dst->d_flags = xfs_qm_export_qtype_flags(dqp->q_core.d_flags);
  692. dst->d_id = be32_to_cpu(dqp->q_core.d_id);
  693. dst->d_blk_hardlimit =
  694. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
  695. dst->d_blk_softlimit =
  696. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
  697. dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  698. dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  699. dst->d_bcount = XFS_FSB_TO_BB(mp, dqp->q_res_bcount);
  700. dst->d_icount = dqp->q_res_icount;
  701. dst->d_btimer = be32_to_cpu(dqp->q_core.d_btimer);
  702. dst->d_itimer = be32_to_cpu(dqp->q_core.d_itimer);
  703. dst->d_iwarns = be16_to_cpu(dqp->q_core.d_iwarns);
  704. dst->d_bwarns = be16_to_cpu(dqp->q_core.d_bwarns);
  705. dst->d_rtb_hardlimit =
  706. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
  707. dst->d_rtb_softlimit =
  708. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
  709. dst->d_rtbcount = XFS_FSB_TO_BB(mp, dqp->q_res_rtbcount);
  710. dst->d_rtbtimer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  711. dst->d_rtbwarns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  712. /*
  713. * Internally, we don't reset all the timers when quota enforcement
  714. * gets turned off. No need to confuse the user level code,
  715. * so return zeroes in that case.
  716. */
  717. if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
  718. dqp->q_core.d_flags == XFS_DQ_USER) ||
  719. (!XFS_IS_GQUOTA_ENFORCED(mp) &&
  720. dqp->q_core.d_flags == XFS_DQ_GROUP) ||
  721. (!XFS_IS_PQUOTA_ENFORCED(mp) &&
  722. dqp->q_core.d_flags == XFS_DQ_PROJ)) {
  723. dst->d_btimer = 0;
  724. dst->d_itimer = 0;
  725. dst->d_rtbtimer = 0;
  726. }
  727. #ifdef DEBUG
  728. if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == FS_USER_QUOTA) ||
  729. (XFS_IS_GQUOTA_ENFORCED(mp) && dst->d_flags == FS_GROUP_QUOTA) ||
  730. (XFS_IS_PQUOTA_ENFORCED(mp) && dst->d_flags == FS_PROJ_QUOTA)) &&
  731. dst->d_id != 0) {
  732. if ((dst->d_bcount > dst->d_blk_softlimit) &&
  733. (dst->d_blk_softlimit > 0)) {
  734. ASSERT(dst->d_btimer != 0);
  735. }
  736. if ((dst->d_icount > dst->d_ino_softlimit) &&
  737. (dst->d_ino_softlimit > 0)) {
  738. ASSERT(dst->d_itimer != 0);
  739. }
  740. }
  741. #endif
  742. out_put:
  743. xfs_qm_dqput(dqp);
  744. return error;
  745. }
  746. STATIC uint
  747. xfs_qm_export_qtype_flags(
  748. uint flags)
  749. {
  750. /*
  751. * Can't be more than one, or none.
  752. */
  753. ASSERT((flags & (FS_PROJ_QUOTA | FS_USER_QUOTA)) !=
  754. (FS_PROJ_QUOTA | FS_USER_QUOTA));
  755. ASSERT((flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)) !=
  756. (FS_PROJ_QUOTA | FS_GROUP_QUOTA));
  757. ASSERT((flags & (FS_USER_QUOTA | FS_GROUP_QUOTA)) !=
  758. (FS_USER_QUOTA | FS_GROUP_QUOTA));
  759. ASSERT((flags & (FS_PROJ_QUOTA|FS_USER_QUOTA|FS_GROUP_QUOTA)) != 0);
  760. return (flags & XFS_DQ_USER) ?
  761. FS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
  762. FS_PROJ_QUOTA : FS_GROUP_QUOTA;
  763. }
  764. STATIC uint
  765. xfs_qm_export_flags(
  766. uint flags)
  767. {
  768. uint uflags;
  769. uflags = 0;
  770. if (flags & XFS_UQUOTA_ACCT)
  771. uflags |= FS_QUOTA_UDQ_ACCT;
  772. if (flags & XFS_GQUOTA_ACCT)
  773. uflags |= FS_QUOTA_GDQ_ACCT;
  774. if (flags & XFS_PQUOTA_ACCT)
  775. uflags |= FS_QUOTA_PDQ_ACCT;
  776. if (flags & XFS_UQUOTA_ENFD)
  777. uflags |= FS_QUOTA_UDQ_ENFD;
  778. if (flags & XFS_GQUOTA_ENFD)
  779. uflags |= FS_QUOTA_GDQ_ENFD;
  780. if (flags & XFS_PQUOTA_ENFD)
  781. uflags |= FS_QUOTA_PDQ_ENFD;
  782. return (uflags);
  783. }
  784. STATIC int
  785. xfs_dqrele_inode(
  786. struct xfs_inode *ip,
  787. struct xfs_perag *pag,
  788. int flags,
  789. void *args)
  790. {
  791. /* skip quota inodes */
  792. if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
  793. ip == ip->i_mount->m_quotainfo->qi_gquotaip ||
  794. ip == ip->i_mount->m_quotainfo->qi_pquotaip) {
  795. ASSERT(ip->i_udquot == NULL);
  796. ASSERT(ip->i_gdquot == NULL);
  797. ASSERT(ip->i_pdquot == NULL);
  798. return 0;
  799. }
  800. xfs_ilock(ip, XFS_ILOCK_EXCL);
  801. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  802. xfs_qm_dqrele(ip->i_udquot);
  803. ip->i_udquot = NULL;
  804. }
  805. if ((flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  806. xfs_qm_dqrele(ip->i_gdquot);
  807. ip->i_gdquot = NULL;
  808. }
  809. if ((flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) {
  810. xfs_qm_dqrele(ip->i_pdquot);
  811. ip->i_pdquot = NULL;
  812. }
  813. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  814. return 0;
  815. }
  816. /*
  817. * Go thru all the inodes in the file system, releasing their dquots.
  818. *
  819. * Note that the mount structure gets modified to indicate that quotas are off
  820. * AFTER this, in the case of quotaoff.
  821. */
  822. void
  823. xfs_qm_dqrele_all_inodes(
  824. struct xfs_mount *mp,
  825. uint flags)
  826. {
  827. ASSERT(mp->m_quotainfo);
  828. xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, NULL);
  829. }