xfs_qm_syscalls.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  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_GQUOTA_ACCT) == 0 &&
  408. (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
  409. (flags & XFS_OQUOTA_ENFD))) {
  410. qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
  411. flags, mp->m_sb.sb_qflags);
  412. return XFS_ERROR(EINVAL);
  413. }
  414. /*
  415. * If everything's upto-date incore, then don't waste time.
  416. */
  417. if ((mp->m_qflags & flags) == flags)
  418. return XFS_ERROR(EEXIST);
  419. /*
  420. * Change sb_qflags on disk but not incore mp->qflags
  421. * if this is the root filesystem.
  422. */
  423. s = XFS_SB_LOCK(mp);
  424. qf = mp->m_sb.sb_qflags;
  425. mp->m_sb.sb_qflags = qf | flags;
  426. XFS_SB_UNLOCK(mp, s);
  427. /*
  428. * There's nothing to change if it's the same.
  429. */
  430. if ((qf & flags) == flags && sbflags == 0)
  431. return XFS_ERROR(EEXIST);
  432. sbflags |= XFS_SB_QFLAGS;
  433. if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
  434. return (error);
  435. /*
  436. * If we aren't trying to switch on quota enforcement, we are done.
  437. */
  438. if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
  439. (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
  440. ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
  441. (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
  442. ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
  443. (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
  444. (flags & XFS_ALL_QUOTA_ENFD) == 0)
  445. return (0);
  446. if (! XFS_IS_QUOTA_RUNNING(mp))
  447. return XFS_ERROR(ESRCH);
  448. /*
  449. * Switch on quota enforcement in core.
  450. */
  451. mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
  452. mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
  453. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  454. return (0);
  455. }
  456. /*
  457. * Return quota status information, such as uquota-off, enforcements, etc.
  458. */
  459. STATIC int
  460. xfs_qm_scall_getqstat(
  461. xfs_mount_t *mp,
  462. fs_quota_stat_t *out)
  463. {
  464. xfs_inode_t *uip, *gip;
  465. boolean_t tempuqip, tempgqip;
  466. uip = gip = NULL;
  467. tempuqip = tempgqip = B_FALSE;
  468. memset(out, 0, sizeof(fs_quota_stat_t));
  469. out->qs_version = FS_QSTAT_VERSION;
  470. if (! XFS_SB_VERSION_HASQUOTA(&mp->m_sb)) {
  471. out->qs_uquota.qfs_ino = NULLFSINO;
  472. out->qs_gquota.qfs_ino = NULLFSINO;
  473. return (0);
  474. }
  475. out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
  476. (XFS_ALL_QUOTA_ACCT|
  477. XFS_ALL_QUOTA_ENFD));
  478. out->qs_pad = 0;
  479. out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
  480. out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
  481. if (mp->m_quotainfo) {
  482. uip = mp->m_quotainfo->qi_uquotaip;
  483. gip = mp->m_quotainfo->qi_gquotaip;
  484. }
  485. if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
  486. if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  487. 0, 0, &uip, 0) == 0)
  488. tempuqip = B_TRUE;
  489. }
  490. if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
  491. if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  492. 0, 0, &gip, 0) == 0)
  493. tempgqip = B_TRUE;
  494. }
  495. if (uip) {
  496. out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
  497. out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
  498. if (tempuqip)
  499. VN_RELE(XFS_ITOV(uip));
  500. }
  501. if (gip) {
  502. out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
  503. out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
  504. if (tempgqip)
  505. VN_RELE(XFS_ITOV(gip));
  506. }
  507. if (mp->m_quotainfo) {
  508. out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
  509. out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
  510. out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
  511. out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
  512. out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
  513. out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
  514. }
  515. return (0);
  516. }
  517. /*
  518. * Adjust quota limits, and start/stop timers accordingly.
  519. */
  520. STATIC int
  521. xfs_qm_scall_setqlim(
  522. xfs_mount_t *mp,
  523. xfs_dqid_t id,
  524. uint type,
  525. fs_disk_quota_t *newlim)
  526. {
  527. xfs_disk_dquot_t *ddq;
  528. xfs_dquot_t *dqp;
  529. xfs_trans_t *tp;
  530. int error;
  531. xfs_qcnt_t hard, soft;
  532. if (!capable(CAP_SYS_ADMIN))
  533. return XFS_ERROR(EPERM);
  534. if ((newlim->d_fieldmask &
  535. (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
  536. return (0);
  537. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
  538. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
  539. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  540. xfs_trans_cancel(tp, 0);
  541. return (error);
  542. }
  543. /*
  544. * We don't want to race with a quotaoff so take the quotaoff lock.
  545. * (We don't hold an inode lock, so there's nothing else to stop
  546. * a quotaoff from happening). (XXXThis doesn't currently happen
  547. * because we take the vfslock before calling xfs_qm_sysent).
  548. */
  549. mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
  550. /*
  551. * Get the dquot (locked), and join it to the transaction.
  552. * Allocate the dquot if this doesn't exist.
  553. */
  554. if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
  555. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  556. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  557. ASSERT(error != ENOENT);
  558. return (error);
  559. }
  560. xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
  561. xfs_trans_dqjoin(tp, dqp);
  562. ddq = &dqp->q_core;
  563. /*
  564. * Make sure that hardlimits are >= soft limits before changing.
  565. */
  566. hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
  567. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
  568. be64_to_cpu(ddq->d_blk_hardlimit);
  569. soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
  570. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
  571. be64_to_cpu(ddq->d_blk_softlimit);
  572. if (hard == 0 || hard >= soft) {
  573. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  574. ddq->d_blk_softlimit = cpu_to_be64(soft);
  575. if (id == 0) {
  576. mp->m_quotainfo->qi_bhardlimit = hard;
  577. mp->m_quotainfo->qi_bsoftlimit = soft;
  578. }
  579. } else {
  580. qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
  581. }
  582. hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
  583. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
  584. be64_to_cpu(ddq->d_rtb_hardlimit);
  585. soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
  586. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
  587. be64_to_cpu(ddq->d_rtb_softlimit);
  588. if (hard == 0 || hard >= soft) {
  589. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  590. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  591. if (id == 0) {
  592. mp->m_quotainfo->qi_rtbhardlimit = hard;
  593. mp->m_quotainfo->qi_rtbsoftlimit = soft;
  594. }
  595. } else {
  596. qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
  597. }
  598. hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
  599. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  600. be64_to_cpu(ddq->d_ino_hardlimit);
  601. soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
  602. (xfs_qcnt_t) newlim->d_ino_softlimit :
  603. be64_to_cpu(ddq->d_ino_softlimit);
  604. if (hard == 0 || hard >= soft) {
  605. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  606. ddq->d_ino_softlimit = cpu_to_be64(soft);
  607. if (id == 0) {
  608. mp->m_quotainfo->qi_ihardlimit = hard;
  609. mp->m_quotainfo->qi_isoftlimit = soft;
  610. }
  611. } else {
  612. qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
  613. }
  614. /*
  615. * Update warnings counter(s) if requested
  616. */
  617. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  618. ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
  619. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  620. ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
  621. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  622. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
  623. if (id == 0) {
  624. /*
  625. * Timelimits for the super user set the relative time
  626. * the other users can be over quota for this file system.
  627. * If it is zero a default is used. Ditto for the default
  628. * soft and hard limit values (already done, above), and
  629. * for warnings.
  630. */
  631. if (newlim->d_fieldmask & FS_DQ_BTIMER) {
  632. mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
  633. ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
  634. }
  635. if (newlim->d_fieldmask & FS_DQ_ITIMER) {
  636. mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
  637. ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
  638. }
  639. if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
  640. mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
  641. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
  642. }
  643. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  644. mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
  645. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  646. mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
  647. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  648. mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
  649. } else {
  650. /*
  651. * If the user is now over quota, start the timelimit.
  652. * The user will not be 'warned'.
  653. * Note that we keep the timers ticking, whether enforcement
  654. * is on or off. We don't really want to bother with iterating
  655. * over all ondisk dquots and turning the timers on/off.
  656. */
  657. xfs_qm_adjust_dqtimers(mp, ddq);
  658. }
  659. dqp->dq_flags |= XFS_DQ_DIRTY;
  660. xfs_trans_log_dquot(tp, dqp);
  661. xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
  662. xfs_trans_commit(tp, 0);
  663. xfs_qm_dqprint(dqp);
  664. xfs_qm_dqrele(dqp);
  665. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  666. return (0);
  667. }
  668. STATIC int
  669. xfs_qm_scall_getquota(
  670. xfs_mount_t *mp,
  671. xfs_dqid_t id,
  672. uint type,
  673. fs_disk_quota_t *out)
  674. {
  675. xfs_dquot_t *dqp;
  676. int error;
  677. /*
  678. * Try to get the dquot. We don't want it allocated on disk, so
  679. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  680. * exist, we'll get ENOENT back.
  681. */
  682. if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
  683. return (error);
  684. }
  685. xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
  686. /*
  687. * If everything's NULL, this dquot doesn't quite exist as far as
  688. * our utility programs are concerned.
  689. */
  690. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  691. xfs_qm_dqput(dqp);
  692. return XFS_ERROR(ENOENT);
  693. }
  694. /* xfs_qm_dqprint(dqp); */
  695. /*
  696. * Convert the disk dquot to the exportable format
  697. */
  698. xfs_qm_export_dquot(mp, &dqp->q_core, out);
  699. xfs_qm_dqput(dqp);
  700. return (error ? XFS_ERROR(EFAULT) : 0);
  701. }
  702. STATIC int
  703. xfs_qm_log_quotaoff_end(
  704. xfs_mount_t *mp,
  705. xfs_qoff_logitem_t *startqoff,
  706. uint flags)
  707. {
  708. xfs_trans_t *tp;
  709. int error;
  710. xfs_qoff_logitem_t *qoffi;
  711. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  712. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
  713. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  714. xfs_trans_cancel(tp, 0);
  715. return (error);
  716. }
  717. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  718. flags & XFS_ALL_QUOTA_ACCT);
  719. xfs_trans_log_quotaoff_item(tp, qoffi);
  720. /*
  721. * We have to make sure that the transaction is secure on disk before we
  722. * return and actually stop quota accounting. So, make it synchronous.
  723. * We don't care about quotoff's performance.
  724. */
  725. xfs_trans_set_sync(tp);
  726. error = xfs_trans_commit(tp, 0);
  727. return (error);
  728. }
  729. STATIC int
  730. xfs_qm_log_quotaoff(
  731. xfs_mount_t *mp,
  732. xfs_qoff_logitem_t **qoffstartp,
  733. uint flags)
  734. {
  735. xfs_trans_t *tp;
  736. int error;
  737. unsigned long s;
  738. xfs_qoff_logitem_t *qoffi=NULL;
  739. uint oldsbqflag=0;
  740. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  741. if ((error = xfs_trans_reserve(tp, 0,
  742. sizeof(xfs_qoff_logitem_t) * 2 +
  743. mp->m_sb.sb_sectsize + 128,
  744. 0,
  745. 0,
  746. XFS_DEFAULT_LOG_COUNT))) {
  747. goto error0;
  748. }
  749. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  750. xfs_trans_log_quotaoff_item(tp, qoffi);
  751. s = XFS_SB_LOCK(mp);
  752. oldsbqflag = mp->m_sb.sb_qflags;
  753. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  754. XFS_SB_UNLOCK(mp, s);
  755. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  756. /*
  757. * We have to make sure that the transaction is secure on disk before we
  758. * return and actually stop quota accounting. So, make it synchronous.
  759. * We don't care about quotoff's performance.
  760. */
  761. xfs_trans_set_sync(tp);
  762. error = xfs_trans_commit(tp, 0);
  763. error0:
  764. if (error) {
  765. xfs_trans_cancel(tp, 0);
  766. /*
  767. * No one else is modifying sb_qflags, so this is OK.
  768. * We still hold the quotaofflock.
  769. */
  770. s = XFS_SB_LOCK(mp);
  771. mp->m_sb.sb_qflags = oldsbqflag;
  772. XFS_SB_UNLOCK(mp, s);
  773. }
  774. *qoffstartp = qoffi;
  775. return (error);
  776. }
  777. /*
  778. * Translate an internal style on-disk-dquot to the exportable format.
  779. * The main differences are that the counters/limits are all in Basic
  780. * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
  781. * to be converted to the native endianness.
  782. */
  783. STATIC void
  784. xfs_qm_export_dquot(
  785. xfs_mount_t *mp,
  786. xfs_disk_dquot_t *src,
  787. struct fs_disk_quota *dst)
  788. {
  789. memset(dst, 0, sizeof(*dst));
  790. dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
  791. dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
  792. dst->d_id = be32_to_cpu(src->d_id);
  793. dst->d_blk_hardlimit =
  794. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
  795. dst->d_blk_softlimit =
  796. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
  797. dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
  798. dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
  799. dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
  800. dst->d_icount = be64_to_cpu(src->d_icount);
  801. dst->d_btimer = be32_to_cpu(src->d_btimer);
  802. dst->d_itimer = be32_to_cpu(src->d_itimer);
  803. dst->d_iwarns = be16_to_cpu(src->d_iwarns);
  804. dst->d_bwarns = be16_to_cpu(src->d_bwarns);
  805. dst->d_rtb_hardlimit =
  806. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
  807. dst->d_rtb_softlimit =
  808. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
  809. dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
  810. dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
  811. dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
  812. /*
  813. * Internally, we don't reset all the timers when quota enforcement
  814. * gets turned off. No need to confuse the user level code,
  815. * so return zeroes in that case.
  816. */
  817. if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
  818. (!XFS_IS_OQUOTA_ENFORCED(mp) &&
  819. (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
  820. dst->d_btimer = 0;
  821. dst->d_itimer = 0;
  822. dst->d_rtbtimer = 0;
  823. }
  824. #ifdef DEBUG
  825. if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
  826. (XFS_IS_OQUOTA_ENFORCED(mp) &&
  827. (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
  828. dst->d_id != 0) {
  829. if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
  830. (dst->d_blk_softlimit > 0)) {
  831. ASSERT(dst->d_btimer != 0);
  832. }
  833. if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
  834. (dst->d_ino_softlimit > 0)) {
  835. ASSERT(dst->d_itimer != 0);
  836. }
  837. }
  838. #endif
  839. }
  840. STATIC uint
  841. xfs_qm_import_qtype_flags(
  842. uint uflags)
  843. {
  844. uint oflags = 0;
  845. /*
  846. * Can't be more than one, or none.
  847. */
  848. if (((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ==
  849. (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ||
  850. ((uflags & (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ==
  851. (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ||
  852. ((uflags & (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ==
  853. (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ||
  854. ((uflags & (XFS_GROUP_QUOTA|XFS_USER_QUOTA|XFS_PROJ_QUOTA)) == 0))
  855. return (0);
  856. oflags |= (uflags & XFS_USER_QUOTA) ? XFS_DQ_USER : 0;
  857. oflags |= (uflags & XFS_PROJ_QUOTA) ? XFS_DQ_PROJ : 0;
  858. oflags |= (uflags & XFS_GROUP_QUOTA) ? XFS_DQ_GROUP: 0;
  859. return oflags;
  860. }
  861. STATIC uint
  862. xfs_qm_export_qtype_flags(
  863. uint flags)
  864. {
  865. /*
  866. * Can't be more than one, or none.
  867. */
  868. ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
  869. (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
  870. ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
  871. (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
  872. ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
  873. (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
  874. ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
  875. return (flags & XFS_DQ_USER) ?
  876. XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
  877. XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
  878. }
  879. STATIC uint
  880. xfs_qm_import_flags(
  881. uint uflags)
  882. {
  883. uint flags = 0;
  884. if (uflags & XFS_QUOTA_UDQ_ACCT)
  885. flags |= XFS_UQUOTA_ACCT;
  886. if (uflags & XFS_QUOTA_PDQ_ACCT)
  887. flags |= XFS_PQUOTA_ACCT;
  888. if (uflags & XFS_QUOTA_GDQ_ACCT)
  889. flags |= XFS_GQUOTA_ACCT;
  890. if (uflags & XFS_QUOTA_UDQ_ENFD)
  891. flags |= XFS_UQUOTA_ENFD;
  892. if (uflags & (XFS_QUOTA_PDQ_ENFD|XFS_QUOTA_GDQ_ENFD))
  893. flags |= XFS_OQUOTA_ENFD;
  894. return (flags);
  895. }
  896. STATIC uint
  897. xfs_qm_export_flags(
  898. uint flags)
  899. {
  900. uint uflags;
  901. uflags = 0;
  902. if (flags & XFS_UQUOTA_ACCT)
  903. uflags |= XFS_QUOTA_UDQ_ACCT;
  904. if (flags & XFS_PQUOTA_ACCT)
  905. uflags |= XFS_QUOTA_PDQ_ACCT;
  906. if (flags & XFS_GQUOTA_ACCT)
  907. uflags |= XFS_QUOTA_GDQ_ACCT;
  908. if (flags & XFS_UQUOTA_ENFD)
  909. uflags |= XFS_QUOTA_UDQ_ENFD;
  910. if (flags & (XFS_OQUOTA_ENFD)) {
  911. uflags |= (flags & XFS_GQUOTA_ACCT) ?
  912. XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
  913. }
  914. return (uflags);
  915. }
  916. /*
  917. * Go thru all the inodes in the file system, releasing their dquots.
  918. * Note that the mount structure gets modified to indicate that quotas are off
  919. * AFTER this, in the case of quotaoff. This also gets called from
  920. * xfs_rootumount.
  921. */
  922. void
  923. xfs_qm_dqrele_all_inodes(
  924. struct xfs_mount *mp,
  925. uint flags)
  926. {
  927. xfs_inode_t *ip, *topino;
  928. uint ireclaims;
  929. bhv_vnode_t *vp;
  930. boolean_t vnode_refd;
  931. ASSERT(mp->m_quotainfo);
  932. XFS_MOUNT_ILOCK(mp);
  933. again:
  934. ip = mp->m_inodes;
  935. if (ip == NULL) {
  936. XFS_MOUNT_IUNLOCK(mp);
  937. return;
  938. }
  939. do {
  940. /* Skip markers inserted by xfs_sync */
  941. if (ip->i_mount == NULL) {
  942. ip = ip->i_mnext;
  943. continue;
  944. }
  945. /* Root inode, rbmip and rsumip have associated blocks */
  946. if (ip == XFS_QI_UQIP(mp) || ip == XFS_QI_GQIP(mp)) {
  947. ASSERT(ip->i_udquot == NULL);
  948. ASSERT(ip->i_gdquot == NULL);
  949. ip = ip->i_mnext;
  950. continue;
  951. }
  952. vp = XFS_ITOV_NULL(ip);
  953. if (!vp) {
  954. ASSERT(ip->i_udquot == NULL);
  955. ASSERT(ip->i_gdquot == NULL);
  956. ip = ip->i_mnext;
  957. continue;
  958. }
  959. vnode_refd = B_FALSE;
  960. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
  961. ireclaims = mp->m_ireclaims;
  962. topino = mp->m_inodes;
  963. vp = vn_grab(vp);
  964. if (!vp)
  965. goto again;
  966. XFS_MOUNT_IUNLOCK(mp);
  967. /* XXX restart limit ? */
  968. xfs_ilock(ip, XFS_ILOCK_EXCL);
  969. vnode_refd = B_TRUE;
  970. } else {
  971. ireclaims = mp->m_ireclaims;
  972. topino = mp->m_inodes;
  973. XFS_MOUNT_IUNLOCK(mp);
  974. }
  975. /*
  976. * We don't keep the mountlock across the dqrele() call,
  977. * since it can take a while..
  978. */
  979. if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
  980. xfs_qm_dqrele(ip->i_udquot);
  981. ip->i_udquot = NULL;
  982. }
  983. if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
  984. xfs_qm_dqrele(ip->i_gdquot);
  985. ip->i_gdquot = NULL;
  986. }
  987. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  988. /*
  989. * Wait until we've dropped the ilock and mountlock to
  990. * do the vn_rele. Or be condemned to an eternity in the
  991. * inactive code in hell.
  992. */
  993. if (vnode_refd)
  994. VN_RELE(vp);
  995. XFS_MOUNT_ILOCK(mp);
  996. /*
  997. * If an inode was inserted or removed, we gotta
  998. * start over again.
  999. */
  1000. if (topino != mp->m_inodes || mp->m_ireclaims != ireclaims) {
  1001. /* XXX use a sentinel */
  1002. goto again;
  1003. }
  1004. ip = ip->i_mnext;
  1005. } while (ip != mp->m_inodes);
  1006. XFS_MOUNT_IUNLOCK(mp);
  1007. }
  1008. /*------------------------------------------------------------------------*/
  1009. #ifdef DEBUG
  1010. /*
  1011. * This contains all the test functions for XFS disk quotas.
  1012. * Currently it does a quota accounting check. ie. it walks through
  1013. * all inodes in the file system, calculating the dquot accounting fields,
  1014. * and prints out any inconsistencies.
  1015. */
  1016. xfs_dqhash_t *qmtest_udqtab;
  1017. xfs_dqhash_t *qmtest_gdqtab;
  1018. int qmtest_hashmask;
  1019. int qmtest_nfails;
  1020. mutex_t qcheck_lock;
  1021. #define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
  1022. (__psunsigned_t)(id)) & \
  1023. (qmtest_hashmask - 1))
  1024. #define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
  1025. (qmtest_udqtab + \
  1026. DQTEST_HASHVAL(mp, id)) : \
  1027. (qmtest_gdqtab + \
  1028. DQTEST_HASHVAL(mp, id)))
  1029. #define DQTEST_LIST_PRINT(l, NXT, title) \
  1030. { \
  1031. xfs_dqtest_t *dqp; int i = 0;\
  1032. cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
  1033. for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
  1034. dqp = (xfs_dqtest_t *)dqp->NXT) { \
  1035. cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
  1036. ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
  1037. dqp->d_bcount, dqp->d_icount); } \
  1038. }
  1039. typedef struct dqtest {
  1040. xfs_dqmarker_t q_lists;
  1041. xfs_dqhash_t *q_hash; /* the hashchain header */
  1042. xfs_mount_t *q_mount; /* filesystem this relates to */
  1043. xfs_dqid_t d_id; /* user id or group id */
  1044. xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
  1045. xfs_qcnt_t d_icount; /* # inodes owned by the user */
  1046. } xfs_dqtest_t;
  1047. STATIC void
  1048. xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
  1049. {
  1050. xfs_dquot_t *d;
  1051. if (((d) = (h)->qh_next))
  1052. (d)->HL_PREVP = &((dqp)->HL_NEXT);
  1053. (dqp)->HL_NEXT = d;
  1054. (dqp)->HL_PREVP = &((h)->qh_next);
  1055. (h)->qh_next = (xfs_dquot_t *)dqp;
  1056. (h)->qh_version++;
  1057. (h)->qh_nelems++;
  1058. }
  1059. STATIC void
  1060. xfs_qm_dqtest_print(
  1061. xfs_dqtest_t *d)
  1062. {
  1063. cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
  1064. cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
  1065. cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
  1066. cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
  1067. d->d_bcount, (int)d->d_bcount);
  1068. cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
  1069. d->d_icount, (int)d->d_icount);
  1070. cmn_err(CE_DEBUG, "---------------------------");
  1071. }
  1072. STATIC void
  1073. xfs_qm_dqtest_failed(
  1074. xfs_dqtest_t *d,
  1075. xfs_dquot_t *dqp,
  1076. char *reason,
  1077. xfs_qcnt_t a,
  1078. xfs_qcnt_t b,
  1079. int error)
  1080. {
  1081. qmtest_nfails++;
  1082. if (error)
  1083. cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
  1084. d->d_id, error, reason);
  1085. else
  1086. cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
  1087. d->d_id, reason, (int)a, (int)b);
  1088. xfs_qm_dqtest_print(d);
  1089. if (dqp)
  1090. xfs_qm_dqprint(dqp);
  1091. }
  1092. STATIC int
  1093. xfs_dqtest_cmp2(
  1094. xfs_dqtest_t *d,
  1095. xfs_dquot_t *dqp)
  1096. {
  1097. int err = 0;
  1098. if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
  1099. xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
  1100. be64_to_cpu(dqp->q_core.d_icount),
  1101. d->d_icount, 0);
  1102. err++;
  1103. }
  1104. if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
  1105. xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
  1106. be64_to_cpu(dqp->q_core.d_bcount),
  1107. d->d_bcount, 0);
  1108. err++;
  1109. }
  1110. if (dqp->q_core.d_blk_softlimit &&
  1111. be64_to_cpu(dqp->q_core.d_bcount) >=
  1112. be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
  1113. if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
  1114. cmn_err(CE_DEBUG,
  1115. "%d [%s] [0x%p] BLK TIMER NOT STARTED",
  1116. d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
  1117. err++;
  1118. }
  1119. }
  1120. if (dqp->q_core.d_ino_softlimit &&
  1121. be64_to_cpu(dqp->q_core.d_icount) >=
  1122. be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
  1123. if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
  1124. cmn_err(CE_DEBUG,
  1125. "%d [%s] [0x%p] INO TIMER NOT STARTED",
  1126. d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
  1127. err++;
  1128. }
  1129. }
  1130. #ifdef QUOTADEBUG
  1131. if (!err) {
  1132. cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
  1133. d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
  1134. }
  1135. #endif
  1136. return (err);
  1137. }
  1138. STATIC void
  1139. xfs_dqtest_cmp(
  1140. xfs_dqtest_t *d)
  1141. {
  1142. xfs_dquot_t *dqp;
  1143. int error;
  1144. /* xfs_qm_dqtest_print(d); */
  1145. if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
  1146. &dqp))) {
  1147. xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
  1148. return;
  1149. }
  1150. xfs_dqtest_cmp2(d, dqp);
  1151. xfs_qm_dqput(dqp);
  1152. }
  1153. STATIC int
  1154. xfs_qm_internalqcheck_dqget(
  1155. xfs_mount_t *mp,
  1156. xfs_dqid_t id,
  1157. uint type,
  1158. xfs_dqtest_t **O_dq)
  1159. {
  1160. xfs_dqtest_t *d;
  1161. xfs_dqhash_t *h;
  1162. h = DQTEST_HASH(mp, id, type);
  1163. for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
  1164. d = (xfs_dqtest_t *) d->HL_NEXT) {
  1165. /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
  1166. if (d->d_id == id && mp == d->q_mount) {
  1167. *O_dq = d;
  1168. return (0);
  1169. }
  1170. }
  1171. d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
  1172. d->dq_flags = type;
  1173. d->d_id = id;
  1174. d->q_mount = mp;
  1175. d->q_hash = h;
  1176. xfs_qm_hashinsert(h, d);
  1177. *O_dq = d;
  1178. return (0);
  1179. }
  1180. STATIC void
  1181. xfs_qm_internalqcheck_get_dquots(
  1182. xfs_mount_t *mp,
  1183. xfs_dqid_t uid,
  1184. xfs_dqid_t projid,
  1185. xfs_dqid_t gid,
  1186. xfs_dqtest_t **ud,
  1187. xfs_dqtest_t **gd)
  1188. {
  1189. if (XFS_IS_UQUOTA_ON(mp))
  1190. xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
  1191. if (XFS_IS_GQUOTA_ON(mp))
  1192. xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
  1193. else if (XFS_IS_PQUOTA_ON(mp))
  1194. xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
  1195. }
  1196. STATIC void
  1197. xfs_qm_internalqcheck_dqadjust(
  1198. xfs_inode_t *ip,
  1199. xfs_dqtest_t *d)
  1200. {
  1201. d->d_icount++;
  1202. d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
  1203. }
  1204. STATIC int
  1205. xfs_qm_internalqcheck_adjust(
  1206. xfs_mount_t *mp, /* mount point for filesystem */
  1207. xfs_ino_t ino, /* inode number to get data for */
  1208. void __user *buffer, /* not used */
  1209. int ubsize, /* not used */
  1210. void *private_data, /* not used */
  1211. xfs_daddr_t bno, /* starting block of inode cluster */
  1212. int *ubused, /* not used */
  1213. void *dip, /* not used */
  1214. int *res) /* bulkstat result code */
  1215. {
  1216. xfs_inode_t *ip;
  1217. xfs_dqtest_t *ud, *gd;
  1218. uint lock_flags;
  1219. boolean_t ipreleased;
  1220. int error;
  1221. ASSERT(XFS_IS_QUOTA_RUNNING(mp));
  1222. if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
  1223. *res = BULKSTAT_RV_NOTHING;
  1224. qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
  1225. (unsigned long long) ino,
  1226. (unsigned long long) mp->m_sb.sb_uquotino,
  1227. (unsigned long long) mp->m_sb.sb_gquotino);
  1228. return XFS_ERROR(EINVAL);
  1229. }
  1230. ipreleased = B_FALSE;
  1231. again:
  1232. lock_flags = XFS_ILOCK_SHARED;
  1233. if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
  1234. *res = BULKSTAT_RV_NOTHING;
  1235. return (error);
  1236. }
  1237. if (ip->i_d.di_mode == 0) {
  1238. xfs_iput_new(ip, lock_flags);
  1239. *res = BULKSTAT_RV_NOTHING;
  1240. return XFS_ERROR(ENOENT);
  1241. }
  1242. /*
  1243. * This inode can have blocks after eof which can get released
  1244. * when we send it to inactive. Since we don't check the dquot
  1245. * until the after all our calculations are done, we must get rid
  1246. * of those now.
  1247. */
  1248. if (! ipreleased) {
  1249. xfs_iput(ip, lock_flags);
  1250. ipreleased = B_TRUE;
  1251. goto again;
  1252. }
  1253. xfs_qm_internalqcheck_get_dquots(mp,
  1254. (xfs_dqid_t) ip->i_d.di_uid,
  1255. (xfs_dqid_t) ip->i_d.di_projid,
  1256. (xfs_dqid_t) ip->i_d.di_gid,
  1257. &ud, &gd);
  1258. if (XFS_IS_UQUOTA_ON(mp)) {
  1259. ASSERT(ud);
  1260. xfs_qm_internalqcheck_dqadjust(ip, ud);
  1261. }
  1262. if (XFS_IS_OQUOTA_ON(mp)) {
  1263. ASSERT(gd);
  1264. xfs_qm_internalqcheck_dqadjust(ip, gd);
  1265. }
  1266. xfs_iput(ip, lock_flags);
  1267. *res = BULKSTAT_RV_DIDONE;
  1268. return (0);
  1269. }
  1270. /* PRIVATE, debugging */
  1271. int
  1272. xfs_qm_internalqcheck(
  1273. xfs_mount_t *mp)
  1274. {
  1275. xfs_ino_t lastino;
  1276. int done, count;
  1277. int i;
  1278. xfs_dqtest_t *d, *e;
  1279. xfs_dqhash_t *h1;
  1280. int error;
  1281. lastino = 0;
  1282. qmtest_hashmask = 32;
  1283. count = 5;
  1284. done = 0;
  1285. qmtest_nfails = 0;
  1286. if (! XFS_IS_QUOTA_ON(mp))
  1287. return XFS_ERROR(ESRCH);
  1288. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  1289. XFS_bflush(mp->m_ddev_targp);
  1290. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  1291. XFS_bflush(mp->m_ddev_targp);
  1292. mutex_lock(&qcheck_lock);
  1293. /* There should be absolutely no quota activity while this
  1294. is going on. */
  1295. qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
  1296. sizeof(xfs_dqhash_t), KM_SLEEP);
  1297. qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
  1298. sizeof(xfs_dqhash_t), KM_SLEEP);
  1299. do {
  1300. /*
  1301. * Iterate thru all the inodes in the file system,
  1302. * adjusting the corresponding dquot counters
  1303. */
  1304. if ((error = xfs_bulkstat(mp, &lastino, &count,
  1305. xfs_qm_internalqcheck_adjust, NULL,
  1306. 0, NULL, BULKSTAT_FG_IGET, &done))) {
  1307. break;
  1308. }
  1309. } while (! done);
  1310. if (error) {
  1311. cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
  1312. }
  1313. cmn_err(CE_DEBUG, "Checking results against system dquots");
  1314. for (i = 0; i < qmtest_hashmask; i++) {
  1315. h1 = &qmtest_udqtab[i];
  1316. for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
  1317. xfs_dqtest_cmp(d);
  1318. e = (xfs_dqtest_t *) d->HL_NEXT;
  1319. kmem_free(d, sizeof(xfs_dqtest_t));
  1320. d = e;
  1321. }
  1322. h1 = &qmtest_gdqtab[i];
  1323. for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
  1324. xfs_dqtest_cmp(d);
  1325. e = (xfs_dqtest_t *) d->HL_NEXT;
  1326. kmem_free(d, sizeof(xfs_dqtest_t));
  1327. d = e;
  1328. }
  1329. }
  1330. if (qmtest_nfails) {
  1331. cmn_err(CE_DEBUG, "******** quotacheck failed ********");
  1332. cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
  1333. } else {
  1334. cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
  1335. }
  1336. kmem_free(qmtest_udqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
  1337. kmem_free(qmtest_gdqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
  1338. mutex_unlock(&qcheck_lock);
  1339. return (qmtest_nfails);
  1340. }
  1341. #endif /* DEBUG */