xfs_qm_syscalls.c 27 KB

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