xfs_qm_syscalls.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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_inum.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. 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. boolean_t tempuqip, tempgqip;
  362. uip = gip = NULL;
  363. tempuqip = tempgqip = B_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 = B_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 = B_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. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
  437. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  438. xfs_trans_cancel(tp, 0);
  439. return (error);
  440. }
  441. /*
  442. * We don't want to race with a quotaoff so take the quotaoff lock.
  443. * (We don't hold an inode lock, so there's nothing else to stop
  444. * a quotaoff from happening). (XXXThis doesn't currently happen
  445. * because we take the vfslock before calling xfs_qm_sysent).
  446. */
  447. mutex_lock(&q->qi_quotaofflock);
  448. /*
  449. * Get the dquot (locked), and join it to the transaction.
  450. * Allocate the dquot if this doesn't exist.
  451. */
  452. if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
  453. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  454. ASSERT(error != ENOENT);
  455. goto out_unlock;
  456. }
  457. xfs_trans_dqjoin(tp, dqp);
  458. ddq = &dqp->q_core;
  459. /*
  460. * Make sure that hardlimits are >= soft limits before changing.
  461. */
  462. hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
  463. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
  464. be64_to_cpu(ddq->d_blk_hardlimit);
  465. soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
  466. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
  467. be64_to_cpu(ddq->d_blk_softlimit);
  468. if (hard == 0 || hard >= soft) {
  469. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  470. ddq->d_blk_softlimit = cpu_to_be64(soft);
  471. if (id == 0) {
  472. q->qi_bhardlimit = hard;
  473. q->qi_bsoftlimit = soft;
  474. }
  475. } else {
  476. xfs_debug(mp, "blkhard %Ld < blksoft %Ld\n", hard, soft);
  477. }
  478. hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
  479. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
  480. be64_to_cpu(ddq->d_rtb_hardlimit);
  481. soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
  482. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
  483. be64_to_cpu(ddq->d_rtb_softlimit);
  484. if (hard == 0 || hard >= soft) {
  485. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  486. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  487. if (id == 0) {
  488. q->qi_rtbhardlimit = hard;
  489. q->qi_rtbsoftlimit = soft;
  490. }
  491. } else {
  492. xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
  493. }
  494. hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
  495. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  496. be64_to_cpu(ddq->d_ino_hardlimit);
  497. soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
  498. (xfs_qcnt_t) newlim->d_ino_softlimit :
  499. be64_to_cpu(ddq->d_ino_softlimit);
  500. if (hard == 0 || hard >= soft) {
  501. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  502. ddq->d_ino_softlimit = cpu_to_be64(soft);
  503. if (id == 0) {
  504. q->qi_ihardlimit = hard;
  505. q->qi_isoftlimit = soft;
  506. }
  507. } else {
  508. xfs_debug(mp, "ihard %Ld < isoft %Ld\n", hard, soft);
  509. }
  510. /*
  511. * Update warnings counter(s) if requested
  512. */
  513. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  514. ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
  515. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  516. ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
  517. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  518. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
  519. if (id == 0) {
  520. /*
  521. * Timelimits for the super user set the relative time
  522. * the other users can be over quota for this file system.
  523. * If it is zero a default is used. Ditto for the default
  524. * soft and hard limit values (already done, above), and
  525. * for warnings.
  526. */
  527. if (newlim->d_fieldmask & FS_DQ_BTIMER) {
  528. q->qi_btimelimit = newlim->d_btimer;
  529. ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
  530. }
  531. if (newlim->d_fieldmask & FS_DQ_ITIMER) {
  532. q->qi_itimelimit = newlim->d_itimer;
  533. ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
  534. }
  535. if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
  536. q->qi_rtbtimelimit = newlim->d_rtbtimer;
  537. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
  538. }
  539. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  540. q->qi_bwarnlimit = newlim->d_bwarns;
  541. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  542. q->qi_iwarnlimit = newlim->d_iwarns;
  543. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  544. q->qi_rtbwarnlimit = newlim->d_rtbwarns;
  545. } else {
  546. /*
  547. * If the user is now over quota, start the timelimit.
  548. * The user will not be 'warned'.
  549. * Note that we keep the timers ticking, whether enforcement
  550. * is on or off. We don't really want to bother with iterating
  551. * over all ondisk dquots and turning the timers on/off.
  552. */
  553. xfs_qm_adjust_dqtimers(mp, ddq);
  554. }
  555. dqp->dq_flags |= XFS_DQ_DIRTY;
  556. xfs_trans_log_dquot(tp, dqp);
  557. error = xfs_trans_commit(tp, 0);
  558. xfs_qm_dqrele(dqp);
  559. out_unlock:
  560. mutex_unlock(&q->qi_quotaofflock);
  561. return error;
  562. }
  563. STATIC int
  564. xfs_qm_log_quotaoff_end(
  565. xfs_mount_t *mp,
  566. xfs_qoff_logitem_t *startqoff,
  567. uint flags)
  568. {
  569. xfs_trans_t *tp;
  570. int error;
  571. xfs_qoff_logitem_t *qoffi;
  572. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  573. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
  574. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  575. xfs_trans_cancel(tp, 0);
  576. return (error);
  577. }
  578. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  579. flags & XFS_ALL_QUOTA_ACCT);
  580. xfs_trans_log_quotaoff_item(tp, qoffi);
  581. /*
  582. * We have to make sure that the transaction is secure on disk before we
  583. * return and actually stop quota accounting. So, make it synchronous.
  584. * We don't care about quotoff's performance.
  585. */
  586. xfs_trans_set_sync(tp);
  587. error = xfs_trans_commit(tp, 0);
  588. return (error);
  589. }
  590. STATIC int
  591. xfs_qm_log_quotaoff(
  592. xfs_mount_t *mp,
  593. xfs_qoff_logitem_t **qoffstartp,
  594. uint flags)
  595. {
  596. xfs_trans_t *tp;
  597. int error;
  598. xfs_qoff_logitem_t *qoffi=NULL;
  599. uint oldsbqflag=0;
  600. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  601. if ((error = xfs_trans_reserve(tp, 0,
  602. sizeof(xfs_qoff_logitem_t) * 2 +
  603. mp->m_sb.sb_sectsize + 128,
  604. 0,
  605. 0,
  606. XFS_DEFAULT_LOG_COUNT))) {
  607. goto error0;
  608. }
  609. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  610. xfs_trans_log_quotaoff_item(tp, qoffi);
  611. spin_lock(&mp->m_sb_lock);
  612. oldsbqflag = mp->m_sb.sb_qflags;
  613. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  614. spin_unlock(&mp->m_sb_lock);
  615. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  616. /*
  617. * We have to make sure that the transaction is secure on disk before we
  618. * return and actually stop quota accounting. So, make it synchronous.
  619. * We don't care about quotoff's performance.
  620. */
  621. xfs_trans_set_sync(tp);
  622. error = xfs_trans_commit(tp, 0);
  623. error0:
  624. if (error) {
  625. xfs_trans_cancel(tp, 0);
  626. /*
  627. * No one else is modifying sb_qflags, so this is OK.
  628. * We still hold the quotaofflock.
  629. */
  630. spin_lock(&mp->m_sb_lock);
  631. mp->m_sb.sb_qflags = oldsbqflag;
  632. spin_unlock(&mp->m_sb_lock);
  633. }
  634. *qoffstartp = qoffi;
  635. return (error);
  636. }
  637. int
  638. xfs_qm_scall_getquota(
  639. struct xfs_mount *mp,
  640. xfs_dqid_t id,
  641. uint type,
  642. struct fs_disk_quota *dst)
  643. {
  644. struct xfs_dquot *dqp;
  645. int error;
  646. /*
  647. * Try to get the dquot. We don't want it allocated on disk, so
  648. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  649. * exist, we'll get ENOENT back.
  650. */
  651. error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp);
  652. if (error)
  653. return error;
  654. /*
  655. * If everything's NULL, this dquot doesn't quite exist as far as
  656. * our utility programs are concerned.
  657. */
  658. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  659. error = XFS_ERROR(ENOENT);
  660. goto out_put;
  661. }
  662. memset(dst, 0, sizeof(*dst));
  663. dst->d_version = FS_DQUOT_VERSION;
  664. dst->d_flags = xfs_qm_export_qtype_flags(dqp->q_core.d_flags);
  665. dst->d_id = be32_to_cpu(dqp->q_core.d_id);
  666. dst->d_blk_hardlimit =
  667. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
  668. dst->d_blk_softlimit =
  669. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
  670. dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  671. dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  672. dst->d_bcount = XFS_FSB_TO_BB(mp, dqp->q_res_bcount);
  673. dst->d_icount = dqp->q_res_icount;
  674. dst->d_btimer = be32_to_cpu(dqp->q_core.d_btimer);
  675. dst->d_itimer = be32_to_cpu(dqp->q_core.d_itimer);
  676. dst->d_iwarns = be16_to_cpu(dqp->q_core.d_iwarns);
  677. dst->d_bwarns = be16_to_cpu(dqp->q_core.d_bwarns);
  678. dst->d_rtb_hardlimit =
  679. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
  680. dst->d_rtb_softlimit =
  681. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
  682. dst->d_rtbcount = XFS_FSB_TO_BB(mp, dqp->q_res_rtbcount);
  683. dst->d_rtbtimer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  684. dst->d_rtbwarns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  685. /*
  686. * Internally, we don't reset all the timers when quota enforcement
  687. * gets turned off. No need to confuse the user level code,
  688. * so return zeroes in that case.
  689. */
  690. if ((!XFS_IS_UQUOTA_ENFORCED(mp) && dqp->q_core.d_flags == XFS_DQ_USER) ||
  691. (!XFS_IS_OQUOTA_ENFORCED(mp) &&
  692. (dqp->q_core.d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
  693. dst->d_btimer = 0;
  694. dst->d_itimer = 0;
  695. dst->d_rtbtimer = 0;
  696. }
  697. #ifdef DEBUG
  698. if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == FS_USER_QUOTA) ||
  699. (XFS_IS_OQUOTA_ENFORCED(mp) &&
  700. (dst->d_flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)))) &&
  701. dst->d_id != 0) {
  702. if (((int) dst->d_bcount > (int) dst->d_blk_softlimit) &&
  703. (dst->d_blk_softlimit > 0)) {
  704. ASSERT(dst->d_btimer != 0);
  705. }
  706. if (((int) dst->d_icount > (int) dst->d_ino_softlimit) &&
  707. (dst->d_ino_softlimit > 0)) {
  708. ASSERT(dst->d_itimer != 0);
  709. }
  710. }
  711. #endif
  712. out_put:
  713. xfs_qm_dqput(dqp);
  714. return error;
  715. }
  716. STATIC uint
  717. xfs_qm_export_qtype_flags(
  718. uint flags)
  719. {
  720. /*
  721. * Can't be more than one, or none.
  722. */
  723. ASSERT((flags & (FS_PROJ_QUOTA | FS_USER_QUOTA)) !=
  724. (FS_PROJ_QUOTA | FS_USER_QUOTA));
  725. ASSERT((flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)) !=
  726. (FS_PROJ_QUOTA | FS_GROUP_QUOTA));
  727. ASSERT((flags & (FS_USER_QUOTA | FS_GROUP_QUOTA)) !=
  728. (FS_USER_QUOTA | FS_GROUP_QUOTA));
  729. ASSERT((flags & (FS_PROJ_QUOTA|FS_USER_QUOTA|FS_GROUP_QUOTA)) != 0);
  730. return (flags & XFS_DQ_USER) ?
  731. FS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
  732. FS_PROJ_QUOTA : FS_GROUP_QUOTA;
  733. }
  734. STATIC uint
  735. xfs_qm_export_flags(
  736. uint flags)
  737. {
  738. uint uflags;
  739. uflags = 0;
  740. if (flags & XFS_UQUOTA_ACCT)
  741. uflags |= FS_QUOTA_UDQ_ACCT;
  742. if (flags & XFS_PQUOTA_ACCT)
  743. uflags |= FS_QUOTA_PDQ_ACCT;
  744. if (flags & XFS_GQUOTA_ACCT)
  745. uflags |= FS_QUOTA_GDQ_ACCT;
  746. if (flags & XFS_UQUOTA_ENFD)
  747. uflags |= FS_QUOTA_UDQ_ENFD;
  748. if (flags & (XFS_OQUOTA_ENFD)) {
  749. uflags |= (flags & XFS_GQUOTA_ACCT) ?
  750. FS_QUOTA_GDQ_ENFD : FS_QUOTA_PDQ_ENFD;
  751. }
  752. return (uflags);
  753. }
  754. STATIC int
  755. xfs_dqrele_inode(
  756. struct xfs_inode *ip,
  757. struct xfs_perag *pag,
  758. int flags)
  759. {
  760. /* skip quota inodes */
  761. if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
  762. ip == ip->i_mount->m_quotainfo->qi_gquotaip) {
  763. ASSERT(ip->i_udquot == NULL);
  764. ASSERT(ip->i_gdquot == NULL);
  765. return 0;
  766. }
  767. xfs_ilock(ip, XFS_ILOCK_EXCL);
  768. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  769. xfs_qm_dqrele(ip->i_udquot);
  770. ip->i_udquot = NULL;
  771. }
  772. if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  773. xfs_qm_dqrele(ip->i_gdquot);
  774. ip->i_gdquot = NULL;
  775. }
  776. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  777. return 0;
  778. }
  779. /*
  780. * Go thru all the inodes in the file system, releasing their dquots.
  781. *
  782. * Note that the mount structure gets modified to indicate that quotas are off
  783. * AFTER this, in the case of quotaoff.
  784. */
  785. void
  786. xfs_qm_dqrele_all_inodes(
  787. struct xfs_mount *mp,
  788. uint flags)
  789. {
  790. ASSERT(mp->m_quotainfo);
  791. xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags);
  792. }