xfs_qm_syscalls.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.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_itable.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_rtalloc.h"
  35. #include "xfs_error.h"
  36. #include "xfs_attr.h"
  37. #include "xfs_buf_item.h"
  38. #include "xfs_utils.h"
  39. #include "xfs_qm.h"
  40. #include "xfs_trace.h"
  41. STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
  42. STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
  43. uint);
  44. STATIC uint xfs_qm_export_flags(uint);
  45. STATIC uint xfs_qm_export_qtype_flags(uint);
  46. STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
  47. fs_disk_quota_t *);
  48. /*
  49. * Turn off quota accounting and/or enforcement for all udquots and/or
  50. * gdquots. Called only at unmount time.
  51. *
  52. * This assumes that there are no dquots of this file system cached
  53. * incore, and modifies the ondisk dquot directly. Therefore, for example,
  54. * it is an error to call this twice, without purging the cache.
  55. */
  56. int
  57. xfs_qm_scall_quotaoff(
  58. xfs_mount_t *mp,
  59. uint flags)
  60. {
  61. struct xfs_quotainfo *q = mp->m_quotainfo;
  62. uint dqtype;
  63. int error;
  64. uint inactivate_flags;
  65. xfs_qoff_logitem_t *qoffstart;
  66. int nculprits;
  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_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
  112. inactivate_flags |= XFS_GQUOTA_ACTIVE;
  113. } else if (flags & XFS_PQUOTA_ACCT) {
  114. dqtype |= XFS_QMOPT_PQUOTA;
  115. flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
  116. inactivate_flags |= XFS_PQUOTA_ACTIVE;
  117. }
  118. /*
  119. * Nothing to do? Don't complain. This happens when we're just
  120. * turning off quota enforcement.
  121. */
  122. if ((mp->m_qflags & flags) == 0)
  123. goto out_unlock;
  124. /*
  125. * Write the LI_QUOTAOFF log record, and do SB changes atomically,
  126. * and synchronously. If we fail to write, we should abort the
  127. * operation as it cannot be recovered safely if we crash.
  128. */
  129. error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
  130. if (error)
  131. goto out_unlock;
  132. /*
  133. * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
  134. * to take care of the race between dqget and quotaoff. We don't take
  135. * any special locks to reset these bits. All processes need to check
  136. * these bits *after* taking inode lock(s) to see if the particular
  137. * quota type is in the process of being turned off. If *ACTIVE, it is
  138. * guaranteed that all dquot structures and all quotainode ptrs will all
  139. * stay valid as long as that inode is kept locked.
  140. *
  141. * There is no turning back after this.
  142. */
  143. mp->m_qflags &= ~inactivate_flags;
  144. /*
  145. * Give back all the dquot reference(s) held by inodes.
  146. * Here we go thru every single incore inode in this file system, and
  147. * do a dqrele on the i_udquot/i_gdquot that it may have.
  148. * Essentially, as long as somebody has an inode locked, this guarantees
  149. * that quotas will not be turned off. This is handy because in a
  150. * transaction once we lock the inode(s) and check for quotaon, we can
  151. * depend on the quota inodes (and other things) being valid as long as
  152. * we keep the lock(s).
  153. */
  154. xfs_qm_dqrele_all_inodes(mp, flags);
  155. /*
  156. * Next we make the changes in the quota flag in the mount struct.
  157. * This isn't protected by a particular lock directly, because we
  158. * don't want to take a mrlock everytime we depend on quotas being on.
  159. */
  160. mp->m_qflags &= ~(flags);
  161. /*
  162. * Go through all the dquots of this file system and purge them,
  163. * according to what was turned off. We may not be able to get rid
  164. * of all dquots, because dquots can have temporary references that
  165. * are not attached to inodes. eg. xfs_setattr, xfs_create.
  166. * So, if we couldn't purge all the dquots from the filesystem,
  167. * we can't get rid of the incore data structures.
  168. */
  169. while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype)))
  170. delay(10 * nculprits);
  171. /*
  172. * Transactions that had started before ACTIVE state bit was cleared
  173. * could have logged many dquots, so they'd have higher LSNs than
  174. * the first QUOTAOFF log record does. If we happen to crash when
  175. * the tail of the log has gone past the QUOTAOFF record, but
  176. * before the last dquot modification, those dquots __will__
  177. * recover, and that's not good.
  178. *
  179. * So, we have QUOTAOFF start and end logitems; the start
  180. * logitem won't get overwritten until the end logitem appears...
  181. */
  182. error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
  183. if (error) {
  184. /* We're screwed now. Shutdown is the only option. */
  185. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  186. goto out_unlock;
  187. }
  188. /*
  189. * If quotas is completely disabled, close shop.
  190. */
  191. if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
  192. ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
  193. mutex_unlock(&q->qi_quotaofflock);
  194. xfs_qm_destroy_quotainfo(mp);
  195. return (0);
  196. }
  197. /*
  198. * Release our quotainode references if we don't need them anymore.
  199. */
  200. if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
  201. IRELE(q->qi_uquotaip);
  202. q->qi_uquotaip = NULL;
  203. }
  204. if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && q->qi_gquotaip) {
  205. IRELE(q->qi_gquotaip);
  206. q->qi_gquotaip = NULL;
  207. }
  208. out_unlock:
  209. mutex_unlock(&q->qi_quotaofflock);
  210. return error;
  211. }
  212. STATIC int
  213. xfs_qm_scall_trunc_qfile(
  214. struct xfs_mount *mp,
  215. xfs_ino_t ino)
  216. {
  217. struct xfs_inode *ip;
  218. struct xfs_trans *tp;
  219. int error;
  220. if (ino == NULLFSINO)
  221. return 0;
  222. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  223. if (error)
  224. return error;
  225. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  226. tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
  227. error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  228. XFS_TRANS_PERM_LOG_RES,
  229. XFS_ITRUNCATE_LOG_COUNT);
  230. if (error) {
  231. xfs_trans_cancel(tp, 0);
  232. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  233. goto out_put;
  234. }
  235. xfs_ilock(ip, XFS_ILOCK_EXCL);
  236. xfs_trans_ijoin(tp, ip);
  237. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK, 1);
  238. if (error) {
  239. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
  240. XFS_TRANS_ABORT);
  241. goto out_unlock;
  242. }
  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\n",
  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|XFS_DQ_PROJ))
  265. error2 = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
  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. uint accflags;
  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. accflags = flags & XFS_ALL_QUOTA_ACCT;
  287. flags &= ~(XFS_ALL_QUOTA_ACCT);
  288. sbflags = 0;
  289. if (flags == 0) {
  290. xfs_debug(mp, "%s: zero flags, m_qflags=%x\n",
  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. ||
  305. ((flags & XFS_PQUOTA_ACCT) == 0 &&
  306. (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
  307. (flags & XFS_GQUOTA_ACCT) == 0 &&
  308. (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
  309. (flags & XFS_OQUOTA_ENFD))) {
  310. xfs_debug(mp,
  311. "%s: Can't enforce without acct, flags=%x sbflags=%x\n",
  312. __func__, flags, mp->m_sb.sb_qflags);
  313. return XFS_ERROR(EINVAL);
  314. }
  315. /*
  316. * If everything's upto-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. */
  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, *gip;
  367. boolean_t tempuqip, tempgqip;
  368. uip = gip = NULL;
  369. tempuqip = tempgqip = B_FALSE;
  370. memset(out, 0, sizeof(fs_quota_stat_t));
  371. out->qs_version = FS_QSTAT_VERSION;
  372. if (!xfs_sb_version_hasquota(&mp->m_sb)) {
  373. out->qs_uquota.qfs_ino = NULLFSINO;
  374. out->qs_gquota.qfs_ino = NULLFSINO;
  375. return (0);
  376. }
  377. out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
  378. (XFS_ALL_QUOTA_ACCT|
  379. XFS_ALL_QUOTA_ENFD));
  380. out->qs_pad = 0;
  381. out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
  382. out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
  383. if (q) {
  384. uip = q->qi_uquotaip;
  385. gip = q->qi_gquotaip;
  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 = B_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 = B_TRUE;
  396. }
  397. if (uip) {
  398. out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
  399. out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
  400. if (tempuqip)
  401. IRELE(uip);
  402. }
  403. if (gip) {
  404. out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
  405. out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
  406. if (tempgqip)
  407. IRELE(gip);
  408. }
  409. if (q) {
  410. out->qs_incoredqs = q->qi_dquots;
  411. out->qs_btimelimit = q->qi_btimelimit;
  412. out->qs_itimelimit = q->qi_itimelimit;
  413. out->qs_rtbtimelimit = q->qi_rtbtimelimit;
  414. out->qs_bwarnlimit = q->qi_bwarnlimit;
  415. out->qs_iwarnlimit = q->qi_iwarnlimit;
  416. }
  417. return 0;
  418. }
  419. #define XFS_DQ_MASK \
  420. (FS_DQ_LIMIT_MASK | FS_DQ_TIMER_MASK | FS_DQ_WARNS_MASK)
  421. /*
  422. * Adjust quota limits, and start/stop timers accordingly.
  423. */
  424. int
  425. xfs_qm_scall_setqlim(
  426. xfs_mount_t *mp,
  427. xfs_dqid_t id,
  428. uint type,
  429. fs_disk_quota_t *newlim)
  430. {
  431. struct xfs_quotainfo *q = mp->m_quotainfo;
  432. xfs_disk_dquot_t *ddq;
  433. xfs_dquot_t *dqp;
  434. xfs_trans_t *tp;
  435. int error;
  436. xfs_qcnt_t hard, soft;
  437. if (newlim->d_fieldmask & ~XFS_DQ_MASK)
  438. return EINVAL;
  439. if ((newlim->d_fieldmask & XFS_DQ_MASK) == 0)
  440. return 0;
  441. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
  442. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
  443. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  444. xfs_trans_cancel(tp, 0);
  445. return (error);
  446. }
  447. /*
  448. * We don't want to race with a quotaoff so take the quotaoff lock.
  449. * (We don't hold an inode lock, so there's nothing else to stop
  450. * a quotaoff from happening). (XXXThis doesn't currently happen
  451. * because we take the vfslock before calling xfs_qm_sysent).
  452. */
  453. mutex_lock(&q->qi_quotaofflock);
  454. /*
  455. * Get the dquot (locked), and join it to the transaction.
  456. * Allocate the dquot if this doesn't exist.
  457. */
  458. if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
  459. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  460. ASSERT(error != ENOENT);
  461. goto out_unlock;
  462. }
  463. xfs_trans_dqjoin(tp, dqp);
  464. ddq = &dqp->q_core;
  465. /*
  466. * Make sure that hardlimits are >= soft limits before changing.
  467. */
  468. hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
  469. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
  470. be64_to_cpu(ddq->d_blk_hardlimit);
  471. soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
  472. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
  473. be64_to_cpu(ddq->d_blk_softlimit);
  474. if (hard == 0 || hard >= soft) {
  475. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  476. ddq->d_blk_softlimit = cpu_to_be64(soft);
  477. if (id == 0) {
  478. q->qi_bhardlimit = hard;
  479. q->qi_bsoftlimit = soft;
  480. }
  481. } else {
  482. xfs_debug(mp, "blkhard %Ld < blksoft %Ld\n", hard, soft);
  483. }
  484. hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
  485. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
  486. be64_to_cpu(ddq->d_rtb_hardlimit);
  487. soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
  488. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
  489. be64_to_cpu(ddq->d_rtb_softlimit);
  490. if (hard == 0 || hard >= soft) {
  491. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  492. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  493. if (id == 0) {
  494. q->qi_rtbhardlimit = hard;
  495. q->qi_rtbsoftlimit = soft;
  496. }
  497. } else {
  498. xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
  499. }
  500. hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
  501. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  502. be64_to_cpu(ddq->d_ino_hardlimit);
  503. soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
  504. (xfs_qcnt_t) newlim->d_ino_softlimit :
  505. be64_to_cpu(ddq->d_ino_softlimit);
  506. if (hard == 0 || hard >= soft) {
  507. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  508. ddq->d_ino_softlimit = cpu_to_be64(soft);
  509. if (id == 0) {
  510. q->qi_ihardlimit = hard;
  511. q->qi_isoftlimit = soft;
  512. }
  513. } else {
  514. xfs_debug(mp, "ihard %Ld < isoft %Ld\n", hard, soft);
  515. }
  516. /*
  517. * Update warnings counter(s) if requested
  518. */
  519. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  520. ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
  521. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  522. ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
  523. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  524. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
  525. if (id == 0) {
  526. /*
  527. * Timelimits for the super user set the relative time
  528. * the other users can be over quota for this file system.
  529. * If it is zero a default is used. Ditto for the default
  530. * soft and hard limit values (already done, above), and
  531. * for warnings.
  532. */
  533. if (newlim->d_fieldmask & FS_DQ_BTIMER) {
  534. q->qi_btimelimit = newlim->d_btimer;
  535. ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
  536. }
  537. if (newlim->d_fieldmask & FS_DQ_ITIMER) {
  538. q->qi_itimelimit = newlim->d_itimer;
  539. ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
  540. }
  541. if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
  542. q->qi_rtbtimelimit = newlim->d_rtbtimer;
  543. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
  544. }
  545. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  546. q->qi_bwarnlimit = newlim->d_bwarns;
  547. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  548. q->qi_iwarnlimit = newlim->d_iwarns;
  549. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  550. q->qi_rtbwarnlimit = newlim->d_rtbwarns;
  551. } else {
  552. /*
  553. * If the user is now over quota, start the timelimit.
  554. * The user will not be 'warned'.
  555. * Note that we keep the timers ticking, whether enforcement
  556. * is on or off. We don't really want to bother with iterating
  557. * over all ondisk dquots and turning the timers on/off.
  558. */
  559. xfs_qm_adjust_dqtimers(mp, ddq);
  560. }
  561. dqp->dq_flags |= XFS_DQ_DIRTY;
  562. xfs_trans_log_dquot(tp, dqp);
  563. error = xfs_trans_commit(tp, 0);
  564. xfs_qm_dqprint(dqp);
  565. xfs_qm_dqrele(dqp);
  566. out_unlock:
  567. mutex_unlock(&q->qi_quotaofflock);
  568. return error;
  569. }
  570. int
  571. xfs_qm_scall_getquota(
  572. xfs_mount_t *mp,
  573. xfs_dqid_t id,
  574. uint type,
  575. fs_disk_quota_t *out)
  576. {
  577. xfs_dquot_t *dqp;
  578. int error;
  579. /*
  580. * Try to get the dquot. We don't want it allocated on disk, so
  581. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  582. * exist, we'll get ENOENT back.
  583. */
  584. if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
  585. return (error);
  586. }
  587. /*
  588. * If everything's NULL, this dquot doesn't quite exist as far as
  589. * our utility programs are concerned.
  590. */
  591. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  592. xfs_qm_dqput(dqp);
  593. return XFS_ERROR(ENOENT);
  594. }
  595. /* xfs_qm_dqprint(dqp); */
  596. /*
  597. * Convert the disk dquot to the exportable format
  598. */
  599. xfs_qm_export_dquot(mp, &dqp->q_core, out);
  600. xfs_qm_dqput(dqp);
  601. return (error ? XFS_ERROR(EFAULT) : 0);
  602. }
  603. STATIC int
  604. xfs_qm_log_quotaoff_end(
  605. xfs_mount_t *mp,
  606. xfs_qoff_logitem_t *startqoff,
  607. uint flags)
  608. {
  609. xfs_trans_t *tp;
  610. int error;
  611. xfs_qoff_logitem_t *qoffi;
  612. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  613. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
  614. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  615. xfs_trans_cancel(tp, 0);
  616. return (error);
  617. }
  618. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  619. flags & XFS_ALL_QUOTA_ACCT);
  620. xfs_trans_log_quotaoff_item(tp, qoffi);
  621. /*
  622. * We have to make sure that the transaction is secure on disk before we
  623. * return and actually stop quota accounting. So, make it synchronous.
  624. * We don't care about quotoff's performance.
  625. */
  626. xfs_trans_set_sync(tp);
  627. error = xfs_trans_commit(tp, 0);
  628. return (error);
  629. }
  630. STATIC int
  631. xfs_qm_log_quotaoff(
  632. xfs_mount_t *mp,
  633. xfs_qoff_logitem_t **qoffstartp,
  634. uint flags)
  635. {
  636. xfs_trans_t *tp;
  637. int error;
  638. xfs_qoff_logitem_t *qoffi=NULL;
  639. uint oldsbqflag=0;
  640. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  641. if ((error = xfs_trans_reserve(tp, 0,
  642. sizeof(xfs_qoff_logitem_t) * 2 +
  643. mp->m_sb.sb_sectsize + 128,
  644. 0,
  645. 0,
  646. XFS_DEFAULT_LOG_COUNT))) {
  647. goto error0;
  648. }
  649. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  650. xfs_trans_log_quotaoff_item(tp, qoffi);
  651. spin_lock(&mp->m_sb_lock);
  652. oldsbqflag = mp->m_sb.sb_qflags;
  653. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  654. spin_unlock(&mp->m_sb_lock);
  655. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  656. /*
  657. * We have to make sure that the transaction is secure on disk before we
  658. * return and actually stop quota accounting. So, make it synchronous.
  659. * We don't care about quotoff's performance.
  660. */
  661. xfs_trans_set_sync(tp);
  662. error = xfs_trans_commit(tp, 0);
  663. error0:
  664. if (error) {
  665. xfs_trans_cancel(tp, 0);
  666. /*
  667. * No one else is modifying sb_qflags, so this is OK.
  668. * We still hold the quotaofflock.
  669. */
  670. spin_lock(&mp->m_sb_lock);
  671. mp->m_sb.sb_qflags = oldsbqflag;
  672. spin_unlock(&mp->m_sb_lock);
  673. }
  674. *qoffstartp = qoffi;
  675. return (error);
  676. }
  677. /*
  678. * Translate an internal style on-disk-dquot to the exportable format.
  679. * The main differences are that the counters/limits are all in Basic
  680. * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
  681. * to be converted to the native endianness.
  682. */
  683. STATIC void
  684. xfs_qm_export_dquot(
  685. xfs_mount_t *mp,
  686. xfs_disk_dquot_t *src,
  687. struct fs_disk_quota *dst)
  688. {
  689. memset(dst, 0, sizeof(*dst));
  690. dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
  691. dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
  692. dst->d_id = be32_to_cpu(src->d_id);
  693. dst->d_blk_hardlimit =
  694. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
  695. dst->d_blk_softlimit =
  696. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
  697. dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
  698. dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
  699. dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
  700. dst->d_icount = be64_to_cpu(src->d_icount);
  701. dst->d_btimer = be32_to_cpu(src->d_btimer);
  702. dst->d_itimer = be32_to_cpu(src->d_itimer);
  703. dst->d_iwarns = be16_to_cpu(src->d_iwarns);
  704. dst->d_bwarns = be16_to_cpu(src->d_bwarns);
  705. dst->d_rtb_hardlimit =
  706. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
  707. dst->d_rtb_softlimit =
  708. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
  709. dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
  710. dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
  711. dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
  712. /*
  713. * Internally, we don't reset all the timers when quota enforcement
  714. * gets turned off. No need to confuse the user level code,
  715. * so return zeroes in that case.
  716. */
  717. if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
  718. (!XFS_IS_OQUOTA_ENFORCED(mp) &&
  719. (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
  720. dst->d_btimer = 0;
  721. dst->d_itimer = 0;
  722. dst->d_rtbtimer = 0;
  723. }
  724. #ifdef DEBUG
  725. if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == FS_USER_QUOTA) ||
  726. (XFS_IS_OQUOTA_ENFORCED(mp) &&
  727. (dst->d_flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)))) &&
  728. dst->d_id != 0) {
  729. if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
  730. (dst->d_blk_softlimit > 0)) {
  731. ASSERT(dst->d_btimer != 0);
  732. }
  733. if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
  734. (dst->d_ino_softlimit > 0)) {
  735. ASSERT(dst->d_itimer != 0);
  736. }
  737. }
  738. #endif
  739. }
  740. STATIC uint
  741. xfs_qm_export_qtype_flags(
  742. uint flags)
  743. {
  744. /*
  745. * Can't be more than one, or none.
  746. */
  747. ASSERT((flags & (FS_PROJ_QUOTA | FS_USER_QUOTA)) !=
  748. (FS_PROJ_QUOTA | FS_USER_QUOTA));
  749. ASSERT((flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)) !=
  750. (FS_PROJ_QUOTA | FS_GROUP_QUOTA));
  751. ASSERT((flags & (FS_USER_QUOTA | FS_GROUP_QUOTA)) !=
  752. (FS_USER_QUOTA | FS_GROUP_QUOTA));
  753. ASSERT((flags & (FS_PROJ_QUOTA|FS_USER_QUOTA|FS_GROUP_QUOTA)) != 0);
  754. return (flags & XFS_DQ_USER) ?
  755. FS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
  756. FS_PROJ_QUOTA : FS_GROUP_QUOTA;
  757. }
  758. STATIC uint
  759. xfs_qm_export_flags(
  760. uint flags)
  761. {
  762. uint uflags;
  763. uflags = 0;
  764. if (flags & XFS_UQUOTA_ACCT)
  765. uflags |= FS_QUOTA_UDQ_ACCT;
  766. if (flags & XFS_PQUOTA_ACCT)
  767. uflags |= FS_QUOTA_PDQ_ACCT;
  768. if (flags & XFS_GQUOTA_ACCT)
  769. uflags |= FS_QUOTA_GDQ_ACCT;
  770. if (flags & XFS_UQUOTA_ENFD)
  771. uflags |= FS_QUOTA_UDQ_ENFD;
  772. if (flags & (XFS_OQUOTA_ENFD)) {
  773. uflags |= (flags & XFS_GQUOTA_ACCT) ?
  774. FS_QUOTA_GDQ_ENFD : FS_QUOTA_PDQ_ENFD;
  775. }
  776. return (uflags);
  777. }
  778. STATIC int
  779. xfs_dqrele_inode(
  780. struct xfs_inode *ip,
  781. struct xfs_perag *pag,
  782. int flags)
  783. {
  784. /* skip quota inodes */
  785. if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
  786. ip == ip->i_mount->m_quotainfo->qi_gquotaip) {
  787. ASSERT(ip->i_udquot == NULL);
  788. ASSERT(ip->i_gdquot == NULL);
  789. return 0;
  790. }
  791. xfs_ilock(ip, XFS_ILOCK_EXCL);
  792. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  793. xfs_qm_dqrele(ip->i_udquot);
  794. ip->i_udquot = NULL;
  795. }
  796. if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  797. xfs_qm_dqrele(ip->i_gdquot);
  798. ip->i_gdquot = NULL;
  799. }
  800. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  801. return 0;
  802. }
  803. /*
  804. * Go thru all the inodes in the file system, releasing their dquots.
  805. *
  806. * Note that the mount structure gets modified to indicate that quotas are off
  807. * AFTER this, in the case of quotaoff.
  808. */
  809. void
  810. xfs_qm_dqrele_all_inodes(
  811. struct xfs_mount *mp,
  812. uint flags)
  813. {
  814. ASSERT(mp->m_quotainfo);
  815. xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags);
  816. }
  817. /*------------------------------------------------------------------------*/
  818. #ifdef DEBUG
  819. /*
  820. * This contains all the test functions for XFS disk quotas.
  821. * Currently it does a quota accounting check. ie. it walks through
  822. * all inodes in the file system, calculating the dquot accounting fields,
  823. * and prints out any inconsistencies.
  824. */
  825. xfs_dqhash_t *qmtest_udqtab;
  826. xfs_dqhash_t *qmtest_gdqtab;
  827. int qmtest_hashmask;
  828. int qmtest_nfails;
  829. struct mutex qcheck_lock;
  830. #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
  831. (__psunsigned_t)(id)) & \
  832. (qmtest_hashmask - 1))
  833. #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
  834. (qmtest_udqtab + \
  835. DQTEST_HASHVAL(mp, id)) : \
  836. (qmtest_gdqtab + \
  837. DQTEST_HASHVAL(mp, id)))
  838. #define DQTEST_LIST_PRINT(l, NXT, title) \
  839. { \
  840. xfs_dqtest_t *dqp; int i = 0;\
  841. xfs_debug(NULL, "%s (#%d)", title, (int) (l)->qh_nelems); \
  842. for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
  843. dqp = (xfs_dqtest_t *)dqp->NXT) { \
  844. xfs_debug(dqp->q_mount, \
  845. " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
  846. ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
  847. dqp->d_bcount, dqp->d_icount); } \
  848. }
  849. typedef struct dqtest {
  850. uint dq_flags; /* various flags (XFS_DQ_*) */
  851. struct list_head q_hashlist;
  852. xfs_dqhash_t *q_hash; /* the hashchain header */
  853. xfs_mount_t *q_mount; /* filesystem this relates to */
  854. xfs_dqid_t d_id; /* user id or group id */
  855. xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
  856. xfs_qcnt_t d_icount; /* # inodes owned by the user */
  857. } xfs_dqtest_t;
  858. STATIC void
  859. xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
  860. {
  861. list_add(&dqp->q_hashlist, &h->qh_list);
  862. h->qh_version++;
  863. h->qh_nelems++;
  864. }
  865. STATIC void
  866. xfs_qm_dqtest_print(
  867. struct xfs_mount *mp,
  868. struct dqtest *d)
  869. {
  870. xfs_debug(mp, "-----------DQTEST DQUOT----------------");
  871. xfs_debug(mp, "---- dquot ID = %d", d->d_id);
  872. xfs_debug(mp, "---- fs = 0x%p", d->q_mount);
  873. xfs_debug(mp, "---- bcount = %Lu (0x%x)",
  874. d->d_bcount, (int)d->d_bcount);
  875. xfs_debug(mp, "---- icount = %Lu (0x%x)",
  876. d->d_icount, (int)d->d_icount);
  877. xfs_debug(mp, "---------------------------");
  878. }
  879. STATIC void
  880. xfs_qm_dqtest_failed(
  881. xfs_dqtest_t *d,
  882. xfs_dquot_t *dqp,
  883. char *reason,
  884. xfs_qcnt_t a,
  885. xfs_qcnt_t b,
  886. int error)
  887. {
  888. qmtest_nfails++;
  889. if (error)
  890. xfs_debug(dqp->q_mount,
  891. "quotacheck failed id=%d, err=%d\nreason: %s",
  892. d->d_id, error, reason);
  893. else
  894. xfs_debug(dqp->q_mount,
  895. "quotacheck failed id=%d (%s) [%d != %d]",
  896. d->d_id, reason, (int)a, (int)b);
  897. xfs_qm_dqtest_print(dqp->q_mount, d);
  898. if (dqp)
  899. xfs_qm_dqprint(dqp);
  900. }
  901. STATIC int
  902. xfs_dqtest_cmp2(
  903. xfs_dqtest_t *d,
  904. xfs_dquot_t *dqp)
  905. {
  906. int err = 0;
  907. if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
  908. xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
  909. be64_to_cpu(dqp->q_core.d_icount),
  910. d->d_icount, 0);
  911. err++;
  912. }
  913. if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
  914. xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
  915. be64_to_cpu(dqp->q_core.d_bcount),
  916. d->d_bcount, 0);
  917. err++;
  918. }
  919. if (dqp->q_core.d_blk_softlimit &&
  920. be64_to_cpu(dqp->q_core.d_bcount) >=
  921. be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
  922. if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
  923. xfs_debug(dqp->q_mount,
  924. "%d [%s] BLK TIMER NOT STARTED",
  925. d->d_id, DQFLAGTO_TYPESTR(d));
  926. err++;
  927. }
  928. }
  929. if (dqp->q_core.d_ino_softlimit &&
  930. be64_to_cpu(dqp->q_core.d_icount) >=
  931. be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
  932. if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
  933. xfs_debug(dqp->q_mount,
  934. "%d [%s] INO TIMER NOT STARTED",
  935. d->d_id, DQFLAGTO_TYPESTR(d));
  936. err++;
  937. }
  938. }
  939. #ifdef QUOTADEBUG
  940. if (!err) {
  941. xfs_debug(dqp->q_mount, "%d [%s] qchecked",
  942. d->d_id, DQFLAGTO_TYPESTR(d));
  943. }
  944. #endif
  945. return (err);
  946. }
  947. STATIC void
  948. xfs_dqtest_cmp(
  949. xfs_dqtest_t *d)
  950. {
  951. xfs_dquot_t *dqp;
  952. int error;
  953. /* xfs_qm_dqtest_print(d); */
  954. if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
  955. &dqp))) {
  956. xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
  957. return;
  958. }
  959. xfs_dqtest_cmp2(d, dqp);
  960. xfs_qm_dqput(dqp);
  961. }
  962. STATIC int
  963. xfs_qm_internalqcheck_dqget(
  964. xfs_mount_t *mp,
  965. xfs_dqid_t id,
  966. uint type,
  967. xfs_dqtest_t **O_dq)
  968. {
  969. xfs_dqtest_t *d;
  970. xfs_dqhash_t *h;
  971. h = DQTEST_HASH(mp, id, type);
  972. list_for_each_entry(d, &h->qh_list, q_hashlist) {
  973. if (d->d_id == id && mp == d->q_mount) {
  974. *O_dq = d;
  975. return (0);
  976. }
  977. }
  978. d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
  979. d->dq_flags = type;
  980. d->d_id = id;
  981. d->q_mount = mp;
  982. d->q_hash = h;
  983. INIT_LIST_HEAD(&d->q_hashlist);
  984. xfs_qm_hashinsert(h, d);
  985. *O_dq = d;
  986. return (0);
  987. }
  988. STATIC void
  989. xfs_qm_internalqcheck_get_dquots(
  990. xfs_mount_t *mp,
  991. xfs_dqid_t uid,
  992. xfs_dqid_t projid,
  993. xfs_dqid_t gid,
  994. xfs_dqtest_t **ud,
  995. xfs_dqtest_t **gd)
  996. {
  997. if (XFS_IS_UQUOTA_ON(mp))
  998. xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
  999. if (XFS_IS_GQUOTA_ON(mp))
  1000. xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
  1001. else if (XFS_IS_PQUOTA_ON(mp))
  1002. xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
  1003. }
  1004. STATIC void
  1005. xfs_qm_internalqcheck_dqadjust(
  1006. xfs_inode_t *ip,
  1007. xfs_dqtest_t *d)
  1008. {
  1009. d->d_icount++;
  1010. d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
  1011. }
  1012. STATIC int
  1013. xfs_qm_internalqcheck_adjust(
  1014. xfs_mount_t *mp, /* mount point for filesystem */
  1015. xfs_ino_t ino, /* inode number to get data for */
  1016. void __user *buffer, /* not used */
  1017. int ubsize, /* not used */
  1018. int *ubused, /* not used */
  1019. int *res) /* bulkstat result code */
  1020. {
  1021. xfs_inode_t *ip;
  1022. xfs_dqtest_t *ud, *gd;
  1023. uint lock_flags;
  1024. boolean_t ipreleased;
  1025. int error;
  1026. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1027. if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
  1028. *res = BULKSTAT_RV_NOTHING;
  1029. xfs_debug(mp, "%s: ino=%llu, uqino=%llu, gqino=%llu\n",
  1030. __func__, (unsigned long long) ino,
  1031. (unsigned long long) mp->m_sb.sb_uquotino,
  1032. (unsigned long long) mp->m_sb.sb_gquotino);
  1033. return XFS_ERROR(EINVAL);
  1034. }
  1035. ipreleased = B_FALSE;
  1036. again:
  1037. lock_flags = XFS_ILOCK_SHARED;
  1038. if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip))) {
  1039. *res = BULKSTAT_RV_NOTHING;
  1040. return (error);
  1041. }
  1042. /*
  1043. * This inode can have blocks after eof which can get released
  1044. * when we send it to inactive. Since we don't check the dquot
  1045. * until the after all our calculations are done, we must get rid
  1046. * of those now.
  1047. */
  1048. if (! ipreleased) {
  1049. xfs_iunlock(ip, lock_flags);
  1050. IRELE(ip);
  1051. ipreleased = B_TRUE;
  1052. goto again;
  1053. }
  1054. xfs_qm_internalqcheck_get_dquots(mp,
  1055. (xfs_dqid_t) ip->i_d.di_uid,
  1056. (xfs_dqid_t) xfs_get_projid(ip),
  1057. (xfs_dqid_t) ip->i_d.di_gid,
  1058. &ud, &gd);
  1059. if (XFS_IS_UQUOTA_ON(mp)) {
  1060. ASSERT(ud);
  1061. xfs_qm_internalqcheck_dqadjust(ip, ud);
  1062. }
  1063. if (XFS_IS_OQUOTA_ON(mp)) {
  1064. ASSERT(gd);
  1065. xfs_qm_internalqcheck_dqadjust(ip, gd);
  1066. }
  1067. xfs_iunlock(ip, lock_flags);
  1068. IRELE(ip);
  1069. *res = BULKSTAT_RV_DIDONE;
  1070. return (0);
  1071. }
  1072. /* PRIVATE, debugging */
  1073. int
  1074. xfs_qm_internalqcheck(
  1075. xfs_mount_t *mp)
  1076. {
  1077. xfs_ino_t lastino;
  1078. int done, count;
  1079. int i;
  1080. int error;
  1081. lastino = 0;
  1082. qmtest_hashmask = 32;
  1083. count = 5;
  1084. done = 0;
  1085. qmtest_nfails = 0;
  1086. if (! XFS_IS_QUOTA_ON(mp))
  1087. return XFS_ERROR(ESRCH);
  1088. xfs_log_force(mp, XFS_LOG_SYNC);
  1089. XFS_bflush(mp->m_ddev_targp);
  1090. xfs_log_force(mp, XFS_LOG_SYNC);
  1091. XFS_bflush(mp->m_ddev_targp);
  1092. mutex_lock(&qcheck_lock);
  1093. /* There should be absolutely no quota activity while this
  1094. is going on. */
  1095. qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
  1096. sizeof(xfs_dqhash_t), KM_SLEEP);
  1097. qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
  1098. sizeof(xfs_dqhash_t), KM_SLEEP);
  1099. do {
  1100. /*
  1101. * Iterate thru all the inodes in the file system,
  1102. * adjusting the corresponding dquot counters
  1103. */
  1104. error = xfs_bulkstat(mp, &lastino, &count,
  1105. xfs_qm_internalqcheck_adjust,
  1106. 0, NULL, &done);
  1107. if (error) {
  1108. xfs_debug(mp, "Bulkstat returned error 0x%x", error);
  1109. break;
  1110. }
  1111. } while (!done);
  1112. xfs_debug(mp, "Checking results against system dquots");
  1113. for (i = 0; i < qmtest_hashmask; i++) {
  1114. xfs_dqtest_t *d, *n;
  1115. xfs_dqhash_t *h;
  1116. h = &qmtest_udqtab[i];
  1117. list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) {
  1118. xfs_dqtest_cmp(d);
  1119. kmem_free(d);
  1120. }
  1121. h = &qmtest_gdqtab[i];
  1122. list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) {
  1123. xfs_dqtest_cmp(d);
  1124. kmem_free(d);
  1125. }
  1126. }
  1127. if (qmtest_nfails) {
  1128. xfs_debug(mp, "******** quotacheck failed ********");
  1129. xfs_debug(mp, "failures = %d", qmtest_nfails);
  1130. } else {
  1131. xfs_debug(mp, "******** quotacheck successful! ********");
  1132. }
  1133. kmem_free(qmtest_udqtab);
  1134. kmem_free(qmtest_gdqtab);
  1135. mutex_unlock(&qcheck_lock);
  1136. return (qmtest_nfails);
  1137. }
  1138. #endif /* DEBUG */