xfs_qm_syscalls.c 25 KB

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