xfs_qm_syscalls.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /*
  2. * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_fs.h"
  34. #include "xfs_inum.h"
  35. #include "xfs_log.h"
  36. #include "xfs_trans.h"
  37. #include "xfs_sb.h"
  38. #include "xfs_dir.h"
  39. #include "xfs_dir2.h"
  40. #include "xfs_alloc.h"
  41. #include "xfs_dmapi.h"
  42. #include "xfs_quota.h"
  43. #include "xfs_mount.h"
  44. #include "xfs_alloc_btree.h"
  45. #include "xfs_bmap_btree.h"
  46. #include "xfs_ialloc_btree.h"
  47. #include "xfs_btree.h"
  48. #include "xfs_ialloc.h"
  49. #include "xfs_attr_sf.h"
  50. #include "xfs_dir_sf.h"
  51. #include "xfs_dir2_sf.h"
  52. #include "xfs_dinode.h"
  53. #include "xfs_inode.h"
  54. #include "xfs_bmap.h"
  55. #include "xfs_bit.h"
  56. #include "xfs_rtalloc.h"
  57. #include "xfs_error.h"
  58. #include "xfs_itable.h"
  59. #include "xfs_rw.h"
  60. #include "xfs_acl.h"
  61. #include "xfs_cap.h"
  62. #include "xfs_mac.h"
  63. #include "xfs_attr.h"
  64. #include "xfs_buf_item.h"
  65. #include "xfs_utils.h"
  66. #include "xfs_qm.h"
  67. #ifdef DEBUG
  68. # define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
  69. #else
  70. # define qdprintk(s, args...) do { } while (0)
  71. #endif
  72. STATIC int xfs_qm_scall_trunc_qfiles(xfs_mount_t *, uint);
  73. STATIC int xfs_qm_scall_getquota(xfs_mount_t *, xfs_dqid_t, uint,
  74. fs_disk_quota_t *);
  75. STATIC int xfs_qm_scall_getqstat(xfs_mount_t *, fs_quota_stat_t *);
  76. STATIC int xfs_qm_scall_setqlim(xfs_mount_t *, xfs_dqid_t, uint,
  77. fs_disk_quota_t *);
  78. STATIC int xfs_qm_scall_quotaon(xfs_mount_t *, uint);
  79. STATIC int xfs_qm_scall_quotaoff(xfs_mount_t *, uint, boolean_t);
  80. STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
  81. STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
  82. uint);
  83. STATIC uint xfs_qm_import_flags(uint);
  84. STATIC uint xfs_qm_export_flags(uint);
  85. STATIC uint xfs_qm_import_qtype_flags(uint);
  86. STATIC uint xfs_qm_export_qtype_flags(uint);
  87. STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
  88. fs_disk_quota_t *);
  89. /*
  90. * The main distribution switch of all XFS quotactl system calls.
  91. */
  92. int
  93. xfs_qm_quotactl(
  94. struct bhv_desc *bdp,
  95. int cmd,
  96. int id,
  97. xfs_caddr_t addr)
  98. {
  99. xfs_mount_t *mp;
  100. int error;
  101. struct vfs *vfsp;
  102. vfsp = bhvtovfs(bdp);
  103. mp = XFS_VFSTOM(vfsp);
  104. if (addr == NULL && cmd != Q_SYNC)
  105. return XFS_ERROR(EINVAL);
  106. if (id < 0 && cmd != Q_SYNC)
  107. return XFS_ERROR(EINVAL);
  108. /*
  109. * The following commands are valid even when quotaoff.
  110. */
  111. switch (cmd) {
  112. /*
  113. * truncate quota files. quota must be off.
  114. */
  115. case Q_XQUOTARM:
  116. if (XFS_IS_QUOTA_ON(mp) || addr == NULL)
  117. return XFS_ERROR(EINVAL);
  118. if (vfsp->vfs_flag & VFS_RDONLY)
  119. return XFS_ERROR(EROFS);
  120. return (xfs_qm_scall_trunc_qfiles(mp,
  121. xfs_qm_import_qtype_flags(*(uint *)addr)));
  122. /*
  123. * Get quota status information.
  124. */
  125. case Q_XGETQSTAT:
  126. return (xfs_qm_scall_getqstat(mp, (fs_quota_stat_t *)addr));
  127. /*
  128. * QUOTAON for root f/s and quota enforcement on others..
  129. * Quota accounting for non-root f/s's must be turned on
  130. * at mount time.
  131. */
  132. case Q_XQUOTAON:
  133. if (addr == NULL)
  134. return XFS_ERROR(EINVAL);
  135. if (vfsp->vfs_flag & VFS_RDONLY)
  136. return XFS_ERROR(EROFS);
  137. return (xfs_qm_scall_quotaon(mp,
  138. xfs_qm_import_flags(*(uint *)addr)));
  139. case Q_XQUOTAOFF:
  140. if (vfsp->vfs_flag & VFS_RDONLY)
  141. return XFS_ERROR(EROFS);
  142. break;
  143. default:
  144. break;
  145. }
  146. if (! XFS_IS_QUOTA_ON(mp))
  147. return XFS_ERROR(ESRCH);
  148. switch (cmd) {
  149. case Q_XQUOTAOFF:
  150. if (vfsp->vfs_flag & VFS_RDONLY)
  151. return XFS_ERROR(EROFS);
  152. error = xfs_qm_scall_quotaoff(mp,
  153. xfs_qm_import_flags(*(uint *)addr),
  154. B_FALSE);
  155. break;
  156. /*
  157. * Defaults to XFS_GETUQUOTA.
  158. */
  159. case Q_XGETQUOTA:
  160. error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_USER,
  161. (fs_disk_quota_t *)addr);
  162. break;
  163. /*
  164. * Set limits, both hard and soft. Defaults to Q_SETUQLIM.
  165. */
  166. case Q_XSETQLIM:
  167. if (vfsp->vfs_flag & VFS_RDONLY)
  168. return XFS_ERROR(EROFS);
  169. error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_USER,
  170. (fs_disk_quota_t *)addr);
  171. break;
  172. case Q_XSETGQLIM:
  173. if (vfsp->vfs_flag & VFS_RDONLY)
  174. return XFS_ERROR(EROFS);
  175. error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
  176. (fs_disk_quota_t *)addr);
  177. break;
  178. case Q_XGETGQUOTA:
  179. error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
  180. (fs_disk_quota_t *)addr);
  181. break;
  182. /*
  183. * Quotas are entirely undefined after quotaoff in XFS quotas.
  184. * For instance, there's no way to set limits when quotaoff.
  185. */
  186. default:
  187. error = XFS_ERROR(EINVAL);
  188. break;
  189. }
  190. return (error);
  191. }
  192. /*
  193. * Turn off quota accounting and/or enforcement for all udquots and/or
  194. * gdquots. Called only at unmount time.
  195. *
  196. * This assumes that there are no dquots of this file system cached
  197. * incore, and modifies the ondisk dquot directly. Therefore, for example,
  198. * it is an error to call this twice, without purging the cache.
  199. */
  200. STATIC int
  201. xfs_qm_scall_quotaoff(
  202. xfs_mount_t *mp,
  203. uint flags,
  204. boolean_t force)
  205. {
  206. uint dqtype;
  207. unsigned long s;
  208. int error;
  209. uint inactivate_flags;
  210. xfs_qoff_logitem_t *qoffstart;
  211. int nculprits;
  212. if (!force && !capable(CAP_SYS_ADMIN))
  213. return XFS_ERROR(EPERM);
  214. /*
  215. * No file system can have quotas enabled on disk but not in core.
  216. * Note that quota utilities (like quotaoff) _expect_
  217. * errno == EEXIST here.
  218. */
  219. if ((mp->m_qflags & flags) == 0)
  220. return XFS_ERROR(EEXIST);
  221. error = 0;
  222. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  223. /*
  224. * We don't want to deal with two quotaoffs messing up each other,
  225. * so we're going to serialize it. quotaoff isn't exactly a performance
  226. * critical thing.
  227. * If quotaoff, then we must be dealing with the root filesystem.
  228. */
  229. ASSERT(mp->m_quotainfo);
  230. if (mp->m_quotainfo)
  231. mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
  232. ASSERT(mp->m_quotainfo);
  233. /*
  234. * If we're just turning off quota enforcement, change mp and go.
  235. */
  236. if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
  237. mp->m_qflags &= ~(flags);
  238. s = XFS_SB_LOCK(mp);
  239. mp->m_sb.sb_qflags = mp->m_qflags;
  240. XFS_SB_UNLOCK(mp, s);
  241. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  242. /* XXX what to do if error ? Revert back to old vals incore ? */
  243. error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
  244. return (error);
  245. }
  246. dqtype = 0;
  247. inactivate_flags = 0;
  248. /*
  249. * If accounting is off, we must turn enforcement off, clear the
  250. * quota 'CHKD' certificate to make it known that we have to
  251. * do a quotacheck the next time this quota is turned on.
  252. */
  253. if (flags & XFS_UQUOTA_ACCT) {
  254. dqtype |= XFS_QMOPT_UQUOTA;
  255. flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
  256. inactivate_flags |= XFS_UQUOTA_ACTIVE;
  257. }
  258. if (flags & XFS_GQUOTA_ACCT) {
  259. dqtype |= XFS_QMOPT_GQUOTA;
  260. flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD);
  261. inactivate_flags |= XFS_GQUOTA_ACTIVE;
  262. }
  263. /*
  264. * Nothing to do? Don't complain. This happens when we're just
  265. * turning off quota enforcement.
  266. */
  267. if ((mp->m_qflags & flags) == 0) {
  268. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  269. return (0);
  270. }
  271. /*
  272. * Write the LI_QUOTAOFF log record, and do SB changes atomically,
  273. * and synchronously.
  274. */
  275. xfs_qm_log_quotaoff(mp, &qoffstart, flags);
  276. /*
  277. * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
  278. * to take care of the race between dqget and quotaoff. We don't take
  279. * any special locks to reset these bits. All processes need to check
  280. * these bits *after* taking inode lock(s) to see if the particular
  281. * quota type is in the process of being turned off. If *ACTIVE, it is
  282. * guaranteed that all dquot structures and all quotainode ptrs will all
  283. * stay valid as long as that inode is kept locked.
  284. *
  285. * There is no turning back after this.
  286. */
  287. mp->m_qflags &= ~inactivate_flags;
  288. /*
  289. * Give back all the dquot reference(s) held by inodes.
  290. * Here we go thru every single incore inode in this file system, and
  291. * do a dqrele on the i_udquot/i_gdquot that it may have.
  292. * Essentially, as long as somebody has an inode locked, this guarantees
  293. * that quotas will not be turned off. This is handy because in a
  294. * transaction once we lock the inode(s) and check for quotaon, we can
  295. * depend on the quota inodes (and other things) being valid as long as
  296. * we keep the lock(s).
  297. */
  298. xfs_qm_dqrele_all_inodes(mp, flags);
  299. /*
  300. * Next we make the changes in the quota flag in the mount struct.
  301. * This isn't protected by a particular lock directly, because we
  302. * don't want to take a mrlock everytime we depend on quotas being on.
  303. */
  304. mp->m_qflags &= ~(flags);
  305. /*
  306. * Go through all the dquots of this file system and purge them,
  307. * according to what was turned off. We may not be able to get rid
  308. * of all dquots, because dquots can have temporary references that
  309. * are not attached to inodes. eg. xfs_setattr, xfs_create.
  310. * So, if we couldn't purge all the dquots from the filesystem,
  311. * we can't get rid of the incore data structures.
  312. */
  313. while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
  314. delay(10 * nculprits);
  315. /*
  316. * Transactions that had started before ACTIVE state bit was cleared
  317. * could have logged many dquots, so they'd have higher LSNs than
  318. * the first QUOTAOFF log record does. If we happen to crash when
  319. * the tail of the log has gone past the QUOTAOFF record, but
  320. * before the last dquot modification, those dquots __will__
  321. * recover, and that's not good.
  322. *
  323. * So, we have QUOTAOFF start and end logitems; the start
  324. * logitem won't get overwritten until the end logitem appears...
  325. */
  326. xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
  327. /*
  328. * If quotas is completely disabled, close shop.
  329. */
  330. if ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_ALL) {
  331. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  332. xfs_qm_destroy_quotainfo(mp);
  333. return (0);
  334. }
  335. /*
  336. * Release our quotainode references, and vn_purge them,
  337. * if we don't need them anymore.
  338. */
  339. if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
  340. XFS_PURGE_INODE(XFS_QI_UQIP(mp));
  341. XFS_QI_UQIP(mp) = NULL;
  342. }
  343. if ((dqtype & XFS_QMOPT_GQUOTA) && XFS_QI_GQIP(mp)) {
  344. XFS_PURGE_INODE(XFS_QI_GQIP(mp));
  345. XFS_QI_GQIP(mp) = NULL;
  346. }
  347. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  348. return (error);
  349. }
  350. STATIC int
  351. xfs_qm_scall_trunc_qfiles(
  352. xfs_mount_t *mp,
  353. uint flags)
  354. {
  355. int error;
  356. xfs_inode_t *qip;
  357. if (!capable(CAP_SYS_ADMIN))
  358. return XFS_ERROR(EPERM);
  359. error = 0;
  360. if (!XFS_SB_VERSION_HASQUOTA(&mp->m_sb) || flags == 0) {
  361. qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
  362. return XFS_ERROR(EINVAL);
  363. }
  364. if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
  365. error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
  366. if (! error) {
  367. (void) xfs_truncate_file(mp, qip);
  368. VN_RELE(XFS_ITOV(qip));
  369. }
  370. }
  371. if ((flags & XFS_DQ_GROUP) && mp->m_sb.sb_gquotino != NULLFSINO) {
  372. error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
  373. if (! error) {
  374. (void) xfs_truncate_file(mp, qip);
  375. VN_RELE(XFS_ITOV(qip));
  376. }
  377. }
  378. return (error);
  379. }
  380. /*
  381. * Switch on (a given) quota enforcement for a filesystem. This takes
  382. * effect immediately.
  383. * (Switching on quota accounting must be done at mount time.)
  384. */
  385. STATIC int
  386. xfs_qm_scall_quotaon(
  387. xfs_mount_t *mp,
  388. uint flags)
  389. {
  390. int error;
  391. unsigned long s;
  392. uint qf;
  393. uint accflags;
  394. __int64_t sbflags;
  395. if (!capable(CAP_SYS_ADMIN))
  396. return XFS_ERROR(EPERM);
  397. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  398. /*
  399. * Switching on quota accounting must be done at mount time.
  400. */
  401. accflags = flags & XFS_ALL_QUOTA_ACCT;
  402. flags &= ~(XFS_ALL_QUOTA_ACCT);
  403. sbflags = 0;
  404. if (flags == 0) {
  405. qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
  406. return XFS_ERROR(EINVAL);
  407. }
  408. /* No fs can turn on quotas with a delayed effect */
  409. ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
  410. /*
  411. * Can't enforce without accounting. We check the superblock
  412. * qflags here instead of m_qflags because rootfs can have
  413. * quota acct on ondisk without m_qflags' knowing.
  414. */
  415. if (((flags & XFS_UQUOTA_ACCT) == 0 &&
  416. (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
  417. (flags & XFS_UQUOTA_ENFD))
  418. ||
  419. ((flags & XFS_GQUOTA_ACCT) == 0 &&
  420. (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
  421. (flags & XFS_GQUOTA_ENFD))) {
  422. qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
  423. flags, mp->m_sb.sb_qflags);
  424. return XFS_ERROR(EINVAL);
  425. }
  426. /*
  427. * If everything's upto-date incore, then don't waste time.
  428. */
  429. if ((mp->m_qflags & flags) == flags)
  430. return XFS_ERROR(EEXIST);
  431. /*
  432. * Change sb_qflags on disk but not incore mp->qflags
  433. * if this is the root filesystem.
  434. */
  435. s = XFS_SB_LOCK(mp);
  436. qf = mp->m_sb.sb_qflags;
  437. mp->m_sb.sb_qflags = qf | flags;
  438. XFS_SB_UNLOCK(mp, s);
  439. /*
  440. * There's nothing to change if it's the same.
  441. */
  442. if ((qf & flags) == flags && sbflags == 0)
  443. return XFS_ERROR(EEXIST);
  444. sbflags |= XFS_SB_QFLAGS;
  445. if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
  446. return (error);
  447. /*
  448. * If we aren't trying to switch on quota enforcement, we are done.
  449. */
  450. if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
  451. (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
  452. (flags & XFS_ALL_QUOTA_ENFD) == 0)
  453. return (0);
  454. if (! XFS_IS_QUOTA_RUNNING(mp))
  455. return XFS_ERROR(ESRCH);
  456. /*
  457. * Switch on quota enforcement in core.
  458. */
  459. mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
  460. mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
  461. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  462. return (0);
  463. }
  464. /*
  465. * Return quota status information, such as uquota-off, enforcements, etc.
  466. */
  467. STATIC int
  468. xfs_qm_scall_getqstat(
  469. xfs_mount_t *mp,
  470. fs_quota_stat_t *out)
  471. {
  472. xfs_inode_t *uip, *gip;
  473. boolean_t tempuqip, tempgqip;
  474. uip = gip = NULL;
  475. tempuqip = tempgqip = B_FALSE;
  476. memset(out, 0, sizeof(fs_quota_stat_t));
  477. out->qs_version = FS_QSTAT_VERSION;
  478. if (! XFS_SB_VERSION_HASQUOTA(&mp->m_sb)) {
  479. out->qs_uquota.qfs_ino = NULLFSINO;
  480. out->qs_gquota.qfs_ino = NULLFSINO;
  481. return (0);
  482. }
  483. out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
  484. (XFS_ALL_QUOTA_ACCT|
  485. XFS_ALL_QUOTA_ENFD));
  486. out->qs_pad = 0;
  487. out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
  488. out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
  489. if (mp->m_quotainfo) {
  490. uip = mp->m_quotainfo->qi_uquotaip;
  491. gip = mp->m_quotainfo->qi_gquotaip;
  492. }
  493. if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
  494. if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  495. 0, 0, &uip, 0) == 0)
  496. tempuqip = B_TRUE;
  497. }
  498. if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
  499. if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  500. 0, 0, &gip, 0) == 0)
  501. tempgqip = B_TRUE;
  502. }
  503. if (uip) {
  504. out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
  505. out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
  506. if (tempuqip)
  507. VN_RELE(XFS_ITOV(uip));
  508. }
  509. if (gip) {
  510. out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
  511. out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
  512. if (tempgqip)
  513. VN_RELE(XFS_ITOV(gip));
  514. }
  515. if (mp->m_quotainfo) {
  516. out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
  517. out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
  518. out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
  519. out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
  520. out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
  521. out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
  522. }
  523. return (0);
  524. }
  525. /*
  526. * Adjust quota limits, and start/stop timers accordingly.
  527. */
  528. STATIC int
  529. xfs_qm_scall_setqlim(
  530. xfs_mount_t *mp,
  531. xfs_dqid_t id,
  532. uint type,
  533. fs_disk_quota_t *newlim)
  534. {
  535. xfs_disk_dquot_t *ddq;
  536. xfs_dquot_t *dqp;
  537. xfs_trans_t *tp;
  538. int error;
  539. xfs_qcnt_t hard, soft;
  540. if (!capable(CAP_SYS_ADMIN))
  541. return XFS_ERROR(EPERM);
  542. if ((newlim->d_fieldmask & (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK)) == 0)
  543. return (0);
  544. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
  545. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
  546. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  547. xfs_trans_cancel(tp, 0);
  548. return (error);
  549. }
  550. /*
  551. * We don't want to race with a quotaoff so take the quotaoff lock.
  552. * (We don't hold an inode lock, so there's nothing else to stop
  553. * a quotaoff from happening). (XXXThis doesn't currently happen
  554. * because we take the vfslock before calling xfs_qm_sysent).
  555. */
  556. mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
  557. /*
  558. * Get the dquot (locked), and join it to the transaction.
  559. * Allocate the dquot if this doesn't exist.
  560. */
  561. if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
  562. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  563. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  564. ASSERT(error != ENOENT);
  565. return (error);
  566. }
  567. xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
  568. xfs_trans_dqjoin(tp, dqp);
  569. ddq = &dqp->q_core;
  570. /*
  571. * Make sure that hardlimits are >= soft limits before changing.
  572. */
  573. hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
  574. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
  575. INT_GET(ddq->d_blk_hardlimit, ARCH_CONVERT);
  576. soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
  577. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
  578. INT_GET(ddq->d_blk_softlimit, ARCH_CONVERT);
  579. if (hard == 0 || hard >= soft) {
  580. INT_SET(ddq->d_blk_hardlimit, ARCH_CONVERT, hard);
  581. INT_SET(ddq->d_blk_softlimit, ARCH_CONVERT, soft);
  582. if (id == 0) {
  583. mp->m_quotainfo->qi_bhardlimit = hard;
  584. mp->m_quotainfo->qi_bsoftlimit = soft;
  585. }
  586. } else {
  587. qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
  588. }
  589. hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
  590. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
  591. INT_GET(ddq->d_rtb_hardlimit, ARCH_CONVERT);
  592. soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
  593. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
  594. INT_GET(ddq->d_rtb_softlimit, ARCH_CONVERT);
  595. if (hard == 0 || hard >= soft) {
  596. INT_SET(ddq->d_rtb_hardlimit, ARCH_CONVERT, hard);
  597. INT_SET(ddq->d_rtb_softlimit, ARCH_CONVERT, soft);
  598. if (id == 0) {
  599. mp->m_quotainfo->qi_rtbhardlimit = hard;
  600. mp->m_quotainfo->qi_rtbsoftlimit = soft;
  601. }
  602. } else {
  603. qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
  604. }
  605. hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
  606. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  607. INT_GET(ddq->d_ino_hardlimit, ARCH_CONVERT);
  608. soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
  609. (xfs_qcnt_t) newlim->d_ino_softlimit :
  610. INT_GET(ddq->d_ino_softlimit, ARCH_CONVERT);
  611. if (hard == 0 || hard >= soft) {
  612. INT_SET(ddq->d_ino_hardlimit, ARCH_CONVERT, hard);
  613. INT_SET(ddq->d_ino_softlimit, ARCH_CONVERT, soft);
  614. if (id == 0) {
  615. mp->m_quotainfo->qi_ihardlimit = hard;
  616. mp->m_quotainfo->qi_isoftlimit = soft;
  617. }
  618. } else {
  619. qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
  620. }
  621. if (id == 0) {
  622. /*
  623. * Timelimits for the super user set the relative time
  624. * the other users can be over quota for this file system.
  625. * If it is zero a default is used. Ditto for the default
  626. * soft and hard limit values (already done, above).
  627. */
  628. if (newlim->d_fieldmask & FS_DQ_BTIMER) {
  629. mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
  630. INT_SET(ddq->d_btimer, ARCH_CONVERT, newlim->d_btimer);
  631. }
  632. if (newlim->d_fieldmask & FS_DQ_ITIMER) {
  633. mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
  634. INT_SET(ddq->d_itimer, ARCH_CONVERT, newlim->d_itimer);
  635. }
  636. if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
  637. mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
  638. INT_SET(ddq->d_rtbtimer, ARCH_CONVERT, newlim->d_rtbtimer);
  639. }
  640. } else /* if (XFS_IS_QUOTA_ENFORCED(mp)) */ {
  641. /*
  642. * If the user is now over quota, start the timelimit.
  643. * The user will not be 'warned'.
  644. * Note that we keep the timers ticking, whether enforcement
  645. * is on or off. We don't really want to bother with iterating
  646. * over all ondisk dquots and turning the timers on/off.
  647. */
  648. xfs_qm_adjust_dqtimers(mp, ddq);
  649. }
  650. dqp->dq_flags |= XFS_DQ_DIRTY;
  651. xfs_trans_log_dquot(tp, dqp);
  652. xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
  653. xfs_trans_commit(tp, 0, NULL);
  654. xfs_qm_dqprint(dqp);
  655. xfs_qm_dqrele(dqp);
  656. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  657. return (0);
  658. }
  659. STATIC int
  660. xfs_qm_scall_getquota(
  661. xfs_mount_t *mp,
  662. xfs_dqid_t id,
  663. uint type,
  664. fs_disk_quota_t *out)
  665. {
  666. xfs_dquot_t *dqp;
  667. int error;
  668. /*
  669. * Try to get the dquot. We don't want it allocated on disk, so
  670. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  671. * exist, we'll get ENOENT back.
  672. */
  673. if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
  674. return (error);
  675. }
  676. xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
  677. /*
  678. * If everything's NULL, this dquot doesn't quite exist as far as
  679. * our utility programs are concerned.
  680. */
  681. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  682. xfs_qm_dqput(dqp);
  683. return XFS_ERROR(ENOENT);
  684. }
  685. /* xfs_qm_dqprint(dqp); */
  686. /*
  687. * Convert the disk dquot to the exportable format
  688. */
  689. xfs_qm_export_dquot(mp, &dqp->q_core, out);
  690. xfs_qm_dqput(dqp);
  691. return (error ? XFS_ERROR(EFAULT) : 0);
  692. }
  693. STATIC int
  694. xfs_qm_log_quotaoff_end(
  695. xfs_mount_t *mp,
  696. xfs_qoff_logitem_t *startqoff,
  697. uint flags)
  698. {
  699. xfs_trans_t *tp;
  700. int error;
  701. xfs_qoff_logitem_t *qoffi;
  702. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  703. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
  704. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  705. xfs_trans_cancel(tp, 0);
  706. return (error);
  707. }
  708. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  709. flags & XFS_ALL_QUOTA_ACCT);
  710. xfs_trans_log_quotaoff_item(tp, qoffi);
  711. /*
  712. * We have to make sure that the transaction is secure on disk before we
  713. * return and actually stop quota accounting. So, make it synchronous.
  714. * We don't care about quotoff's performance.
  715. */
  716. xfs_trans_set_sync(tp);
  717. error = xfs_trans_commit(tp, 0, NULL);
  718. return (error);
  719. }
  720. STATIC int
  721. xfs_qm_log_quotaoff(
  722. xfs_mount_t *mp,
  723. xfs_qoff_logitem_t **qoffstartp,
  724. uint flags)
  725. {
  726. xfs_trans_t *tp;
  727. int error;
  728. unsigned long s;
  729. xfs_qoff_logitem_t *qoffi=NULL;
  730. uint oldsbqflag=0;
  731. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  732. if ((error = xfs_trans_reserve(tp, 0,
  733. sizeof(xfs_qoff_logitem_t) * 2 +
  734. mp->m_sb.sb_sectsize + 128,
  735. 0,
  736. 0,
  737. XFS_DEFAULT_LOG_COUNT))) {
  738. goto error0;
  739. }
  740. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  741. xfs_trans_log_quotaoff_item(tp, qoffi);
  742. s = XFS_SB_LOCK(mp);
  743. oldsbqflag = mp->m_sb.sb_qflags;
  744. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  745. XFS_SB_UNLOCK(mp, s);
  746. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  747. /*
  748. * We have to make sure that the transaction is secure on disk before we
  749. * return and actually stop quota accounting. So, make it synchronous.
  750. * We don't care about quotoff's performance.
  751. */
  752. xfs_trans_set_sync(tp);
  753. error = xfs_trans_commit(tp, 0, NULL);
  754. error0:
  755. if (error) {
  756. xfs_trans_cancel(tp, 0);
  757. /*
  758. * No one else is modifying sb_qflags, so this is OK.
  759. * We still hold the quotaofflock.
  760. */
  761. s = XFS_SB_LOCK(mp);
  762. mp->m_sb.sb_qflags = oldsbqflag;
  763. XFS_SB_UNLOCK(mp, s);
  764. }
  765. *qoffstartp = qoffi;
  766. return (error);
  767. }
  768. /*
  769. * Translate an internal style on-disk-dquot to the exportable format.
  770. * The main differences are that the counters/limits are all in Basic
  771. * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
  772. * to be converted to the native endianness.
  773. */
  774. STATIC void
  775. xfs_qm_export_dquot(
  776. xfs_mount_t *mp,
  777. xfs_disk_dquot_t *src,
  778. struct fs_disk_quota *dst)
  779. {
  780. memset(dst, 0, sizeof(*dst));
  781. dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
  782. dst->d_flags =
  783. xfs_qm_export_qtype_flags(INT_GET(src->d_flags, ARCH_CONVERT));
  784. dst->d_id = INT_GET(src->d_id, ARCH_CONVERT);
  785. dst->d_blk_hardlimit = (__uint64_t)
  786. XFS_FSB_TO_BB(mp, INT_GET(src->d_blk_hardlimit, ARCH_CONVERT));
  787. dst->d_blk_softlimit = (__uint64_t)
  788. XFS_FSB_TO_BB(mp, INT_GET(src->d_blk_softlimit, ARCH_CONVERT));
  789. dst->d_ino_hardlimit = (__uint64_t)
  790. INT_GET(src->d_ino_hardlimit, ARCH_CONVERT);
  791. dst->d_ino_softlimit = (__uint64_t)
  792. INT_GET(src->d_ino_softlimit, ARCH_CONVERT);
  793. dst->d_bcount = (__uint64_t)
  794. XFS_FSB_TO_BB(mp, INT_GET(src->d_bcount, ARCH_CONVERT));
  795. dst->d_icount = (__uint64_t) INT_GET(src->d_icount, ARCH_CONVERT);
  796. dst->d_btimer = (__uint32_t) INT_GET(src->d_btimer, ARCH_CONVERT);
  797. dst->d_itimer = (__uint32_t) INT_GET(src->d_itimer, ARCH_CONVERT);
  798. dst->d_iwarns = INT_GET(src->d_iwarns, ARCH_CONVERT);
  799. dst->d_bwarns = INT_GET(src->d_bwarns, ARCH_CONVERT);
  800. dst->d_rtb_hardlimit = (__uint64_t)
  801. XFS_FSB_TO_BB(mp, INT_GET(src->d_rtb_hardlimit, ARCH_CONVERT));
  802. dst->d_rtb_softlimit = (__uint64_t)
  803. XFS_FSB_TO_BB(mp, INT_GET(src->d_rtb_softlimit, ARCH_CONVERT));
  804. dst->d_rtbcount = (__uint64_t)
  805. XFS_FSB_TO_BB(mp, INT_GET(src->d_rtbcount, ARCH_CONVERT));
  806. dst->d_rtbtimer = (__uint32_t) INT_GET(src->d_rtbtimer, ARCH_CONVERT);
  807. dst->d_rtbwarns = INT_GET(src->d_rtbwarns, ARCH_CONVERT);
  808. /*
  809. * Internally, we don't reset all the timers when quota enforcement
  810. * gets turned off. No need to confuse the userlevel code,
  811. * so return zeroes in that case.
  812. */
  813. if (! XFS_IS_QUOTA_ENFORCED(mp)) {
  814. dst->d_btimer = 0;
  815. dst->d_itimer = 0;
  816. dst->d_rtbtimer = 0;
  817. }
  818. #ifdef DEBUG
  819. if (XFS_IS_QUOTA_ENFORCED(mp) && dst->d_id != 0) {
  820. if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
  821. (dst->d_blk_softlimit > 0)) {
  822. ASSERT(dst->d_btimer != 0);
  823. }
  824. if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
  825. (dst->d_ino_softlimit > 0)) {
  826. ASSERT(dst->d_itimer != 0);
  827. }
  828. }
  829. #endif
  830. }
  831. STATIC uint
  832. xfs_qm_import_qtype_flags(
  833. uint uflags)
  834. {
  835. /*
  836. * Can't be both at the same time.
  837. */
  838. if (((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ==
  839. (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ||
  840. ((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) == 0))
  841. return (0);
  842. return (uflags & XFS_USER_QUOTA) ?
  843. XFS_DQ_USER : XFS_DQ_GROUP;
  844. }
  845. STATIC uint
  846. xfs_qm_export_qtype_flags(
  847. uint flags)
  848. {
  849. /*
  850. * Can't be both at the same time.
  851. */
  852. ASSERT((flags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) !=
  853. (XFS_GROUP_QUOTA | XFS_USER_QUOTA));
  854. ASSERT((flags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) != 0);
  855. return (flags & XFS_DQ_USER) ?
  856. XFS_USER_QUOTA : XFS_GROUP_QUOTA;
  857. }
  858. STATIC uint
  859. xfs_qm_import_flags(
  860. uint uflags)
  861. {
  862. uint flags = 0;
  863. if (uflags & XFS_QUOTA_UDQ_ACCT)
  864. flags |= XFS_UQUOTA_ACCT;
  865. if (uflags & XFS_QUOTA_GDQ_ACCT)
  866. flags |= XFS_GQUOTA_ACCT;
  867. if (uflags & XFS_QUOTA_UDQ_ENFD)
  868. flags |= XFS_UQUOTA_ENFD;
  869. if (uflags & XFS_QUOTA_GDQ_ENFD)
  870. flags |= XFS_GQUOTA_ENFD;
  871. return (flags);
  872. }
  873. STATIC uint
  874. xfs_qm_export_flags(
  875. uint flags)
  876. {
  877. uint uflags;
  878. uflags = 0;
  879. if (flags & XFS_UQUOTA_ACCT)
  880. uflags |= XFS_QUOTA_UDQ_ACCT;
  881. if (flags & XFS_GQUOTA_ACCT)
  882. uflags |= XFS_QUOTA_GDQ_ACCT;
  883. if (flags & XFS_UQUOTA_ENFD)
  884. uflags |= XFS_QUOTA_UDQ_ENFD;
  885. if (flags & XFS_GQUOTA_ENFD)
  886. uflags |= XFS_QUOTA_GDQ_ENFD;
  887. return (uflags);
  888. }
  889. /*
  890. * Go thru all the inodes in the file system, releasing their dquots.
  891. * Note that the mount structure gets modified to indicate that quotas are off
  892. * AFTER this, in the case of quotaoff. This also gets called from
  893. * xfs_rootumount.
  894. */
  895. void
  896. xfs_qm_dqrele_all_inodes(
  897. struct xfs_mount *mp,
  898. uint flags)
  899. {
  900. vmap_t vmap;
  901. xfs_inode_t *ip, *topino;
  902. uint ireclaims;
  903. vnode_t *vp;
  904. boolean_t vnode_refd;
  905. ASSERT(mp->m_quotainfo);
  906. again:
  907. XFS_MOUNT_ILOCK(mp);
  908. ip = mp->m_inodes;
  909. if (ip == NULL) {
  910. XFS_MOUNT_IUNLOCK(mp);
  911. return;
  912. }
  913. do {
  914. /* Skip markers inserted by xfs_sync */
  915. if (ip->i_mount == NULL) {
  916. ip = ip->i_mnext;
  917. continue;
  918. }
  919. /* Root inode, rbmip and rsumip have associated blocks */
  920. if (ip == XFS_QI_UQIP(mp) || ip == XFS_QI_GQIP(mp)) {
  921. ASSERT(ip->i_udquot == NULL);
  922. ASSERT(ip->i_gdquot == NULL);
  923. ip = ip->i_mnext;
  924. continue;
  925. }
  926. vp = XFS_ITOV_NULL(ip);
  927. if (!vp) {
  928. ASSERT(ip->i_udquot == NULL);
  929. ASSERT(ip->i_gdquot == NULL);
  930. ip = ip->i_mnext;
  931. continue;
  932. }
  933. vnode_refd = B_FALSE;
  934. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
  935. /*
  936. * Sample vp mapping while holding the mplock, lest
  937. * we come across a non-existent vnode.
  938. */
  939. VMAP(vp, vmap);
  940. ireclaims = mp->m_ireclaims;
  941. topino = mp->m_inodes;
  942. XFS_MOUNT_IUNLOCK(mp);
  943. /* XXX restart limit ? */
  944. if ( ! (vp = vn_get(vp, &vmap)))
  945. goto again;
  946. xfs_ilock(ip, XFS_ILOCK_EXCL);
  947. vnode_refd = B_TRUE;
  948. } else {
  949. ireclaims = mp->m_ireclaims;
  950. topino = mp->m_inodes;
  951. XFS_MOUNT_IUNLOCK(mp);
  952. }
  953. /*
  954. * We don't keep the mountlock across the dqrele() call,
  955. * since it can take a while..
  956. */
  957. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  958. xfs_qm_dqrele(ip->i_udquot);
  959. ip->i_udquot = NULL;
  960. }
  961. if ((flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  962. xfs_qm_dqrele(ip->i_gdquot);
  963. ip->i_gdquot = NULL;
  964. }
  965. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  966. /*
  967. * Wait until we've dropped the ilock and mountlock to
  968. * do the vn_rele. Or be condemned to an eternity in the
  969. * inactive code in hell.
  970. */
  971. if (vnode_refd)
  972. VN_RELE(vp);
  973. XFS_MOUNT_ILOCK(mp);
  974. /*
  975. * If an inode was inserted or removed, we gotta
  976. * start over again.
  977. */
  978. if (topino != mp->m_inodes || mp->m_ireclaims != ireclaims) {
  979. /* XXX use a sentinel */
  980. XFS_MOUNT_IUNLOCK(mp);
  981. goto again;
  982. }
  983. ip = ip->i_mnext;
  984. } while (ip != mp->m_inodes);
  985. XFS_MOUNT_IUNLOCK(mp);
  986. }
  987. /*------------------------------------------------------------------------*/
  988. #ifdef DEBUG
  989. /*
  990. * This contains all the test functions for XFS disk quotas.
  991. * Currently it does a quota accounting check. ie. it walks through
  992. * all inodes in the file system, calculating the dquot accounting fields,
  993. * and prints out any inconsistencies.
  994. */
  995. xfs_dqhash_t *qmtest_udqtab;
  996. xfs_dqhash_t *qmtest_gdqtab;
  997. int qmtest_hashmask;
  998. int qmtest_nfails;
  999. mutex_t qcheck_lock;
  1000. #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
  1001. (__psunsigned_t)(id)) & \
  1002. (qmtest_hashmask - 1))
  1003. #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
  1004. (qmtest_udqtab + \
  1005. DQTEST_HASHVAL(mp, id)) : \
  1006. (qmtest_gdqtab + \
  1007. DQTEST_HASHVAL(mp, id)))
  1008. #define DQTEST_LIST_PRINT(l, NXT, title) \
  1009. { \
  1010. xfs_dqtest_t *dqp; int i = 0;\
  1011. cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
  1012. for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
  1013. dqp = (xfs_dqtest_t *)dqp->NXT) { \
  1014. cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
  1015. ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
  1016. dqp->d_bcount, dqp->d_icount); } \
  1017. }
  1018. typedef struct dqtest {
  1019. xfs_dqmarker_t q_lists;
  1020. xfs_dqhash_t *q_hash; /* the hashchain header */
  1021. xfs_mount_t *q_mount; /* filesystem this relates to */
  1022. xfs_dqid_t d_id; /* user id or group id */
  1023. xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
  1024. xfs_qcnt_t d_icount; /* # inodes owned by the user */
  1025. } xfs_dqtest_t;
  1026. STATIC void
  1027. xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
  1028. {
  1029. xfs_dquot_t *d;
  1030. if (((d) = (h)->qh_next))
  1031. (d)->HL_PREVP = &((dqp)->HL_NEXT);
  1032. (dqp)->HL_NEXT = d;
  1033. (dqp)->HL_PREVP = &((h)->qh_next);
  1034. (h)->qh_next = (xfs_dquot_t *)dqp;
  1035. (h)->qh_version++;
  1036. (h)->qh_nelems++;
  1037. }
  1038. STATIC void
  1039. xfs_qm_dqtest_print(
  1040. xfs_dqtest_t *d)
  1041. {
  1042. cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
  1043. cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
  1044. cmn_err(CE_DEBUG, "---- type = %s", XFS_QM_ISUDQ(d)? "USR" : "GRP");
  1045. cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
  1046. cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
  1047. d->d_bcount, (int)d->d_bcount);
  1048. cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
  1049. d->d_icount, (int)d->d_icount);
  1050. cmn_err(CE_DEBUG, "---------------------------");
  1051. }
  1052. STATIC void
  1053. xfs_qm_dqtest_failed(
  1054. xfs_dqtest_t *d,
  1055. xfs_dquot_t *dqp,
  1056. char *reason,
  1057. xfs_qcnt_t a,
  1058. xfs_qcnt_t b,
  1059. int error)
  1060. {
  1061. qmtest_nfails++;
  1062. if (error)
  1063. cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
  1064. INT_GET(d->d_id, ARCH_CONVERT), error, reason);
  1065. else
  1066. cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
  1067. INT_GET(d->d_id, ARCH_CONVERT), reason, (int)a, (int)b);
  1068. xfs_qm_dqtest_print(d);
  1069. if (dqp)
  1070. xfs_qm_dqprint(dqp);
  1071. }
  1072. STATIC int
  1073. xfs_dqtest_cmp2(
  1074. xfs_dqtest_t *d,
  1075. xfs_dquot_t *dqp)
  1076. {
  1077. int err = 0;
  1078. if (INT_GET(dqp->q_core.d_icount, ARCH_CONVERT) != d->d_icount) {
  1079. xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
  1080. INT_GET(dqp->q_core.d_icount, ARCH_CONVERT),
  1081. d->d_icount, 0);
  1082. err++;
  1083. }
  1084. if (INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT) != d->d_bcount) {
  1085. xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
  1086. INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT),
  1087. d->d_bcount, 0);
  1088. err++;
  1089. }
  1090. if (INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT) &&
  1091. INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT) >=
  1092. INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT)) {
  1093. if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
  1094. cmn_err(CE_DEBUG,
  1095. "%d [%s] [0x%p] BLK TIMER NOT STARTED",
  1096. d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
  1097. err++;
  1098. }
  1099. }
  1100. if (INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT) &&
  1101. INT_GET(dqp->q_core.d_icount, ARCH_CONVERT) >=
  1102. INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT)) {
  1103. if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
  1104. cmn_err(CE_DEBUG,
  1105. "%d [%s] [0x%p] INO TIMER NOT STARTED",
  1106. d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
  1107. err++;
  1108. }
  1109. }
  1110. #ifdef QUOTADEBUG
  1111. if (!err) {
  1112. cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
  1113. d->d_id, XFS_QM_ISUDQ(d) ? "USR" : "GRP", d->q_mount);
  1114. }
  1115. #endif
  1116. return (err);
  1117. }
  1118. STATIC void
  1119. xfs_dqtest_cmp(
  1120. xfs_dqtest_t *d)
  1121. {
  1122. xfs_dquot_t *dqp;
  1123. int error;
  1124. /* xfs_qm_dqtest_print(d); */
  1125. if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
  1126. &dqp))) {
  1127. xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
  1128. return;
  1129. }
  1130. xfs_dqtest_cmp2(d, dqp);
  1131. xfs_qm_dqput(dqp);
  1132. }
  1133. STATIC int
  1134. xfs_qm_internalqcheck_dqget(
  1135. xfs_mount_t *mp,
  1136. xfs_dqid_t id,
  1137. uint type,
  1138. xfs_dqtest_t **O_dq)
  1139. {
  1140. xfs_dqtest_t *d;
  1141. xfs_dqhash_t *h;
  1142. h = DQTEST_HASH(mp, id, type);
  1143. for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
  1144. d = (xfs_dqtest_t *) d->HL_NEXT) {
  1145. /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
  1146. if (d->d_id == id && mp == d->q_mount) {
  1147. *O_dq = d;
  1148. return (0);
  1149. }
  1150. }
  1151. d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
  1152. d->dq_flags = type;
  1153. d->d_id = id;
  1154. d->q_mount = mp;
  1155. d->q_hash = h;
  1156. xfs_qm_hashinsert(h, d);
  1157. *O_dq = d;
  1158. return (0);
  1159. }
  1160. STATIC void
  1161. xfs_qm_internalqcheck_get_dquots(
  1162. xfs_mount_t *mp,
  1163. xfs_dqid_t uid,
  1164. xfs_dqid_t gid,
  1165. xfs_dqtest_t **ud,
  1166. xfs_dqtest_t **gd)
  1167. {
  1168. if (XFS_IS_UQUOTA_ON(mp))
  1169. xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
  1170. if (XFS_IS_GQUOTA_ON(mp))
  1171. xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
  1172. }
  1173. STATIC void
  1174. xfs_qm_internalqcheck_dqadjust(
  1175. xfs_inode_t *ip,
  1176. xfs_dqtest_t *d)
  1177. {
  1178. d->d_icount++;
  1179. d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
  1180. }
  1181. STATIC int
  1182. xfs_qm_internalqcheck_adjust(
  1183. xfs_mount_t *mp, /* mount point for filesystem */
  1184. xfs_ino_t ino, /* inode number to get data for */
  1185. void __user *buffer, /* not used */
  1186. int ubsize, /* not used */
  1187. void *private_data, /* not used */
  1188. xfs_daddr_t bno, /* starting block of inode cluster */
  1189. int *ubused, /* not used */
  1190. void *dip, /* not used */
  1191. int *res) /* bulkstat result code */
  1192. {
  1193. xfs_inode_t *ip;
  1194. xfs_dqtest_t *ud, *gd;
  1195. uint lock_flags;
  1196. boolean_t ipreleased;
  1197. int error;
  1198. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1199. if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
  1200. *res = BULKSTAT_RV_NOTHING;
  1201. qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
  1202. (unsigned long long) ino,
  1203. (unsigned long long) mp->m_sb.sb_uquotino,
  1204. (unsigned long long) mp->m_sb.sb_gquotino);
  1205. return XFS_ERROR(EINVAL);
  1206. }
  1207. ipreleased = B_FALSE;
  1208. again:
  1209. lock_flags = XFS_ILOCK_SHARED;
  1210. if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
  1211. *res = BULKSTAT_RV_NOTHING;
  1212. return (error);
  1213. }
  1214. if (ip->i_d.di_mode == 0) {
  1215. xfs_iput_new(ip, lock_flags);
  1216. *res = BULKSTAT_RV_NOTHING;
  1217. return XFS_ERROR(ENOENT);
  1218. }
  1219. /*
  1220. * This inode can have blocks after eof which can get released
  1221. * when we send it to inactive. Since we don't check the dquot
  1222. * until the after all our calculations are done, we must get rid
  1223. * of those now.
  1224. */
  1225. if (! ipreleased) {
  1226. xfs_iput(ip, lock_flags);
  1227. ipreleased = B_TRUE;
  1228. goto again;
  1229. }
  1230. xfs_qm_internalqcheck_get_dquots(mp,
  1231. (xfs_dqid_t) ip->i_d.di_uid,
  1232. (xfs_dqid_t) ip->i_d.di_gid,
  1233. &ud, &gd);
  1234. if (XFS_IS_UQUOTA_ON(mp)) {
  1235. ASSERT(ud);
  1236. xfs_qm_internalqcheck_dqadjust(ip, ud);
  1237. }
  1238. if (XFS_IS_GQUOTA_ON(mp)) {
  1239. ASSERT(gd);
  1240. xfs_qm_internalqcheck_dqadjust(ip, gd);
  1241. }
  1242. xfs_iput(ip, lock_flags);
  1243. *res = BULKSTAT_RV_DIDONE;
  1244. return (0);
  1245. }
  1246. /* PRIVATE, debugging */
  1247. int
  1248. xfs_qm_internalqcheck(
  1249. xfs_mount_t *mp)
  1250. {
  1251. xfs_ino_t lastino;
  1252. int done, count;
  1253. int i;
  1254. xfs_dqtest_t *d, *e;
  1255. xfs_dqhash_t *h1;
  1256. int error;
  1257. lastino = 0;
  1258. qmtest_hashmask = 32;
  1259. count = 5;
  1260. done = 0;
  1261. qmtest_nfails = 0;
  1262. if (! XFS_IS_QUOTA_ON(mp))
  1263. return XFS_ERROR(ESRCH);
  1264. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  1265. XFS_bflush(mp->m_ddev_targp);
  1266. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  1267. XFS_bflush(mp->m_ddev_targp);
  1268. mutex_lock(&qcheck_lock, PINOD);
  1269. /* There should be absolutely no quota activity while this
  1270. is going on. */
  1271. qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
  1272. sizeof(xfs_dqhash_t), KM_SLEEP);
  1273. qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
  1274. sizeof(xfs_dqhash_t), KM_SLEEP);
  1275. do {
  1276. /*
  1277. * Iterate thru all the inodes in the file system,
  1278. * adjusting the corresponding dquot counters
  1279. */
  1280. if ((error = xfs_bulkstat(mp, &lastino, &count,
  1281. xfs_qm_internalqcheck_adjust, NULL,
  1282. 0, NULL, BULKSTAT_FG_IGET, &done))) {
  1283. break;
  1284. }
  1285. } while (! done);
  1286. if (error) {
  1287. cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
  1288. }
  1289. cmn_err(CE_DEBUG, "Checking results against system dquots");
  1290. for (i = 0; i < qmtest_hashmask; i++) {
  1291. h1 = &qmtest_udqtab[i];
  1292. for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
  1293. xfs_dqtest_cmp(d);
  1294. e = (xfs_dqtest_t *) d->HL_NEXT;
  1295. kmem_free(d, sizeof(xfs_dqtest_t));
  1296. d = e;
  1297. }
  1298. h1 = &qmtest_gdqtab[i];
  1299. for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
  1300. xfs_dqtest_cmp(d);
  1301. e = (xfs_dqtest_t *) d->HL_NEXT;
  1302. kmem_free(d, sizeof(xfs_dqtest_t));
  1303. d = e;
  1304. }
  1305. }
  1306. if (qmtest_nfails) {
  1307. cmn_err(CE_DEBUG, "******** quotacheck failed ********");
  1308. cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
  1309. } else {
  1310. cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
  1311. }
  1312. kmem_free(qmtest_udqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
  1313. kmem_free(qmtest_gdqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
  1314. mutex_unlock(&qcheck_lock);
  1315. return (qmtest_nfails);
  1316. }
  1317. #endif /* DEBUG */