xfs_qm_syscalls.c 24 KB

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