xfs_qm_syscalls.c 34 KB

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