xfs_qm_syscalls.c 27 KB

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