xfs_qm_syscalls.c 33 KB

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