xfs_qm_syscalls.c 24 KB

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