xfs_qm_syscalls.c 34 KB

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