xfs_qm_syscalls.c 33 KB

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