xfs_qm_syscalls.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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_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. STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
  47. fs_disk_quota_t *);
  48. /*
  49. * Turn off quota accounting and/or enforcement for all udquots and/or
  50. * gdquots. Called only at unmount time.
  51. *
  52. * This assumes that there are no dquots of this file system cached
  53. * incore, and modifies the ondisk dquot directly. Therefore, for example,
  54. * it is an error to call this twice, without purging the cache.
  55. */
  56. int
  57. xfs_qm_scall_quotaoff(
  58. xfs_mount_t *mp,
  59. uint flags)
  60. {
  61. struct xfs_quotainfo *q = mp->m_quotainfo;
  62. uint dqtype;
  63. int error;
  64. uint inactivate_flags;
  65. xfs_qoff_logitem_t *qoffstart;
  66. int nculprits;
  67. /*
  68. * No file system can have quotas enabled on disk but not in core.
  69. * Note that quota utilities (like quotaoff) _expect_
  70. * errno == EEXIST here.
  71. */
  72. if ((mp->m_qflags & flags) == 0)
  73. return XFS_ERROR(EEXIST);
  74. error = 0;
  75. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  76. /*
  77. * We don't want to deal with two quotaoffs messing up each other,
  78. * so we're going to serialize it. quotaoff isn't exactly a performance
  79. * critical thing.
  80. * If quotaoff, then we must be dealing with the root filesystem.
  81. */
  82. ASSERT(q);
  83. mutex_lock(&q->qi_quotaofflock);
  84. /*
  85. * If we're just turning off quota enforcement, change mp and go.
  86. */
  87. if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
  88. mp->m_qflags &= ~(flags);
  89. spin_lock(&mp->m_sb_lock);
  90. mp->m_sb.sb_qflags = mp->m_qflags;
  91. spin_unlock(&mp->m_sb_lock);
  92. mutex_unlock(&q->qi_quotaofflock);
  93. /* XXX what to do if error ? Revert back to old vals incore ? */
  94. error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
  95. return (error);
  96. }
  97. dqtype = 0;
  98. inactivate_flags = 0;
  99. /*
  100. * If accounting is off, we must turn enforcement off, clear the
  101. * quota 'CHKD' certificate to make it known that we have to
  102. * do a quotacheck the next time this quota is turned on.
  103. */
  104. if (flags & XFS_UQUOTA_ACCT) {
  105. dqtype |= XFS_QMOPT_UQUOTA;
  106. flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
  107. inactivate_flags |= XFS_UQUOTA_ACTIVE;
  108. }
  109. if (flags & XFS_GQUOTA_ACCT) {
  110. dqtype |= XFS_QMOPT_GQUOTA;
  111. flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
  112. inactivate_flags |= XFS_GQUOTA_ACTIVE;
  113. } else if (flags & XFS_PQUOTA_ACCT) {
  114. dqtype |= XFS_QMOPT_PQUOTA;
  115. flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
  116. inactivate_flags |= XFS_PQUOTA_ACTIVE;
  117. }
  118. /*
  119. * Nothing to do? Don't complain. This happens when we're just
  120. * turning off quota enforcement.
  121. */
  122. if ((mp->m_qflags & flags) == 0)
  123. goto out_unlock;
  124. /*
  125. * Write the LI_QUOTAOFF log record, and do SB changes atomically,
  126. * and synchronously. If we fail to write, we should abort the
  127. * operation as it cannot be recovered safely if we crash.
  128. */
  129. error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
  130. if (error)
  131. goto out_unlock;
  132. /*
  133. * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
  134. * to take care of the race between dqget and quotaoff. We don't take
  135. * any special locks to reset these bits. All processes need to check
  136. * these bits *after* taking inode lock(s) to see if the particular
  137. * quota type is in the process of being turned off. If *ACTIVE, it is
  138. * guaranteed that all dquot structures and all quotainode ptrs will all
  139. * stay valid as long as that inode is kept locked.
  140. *
  141. * There is no turning back after this.
  142. */
  143. mp->m_qflags &= ~inactivate_flags;
  144. /*
  145. * Give back all the dquot reference(s) held by inodes.
  146. * Here we go thru every single incore inode in this file system, and
  147. * do a dqrele on the i_udquot/i_gdquot that it may have.
  148. * Essentially, as long as somebody has an inode locked, this guarantees
  149. * that quotas will not be turned off. This is handy because in a
  150. * transaction once we lock the inode(s) and check for quotaon, we can
  151. * depend on the quota inodes (and other things) being valid as long as
  152. * we keep the lock(s).
  153. */
  154. xfs_qm_dqrele_all_inodes(mp, flags);
  155. /*
  156. * Next we make the changes in the quota flag in the mount struct.
  157. * This isn't protected by a particular lock directly, because we
  158. * don't want to take a mrlock every time we depend on quotas being on.
  159. */
  160. mp->m_qflags &= ~(flags);
  161. /*
  162. * Go through all the dquots of this file system and purge them,
  163. * according to what was turned off. We may not be able to get rid
  164. * of all dquots, because dquots can have temporary references that
  165. * are not attached to inodes. eg. xfs_setattr, xfs_create.
  166. * So, if we couldn't purge all the dquots from the filesystem,
  167. * we can't get rid of the incore data structures.
  168. */
  169. while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype)))
  170. delay(10 * nculprits);
  171. /*
  172. * Transactions that had started before ACTIVE state bit was cleared
  173. * could have logged many dquots, so they'd have higher LSNs than
  174. * the first QUOTAOFF log record does. If we happen to crash when
  175. * the tail of the log has gone past the QUOTAOFF record, but
  176. * before the last dquot modification, those dquots __will__
  177. * recover, and that's not good.
  178. *
  179. * So, we have QUOTAOFF start and end logitems; the start
  180. * logitem won't get overwritten until the end logitem appears...
  181. */
  182. error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
  183. if (error) {
  184. /* We're screwed now. Shutdown is the only option. */
  185. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  186. goto out_unlock;
  187. }
  188. /*
  189. * If quotas is completely disabled, close shop.
  190. */
  191. if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
  192. ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
  193. mutex_unlock(&q->qi_quotaofflock);
  194. xfs_qm_destroy_quotainfo(mp);
  195. return (0);
  196. }
  197. /*
  198. * Release our quotainode references if we don't need them anymore.
  199. */
  200. if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
  201. IRELE(q->qi_uquotaip);
  202. q->qi_uquotaip = NULL;
  203. }
  204. if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && q->qi_gquotaip) {
  205. IRELE(q->qi_gquotaip);
  206. q->qi_gquotaip = NULL;
  207. }
  208. out_unlock:
  209. mutex_unlock(&q->qi_quotaofflock);
  210. return error;
  211. }
  212. STATIC int
  213. xfs_qm_scall_trunc_qfile(
  214. struct xfs_mount *mp,
  215. xfs_ino_t ino)
  216. {
  217. struct xfs_inode *ip;
  218. struct xfs_trans *tp;
  219. int error;
  220. if (ino == NULLFSINO)
  221. return 0;
  222. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  223. if (error)
  224. return error;
  225. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  226. tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
  227. error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  228. XFS_TRANS_PERM_LOG_RES,
  229. XFS_ITRUNCATE_LOG_COUNT);
  230. if (error) {
  231. xfs_trans_cancel(tp, 0);
  232. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  233. goto out_put;
  234. }
  235. xfs_ilock(ip, XFS_ILOCK_EXCL);
  236. xfs_trans_ijoin(tp, ip);
  237. error = xfs_itruncate_data(&tp, ip, 0);
  238. if (error) {
  239. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
  240. XFS_TRANS_ABORT);
  241. goto out_unlock;
  242. }
  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. ||
  303. ((flags & XFS_PQUOTA_ACCT) == 0 &&
  304. (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
  305. (flags & XFS_GQUOTA_ACCT) == 0 &&
  306. (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
  307. (flags & XFS_OQUOTA_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, *gip;
  365. boolean_t tempuqip, tempgqip;
  366. uip = gip = NULL;
  367. tempuqip = tempgqip = B_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 = B_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 = B_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. xfs_mount_t *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. xfs_disk_dquot_t *ddq;
  431. xfs_dquot_t *dqp;
  432. xfs_trans_t *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. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
  440. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
  441. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  442. xfs_trans_cancel(tp, 0);
  443. return (error);
  444. }
  445. /*
  446. * We don't want to race with a quotaoff so take the quotaoff lock.
  447. * (We don't hold an inode lock, so there's nothing else to stop
  448. * a quotaoff from happening). (XXXThis doesn't currently happen
  449. * because we take the vfslock before calling xfs_qm_sysent).
  450. */
  451. mutex_lock(&q->qi_quotaofflock);
  452. /*
  453. * Get the dquot (locked), and join it to the transaction.
  454. * Allocate the dquot if this doesn't exist.
  455. */
  456. if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
  457. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  458. ASSERT(error != ENOENT);
  459. goto out_unlock;
  460. }
  461. xfs_trans_dqjoin(tp, dqp);
  462. ddq = &dqp->q_core;
  463. /*
  464. * Make sure that hardlimits are >= soft limits before changing.
  465. */
  466. hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
  467. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
  468. be64_to_cpu(ddq->d_blk_hardlimit);
  469. soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
  470. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
  471. be64_to_cpu(ddq->d_blk_softlimit);
  472. if (hard == 0 || hard >= soft) {
  473. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  474. ddq->d_blk_softlimit = cpu_to_be64(soft);
  475. if (id == 0) {
  476. q->qi_bhardlimit = hard;
  477. q->qi_bsoftlimit = soft;
  478. }
  479. } else {
  480. xfs_debug(mp, "blkhard %Ld < blksoft %Ld\n", hard, soft);
  481. }
  482. hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
  483. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
  484. be64_to_cpu(ddq->d_rtb_hardlimit);
  485. soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
  486. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
  487. be64_to_cpu(ddq->d_rtb_softlimit);
  488. if (hard == 0 || hard >= soft) {
  489. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  490. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  491. if (id == 0) {
  492. q->qi_rtbhardlimit = hard;
  493. q->qi_rtbsoftlimit = soft;
  494. }
  495. } else {
  496. xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
  497. }
  498. hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
  499. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  500. be64_to_cpu(ddq->d_ino_hardlimit);
  501. soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
  502. (xfs_qcnt_t) newlim->d_ino_softlimit :
  503. be64_to_cpu(ddq->d_ino_softlimit);
  504. if (hard == 0 || hard >= soft) {
  505. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  506. ddq->d_ino_softlimit = cpu_to_be64(soft);
  507. if (id == 0) {
  508. q->qi_ihardlimit = hard;
  509. q->qi_isoftlimit = soft;
  510. }
  511. } else {
  512. xfs_debug(mp, "ihard %Ld < isoft %Ld\n", hard, soft);
  513. }
  514. /*
  515. * Update warnings counter(s) if requested
  516. */
  517. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  518. ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
  519. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  520. ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
  521. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  522. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
  523. if (id == 0) {
  524. /*
  525. * Timelimits for the super user set the relative time
  526. * the other users can be over quota for this file system.
  527. * If it is zero a default is used. Ditto for the default
  528. * soft and hard limit values (already done, above), and
  529. * for warnings.
  530. */
  531. if (newlim->d_fieldmask & FS_DQ_BTIMER) {
  532. q->qi_btimelimit = newlim->d_btimer;
  533. ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
  534. }
  535. if (newlim->d_fieldmask & FS_DQ_ITIMER) {
  536. q->qi_itimelimit = newlim->d_itimer;
  537. ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
  538. }
  539. if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
  540. q->qi_rtbtimelimit = newlim->d_rtbtimer;
  541. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
  542. }
  543. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  544. q->qi_bwarnlimit = newlim->d_bwarns;
  545. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  546. q->qi_iwarnlimit = newlim->d_iwarns;
  547. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  548. q->qi_rtbwarnlimit = newlim->d_rtbwarns;
  549. } else {
  550. /*
  551. * If the user is now over quota, start the timelimit.
  552. * The user will not be 'warned'.
  553. * Note that we keep the timers ticking, whether enforcement
  554. * is on or off. We don't really want to bother with iterating
  555. * over all ondisk dquots and turning the timers on/off.
  556. */
  557. xfs_qm_adjust_dqtimers(mp, ddq);
  558. }
  559. dqp->dq_flags |= XFS_DQ_DIRTY;
  560. xfs_trans_log_dquot(tp, dqp);
  561. error = xfs_trans_commit(tp, 0);
  562. xfs_qm_dqrele(dqp);
  563. out_unlock:
  564. mutex_unlock(&q->qi_quotaofflock);
  565. return error;
  566. }
  567. int
  568. xfs_qm_scall_getquota(
  569. xfs_mount_t *mp,
  570. xfs_dqid_t id,
  571. uint type,
  572. fs_disk_quota_t *out)
  573. {
  574. xfs_dquot_t *dqp;
  575. int error;
  576. /*
  577. * Try to get the dquot. We don't want it allocated on disk, so
  578. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  579. * exist, we'll get ENOENT back.
  580. */
  581. if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
  582. return (error);
  583. }
  584. /*
  585. * If everything's NULL, this dquot doesn't quite exist as far as
  586. * our utility programs are concerned.
  587. */
  588. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  589. xfs_qm_dqput(dqp);
  590. return XFS_ERROR(ENOENT);
  591. }
  592. /*
  593. * Convert the disk dquot to the exportable format
  594. */
  595. xfs_qm_export_dquot(mp, &dqp->q_core, out);
  596. xfs_qm_dqput(dqp);
  597. return (error ? XFS_ERROR(EFAULT) : 0);
  598. }
  599. STATIC int
  600. xfs_qm_log_quotaoff_end(
  601. xfs_mount_t *mp,
  602. xfs_qoff_logitem_t *startqoff,
  603. uint flags)
  604. {
  605. xfs_trans_t *tp;
  606. int error;
  607. xfs_qoff_logitem_t *qoffi;
  608. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  609. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
  610. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  611. xfs_trans_cancel(tp, 0);
  612. return (error);
  613. }
  614. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  615. flags & XFS_ALL_QUOTA_ACCT);
  616. xfs_trans_log_quotaoff_item(tp, qoffi);
  617. /*
  618. * We have to make sure that the transaction is secure on disk before we
  619. * return and actually stop quota accounting. So, make it synchronous.
  620. * We don't care about quotoff's performance.
  621. */
  622. xfs_trans_set_sync(tp);
  623. error = xfs_trans_commit(tp, 0);
  624. return (error);
  625. }
  626. STATIC int
  627. xfs_qm_log_quotaoff(
  628. xfs_mount_t *mp,
  629. xfs_qoff_logitem_t **qoffstartp,
  630. uint flags)
  631. {
  632. xfs_trans_t *tp;
  633. int error;
  634. xfs_qoff_logitem_t *qoffi=NULL;
  635. uint oldsbqflag=0;
  636. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  637. if ((error = xfs_trans_reserve(tp, 0,
  638. sizeof(xfs_qoff_logitem_t) * 2 +
  639. mp->m_sb.sb_sectsize + 128,
  640. 0,
  641. 0,
  642. XFS_DEFAULT_LOG_COUNT))) {
  643. goto error0;
  644. }
  645. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  646. xfs_trans_log_quotaoff_item(tp, qoffi);
  647. spin_lock(&mp->m_sb_lock);
  648. oldsbqflag = mp->m_sb.sb_qflags;
  649. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  650. spin_unlock(&mp->m_sb_lock);
  651. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  652. /*
  653. * We have to make sure that the transaction is secure on disk before we
  654. * return and actually stop quota accounting. So, make it synchronous.
  655. * We don't care about quotoff's performance.
  656. */
  657. xfs_trans_set_sync(tp);
  658. error = xfs_trans_commit(tp, 0);
  659. error0:
  660. if (error) {
  661. xfs_trans_cancel(tp, 0);
  662. /*
  663. * No one else is modifying sb_qflags, so this is OK.
  664. * We still hold the quotaofflock.
  665. */
  666. spin_lock(&mp->m_sb_lock);
  667. mp->m_sb.sb_qflags = oldsbqflag;
  668. spin_unlock(&mp->m_sb_lock);
  669. }
  670. *qoffstartp = qoffi;
  671. return (error);
  672. }
  673. /*
  674. * Translate an internal style on-disk-dquot to the exportable format.
  675. * The main differences are that the counters/limits are all in Basic
  676. * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
  677. * to be converted to the native endianness.
  678. */
  679. STATIC void
  680. xfs_qm_export_dquot(
  681. xfs_mount_t *mp,
  682. xfs_disk_dquot_t *src,
  683. struct fs_disk_quota *dst)
  684. {
  685. memset(dst, 0, sizeof(*dst));
  686. dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
  687. dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
  688. dst->d_id = be32_to_cpu(src->d_id);
  689. dst->d_blk_hardlimit =
  690. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
  691. dst->d_blk_softlimit =
  692. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
  693. dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
  694. dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
  695. dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
  696. dst->d_icount = be64_to_cpu(src->d_icount);
  697. dst->d_btimer = be32_to_cpu(src->d_btimer);
  698. dst->d_itimer = be32_to_cpu(src->d_itimer);
  699. dst->d_iwarns = be16_to_cpu(src->d_iwarns);
  700. dst->d_bwarns = be16_to_cpu(src->d_bwarns);
  701. dst->d_rtb_hardlimit =
  702. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
  703. dst->d_rtb_softlimit =
  704. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
  705. dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
  706. dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
  707. dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
  708. /*
  709. * Internally, we don't reset all the timers when quota enforcement
  710. * gets turned off. No need to confuse the user level code,
  711. * so return zeroes in that case.
  712. */
  713. if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
  714. (!XFS_IS_OQUOTA_ENFORCED(mp) &&
  715. (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
  716. dst->d_btimer = 0;
  717. dst->d_itimer = 0;
  718. dst->d_rtbtimer = 0;
  719. }
  720. #ifdef DEBUG
  721. if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == FS_USER_QUOTA) ||
  722. (XFS_IS_OQUOTA_ENFORCED(mp) &&
  723. (dst->d_flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)))) &&
  724. dst->d_id != 0) {
  725. if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
  726. (dst->d_blk_softlimit > 0)) {
  727. ASSERT(dst->d_btimer != 0);
  728. }
  729. if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
  730. (dst->d_ino_softlimit > 0)) {
  731. ASSERT(dst->d_itimer != 0);
  732. }
  733. }
  734. #endif
  735. }
  736. STATIC uint
  737. xfs_qm_export_qtype_flags(
  738. uint flags)
  739. {
  740. /*
  741. * Can't be more than one, or none.
  742. */
  743. ASSERT((flags & (FS_PROJ_QUOTA | FS_USER_QUOTA)) !=
  744. (FS_PROJ_QUOTA | FS_USER_QUOTA));
  745. ASSERT((flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)) !=
  746. (FS_PROJ_QUOTA | FS_GROUP_QUOTA));
  747. ASSERT((flags & (FS_USER_QUOTA | FS_GROUP_QUOTA)) !=
  748. (FS_USER_QUOTA | FS_GROUP_QUOTA));
  749. ASSERT((flags & (FS_PROJ_QUOTA|FS_USER_QUOTA|FS_GROUP_QUOTA)) != 0);
  750. return (flags & XFS_DQ_USER) ?
  751. FS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
  752. FS_PROJ_QUOTA : FS_GROUP_QUOTA;
  753. }
  754. STATIC uint
  755. xfs_qm_export_flags(
  756. uint flags)
  757. {
  758. uint uflags;
  759. uflags = 0;
  760. if (flags & XFS_UQUOTA_ACCT)
  761. uflags |= FS_QUOTA_UDQ_ACCT;
  762. if (flags & XFS_PQUOTA_ACCT)
  763. uflags |= FS_QUOTA_PDQ_ACCT;
  764. if (flags & XFS_GQUOTA_ACCT)
  765. uflags |= FS_QUOTA_GDQ_ACCT;
  766. if (flags & XFS_UQUOTA_ENFD)
  767. uflags |= FS_QUOTA_UDQ_ENFD;
  768. if (flags & (XFS_OQUOTA_ENFD)) {
  769. uflags |= (flags & XFS_GQUOTA_ACCT) ?
  770. FS_QUOTA_GDQ_ENFD : FS_QUOTA_PDQ_ENFD;
  771. }
  772. return (uflags);
  773. }
  774. STATIC int
  775. xfs_dqrele_inode(
  776. struct xfs_inode *ip,
  777. struct xfs_perag *pag,
  778. int flags)
  779. {
  780. /* skip quota inodes */
  781. if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
  782. ip == ip->i_mount->m_quotainfo->qi_gquotaip) {
  783. ASSERT(ip->i_udquot == NULL);
  784. ASSERT(ip->i_gdquot == NULL);
  785. return 0;
  786. }
  787. xfs_ilock(ip, XFS_ILOCK_EXCL);
  788. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  789. xfs_qm_dqrele(ip->i_udquot);
  790. ip->i_udquot = NULL;
  791. }
  792. if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  793. xfs_qm_dqrele(ip->i_gdquot);
  794. ip->i_gdquot = NULL;
  795. }
  796. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  797. return 0;
  798. }
  799. /*
  800. * Go thru all the inodes in the file system, releasing their dquots.
  801. *
  802. * Note that the mount structure gets modified to indicate that quotas are off
  803. * AFTER this, in the case of quotaoff.
  804. */
  805. void
  806. xfs_qm_dqrele_all_inodes(
  807. struct xfs_mount *mp,
  808. uint flags)
  809. {
  810. ASSERT(mp->m_quotainfo);
  811. xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags);
  812. }