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_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, 0);
  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, 0);
  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) == 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) == 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. /*
  405. * Adjust quota limits, and start/stop timers accordingly.
  406. */
  407. int
  408. xfs_qm_scall_setqlim(
  409. xfs_mount_t *mp,
  410. xfs_dqid_t id,
  411. uint type,
  412. fs_disk_quota_t *newlim)
  413. {
  414. struct xfs_quotainfo *q = mp->m_quotainfo;
  415. xfs_disk_dquot_t *ddq;
  416. xfs_dquot_t *dqp;
  417. xfs_trans_t *tp;
  418. int error;
  419. xfs_qcnt_t hard, soft;
  420. if ((newlim->d_fieldmask &
  421. (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
  422. return (0);
  423. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
  424. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
  425. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  426. xfs_trans_cancel(tp, 0);
  427. return (error);
  428. }
  429. /*
  430. * We don't want to race with a quotaoff so take the quotaoff lock.
  431. * (We don't hold an inode lock, so there's nothing else to stop
  432. * a quotaoff from happening). (XXXThis doesn't currently happen
  433. * because we take the vfslock before calling xfs_qm_sysent).
  434. */
  435. mutex_lock(&q->qi_quotaofflock);
  436. /*
  437. * Get the dquot (locked), and join it to the transaction.
  438. * Allocate the dquot if this doesn't exist.
  439. */
  440. if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
  441. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  442. ASSERT(error != ENOENT);
  443. goto out_unlock;
  444. }
  445. xfs_trans_dqjoin(tp, dqp);
  446. ddq = &dqp->q_core;
  447. /*
  448. * Make sure that hardlimits are >= soft limits before changing.
  449. */
  450. hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
  451. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
  452. be64_to_cpu(ddq->d_blk_hardlimit);
  453. soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
  454. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
  455. be64_to_cpu(ddq->d_blk_softlimit);
  456. if (hard == 0 || hard >= soft) {
  457. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  458. ddq->d_blk_softlimit = cpu_to_be64(soft);
  459. if (id == 0) {
  460. q->qi_bhardlimit = hard;
  461. q->qi_bsoftlimit = soft;
  462. }
  463. } else {
  464. qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
  465. }
  466. hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
  467. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
  468. be64_to_cpu(ddq->d_rtb_hardlimit);
  469. soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
  470. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
  471. be64_to_cpu(ddq->d_rtb_softlimit);
  472. if (hard == 0 || hard >= soft) {
  473. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  474. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  475. if (id == 0) {
  476. q->qi_rtbhardlimit = hard;
  477. q->qi_rtbsoftlimit = soft;
  478. }
  479. } else {
  480. qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
  481. }
  482. hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
  483. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  484. be64_to_cpu(ddq->d_ino_hardlimit);
  485. soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
  486. (xfs_qcnt_t) newlim->d_ino_softlimit :
  487. be64_to_cpu(ddq->d_ino_softlimit);
  488. if (hard == 0 || hard >= soft) {
  489. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  490. ddq->d_ino_softlimit = cpu_to_be64(soft);
  491. if (id == 0) {
  492. q->qi_ihardlimit = hard;
  493. q->qi_isoftlimit = soft;
  494. }
  495. } else {
  496. qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
  497. }
  498. /*
  499. * Update warnings counter(s) if requested
  500. */
  501. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  502. ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
  503. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  504. ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
  505. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  506. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
  507. if (id == 0) {
  508. /*
  509. * Timelimits for the super user set the relative time
  510. * the other users can be over quota for this file system.
  511. * If it is zero a default is used. Ditto for the default
  512. * soft and hard limit values (already done, above), and
  513. * for warnings.
  514. */
  515. if (newlim->d_fieldmask & FS_DQ_BTIMER) {
  516. q->qi_btimelimit = newlim->d_btimer;
  517. ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
  518. }
  519. if (newlim->d_fieldmask & FS_DQ_ITIMER) {
  520. q->qi_itimelimit = newlim->d_itimer;
  521. ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
  522. }
  523. if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
  524. q->qi_rtbtimelimit = newlim->d_rtbtimer;
  525. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
  526. }
  527. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  528. q->qi_bwarnlimit = newlim->d_bwarns;
  529. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  530. q->qi_iwarnlimit = newlim->d_iwarns;
  531. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  532. q->qi_rtbwarnlimit = newlim->d_rtbwarns;
  533. } else {
  534. /*
  535. * If the user is now over quota, start the timelimit.
  536. * The user will not be 'warned'.
  537. * Note that we keep the timers ticking, whether enforcement
  538. * is on or off. We don't really want to bother with iterating
  539. * over all ondisk dquots and turning the timers on/off.
  540. */
  541. xfs_qm_adjust_dqtimers(mp, ddq);
  542. }
  543. dqp->dq_flags |= XFS_DQ_DIRTY;
  544. xfs_trans_log_dquot(tp, dqp);
  545. error = xfs_trans_commit(tp, 0);
  546. xfs_qm_dqprint(dqp);
  547. xfs_qm_dqrele(dqp);
  548. out_unlock:
  549. mutex_unlock(&q->qi_quotaofflock);
  550. return error;
  551. }
  552. int
  553. xfs_qm_scall_getquota(
  554. xfs_mount_t *mp,
  555. xfs_dqid_t id,
  556. uint type,
  557. fs_disk_quota_t *out)
  558. {
  559. xfs_dquot_t *dqp;
  560. int error;
  561. /*
  562. * Try to get the dquot. We don't want it allocated on disk, so
  563. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  564. * exist, we'll get ENOENT back.
  565. */
  566. if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
  567. return (error);
  568. }
  569. /*
  570. * If everything's NULL, this dquot doesn't quite exist as far as
  571. * our utility programs are concerned.
  572. */
  573. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  574. xfs_qm_dqput(dqp);
  575. return XFS_ERROR(ENOENT);
  576. }
  577. /* xfs_qm_dqprint(dqp); */
  578. /*
  579. * Convert the disk dquot to the exportable format
  580. */
  581. xfs_qm_export_dquot(mp, &dqp->q_core, out);
  582. xfs_qm_dqput(dqp);
  583. return (error ? XFS_ERROR(EFAULT) : 0);
  584. }
  585. STATIC int
  586. xfs_qm_log_quotaoff_end(
  587. xfs_mount_t *mp,
  588. xfs_qoff_logitem_t *startqoff,
  589. uint flags)
  590. {
  591. xfs_trans_t *tp;
  592. int error;
  593. xfs_qoff_logitem_t *qoffi;
  594. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  595. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
  596. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  597. xfs_trans_cancel(tp, 0);
  598. return (error);
  599. }
  600. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  601. flags & XFS_ALL_QUOTA_ACCT);
  602. xfs_trans_log_quotaoff_item(tp, qoffi);
  603. /*
  604. * We have to make sure that the transaction is secure on disk before we
  605. * return and actually stop quota accounting. So, make it synchronous.
  606. * We don't care about quotoff's performance.
  607. */
  608. xfs_trans_set_sync(tp);
  609. error = xfs_trans_commit(tp, 0);
  610. return (error);
  611. }
  612. STATIC int
  613. xfs_qm_log_quotaoff(
  614. xfs_mount_t *mp,
  615. xfs_qoff_logitem_t **qoffstartp,
  616. uint flags)
  617. {
  618. xfs_trans_t *tp;
  619. int error;
  620. xfs_qoff_logitem_t *qoffi=NULL;
  621. uint oldsbqflag=0;
  622. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  623. if ((error = xfs_trans_reserve(tp, 0,
  624. sizeof(xfs_qoff_logitem_t) * 2 +
  625. mp->m_sb.sb_sectsize + 128,
  626. 0,
  627. 0,
  628. XFS_DEFAULT_LOG_COUNT))) {
  629. goto error0;
  630. }
  631. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  632. xfs_trans_log_quotaoff_item(tp, qoffi);
  633. spin_lock(&mp->m_sb_lock);
  634. oldsbqflag = mp->m_sb.sb_qflags;
  635. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  636. spin_unlock(&mp->m_sb_lock);
  637. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  638. /*
  639. * We have to make sure that the transaction is secure on disk before we
  640. * return and actually stop quota accounting. So, make it synchronous.
  641. * We don't care about quotoff's performance.
  642. */
  643. xfs_trans_set_sync(tp);
  644. error = xfs_trans_commit(tp, 0);
  645. error0:
  646. if (error) {
  647. xfs_trans_cancel(tp, 0);
  648. /*
  649. * No one else is modifying sb_qflags, so this is OK.
  650. * We still hold the quotaofflock.
  651. */
  652. spin_lock(&mp->m_sb_lock);
  653. mp->m_sb.sb_qflags = oldsbqflag;
  654. spin_unlock(&mp->m_sb_lock);
  655. }
  656. *qoffstartp = qoffi;
  657. return (error);
  658. }
  659. /*
  660. * Translate an internal style on-disk-dquot to the exportable format.
  661. * The main differences are that the counters/limits are all in Basic
  662. * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
  663. * to be converted to the native endianness.
  664. */
  665. STATIC void
  666. xfs_qm_export_dquot(
  667. xfs_mount_t *mp,
  668. xfs_disk_dquot_t *src,
  669. struct fs_disk_quota *dst)
  670. {
  671. memset(dst, 0, sizeof(*dst));
  672. dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
  673. dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
  674. dst->d_id = be32_to_cpu(src->d_id);
  675. dst->d_blk_hardlimit =
  676. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
  677. dst->d_blk_softlimit =
  678. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
  679. dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
  680. dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
  681. dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
  682. dst->d_icount = be64_to_cpu(src->d_icount);
  683. dst->d_btimer = be32_to_cpu(src->d_btimer);
  684. dst->d_itimer = be32_to_cpu(src->d_itimer);
  685. dst->d_iwarns = be16_to_cpu(src->d_iwarns);
  686. dst->d_bwarns = be16_to_cpu(src->d_bwarns);
  687. dst->d_rtb_hardlimit =
  688. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
  689. dst->d_rtb_softlimit =
  690. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
  691. dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
  692. dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
  693. dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
  694. /*
  695. * Internally, we don't reset all the timers when quota enforcement
  696. * gets turned off. No need to confuse the user level code,
  697. * so return zeroes in that case.
  698. */
  699. if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
  700. (!XFS_IS_OQUOTA_ENFORCED(mp) &&
  701. (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
  702. dst->d_btimer = 0;
  703. dst->d_itimer = 0;
  704. dst->d_rtbtimer = 0;
  705. }
  706. #ifdef DEBUG
  707. if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
  708. (XFS_IS_OQUOTA_ENFORCED(mp) &&
  709. (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
  710. dst->d_id != 0) {
  711. if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
  712. (dst->d_blk_softlimit > 0)) {
  713. ASSERT(dst->d_btimer != 0);
  714. }
  715. if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
  716. (dst->d_ino_softlimit > 0)) {
  717. ASSERT(dst->d_itimer != 0);
  718. }
  719. }
  720. #endif
  721. }
  722. STATIC uint
  723. xfs_qm_export_qtype_flags(
  724. uint flags)
  725. {
  726. /*
  727. * Can't be more than one, or none.
  728. */
  729. ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
  730. (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
  731. ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
  732. (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
  733. ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
  734. (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
  735. ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
  736. return (flags & XFS_DQ_USER) ?
  737. XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
  738. XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
  739. }
  740. STATIC uint
  741. xfs_qm_export_flags(
  742. uint flags)
  743. {
  744. uint uflags;
  745. uflags = 0;
  746. if (flags & XFS_UQUOTA_ACCT)
  747. uflags |= XFS_QUOTA_UDQ_ACCT;
  748. if (flags & XFS_PQUOTA_ACCT)
  749. uflags |= XFS_QUOTA_PDQ_ACCT;
  750. if (flags & XFS_GQUOTA_ACCT)
  751. uflags |= XFS_QUOTA_GDQ_ACCT;
  752. if (flags & XFS_UQUOTA_ENFD)
  753. uflags |= XFS_QUOTA_UDQ_ENFD;
  754. if (flags & (XFS_OQUOTA_ENFD)) {
  755. uflags |= (flags & XFS_GQUOTA_ACCT) ?
  756. XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
  757. }
  758. return (uflags);
  759. }
  760. STATIC int
  761. xfs_dqrele_inode(
  762. struct xfs_inode *ip,
  763. struct xfs_perag *pag,
  764. int flags)
  765. {
  766. int error;
  767. /* skip quota inodes */
  768. if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
  769. ip == ip->i_mount->m_quotainfo->qi_gquotaip) {
  770. ASSERT(ip->i_udquot == NULL);
  771. ASSERT(ip->i_gdquot == NULL);
  772. read_unlock(&pag->pag_ici_lock);
  773. return 0;
  774. }
  775. error = xfs_sync_inode_valid(ip, pag);
  776. if (error)
  777. return error;
  778. xfs_ilock(ip, XFS_ILOCK_EXCL);
  779. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  780. xfs_qm_dqrele(ip->i_udquot);
  781. ip->i_udquot = NULL;
  782. }
  783. if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  784. xfs_qm_dqrele(ip->i_gdquot);
  785. ip->i_gdquot = NULL;
  786. }
  787. xfs_iput(ip, XFS_ILOCK_EXCL);
  788. return 0;
  789. }
  790. /*
  791. * Go thru all the inodes in the file system, releasing their dquots.
  792. *
  793. * Note that the mount structure gets modified to indicate that quotas are off
  794. * AFTER this, in the case of quotaoff.
  795. */
  796. void
  797. xfs_qm_dqrele_all_inodes(
  798. struct xfs_mount *mp,
  799. uint flags)
  800. {
  801. ASSERT(mp->m_quotainfo);
  802. xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags,
  803. XFS_ICI_NO_TAG, 0, NULL);
  804. }
  805. /*------------------------------------------------------------------------*/
  806. #ifdef DEBUG
  807. /*
  808. * This contains all the test functions for XFS disk quotas.
  809. * Currently it does a quota accounting check. ie. it walks through
  810. * all inodes in the file system, calculating the dquot accounting fields,
  811. * and prints out any inconsistencies.
  812. */
  813. xfs_dqhash_t *qmtest_udqtab;
  814. xfs_dqhash_t *qmtest_gdqtab;
  815. int qmtest_hashmask;
  816. int qmtest_nfails;
  817. struct mutex qcheck_lock;
  818. #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
  819. (__psunsigned_t)(id)) & \
  820. (qmtest_hashmask - 1))
  821. #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
  822. (qmtest_udqtab + \
  823. DQTEST_HASHVAL(mp, id)) : \
  824. (qmtest_gdqtab + \
  825. DQTEST_HASHVAL(mp, id)))
  826. #define DQTEST_LIST_PRINT(l, NXT, title) \
  827. { \
  828. xfs_dqtest_t *dqp; int i = 0;\
  829. cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
  830. for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
  831. dqp = (xfs_dqtest_t *)dqp->NXT) { \
  832. cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
  833. ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
  834. dqp->d_bcount, dqp->d_icount); } \
  835. }
  836. typedef struct dqtest {
  837. uint dq_flags; /* various flags (XFS_DQ_*) */
  838. struct list_head q_hashlist;
  839. xfs_dqhash_t *q_hash; /* the hashchain header */
  840. xfs_mount_t *q_mount; /* filesystem this relates to */
  841. xfs_dqid_t d_id; /* user id or group id */
  842. xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
  843. xfs_qcnt_t d_icount; /* # inodes owned by the user */
  844. } xfs_dqtest_t;
  845. STATIC void
  846. xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
  847. {
  848. list_add(&dqp->q_hashlist, &h->qh_list);
  849. h->qh_version++;
  850. h->qh_nelems++;
  851. }
  852. STATIC void
  853. xfs_qm_dqtest_print(
  854. xfs_dqtest_t *d)
  855. {
  856. cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
  857. cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
  858. cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
  859. cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
  860. d->d_bcount, (int)d->d_bcount);
  861. cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
  862. d->d_icount, (int)d->d_icount);
  863. cmn_err(CE_DEBUG, "---------------------------");
  864. }
  865. STATIC void
  866. xfs_qm_dqtest_failed(
  867. xfs_dqtest_t *d,
  868. xfs_dquot_t *dqp,
  869. char *reason,
  870. xfs_qcnt_t a,
  871. xfs_qcnt_t b,
  872. int error)
  873. {
  874. qmtest_nfails++;
  875. if (error)
  876. cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
  877. d->d_id, error, reason);
  878. else
  879. cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
  880. d->d_id, reason, (int)a, (int)b);
  881. xfs_qm_dqtest_print(d);
  882. if (dqp)
  883. xfs_qm_dqprint(dqp);
  884. }
  885. STATIC int
  886. xfs_dqtest_cmp2(
  887. xfs_dqtest_t *d,
  888. xfs_dquot_t *dqp)
  889. {
  890. int err = 0;
  891. if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
  892. xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
  893. be64_to_cpu(dqp->q_core.d_icount),
  894. d->d_icount, 0);
  895. err++;
  896. }
  897. if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
  898. xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
  899. be64_to_cpu(dqp->q_core.d_bcount),
  900. d->d_bcount, 0);
  901. err++;
  902. }
  903. if (dqp->q_core.d_blk_softlimit &&
  904. be64_to_cpu(dqp->q_core.d_bcount) >=
  905. be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
  906. if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
  907. cmn_err(CE_DEBUG,
  908. "%d [%s] [0x%p] BLK TIMER NOT STARTED",
  909. d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
  910. err++;
  911. }
  912. }
  913. if (dqp->q_core.d_ino_softlimit &&
  914. be64_to_cpu(dqp->q_core.d_icount) >=
  915. be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
  916. if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
  917. cmn_err(CE_DEBUG,
  918. "%d [%s] [0x%p] INO TIMER NOT STARTED",
  919. d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
  920. err++;
  921. }
  922. }
  923. #ifdef QUOTADEBUG
  924. if (!err) {
  925. cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
  926. d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
  927. }
  928. #endif
  929. return (err);
  930. }
  931. STATIC void
  932. xfs_dqtest_cmp(
  933. xfs_dqtest_t *d)
  934. {
  935. xfs_dquot_t *dqp;
  936. int error;
  937. /* xfs_qm_dqtest_print(d); */
  938. if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
  939. &dqp))) {
  940. xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
  941. return;
  942. }
  943. xfs_dqtest_cmp2(d, dqp);
  944. xfs_qm_dqput(dqp);
  945. }
  946. STATIC int
  947. xfs_qm_internalqcheck_dqget(
  948. xfs_mount_t *mp,
  949. xfs_dqid_t id,
  950. uint type,
  951. xfs_dqtest_t **O_dq)
  952. {
  953. xfs_dqtest_t *d;
  954. xfs_dqhash_t *h;
  955. h = DQTEST_HASH(mp, id, type);
  956. list_for_each_entry(d, &h->qh_list, q_hashlist) {
  957. if (d->d_id == id && mp == d->q_mount) {
  958. *O_dq = d;
  959. return (0);
  960. }
  961. }
  962. d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
  963. d->dq_flags = type;
  964. d->d_id = id;
  965. d->q_mount = mp;
  966. d->q_hash = h;
  967. INIT_LIST_HEAD(&d->q_hashlist);
  968. xfs_qm_hashinsert(h, d);
  969. *O_dq = d;
  970. return (0);
  971. }
  972. STATIC void
  973. xfs_qm_internalqcheck_get_dquots(
  974. xfs_mount_t *mp,
  975. xfs_dqid_t uid,
  976. xfs_dqid_t projid,
  977. xfs_dqid_t gid,
  978. xfs_dqtest_t **ud,
  979. xfs_dqtest_t **gd)
  980. {
  981. if (XFS_IS_UQUOTA_ON(mp))
  982. xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
  983. if (XFS_IS_GQUOTA_ON(mp))
  984. xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
  985. else if (XFS_IS_PQUOTA_ON(mp))
  986. xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
  987. }
  988. STATIC void
  989. xfs_qm_internalqcheck_dqadjust(
  990. xfs_inode_t *ip,
  991. xfs_dqtest_t *d)
  992. {
  993. d->d_icount++;
  994. d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
  995. }
  996. STATIC int
  997. xfs_qm_internalqcheck_adjust(
  998. xfs_mount_t *mp, /* mount point for filesystem */
  999. xfs_ino_t ino, /* inode number to get data for */
  1000. void __user *buffer, /* not used */
  1001. int ubsize, /* not used */
  1002. void *private_data, /* not used */
  1003. xfs_daddr_t bno, /* starting block of inode cluster */
  1004. int *ubused, /* not used */
  1005. void *dip, /* 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, bno))) {
  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. if ((error = xfs_bulkstat(mp, &lastino, &count,
  1090. xfs_qm_internalqcheck_adjust, NULL,
  1091. 0, NULL, BULKSTAT_FG_IGET, &done))) {
  1092. break;
  1093. }
  1094. } while (! done);
  1095. if (error) {
  1096. cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
  1097. }
  1098. cmn_err(CE_DEBUG, "Checking results against system dquots");
  1099. for (i = 0; i < qmtest_hashmask; i++) {
  1100. xfs_dqtest_t *d, *n;
  1101. xfs_dqhash_t *h;
  1102. h = &qmtest_udqtab[i];
  1103. list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) {
  1104. xfs_dqtest_cmp(d);
  1105. kmem_free(d);
  1106. }
  1107. h = &qmtest_gdqtab[i];
  1108. list_for_each_entry_safe(d, n, &h->qh_list, q_hashlist) {
  1109. xfs_dqtest_cmp(d);
  1110. kmem_free(d);
  1111. }
  1112. }
  1113. if (qmtest_nfails) {
  1114. cmn_err(CE_DEBUG, "******** quotacheck failed ********");
  1115. cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
  1116. } else {
  1117. cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
  1118. }
  1119. kmem_free(qmtest_udqtab);
  1120. kmem_free(qmtest_gdqtab);
  1121. mutex_unlock(&qcheck_lock);
  1122. return (qmtest_nfails);
  1123. }
  1124. #endif /* DEBUG */