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