xfs_qm_syscalls.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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_shared.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_bit.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_error.h"
  32. #include "xfs_quota.h"
  33. #include "xfs_qm.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_icache.h"
  36. STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
  37. STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
  38. uint);
  39. STATIC uint xfs_qm_export_flags(uint);
  40. /*
  41. * Turn off quota accounting and/or enforcement for all udquots and/or
  42. * gdquots. Called only at unmount time.
  43. *
  44. * This assumes that there are no dquots of this file system cached
  45. * incore, and modifies the ondisk dquot directly. Therefore, for example,
  46. * it is an error to call this twice, without purging the cache.
  47. */
  48. int
  49. xfs_qm_scall_quotaoff(
  50. xfs_mount_t *mp,
  51. uint flags)
  52. {
  53. struct xfs_quotainfo *q = mp->m_quotainfo;
  54. uint dqtype;
  55. int error;
  56. uint inactivate_flags;
  57. xfs_qoff_logitem_t *qoffstart;
  58. /*
  59. * No file system can have quotas enabled on disk but not in core.
  60. * Note that quota utilities (like quotaoff) _expect_
  61. * errno == -EEXIST here.
  62. */
  63. if ((mp->m_qflags & flags) == 0)
  64. return -EEXIST;
  65. error = 0;
  66. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  67. /*
  68. * We don't want to deal with two quotaoffs messing up each other,
  69. * so we're going to serialize it. quotaoff isn't exactly a performance
  70. * critical thing.
  71. * If quotaoff, then we must be dealing with the root filesystem.
  72. */
  73. ASSERT(q);
  74. mutex_lock(&q->qi_quotaofflock);
  75. /*
  76. * If we're just turning off quota enforcement, change mp and go.
  77. */
  78. if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
  79. mp->m_qflags &= ~(flags);
  80. spin_lock(&mp->m_sb_lock);
  81. mp->m_sb.sb_qflags = mp->m_qflags;
  82. spin_unlock(&mp->m_sb_lock);
  83. mutex_unlock(&q->qi_quotaofflock);
  84. /* XXX what to do if error ? Revert back to old vals incore ? */
  85. error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
  86. return error;
  87. }
  88. dqtype = 0;
  89. inactivate_flags = 0;
  90. /*
  91. * If accounting is off, we must turn enforcement off, clear the
  92. * quota 'CHKD' certificate to make it known that we have to
  93. * do a quotacheck the next time this quota is turned on.
  94. */
  95. if (flags & XFS_UQUOTA_ACCT) {
  96. dqtype |= XFS_QMOPT_UQUOTA;
  97. flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
  98. inactivate_flags |= XFS_UQUOTA_ACTIVE;
  99. }
  100. if (flags & XFS_GQUOTA_ACCT) {
  101. dqtype |= XFS_QMOPT_GQUOTA;
  102. flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD);
  103. inactivate_flags |= XFS_GQUOTA_ACTIVE;
  104. }
  105. if (flags & XFS_PQUOTA_ACCT) {
  106. dqtype |= XFS_QMOPT_PQUOTA;
  107. flags |= (XFS_PQUOTA_CHKD | XFS_PQUOTA_ENFD);
  108. inactivate_flags |= XFS_PQUOTA_ACTIVE;
  109. }
  110. /*
  111. * Nothing to do? Don't complain. This happens when we're just
  112. * turning off quota enforcement.
  113. */
  114. if ((mp->m_qflags & flags) == 0)
  115. goto out_unlock;
  116. /*
  117. * Write the LI_QUOTAOFF log record, and do SB changes atomically,
  118. * and synchronously. If we fail to write, we should abort the
  119. * operation as it cannot be recovered safely if we crash.
  120. */
  121. error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
  122. if (error)
  123. goto out_unlock;
  124. /*
  125. * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
  126. * to take care of the race between dqget and quotaoff. We don't take
  127. * any special locks to reset these bits. All processes need to check
  128. * these bits *after* taking inode lock(s) to see if the particular
  129. * quota type is in the process of being turned off. If *ACTIVE, it is
  130. * guaranteed that all dquot structures and all quotainode ptrs will all
  131. * stay valid as long as that inode is kept locked.
  132. *
  133. * There is no turning back after this.
  134. */
  135. mp->m_qflags &= ~inactivate_flags;
  136. /*
  137. * Give back all the dquot reference(s) held by inodes.
  138. * Here we go thru every single incore inode in this file system, and
  139. * do a dqrele on the i_udquot/i_gdquot that it may have.
  140. * Essentially, as long as somebody has an inode locked, this guarantees
  141. * that quotas will not be turned off. This is handy because in a
  142. * transaction once we lock the inode(s) and check for quotaon, we can
  143. * depend on the quota inodes (and other things) being valid as long as
  144. * we keep the lock(s).
  145. */
  146. xfs_qm_dqrele_all_inodes(mp, flags);
  147. /*
  148. * Next we make the changes in the quota flag in the mount struct.
  149. * This isn't protected by a particular lock directly, because we
  150. * don't want to take a mrlock every time we depend on quotas being on.
  151. */
  152. mp->m_qflags &= ~flags;
  153. /*
  154. * Go through all the dquots of this file system and purge them,
  155. * according to what was turned off.
  156. */
  157. xfs_qm_dqpurge_all(mp, dqtype);
  158. /*
  159. * Transactions that had started before ACTIVE state bit was cleared
  160. * could have logged many dquots, so they'd have higher LSNs than
  161. * the first QUOTAOFF log record does. If we happen to crash when
  162. * the tail of the log has gone past the QUOTAOFF record, but
  163. * before the last dquot modification, those dquots __will__
  164. * recover, and that's not good.
  165. *
  166. * So, we have QUOTAOFF start and end logitems; the start
  167. * logitem won't get overwritten until the end logitem appears...
  168. */
  169. error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
  170. if (error) {
  171. /* We're screwed now. Shutdown is the only option. */
  172. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  173. goto out_unlock;
  174. }
  175. /*
  176. * If all quotas are completely turned off, close shop.
  177. */
  178. if (mp->m_qflags == 0) {
  179. mutex_unlock(&q->qi_quotaofflock);
  180. xfs_qm_destroy_quotainfo(mp);
  181. return 0;
  182. }
  183. /*
  184. * Release our quotainode references if we don't need them anymore.
  185. */
  186. if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
  187. IRELE(q->qi_uquotaip);
  188. q->qi_uquotaip = NULL;
  189. }
  190. if ((dqtype & XFS_QMOPT_GQUOTA) && q->qi_gquotaip) {
  191. IRELE(q->qi_gquotaip);
  192. q->qi_gquotaip = NULL;
  193. }
  194. if ((dqtype & XFS_QMOPT_PQUOTA) && q->qi_pquotaip) {
  195. IRELE(q->qi_pquotaip);
  196. q->qi_pquotaip = NULL;
  197. }
  198. out_unlock:
  199. mutex_unlock(&q->qi_quotaofflock);
  200. return error;
  201. }
  202. STATIC int
  203. xfs_qm_scall_trunc_qfile(
  204. struct xfs_mount *mp,
  205. xfs_ino_t ino)
  206. {
  207. struct xfs_inode *ip;
  208. struct xfs_trans *tp;
  209. int error;
  210. if (ino == NULLFSINO)
  211. return 0;
  212. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  213. if (error)
  214. return error;
  215. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  216. tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
  217. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  218. if (error) {
  219. xfs_trans_cancel(tp, 0);
  220. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  221. goto out_put;
  222. }
  223. xfs_ilock(ip, XFS_ILOCK_EXCL);
  224. xfs_trans_ijoin(tp, ip, 0);
  225. ip->i_d.di_size = 0;
  226. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  227. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  228. if (error) {
  229. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
  230. XFS_TRANS_ABORT);
  231. goto out_unlock;
  232. }
  233. ASSERT(ip->i_d.di_nextents == 0);
  234. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  235. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  236. out_unlock:
  237. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  238. out_put:
  239. IRELE(ip);
  240. return error;
  241. }
  242. int
  243. xfs_qm_scall_trunc_qfiles(
  244. xfs_mount_t *mp,
  245. uint flags)
  246. {
  247. int error = -EINVAL;
  248. if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0 ||
  249. (flags & ~XFS_DQ_ALLTYPES)) {
  250. xfs_debug(mp, "%s: flags=%x m_qflags=%x",
  251. __func__, flags, mp->m_qflags);
  252. return -EINVAL;
  253. }
  254. if (flags & XFS_DQ_USER) {
  255. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
  256. if (error)
  257. return error;
  258. }
  259. if (flags & XFS_DQ_GROUP) {
  260. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
  261. if (error)
  262. return error;
  263. }
  264. if (flags & XFS_DQ_PROJ)
  265. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
  266. return error;
  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",
  289. __func__, mp->m_qflags);
  290. return -EINVAL;
  291. }
  292. /* No fs can turn on quotas with a delayed effect */
  293. ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
  294. /*
  295. * Can't enforce without accounting. We check the superblock
  296. * qflags here instead of m_qflags because rootfs can have
  297. * quota acct on ondisk without m_qflags' knowing.
  298. */
  299. if (((flags & XFS_UQUOTA_ACCT) == 0 &&
  300. (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
  301. (flags & XFS_UQUOTA_ENFD)) ||
  302. ((flags & XFS_GQUOTA_ACCT) == 0 &&
  303. (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
  304. (flags & XFS_GQUOTA_ENFD)) ||
  305. ((flags & XFS_PQUOTA_ACCT) == 0 &&
  306. (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
  307. (flags & XFS_PQUOTA_ENFD))) {
  308. xfs_debug(mp,
  309. "%s: Can't enforce without acct, flags=%x sbflags=%x",
  310. __func__, flags, mp->m_sb.sb_qflags);
  311. return -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 -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 -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 -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. * for Q_XGETQSTAT command.
  358. */
  359. int
  360. xfs_qm_scall_getqstat(
  361. struct xfs_mount *mp,
  362. struct fs_quota_stat *out)
  363. {
  364. struct xfs_quotainfo *q = mp->m_quotainfo;
  365. struct xfs_inode *uip = NULL;
  366. struct xfs_inode *gip = NULL;
  367. struct xfs_inode *pip = NULL;
  368. bool tempuqip = false;
  369. bool tempgqip = false;
  370. bool temppqip = false;
  371. memset(out, 0, sizeof(fs_quota_stat_t));
  372. out->qs_version = FS_QSTAT_VERSION;
  373. if (!xfs_sb_version_hasquota(&mp->m_sb)) {
  374. out->qs_uquota.qfs_ino = NULLFSINO;
  375. out->qs_gquota.qfs_ino = NULLFSINO;
  376. return 0;
  377. }
  378. out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
  379. (XFS_ALL_QUOTA_ACCT|
  380. XFS_ALL_QUOTA_ENFD));
  381. if (q) {
  382. uip = q->qi_uquotaip;
  383. gip = q->qi_gquotaip;
  384. pip = q->qi_pquotaip;
  385. }
  386. if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
  387. if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  388. 0, 0, &uip) == 0)
  389. tempuqip = true;
  390. }
  391. if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
  392. if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  393. 0, 0, &gip) == 0)
  394. tempgqip = true;
  395. }
  396. /*
  397. * Q_XGETQSTAT doesn't have room for both group and project quotas.
  398. * So, allow the project quota values to be copied out only if
  399. * there is no group quota information available.
  400. */
  401. if (!gip) {
  402. if (!pip && mp->m_sb.sb_pquotino != NULLFSINO) {
  403. if (xfs_iget(mp, NULL, mp->m_sb.sb_pquotino,
  404. 0, 0, &pip) == 0)
  405. temppqip = true;
  406. }
  407. } else
  408. pip = NULL;
  409. if (uip) {
  410. out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
  411. out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
  412. out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
  413. if (tempuqip)
  414. IRELE(uip);
  415. }
  416. if (gip) {
  417. out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
  418. out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
  419. out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
  420. if (tempgqip)
  421. IRELE(gip);
  422. }
  423. if (pip) {
  424. out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
  425. out->qs_gquota.qfs_nblks = pip->i_d.di_nblocks;
  426. out->qs_gquota.qfs_nextents = pip->i_d.di_nextents;
  427. if (temppqip)
  428. IRELE(pip);
  429. }
  430. if (q) {
  431. out->qs_incoredqs = q->qi_dquots;
  432. out->qs_btimelimit = q->qi_btimelimit;
  433. out->qs_itimelimit = q->qi_itimelimit;
  434. out->qs_rtbtimelimit = q->qi_rtbtimelimit;
  435. out->qs_bwarnlimit = q->qi_bwarnlimit;
  436. out->qs_iwarnlimit = q->qi_iwarnlimit;
  437. }
  438. return 0;
  439. }
  440. /*
  441. * Return quota status information, such as uquota-off, enforcements, etc.
  442. * for Q_XGETQSTATV command, to support separate project quota field.
  443. */
  444. int
  445. xfs_qm_scall_getqstatv(
  446. struct xfs_mount *mp,
  447. struct fs_quota_statv *out)
  448. {
  449. struct xfs_quotainfo *q = mp->m_quotainfo;
  450. struct xfs_inode *uip = NULL;
  451. struct xfs_inode *gip = NULL;
  452. struct xfs_inode *pip = NULL;
  453. bool tempuqip = false;
  454. bool tempgqip = false;
  455. bool temppqip = false;
  456. if (!xfs_sb_version_hasquota(&mp->m_sb)) {
  457. out->qs_uquota.qfs_ino = NULLFSINO;
  458. out->qs_gquota.qfs_ino = NULLFSINO;
  459. out->qs_pquota.qfs_ino = NULLFSINO;
  460. return 0;
  461. }
  462. out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
  463. (XFS_ALL_QUOTA_ACCT|
  464. XFS_ALL_QUOTA_ENFD));
  465. out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
  466. out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
  467. out->qs_pquota.qfs_ino = mp->m_sb.sb_pquotino;
  468. if (q) {
  469. uip = q->qi_uquotaip;
  470. gip = q->qi_gquotaip;
  471. pip = q->qi_pquotaip;
  472. }
  473. if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
  474. if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  475. 0, 0, &uip) == 0)
  476. tempuqip = true;
  477. }
  478. if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
  479. if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  480. 0, 0, &gip) == 0)
  481. tempgqip = true;
  482. }
  483. if (!pip && mp->m_sb.sb_pquotino != NULLFSINO) {
  484. if (xfs_iget(mp, NULL, mp->m_sb.sb_pquotino,
  485. 0, 0, &pip) == 0)
  486. temppqip = true;
  487. }
  488. if (uip) {
  489. out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
  490. out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
  491. if (tempuqip)
  492. IRELE(uip);
  493. }
  494. if (gip) {
  495. out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
  496. out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
  497. if (tempgqip)
  498. IRELE(gip);
  499. }
  500. if (pip) {
  501. out->qs_pquota.qfs_nblks = pip->i_d.di_nblocks;
  502. out->qs_pquota.qfs_nextents = pip->i_d.di_nextents;
  503. if (temppqip)
  504. IRELE(pip);
  505. }
  506. if (q) {
  507. out->qs_incoredqs = q->qi_dquots;
  508. out->qs_btimelimit = q->qi_btimelimit;
  509. out->qs_itimelimit = q->qi_itimelimit;
  510. out->qs_rtbtimelimit = q->qi_rtbtimelimit;
  511. out->qs_bwarnlimit = q->qi_bwarnlimit;
  512. out->qs_iwarnlimit = q->qi_iwarnlimit;
  513. }
  514. return 0;
  515. }
  516. #define XFS_QC_MASK \
  517. (QC_LIMIT_MASK | QC_TIMER_MASK | QC_WARNS_MASK)
  518. /*
  519. * Adjust quota limits, and start/stop timers accordingly.
  520. */
  521. int
  522. xfs_qm_scall_setqlim(
  523. struct xfs_mount *mp,
  524. xfs_dqid_t id,
  525. uint type,
  526. struct qc_dqblk *newlim)
  527. {
  528. struct xfs_quotainfo *q = mp->m_quotainfo;
  529. struct xfs_disk_dquot *ddq;
  530. struct xfs_dquot *dqp;
  531. struct xfs_trans *tp;
  532. int error;
  533. xfs_qcnt_t hard, soft;
  534. if (newlim->d_fieldmask & ~XFS_QC_MASK)
  535. return -EINVAL;
  536. if ((newlim->d_fieldmask & XFS_QC_MASK) == 0)
  537. return 0;
  538. /*
  539. * We don't want to race with a quotaoff so take the quotaoff lock.
  540. * We don't hold an inode lock, so there's nothing else to stop
  541. * a quotaoff from happening.
  542. */
  543. mutex_lock(&q->qi_quotaofflock);
  544. /*
  545. * Get the dquot (locked) before we start, as we need to do a
  546. * transaction to allocate it if it doesn't exist. Once we have the
  547. * dquot, unlock it so we can start the next transaction safely. We hold
  548. * a reference to the dquot, so it's safe to do this unlock/lock without
  549. * it being reclaimed in the mean time.
  550. */
  551. error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp);
  552. if (error) {
  553. ASSERT(error != -ENOENT);
  554. goto out_unlock;
  555. }
  556. xfs_dqunlock(dqp);
  557. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
  558. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_setqlim, 0, 0);
  559. if (error) {
  560. xfs_trans_cancel(tp, 0);
  561. goto out_rele;
  562. }
  563. xfs_dqlock(dqp);
  564. xfs_trans_dqjoin(tp, dqp);
  565. ddq = &dqp->q_core;
  566. /*
  567. * Make sure that hardlimits are >= soft limits before changing.
  568. */
  569. hard = (newlim->d_fieldmask & QC_SPC_HARD) ?
  570. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_hardlimit) :
  571. be64_to_cpu(ddq->d_blk_hardlimit);
  572. soft = (newlim->d_fieldmask & QC_SPC_SOFT) ?
  573. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_softlimit) :
  574. be64_to_cpu(ddq->d_blk_softlimit);
  575. if (hard == 0 || hard >= soft) {
  576. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  577. ddq->d_blk_softlimit = cpu_to_be64(soft);
  578. xfs_dquot_set_prealloc_limits(dqp);
  579. if (id == 0) {
  580. q->qi_bhardlimit = hard;
  581. q->qi_bsoftlimit = soft;
  582. }
  583. } else {
  584. xfs_debug(mp, "blkhard %Ld < blksoft %Ld", hard, soft);
  585. }
  586. hard = (newlim->d_fieldmask & QC_RT_SPC_HARD) ?
  587. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_hardlimit) :
  588. be64_to_cpu(ddq->d_rtb_hardlimit);
  589. soft = (newlim->d_fieldmask & QC_RT_SPC_SOFT) ?
  590. (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_softlimit) :
  591. be64_to_cpu(ddq->d_rtb_softlimit);
  592. if (hard == 0 || hard >= soft) {
  593. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  594. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  595. if (id == 0) {
  596. q->qi_rtbhardlimit = hard;
  597. q->qi_rtbsoftlimit = soft;
  598. }
  599. } else {
  600. xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld", hard, soft);
  601. }
  602. hard = (newlim->d_fieldmask & QC_INO_HARD) ?
  603. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  604. be64_to_cpu(ddq->d_ino_hardlimit);
  605. soft = (newlim->d_fieldmask & QC_INO_SOFT) ?
  606. (xfs_qcnt_t) newlim->d_ino_softlimit :
  607. be64_to_cpu(ddq->d_ino_softlimit);
  608. if (hard == 0 || hard >= soft) {
  609. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  610. ddq->d_ino_softlimit = cpu_to_be64(soft);
  611. if (id == 0) {
  612. q->qi_ihardlimit = hard;
  613. q->qi_isoftlimit = soft;
  614. }
  615. } else {
  616. xfs_debug(mp, "ihard %Ld < isoft %Ld", hard, soft);
  617. }
  618. /*
  619. * Update warnings counter(s) if requested
  620. */
  621. if (newlim->d_fieldmask & QC_SPC_WARNS)
  622. ddq->d_bwarns = cpu_to_be16(newlim->d_spc_warns);
  623. if (newlim->d_fieldmask & QC_INO_WARNS)
  624. ddq->d_iwarns = cpu_to_be16(newlim->d_ino_warns);
  625. if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
  626. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rt_spc_warns);
  627. if (id == 0) {
  628. /*
  629. * Timelimits for the super user set the relative time
  630. * the other users can be over quota for this file system.
  631. * If it is zero a default is used. Ditto for the default
  632. * soft and hard limit values (already done, above), and
  633. * for warnings.
  634. */
  635. if (newlim->d_fieldmask & QC_SPC_TIMER) {
  636. q->qi_btimelimit = newlim->d_spc_timer;
  637. ddq->d_btimer = cpu_to_be32(newlim->d_spc_timer);
  638. }
  639. if (newlim->d_fieldmask & QC_INO_TIMER) {
  640. q->qi_itimelimit = newlim->d_ino_timer;
  641. ddq->d_itimer = cpu_to_be32(newlim->d_ino_timer);
  642. }
  643. if (newlim->d_fieldmask & QC_RT_SPC_TIMER) {
  644. q->qi_rtbtimelimit = newlim->d_rt_spc_timer;
  645. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rt_spc_timer);
  646. }
  647. if (newlim->d_fieldmask & QC_SPC_WARNS)
  648. q->qi_bwarnlimit = newlim->d_spc_warns;
  649. if (newlim->d_fieldmask & QC_INO_WARNS)
  650. q->qi_iwarnlimit = newlim->d_ino_warns;
  651. if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
  652. q->qi_rtbwarnlimit = newlim->d_rt_spc_warns;
  653. } else {
  654. /*
  655. * If the user is now over quota, start the timelimit.
  656. * The user will not be 'warned'.
  657. * Note that we keep the timers ticking, whether enforcement
  658. * is on or off. We don't really want to bother with iterating
  659. * over all ondisk dquots and turning the timers on/off.
  660. */
  661. xfs_qm_adjust_dqtimers(mp, ddq);
  662. }
  663. dqp->dq_flags |= XFS_DQ_DIRTY;
  664. xfs_trans_log_dquot(tp, dqp);
  665. error = xfs_trans_commit(tp, 0);
  666. out_rele:
  667. xfs_qm_dqrele(dqp);
  668. out_unlock:
  669. mutex_unlock(&q->qi_quotaofflock);
  670. return error;
  671. }
  672. STATIC int
  673. xfs_qm_log_quotaoff_end(
  674. xfs_mount_t *mp,
  675. xfs_qoff_logitem_t *startqoff,
  676. uint flags)
  677. {
  678. xfs_trans_t *tp;
  679. int error;
  680. xfs_qoff_logitem_t *qoffi;
  681. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  682. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_equotaoff, 0, 0);
  683. if (error) {
  684. xfs_trans_cancel(tp, 0);
  685. return error;
  686. }
  687. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  688. flags & XFS_ALL_QUOTA_ACCT);
  689. xfs_trans_log_quotaoff_item(tp, qoffi);
  690. /*
  691. * We have to make sure that the transaction is secure on disk before we
  692. * return and actually stop quota accounting. So, make it synchronous.
  693. * We don't care about quotoff's performance.
  694. */
  695. xfs_trans_set_sync(tp);
  696. error = xfs_trans_commit(tp, 0);
  697. return error;
  698. }
  699. STATIC int
  700. xfs_qm_log_quotaoff(
  701. xfs_mount_t *mp,
  702. xfs_qoff_logitem_t **qoffstartp,
  703. uint flags)
  704. {
  705. xfs_trans_t *tp;
  706. int error;
  707. xfs_qoff_logitem_t *qoffi=NULL;
  708. uint oldsbqflag=0;
  709. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  710. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_quotaoff, 0, 0);
  711. if (error)
  712. goto error0;
  713. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  714. xfs_trans_log_quotaoff_item(tp, qoffi);
  715. spin_lock(&mp->m_sb_lock);
  716. oldsbqflag = mp->m_sb.sb_qflags;
  717. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  718. spin_unlock(&mp->m_sb_lock);
  719. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  720. /*
  721. * We have to make sure that the transaction is secure on disk before we
  722. * return and actually stop quota accounting. So, make it synchronous.
  723. * We don't care about quotoff's performance.
  724. */
  725. xfs_trans_set_sync(tp);
  726. error = xfs_trans_commit(tp, 0);
  727. error0:
  728. if (error) {
  729. xfs_trans_cancel(tp, 0);
  730. /*
  731. * No one else is modifying sb_qflags, so this is OK.
  732. * We still hold the quotaofflock.
  733. */
  734. spin_lock(&mp->m_sb_lock);
  735. mp->m_sb.sb_qflags = oldsbqflag;
  736. spin_unlock(&mp->m_sb_lock);
  737. }
  738. *qoffstartp = qoffi;
  739. return error;
  740. }
  741. int
  742. xfs_qm_scall_getquota(
  743. struct xfs_mount *mp,
  744. xfs_dqid_t id,
  745. uint type,
  746. struct qc_dqblk *dst)
  747. {
  748. struct xfs_dquot *dqp;
  749. int error;
  750. /*
  751. * Try to get the dquot. We don't want it allocated on disk, so
  752. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  753. * exist, we'll get ENOENT back.
  754. */
  755. error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp);
  756. if (error)
  757. return error;
  758. /*
  759. * If everything's NULL, this dquot doesn't quite exist as far as
  760. * our utility programs are concerned.
  761. */
  762. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  763. error = -ENOENT;
  764. goto out_put;
  765. }
  766. memset(dst, 0, sizeof(*dst));
  767. dst->d_spc_hardlimit =
  768. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
  769. dst->d_spc_softlimit =
  770. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
  771. dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  772. dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  773. dst->d_space = XFS_FSB_TO_B(mp, dqp->q_res_bcount);
  774. dst->d_ino_count = dqp->q_res_icount;
  775. dst->d_spc_timer = be32_to_cpu(dqp->q_core.d_btimer);
  776. dst->d_ino_timer = be32_to_cpu(dqp->q_core.d_itimer);
  777. dst->d_ino_warns = be16_to_cpu(dqp->q_core.d_iwarns);
  778. dst->d_spc_warns = be16_to_cpu(dqp->q_core.d_bwarns);
  779. dst->d_rt_spc_hardlimit =
  780. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
  781. dst->d_rt_spc_softlimit =
  782. XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
  783. dst->d_rt_space = XFS_FSB_TO_B(mp, dqp->q_res_rtbcount);
  784. dst->d_rt_spc_timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  785. dst->d_rt_spc_warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  786. /*
  787. * Internally, we don't reset all the timers when quota enforcement
  788. * gets turned off. No need to confuse the user level code,
  789. * so return zeroes in that case.
  790. */
  791. if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
  792. dqp->q_core.d_flags == XFS_DQ_USER) ||
  793. (!XFS_IS_GQUOTA_ENFORCED(mp) &&
  794. dqp->q_core.d_flags == XFS_DQ_GROUP) ||
  795. (!XFS_IS_PQUOTA_ENFORCED(mp) &&
  796. dqp->q_core.d_flags == XFS_DQ_PROJ)) {
  797. dst->d_spc_timer = 0;
  798. dst->d_ino_timer = 0;
  799. dst->d_rt_spc_timer = 0;
  800. }
  801. #ifdef DEBUG
  802. if (((XFS_IS_UQUOTA_ENFORCED(mp) && type == XFS_DQ_USER) ||
  803. (XFS_IS_GQUOTA_ENFORCED(mp) && type == XFS_DQ_GROUP) ||
  804. (XFS_IS_PQUOTA_ENFORCED(mp) && type == XFS_DQ_PROJ)) &&
  805. id != 0) {
  806. if ((dst->d_space > dst->d_spc_softlimit) &&
  807. (dst->d_spc_softlimit > 0)) {
  808. ASSERT(dst->d_spc_timer != 0);
  809. }
  810. if ((dst->d_ino_count > dst->d_ino_softlimit) &&
  811. (dst->d_ino_softlimit > 0)) {
  812. ASSERT(dst->d_ino_timer != 0);
  813. }
  814. }
  815. #endif
  816. out_put:
  817. xfs_qm_dqput(dqp);
  818. return error;
  819. }
  820. STATIC uint
  821. xfs_qm_export_flags(
  822. uint flags)
  823. {
  824. uint uflags;
  825. uflags = 0;
  826. if (flags & XFS_UQUOTA_ACCT)
  827. uflags |= FS_QUOTA_UDQ_ACCT;
  828. if (flags & XFS_GQUOTA_ACCT)
  829. uflags |= FS_QUOTA_GDQ_ACCT;
  830. if (flags & XFS_PQUOTA_ACCT)
  831. uflags |= FS_QUOTA_PDQ_ACCT;
  832. if (flags & XFS_UQUOTA_ENFD)
  833. uflags |= FS_QUOTA_UDQ_ENFD;
  834. if (flags & XFS_GQUOTA_ENFD)
  835. uflags |= FS_QUOTA_GDQ_ENFD;
  836. if (flags & XFS_PQUOTA_ENFD)
  837. uflags |= FS_QUOTA_PDQ_ENFD;
  838. return uflags;
  839. }
  840. STATIC int
  841. xfs_dqrele_inode(
  842. struct xfs_inode *ip,
  843. int flags,
  844. void *args)
  845. {
  846. /* skip quota inodes */
  847. if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
  848. ip == ip->i_mount->m_quotainfo->qi_gquotaip ||
  849. ip == ip->i_mount->m_quotainfo->qi_pquotaip) {
  850. ASSERT(ip->i_udquot == NULL);
  851. ASSERT(ip->i_gdquot == NULL);
  852. ASSERT(ip->i_pdquot == NULL);
  853. return 0;
  854. }
  855. xfs_ilock(ip, XFS_ILOCK_EXCL);
  856. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  857. xfs_qm_dqrele(ip->i_udquot);
  858. ip->i_udquot = NULL;
  859. }
  860. if ((flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  861. xfs_qm_dqrele(ip->i_gdquot);
  862. ip->i_gdquot = NULL;
  863. }
  864. if ((flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) {
  865. xfs_qm_dqrele(ip->i_pdquot);
  866. ip->i_pdquot = NULL;
  867. }
  868. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  869. return 0;
  870. }
  871. /*
  872. * Go thru all the inodes in the file system, releasing their dquots.
  873. *
  874. * Note that the mount structure gets modified to indicate that quotas are off
  875. * AFTER this, in the case of quotaoff.
  876. */
  877. void
  878. xfs_qm_dqrele_all_inodes(
  879. struct xfs_mount *mp,
  880. uint flags)
  881. {
  882. ASSERT(mp->m_quotainfo);
  883. xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, NULL);
  884. }