xfs_qm_syscalls.c 39 KB

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