xfs_qm_syscalls.c 25 KB

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