xfs_qm_syscalls.c 35 KB

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