xfs_qm_syscalls.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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_mount.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_trans.h"
  30. #include "xfs_error.h"
  31. #include "xfs_quota.h"
  32. #include "xfs_qm.h"
  33. #include "xfs_trace.h"
  34. #include "xfs_icache.h"
  35. STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
  36. STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
  37. uint);
  38. STATIC uint xfs_qm_export_flags(uint);
  39. STATIC uint xfs_qm_export_qtype_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_DQ_MASK \
  517. (FS_DQ_LIMIT_MASK | FS_DQ_TIMER_MASK | FS_DQ_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. fs_disk_quota_t *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_DQ_MASK)
  535. return -EINVAL;
  536. if ((newlim->d_fieldmask & XFS_DQ_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 & FS_DQ_BHARD) ?
  570. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
  571. be64_to_cpu(ddq->d_blk_hardlimit);
  572. soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
  573. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_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 & FS_DQ_RTBHARD) ?
  587. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
  588. be64_to_cpu(ddq->d_rtb_hardlimit);
  589. soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
  590. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_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 & FS_DQ_IHARD) ?
  603. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  604. be64_to_cpu(ddq->d_ino_hardlimit);
  605. soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
  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 & FS_DQ_BWARNS)
  622. ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
  623. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  624. ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
  625. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  626. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
  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 & FS_DQ_BTIMER) {
  636. q->qi_btimelimit = newlim->d_btimer;
  637. ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
  638. }
  639. if (newlim->d_fieldmask & FS_DQ_ITIMER) {
  640. q->qi_itimelimit = newlim->d_itimer;
  641. ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
  642. }
  643. if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
  644. q->qi_rtbtimelimit = newlim->d_rtbtimer;
  645. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
  646. }
  647. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  648. q->qi_bwarnlimit = newlim->d_bwarns;
  649. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  650. q->qi_iwarnlimit = newlim->d_iwarns;
  651. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  652. q->qi_rtbwarnlimit = newlim->d_rtbwarns;
  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;
  708. *qoffstartp = NULL;
  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. xfs_trans_cancel(tp, 0);
  713. goto out;
  714. }
  715. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  716. xfs_trans_log_quotaoff_item(tp, qoffi);
  717. spin_lock(&mp->m_sb_lock);
  718. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  719. spin_unlock(&mp->m_sb_lock);
  720. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  721. /*
  722. * We have to make sure that the transaction is secure on disk before we
  723. * return and actually stop quota accounting. So, make it synchronous.
  724. * We don't care about quotoff's performance.
  725. */
  726. xfs_trans_set_sync(tp);
  727. error = xfs_trans_commit(tp, 0);
  728. if (error)
  729. goto out;
  730. *qoffstartp = qoffi;
  731. out:
  732. return error;
  733. }
  734. int
  735. xfs_qm_scall_getquota(
  736. struct xfs_mount *mp,
  737. xfs_dqid_t id,
  738. uint type,
  739. struct fs_disk_quota *dst)
  740. {
  741. struct xfs_dquot *dqp;
  742. int error;
  743. /*
  744. * Try to get the dquot. We don't want it allocated on disk, so
  745. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  746. * exist, we'll get ENOENT back.
  747. */
  748. error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp);
  749. if (error)
  750. return error;
  751. /*
  752. * If everything's NULL, this dquot doesn't quite exist as far as
  753. * our utility programs are concerned.
  754. */
  755. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  756. error = -ENOENT;
  757. goto out_put;
  758. }
  759. memset(dst, 0, sizeof(*dst));
  760. dst->d_version = FS_DQUOT_VERSION;
  761. dst->d_flags = xfs_qm_export_qtype_flags(dqp->q_core.d_flags);
  762. dst->d_id = be32_to_cpu(dqp->q_core.d_id);
  763. dst->d_blk_hardlimit =
  764. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
  765. dst->d_blk_softlimit =
  766. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
  767. dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  768. dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  769. dst->d_bcount = XFS_FSB_TO_BB(mp, dqp->q_res_bcount);
  770. dst->d_icount = dqp->q_res_icount;
  771. dst->d_btimer = be32_to_cpu(dqp->q_core.d_btimer);
  772. dst->d_itimer = be32_to_cpu(dqp->q_core.d_itimer);
  773. dst->d_iwarns = be16_to_cpu(dqp->q_core.d_iwarns);
  774. dst->d_bwarns = be16_to_cpu(dqp->q_core.d_bwarns);
  775. dst->d_rtb_hardlimit =
  776. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
  777. dst->d_rtb_softlimit =
  778. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
  779. dst->d_rtbcount = XFS_FSB_TO_BB(mp, dqp->q_res_rtbcount);
  780. dst->d_rtbtimer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  781. dst->d_rtbwarns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  782. /*
  783. * Internally, we don't reset all the timers when quota enforcement
  784. * gets turned off. No need to confuse the user level code,
  785. * so return zeroes in that case.
  786. */
  787. if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
  788. dqp->q_core.d_flags == XFS_DQ_USER) ||
  789. (!XFS_IS_GQUOTA_ENFORCED(mp) &&
  790. dqp->q_core.d_flags == XFS_DQ_GROUP) ||
  791. (!XFS_IS_PQUOTA_ENFORCED(mp) &&
  792. dqp->q_core.d_flags == XFS_DQ_PROJ)) {
  793. dst->d_btimer = 0;
  794. dst->d_itimer = 0;
  795. dst->d_rtbtimer = 0;
  796. }
  797. #ifdef DEBUG
  798. if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == FS_USER_QUOTA) ||
  799. (XFS_IS_GQUOTA_ENFORCED(mp) && dst->d_flags == FS_GROUP_QUOTA) ||
  800. (XFS_IS_PQUOTA_ENFORCED(mp) && dst->d_flags == FS_PROJ_QUOTA)) &&
  801. dst->d_id != 0) {
  802. if ((dst->d_bcount > dst->d_blk_softlimit) &&
  803. (dst->d_blk_softlimit > 0)) {
  804. ASSERT(dst->d_btimer != 0);
  805. }
  806. if ((dst->d_icount > dst->d_ino_softlimit) &&
  807. (dst->d_ino_softlimit > 0)) {
  808. ASSERT(dst->d_itimer != 0);
  809. }
  810. }
  811. #endif
  812. out_put:
  813. xfs_qm_dqput(dqp);
  814. return error;
  815. }
  816. STATIC uint
  817. xfs_qm_export_qtype_flags(
  818. uint flags)
  819. {
  820. /*
  821. * Can't be more than one, or none.
  822. */
  823. ASSERT((flags & (FS_PROJ_QUOTA | FS_USER_QUOTA)) !=
  824. (FS_PROJ_QUOTA | FS_USER_QUOTA));
  825. ASSERT((flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)) !=
  826. (FS_PROJ_QUOTA | FS_GROUP_QUOTA));
  827. ASSERT((flags & (FS_USER_QUOTA | FS_GROUP_QUOTA)) !=
  828. (FS_USER_QUOTA | FS_GROUP_QUOTA));
  829. ASSERT((flags & (FS_PROJ_QUOTA|FS_USER_QUOTA|FS_GROUP_QUOTA)) != 0);
  830. return (flags & XFS_DQ_USER) ?
  831. FS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
  832. FS_PROJ_QUOTA : FS_GROUP_QUOTA;
  833. }
  834. STATIC uint
  835. xfs_qm_export_flags(
  836. uint flags)
  837. {
  838. uint uflags;
  839. uflags = 0;
  840. if (flags & XFS_UQUOTA_ACCT)
  841. uflags |= FS_QUOTA_UDQ_ACCT;
  842. if (flags & XFS_GQUOTA_ACCT)
  843. uflags |= FS_QUOTA_GDQ_ACCT;
  844. if (flags & XFS_PQUOTA_ACCT)
  845. uflags |= FS_QUOTA_PDQ_ACCT;
  846. if (flags & XFS_UQUOTA_ENFD)
  847. uflags |= FS_QUOTA_UDQ_ENFD;
  848. if (flags & XFS_GQUOTA_ENFD)
  849. uflags |= FS_QUOTA_GDQ_ENFD;
  850. if (flags & XFS_PQUOTA_ENFD)
  851. uflags |= FS_QUOTA_PDQ_ENFD;
  852. return uflags;
  853. }
  854. STATIC int
  855. xfs_dqrele_inode(
  856. struct xfs_inode *ip,
  857. int flags,
  858. void *args)
  859. {
  860. /* skip quota inodes */
  861. if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
  862. ip == ip->i_mount->m_quotainfo->qi_gquotaip ||
  863. ip == ip->i_mount->m_quotainfo->qi_pquotaip) {
  864. ASSERT(ip->i_udquot == NULL);
  865. ASSERT(ip->i_gdquot == NULL);
  866. ASSERT(ip->i_pdquot == NULL);
  867. return 0;
  868. }
  869. xfs_ilock(ip, XFS_ILOCK_EXCL);
  870. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  871. xfs_qm_dqrele(ip->i_udquot);
  872. ip->i_udquot = NULL;
  873. }
  874. if ((flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  875. xfs_qm_dqrele(ip->i_gdquot);
  876. ip->i_gdquot = NULL;
  877. }
  878. if ((flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) {
  879. xfs_qm_dqrele(ip->i_pdquot);
  880. ip->i_pdquot = NULL;
  881. }
  882. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  883. return 0;
  884. }
  885. /*
  886. * Go thru all the inodes in the file system, releasing their dquots.
  887. *
  888. * Note that the mount structure gets modified to indicate that quotas are off
  889. * AFTER this, in the case of quotaoff.
  890. */
  891. void
  892. xfs_qm_dqrele_all_inodes(
  893. struct xfs_mount *mp,
  894. uint flags)
  895. {
  896. ASSERT(mp->m_quotainfo);
  897. xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, NULL);
  898. }