xfs_qm_syscalls.c 25 KB

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