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_format.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_quota.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_inode_item.h"
  33. #include "xfs_itable.h"
  34. #include "xfs_bmap.h"
  35. #include "xfs_rtalloc.h"
  36. #include "xfs_error.h"
  37. #include "xfs_attr.h"
  38. #include "xfs_buf_item.h"
  39. #include "xfs_qm.h"
  40. #include "xfs_trace.h"
  41. #include "xfs_icache.h"
  42. STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
  43. STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
  44. uint);
  45. STATIC uint xfs_qm_export_flags(uint);
  46. STATIC uint xfs_qm_export_qtype_flags(uint);
  47. /*
  48. * Turn off quota accounting and/or enforcement for all udquots and/or
  49. * gdquots. Called only at unmount time.
  50. *
  51. * This assumes that there are no dquots of this file system cached
  52. * incore, and modifies the ondisk dquot directly. Therefore, for example,
  53. * it is an error to call this twice, without purging the cache.
  54. */
  55. int
  56. xfs_qm_scall_quotaoff(
  57. xfs_mount_t *mp,
  58. uint flags)
  59. {
  60. struct xfs_quotainfo *q = mp->m_quotainfo;
  61. uint dqtype;
  62. int error;
  63. uint inactivate_flags;
  64. xfs_qoff_logitem_t *qoffstart;
  65. /*
  66. * No file system can have quotas enabled on disk but not in core.
  67. * Note that quota utilities (like quotaoff) _expect_
  68. * errno == EEXIST here.
  69. */
  70. if ((mp->m_qflags & flags) == 0)
  71. return XFS_ERROR(EEXIST);
  72. error = 0;
  73. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  74. /*
  75. * We don't want to deal with two quotaoffs messing up each other,
  76. * so we're going to serialize it. quotaoff isn't exactly a performance
  77. * critical thing.
  78. * If quotaoff, then we must be dealing with the root filesystem.
  79. */
  80. ASSERT(q);
  81. mutex_lock(&q->qi_quotaofflock);
  82. /*
  83. * If we're just turning off quota enforcement, change mp and go.
  84. */
  85. if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
  86. mp->m_qflags &= ~(flags);
  87. spin_lock(&mp->m_sb_lock);
  88. mp->m_sb.sb_qflags = mp->m_qflags;
  89. spin_unlock(&mp->m_sb_lock);
  90. mutex_unlock(&q->qi_quotaofflock);
  91. /* XXX what to do if error ? Revert back to old vals incore ? */
  92. error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
  93. return (error);
  94. }
  95. dqtype = 0;
  96. inactivate_flags = 0;
  97. /*
  98. * If accounting is off, we must turn enforcement off, clear the
  99. * quota 'CHKD' certificate to make it known that we have to
  100. * do a quotacheck the next time this quota is turned on.
  101. */
  102. if (flags & XFS_UQUOTA_ACCT) {
  103. dqtype |= XFS_QMOPT_UQUOTA;
  104. flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
  105. inactivate_flags |= XFS_UQUOTA_ACTIVE;
  106. }
  107. if (flags & XFS_GQUOTA_ACCT) {
  108. dqtype |= XFS_QMOPT_GQUOTA;
  109. flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD);
  110. inactivate_flags |= XFS_GQUOTA_ACTIVE;
  111. }
  112. if (flags & XFS_PQUOTA_ACCT) {
  113. dqtype |= XFS_QMOPT_PQUOTA;
  114. flags |= (XFS_PQUOTA_CHKD | XFS_PQUOTA_ENFD);
  115. inactivate_flags |= XFS_PQUOTA_ACTIVE;
  116. }
  117. /*
  118. * Nothing to do? Don't complain. This happens when we're just
  119. * turning off quota enforcement.
  120. */
  121. if ((mp->m_qflags & flags) == 0)
  122. goto out_unlock;
  123. /*
  124. * Write the LI_QUOTAOFF log record, and do SB changes atomically,
  125. * and synchronously. If we fail to write, we should abort the
  126. * operation as it cannot be recovered safely if we crash.
  127. */
  128. error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
  129. if (error)
  130. goto out_unlock;
  131. /*
  132. * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
  133. * to take care of the race between dqget and quotaoff. We don't take
  134. * any special locks to reset these bits. All processes need to check
  135. * these bits *after* taking inode lock(s) to see if the particular
  136. * quota type is in the process of being turned off. If *ACTIVE, it is
  137. * guaranteed that all dquot structures and all quotainode ptrs will all
  138. * stay valid as long as that inode is kept locked.
  139. *
  140. * There is no turning back after this.
  141. */
  142. mp->m_qflags &= ~inactivate_flags;
  143. /*
  144. * Give back all the dquot reference(s) held by inodes.
  145. * Here we go thru every single incore inode in this file system, and
  146. * do a dqrele on the i_udquot/i_gdquot that it may have.
  147. * Essentially, as long as somebody has an inode locked, this guarantees
  148. * that quotas will not be turned off. This is handy because in a
  149. * transaction once we lock the inode(s) and check for quotaon, we can
  150. * depend on the quota inodes (and other things) being valid as long as
  151. * we keep the lock(s).
  152. */
  153. xfs_qm_dqrele_all_inodes(mp, flags);
  154. /*
  155. * Next we make the changes in the quota flag in the mount struct.
  156. * This isn't protected by a particular lock directly, because we
  157. * don't want to take a mrlock every time we depend on quotas being on.
  158. */
  159. mp->m_qflags &= ~flags;
  160. /*
  161. * Go through all the dquots of this file system and purge them,
  162. * according to what was turned off.
  163. */
  164. xfs_qm_dqpurge_all(mp, dqtype);
  165. /*
  166. * Transactions that had started before ACTIVE state bit was cleared
  167. * could have logged many dquots, so they'd have higher LSNs than
  168. * the first QUOTAOFF log record does. If we happen to crash when
  169. * the tail of the log has gone past the QUOTAOFF record, but
  170. * before the last dquot modification, those dquots __will__
  171. * recover, and that's not good.
  172. *
  173. * So, we have QUOTAOFF start and end logitems; the start
  174. * logitem won't get overwritten until the end logitem appears...
  175. */
  176. error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
  177. if (error) {
  178. /* We're screwed now. Shutdown is the only option. */
  179. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  180. goto out_unlock;
  181. }
  182. /*
  183. * If all quotas are completely turned off, close shop.
  184. */
  185. if (mp->m_qflags == 0) {
  186. mutex_unlock(&q->qi_quotaofflock);
  187. xfs_qm_destroy_quotainfo(mp);
  188. return (0);
  189. }
  190. /*
  191. * Release our quotainode references if we don't need them anymore.
  192. */
  193. if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
  194. IRELE(q->qi_uquotaip);
  195. q->qi_uquotaip = NULL;
  196. }
  197. if ((dqtype & XFS_QMOPT_GQUOTA) && q->qi_gquotaip) {
  198. IRELE(q->qi_gquotaip);
  199. q->qi_gquotaip = NULL;
  200. }
  201. if ((dqtype & XFS_QMOPT_PQUOTA) && q->qi_pquotaip) {
  202. IRELE(q->qi_pquotaip);
  203. q->qi_pquotaip = NULL;
  204. }
  205. out_unlock:
  206. mutex_unlock(&q->qi_quotaofflock);
  207. return error;
  208. }
  209. STATIC int
  210. xfs_qm_scall_trunc_qfile(
  211. struct xfs_mount *mp,
  212. xfs_ino_t ino)
  213. {
  214. struct xfs_inode *ip;
  215. struct xfs_trans *tp;
  216. int error;
  217. if (ino == NULLFSINO)
  218. return 0;
  219. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  220. if (error)
  221. return error;
  222. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  223. tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
  224. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  225. if (error) {
  226. xfs_trans_cancel(tp, 0);
  227. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  228. goto out_put;
  229. }
  230. xfs_ilock(ip, XFS_ILOCK_EXCL);
  231. xfs_trans_ijoin(tp, ip, 0);
  232. ip->i_d.di_size = 0;
  233. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  234. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  235. if (error) {
  236. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
  237. XFS_TRANS_ABORT);
  238. goto out_unlock;
  239. }
  240. ASSERT(ip->i_d.di_nextents == 0);
  241. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  242. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  243. out_unlock:
  244. xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  245. out_put:
  246. IRELE(ip);
  247. return error;
  248. }
  249. int
  250. xfs_qm_scall_trunc_qfiles(
  251. xfs_mount_t *mp,
  252. uint flags)
  253. {
  254. int error = 0, error2 = 0;
  255. if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) {
  256. xfs_debug(mp, "%s: flags=%x m_qflags=%x\n",
  257. __func__, flags, mp->m_qflags);
  258. return XFS_ERROR(EINVAL);
  259. }
  260. if (flags & XFS_DQ_USER)
  261. error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
  262. if (flags & XFS_DQ_GROUP)
  263. error2 = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
  264. if (flags & XFS_DQ_PROJ)
  265. error2 = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
  266. return error ? error : error2;
  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\n",
  289. __func__, mp->m_qflags);
  290. return XFS_ERROR(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\n",
  310. __func__, flags, mp->m_sb.sb_qflags);
  311. return XFS_ERROR(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 XFS_ERROR(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 XFS_ERROR(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 XFS_ERROR(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\n", 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\n", 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\n", 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=NULL;
  708. uint oldsbqflag=0;
  709. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  710. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_quotaoff, 0, 0);
  711. if (error)
  712. goto error0;
  713. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  714. xfs_trans_log_quotaoff_item(tp, qoffi);
  715. spin_lock(&mp->m_sb_lock);
  716. oldsbqflag = mp->m_sb.sb_qflags;
  717. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  718. spin_unlock(&mp->m_sb_lock);
  719. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  720. /*
  721. * We have to make sure that the transaction is secure on disk before we
  722. * return and actually stop quota accounting. So, make it synchronous.
  723. * We don't care about quotoff's performance.
  724. */
  725. xfs_trans_set_sync(tp);
  726. error = xfs_trans_commit(tp, 0);
  727. error0:
  728. if (error) {
  729. xfs_trans_cancel(tp, 0);
  730. /*
  731. * No one else is modifying sb_qflags, so this is OK.
  732. * We still hold the quotaofflock.
  733. */
  734. spin_lock(&mp->m_sb_lock);
  735. mp->m_sb.sb_qflags = oldsbqflag;
  736. spin_unlock(&mp->m_sb_lock);
  737. }
  738. *qoffstartp = qoffi;
  739. return (error);
  740. }
  741. int
  742. xfs_qm_scall_getquota(
  743. struct xfs_mount *mp,
  744. xfs_dqid_t id,
  745. uint type,
  746. struct fs_disk_quota *dst)
  747. {
  748. struct xfs_dquot *dqp;
  749. int error;
  750. /*
  751. * Try to get the dquot. We don't want it allocated on disk, so
  752. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  753. * exist, we'll get ENOENT back.
  754. */
  755. error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp);
  756. if (error)
  757. return error;
  758. /*
  759. * If everything's NULL, this dquot doesn't quite exist as far as
  760. * our utility programs are concerned.
  761. */
  762. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  763. error = XFS_ERROR(ENOENT);
  764. goto out_put;
  765. }
  766. memset(dst, 0, sizeof(*dst));
  767. dst->d_version = FS_DQUOT_VERSION;
  768. dst->d_flags = xfs_qm_export_qtype_flags(dqp->q_core.d_flags);
  769. dst->d_id = be32_to_cpu(dqp->q_core.d_id);
  770. dst->d_blk_hardlimit =
  771. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
  772. dst->d_blk_softlimit =
  773. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
  774. dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
  775. dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
  776. dst->d_bcount = XFS_FSB_TO_BB(mp, dqp->q_res_bcount);
  777. dst->d_icount = dqp->q_res_icount;
  778. dst->d_btimer = be32_to_cpu(dqp->q_core.d_btimer);
  779. dst->d_itimer = be32_to_cpu(dqp->q_core.d_itimer);
  780. dst->d_iwarns = be16_to_cpu(dqp->q_core.d_iwarns);
  781. dst->d_bwarns = be16_to_cpu(dqp->q_core.d_bwarns);
  782. dst->d_rtb_hardlimit =
  783. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
  784. dst->d_rtb_softlimit =
  785. XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
  786. dst->d_rtbcount = XFS_FSB_TO_BB(mp, dqp->q_res_rtbcount);
  787. dst->d_rtbtimer = be32_to_cpu(dqp->q_core.d_rtbtimer);
  788. dst->d_rtbwarns = be16_to_cpu(dqp->q_core.d_rtbwarns);
  789. /*
  790. * Internally, we don't reset all the timers when quota enforcement
  791. * gets turned off. No need to confuse the user level code,
  792. * so return zeroes in that case.
  793. */
  794. if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
  795. dqp->q_core.d_flags == XFS_DQ_USER) ||
  796. (!XFS_IS_GQUOTA_ENFORCED(mp) &&
  797. dqp->q_core.d_flags == XFS_DQ_GROUP) ||
  798. (!XFS_IS_PQUOTA_ENFORCED(mp) &&
  799. dqp->q_core.d_flags == XFS_DQ_PROJ)) {
  800. dst->d_btimer = 0;
  801. dst->d_itimer = 0;
  802. dst->d_rtbtimer = 0;
  803. }
  804. #ifdef DEBUG
  805. if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == FS_USER_QUOTA) ||
  806. (XFS_IS_GQUOTA_ENFORCED(mp) && dst->d_flags == FS_GROUP_QUOTA) ||
  807. (XFS_IS_PQUOTA_ENFORCED(mp) && dst->d_flags == FS_PROJ_QUOTA)) &&
  808. dst->d_id != 0) {
  809. if ((dst->d_bcount > dst->d_blk_softlimit) &&
  810. (dst->d_blk_softlimit > 0)) {
  811. ASSERT(dst->d_btimer != 0);
  812. }
  813. if ((dst->d_icount > dst->d_ino_softlimit) &&
  814. (dst->d_ino_softlimit > 0)) {
  815. ASSERT(dst->d_itimer != 0);
  816. }
  817. }
  818. #endif
  819. out_put:
  820. xfs_qm_dqput(dqp);
  821. return error;
  822. }
  823. STATIC uint
  824. xfs_qm_export_qtype_flags(
  825. uint flags)
  826. {
  827. /*
  828. * Can't be more than one, or none.
  829. */
  830. ASSERT((flags & (FS_PROJ_QUOTA | FS_USER_QUOTA)) !=
  831. (FS_PROJ_QUOTA | FS_USER_QUOTA));
  832. ASSERT((flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)) !=
  833. (FS_PROJ_QUOTA | FS_GROUP_QUOTA));
  834. ASSERT((flags & (FS_USER_QUOTA | FS_GROUP_QUOTA)) !=
  835. (FS_USER_QUOTA | FS_GROUP_QUOTA));
  836. ASSERT((flags & (FS_PROJ_QUOTA|FS_USER_QUOTA|FS_GROUP_QUOTA)) != 0);
  837. return (flags & XFS_DQ_USER) ?
  838. FS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
  839. FS_PROJ_QUOTA : FS_GROUP_QUOTA;
  840. }
  841. STATIC uint
  842. xfs_qm_export_flags(
  843. uint flags)
  844. {
  845. uint uflags;
  846. uflags = 0;
  847. if (flags & XFS_UQUOTA_ACCT)
  848. uflags |= FS_QUOTA_UDQ_ACCT;
  849. if (flags & XFS_GQUOTA_ACCT)
  850. uflags |= FS_QUOTA_GDQ_ACCT;
  851. if (flags & XFS_PQUOTA_ACCT)
  852. uflags |= FS_QUOTA_PDQ_ACCT;
  853. if (flags & XFS_UQUOTA_ENFD)
  854. uflags |= FS_QUOTA_UDQ_ENFD;
  855. if (flags & XFS_GQUOTA_ENFD)
  856. uflags |= FS_QUOTA_GDQ_ENFD;
  857. if (flags & XFS_PQUOTA_ENFD)
  858. uflags |= FS_QUOTA_PDQ_ENFD;
  859. return (uflags);
  860. }
  861. STATIC int
  862. xfs_dqrele_inode(
  863. struct xfs_inode *ip,
  864. struct xfs_perag *pag,
  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. }