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. xfs_mount_t *mp,
  79. int cmd,
  80. int id,
  81. xfs_caddr_t addr)
  82. {
  83. bhv_vfs_t *vfsp = XFS_MTOVFS(mp);
  84. int error;
  85. ASSERT(addr != NULL || cmd == Q_XQUOTASYNC);
  86. /*
  87. * The following commands are valid even when quotaoff.
  88. */
  89. switch (cmd) {
  90. case Q_XQUOTARM:
  91. /*
  92. * Truncate quota files. quota must be off.
  93. */
  94. if (XFS_IS_QUOTA_ON(mp))
  95. return XFS_ERROR(EINVAL);
  96. if (vfsp->vfs_flag & VFS_RDONLY)
  97. return XFS_ERROR(EROFS);
  98. return (xfs_qm_scall_trunc_qfiles(mp,
  99. xfs_qm_import_qtype_flags(*(uint *)addr)));
  100. case Q_XGETQSTAT:
  101. /*
  102. * Get quota status information.
  103. */
  104. return (xfs_qm_scall_getqstat(mp, (fs_quota_stat_t *)addr));
  105. case Q_XQUOTAON:
  106. /*
  107. * QUOTAON - enabling quota enforcement.
  108. * Quota accounting must be turned on at mount time.
  109. */
  110. if (vfsp->vfs_flag & VFS_RDONLY)
  111. return XFS_ERROR(EROFS);
  112. return (xfs_qm_scall_quotaon(mp,
  113. xfs_qm_import_flags(*(uint *)addr)));
  114. case Q_XQUOTAOFF:
  115. if (vfsp->vfs_flag & VFS_RDONLY)
  116. return XFS_ERROR(EROFS);
  117. break;
  118. case Q_XQUOTASYNC:
  119. return (xfs_sync_inodes(mp, SYNC_DELWRI, NULL));
  120. default:
  121. break;
  122. }
  123. if (! XFS_IS_QUOTA_ON(mp))
  124. return XFS_ERROR(ESRCH);
  125. switch (cmd) {
  126. case Q_XQUOTAOFF:
  127. if (vfsp->vfs_flag & VFS_RDONLY)
  128. return XFS_ERROR(EROFS);
  129. error = xfs_qm_scall_quotaoff(mp,
  130. xfs_qm_import_flags(*(uint *)addr),
  131. B_FALSE);
  132. break;
  133. case Q_XGETQUOTA:
  134. error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_USER,
  135. (fs_disk_quota_t *)addr);
  136. break;
  137. case Q_XGETGQUOTA:
  138. error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
  139. (fs_disk_quota_t *)addr);
  140. break;
  141. case Q_XGETPQUOTA:
  142. error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
  143. (fs_disk_quota_t *)addr);
  144. break;
  145. case Q_XSETQLIM:
  146. if (vfsp->vfs_flag & VFS_RDONLY)
  147. return XFS_ERROR(EROFS);
  148. error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_USER,
  149. (fs_disk_quota_t *)addr);
  150. break;
  151. case Q_XSETGQLIM:
  152. if (vfsp->vfs_flag & VFS_RDONLY)
  153. return XFS_ERROR(EROFS);
  154. error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
  155. (fs_disk_quota_t *)addr);
  156. break;
  157. case Q_XSETPQLIM:
  158. if (vfsp->vfs_flag & VFS_RDONLY)
  159. return XFS_ERROR(EROFS);
  160. error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
  161. (fs_disk_quota_t *)addr);
  162. break;
  163. default:
  164. error = XFS_ERROR(EINVAL);
  165. break;
  166. }
  167. return (error);
  168. }
  169. /*
  170. * Turn off quota accounting and/or enforcement for all udquots and/or
  171. * gdquots. Called only at unmount time.
  172. *
  173. * This assumes that there are no dquots of this file system cached
  174. * incore, and modifies the ondisk dquot directly. Therefore, for example,
  175. * it is an error to call this twice, without purging the cache.
  176. */
  177. STATIC int
  178. xfs_qm_scall_quotaoff(
  179. xfs_mount_t *mp,
  180. uint flags,
  181. boolean_t force)
  182. {
  183. uint dqtype;
  184. unsigned long s;
  185. int error;
  186. uint inactivate_flags;
  187. xfs_qoff_logitem_t *qoffstart;
  188. int nculprits;
  189. if (!force && !capable(CAP_SYS_ADMIN))
  190. return XFS_ERROR(EPERM);
  191. /*
  192. * No file system can have quotas enabled on disk but not in core.
  193. * Note that quota utilities (like quotaoff) _expect_
  194. * errno == EEXIST here.
  195. */
  196. if ((mp->m_qflags & flags) == 0)
  197. return XFS_ERROR(EEXIST);
  198. error = 0;
  199. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  200. /*
  201. * We don't want to deal with two quotaoffs messing up each other,
  202. * so we're going to serialize it. quotaoff isn't exactly a performance
  203. * critical thing.
  204. * If quotaoff, then we must be dealing with the root filesystem.
  205. */
  206. ASSERT(mp->m_quotainfo);
  207. if (mp->m_quotainfo)
  208. mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
  209. ASSERT(mp->m_quotainfo);
  210. /*
  211. * If we're just turning off quota enforcement, change mp and go.
  212. */
  213. if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
  214. mp->m_qflags &= ~(flags);
  215. s = XFS_SB_LOCK(mp);
  216. mp->m_sb.sb_qflags = mp->m_qflags;
  217. XFS_SB_UNLOCK(mp, s);
  218. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  219. /* XXX what to do if error ? Revert back to old vals incore ? */
  220. error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
  221. return (error);
  222. }
  223. dqtype = 0;
  224. inactivate_flags = 0;
  225. /*
  226. * If accounting is off, we must turn enforcement off, clear the
  227. * quota 'CHKD' certificate to make it known that we have to
  228. * do a quotacheck the next time this quota is turned on.
  229. */
  230. if (flags & XFS_UQUOTA_ACCT) {
  231. dqtype |= XFS_QMOPT_UQUOTA;
  232. flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
  233. inactivate_flags |= XFS_UQUOTA_ACTIVE;
  234. }
  235. if (flags & XFS_GQUOTA_ACCT) {
  236. dqtype |= XFS_QMOPT_GQUOTA;
  237. flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
  238. inactivate_flags |= XFS_GQUOTA_ACTIVE;
  239. } else if (flags & XFS_PQUOTA_ACCT) {
  240. dqtype |= XFS_QMOPT_PQUOTA;
  241. flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
  242. inactivate_flags |= XFS_PQUOTA_ACTIVE;
  243. }
  244. /*
  245. * Nothing to do? Don't complain. This happens when we're just
  246. * turning off quota enforcement.
  247. */
  248. if ((mp->m_qflags & flags) == 0) {
  249. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  250. return (0);
  251. }
  252. /*
  253. * Write the LI_QUOTAOFF log record, and do SB changes atomically,
  254. * and synchronously.
  255. */
  256. xfs_qm_log_quotaoff(mp, &qoffstart, flags);
  257. /*
  258. * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
  259. * to take care of the race between dqget and quotaoff. We don't take
  260. * any special locks to reset these bits. All processes need to check
  261. * these bits *after* taking inode lock(s) to see if the particular
  262. * quota type is in the process of being turned off. If *ACTIVE, it is
  263. * guaranteed that all dquot structures and all quotainode ptrs will all
  264. * stay valid as long as that inode is kept locked.
  265. *
  266. * There is no turning back after this.
  267. */
  268. mp->m_qflags &= ~inactivate_flags;
  269. /*
  270. * Give back all the dquot reference(s) held by inodes.
  271. * Here we go thru every single incore inode in this file system, and
  272. * do a dqrele on the i_udquot/i_gdquot that it may have.
  273. * Essentially, as long as somebody has an inode locked, this guarantees
  274. * that quotas will not be turned off. This is handy because in a
  275. * transaction once we lock the inode(s) and check for quotaon, we can
  276. * depend on the quota inodes (and other things) being valid as long as
  277. * we keep the lock(s).
  278. */
  279. xfs_qm_dqrele_all_inodes(mp, flags);
  280. /*
  281. * Next we make the changes in the quota flag in the mount struct.
  282. * This isn't protected by a particular lock directly, because we
  283. * don't want to take a mrlock everytime we depend on quotas being on.
  284. */
  285. mp->m_qflags &= ~(flags);
  286. /*
  287. * Go through all the dquots of this file system and purge them,
  288. * according to what was turned off. We may not be able to get rid
  289. * of all dquots, because dquots can have temporary references that
  290. * are not attached to inodes. eg. xfs_setattr, xfs_create.
  291. * So, if we couldn't purge all the dquots from the filesystem,
  292. * we can't get rid of the incore data structures.
  293. */
  294. while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
  295. delay(10 * nculprits);
  296. /*
  297. * Transactions that had started before ACTIVE state bit was cleared
  298. * could have logged many dquots, so they'd have higher LSNs than
  299. * the first QUOTAOFF log record does. If we happen to crash when
  300. * the tail of the log has gone past the QUOTAOFF record, but
  301. * before the last dquot modification, those dquots __will__
  302. * recover, and that's not good.
  303. *
  304. * So, we have QUOTAOFF start and end logitems; the start
  305. * logitem won't get overwritten until the end logitem appears...
  306. */
  307. xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
  308. /*
  309. * If quotas is completely disabled, close shop.
  310. */
  311. if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
  312. ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
  313. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  314. xfs_qm_destroy_quotainfo(mp);
  315. return (0);
  316. }
  317. /*
  318. * Release our quotainode references, and vn_purge them,
  319. * if we don't need them anymore.
  320. */
  321. if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
  322. XFS_PURGE_INODE(XFS_QI_UQIP(mp));
  323. XFS_QI_UQIP(mp) = NULL;
  324. }
  325. if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
  326. XFS_PURGE_INODE(XFS_QI_GQIP(mp));
  327. XFS_QI_GQIP(mp) = NULL;
  328. }
  329. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  330. return (error);
  331. }
  332. STATIC int
  333. xfs_qm_scall_trunc_qfiles(
  334. xfs_mount_t *mp,
  335. uint flags)
  336. {
  337. int error;
  338. xfs_inode_t *qip;
  339. if (!capable(CAP_SYS_ADMIN))
  340. return XFS_ERROR(EPERM);
  341. error = 0;
  342. if (!XFS_SB_VERSION_HASQUOTA(&mp->m_sb) || flags == 0) {
  343. qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
  344. return XFS_ERROR(EINVAL);
  345. }
  346. if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
  347. error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
  348. if (! error) {
  349. (void) xfs_truncate_file(mp, qip);
  350. VN_RELE(XFS_ITOV(qip));
  351. }
  352. }
  353. if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
  354. mp->m_sb.sb_gquotino != NULLFSINO) {
  355. error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
  356. if (! error) {
  357. (void) xfs_truncate_file(mp, qip);
  358. VN_RELE(XFS_ITOV(qip));
  359. }
  360. }
  361. return (error);
  362. }
  363. /*
  364. * Switch on (a given) quota enforcement for a filesystem. This takes
  365. * effect immediately.
  366. * (Switching on quota accounting must be done at mount time.)
  367. */
  368. STATIC int
  369. xfs_qm_scall_quotaon(
  370. xfs_mount_t *mp,
  371. uint flags)
  372. {
  373. int error;
  374. unsigned long s;
  375. uint qf;
  376. uint accflags;
  377. __int64_t sbflags;
  378. if (!capable(CAP_SYS_ADMIN))
  379. return XFS_ERROR(EPERM);
  380. flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
  381. /*
  382. * Switching on quota accounting must be done at mount time.
  383. */
  384. accflags = flags & XFS_ALL_QUOTA_ACCT;
  385. flags &= ~(XFS_ALL_QUOTA_ACCT);
  386. sbflags = 0;
  387. if (flags == 0) {
  388. qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
  389. return XFS_ERROR(EINVAL);
  390. }
  391. /* No fs can turn on quotas with a delayed effect */
  392. ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
  393. /*
  394. * Can't enforce without accounting. We check the superblock
  395. * qflags here instead of m_qflags because rootfs can have
  396. * quota acct on ondisk without m_qflags' knowing.
  397. */
  398. if (((flags & XFS_UQUOTA_ACCT) == 0 &&
  399. (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
  400. (flags & XFS_UQUOTA_ENFD))
  401. ||
  402. ((flags & XFS_PQUOTA_ACCT) == 0 &&
  403. (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
  404. (flags & XFS_GQUOTA_ACCT) == 0 &&
  405. (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
  406. (flags & XFS_OQUOTA_ENFD))) {
  407. qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
  408. flags, mp->m_sb.sb_qflags);
  409. return XFS_ERROR(EINVAL);
  410. }
  411. /*
  412. * If everything's upto-date incore, then don't waste time.
  413. */
  414. if ((mp->m_qflags & flags) == flags)
  415. return XFS_ERROR(EEXIST);
  416. /*
  417. * Change sb_qflags on disk but not incore mp->qflags
  418. * if this is the root filesystem.
  419. */
  420. s = XFS_SB_LOCK(mp);
  421. qf = mp->m_sb.sb_qflags;
  422. mp->m_sb.sb_qflags = qf | flags;
  423. XFS_SB_UNLOCK(mp, s);
  424. /*
  425. * There's nothing to change if it's the same.
  426. */
  427. if ((qf & flags) == flags && sbflags == 0)
  428. return XFS_ERROR(EEXIST);
  429. sbflags |= XFS_SB_QFLAGS;
  430. if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
  431. return (error);
  432. /*
  433. * If we aren't trying to switch on quota enforcement, we are done.
  434. */
  435. if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
  436. (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
  437. ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
  438. (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
  439. ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
  440. (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
  441. (flags & XFS_ALL_QUOTA_ENFD) == 0)
  442. return (0);
  443. if (! XFS_IS_QUOTA_RUNNING(mp))
  444. return XFS_ERROR(ESRCH);
  445. /*
  446. * Switch on quota enforcement in core.
  447. */
  448. mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
  449. mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
  450. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  451. return (0);
  452. }
  453. /*
  454. * Return quota status information, such as uquota-off, enforcements, etc.
  455. */
  456. STATIC int
  457. xfs_qm_scall_getqstat(
  458. xfs_mount_t *mp,
  459. fs_quota_stat_t *out)
  460. {
  461. xfs_inode_t *uip, *gip;
  462. boolean_t tempuqip, tempgqip;
  463. uip = gip = NULL;
  464. tempuqip = tempgqip = B_FALSE;
  465. memset(out, 0, sizeof(fs_quota_stat_t));
  466. out->qs_version = FS_QSTAT_VERSION;
  467. if (! XFS_SB_VERSION_HASQUOTA(&mp->m_sb)) {
  468. out->qs_uquota.qfs_ino = NULLFSINO;
  469. out->qs_gquota.qfs_ino = NULLFSINO;
  470. return (0);
  471. }
  472. out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
  473. (XFS_ALL_QUOTA_ACCT|
  474. XFS_ALL_QUOTA_ENFD));
  475. out->qs_pad = 0;
  476. out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
  477. out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
  478. if (mp->m_quotainfo) {
  479. uip = mp->m_quotainfo->qi_uquotaip;
  480. gip = mp->m_quotainfo->qi_gquotaip;
  481. }
  482. if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
  483. if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
  484. 0, 0, &uip, 0) == 0)
  485. tempuqip = B_TRUE;
  486. }
  487. if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
  488. if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
  489. 0, 0, &gip, 0) == 0)
  490. tempgqip = B_TRUE;
  491. }
  492. if (uip) {
  493. out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
  494. out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
  495. if (tempuqip)
  496. VN_RELE(XFS_ITOV(uip));
  497. }
  498. if (gip) {
  499. out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
  500. out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
  501. if (tempgqip)
  502. VN_RELE(XFS_ITOV(gip));
  503. }
  504. if (mp->m_quotainfo) {
  505. out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
  506. out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
  507. out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
  508. out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
  509. out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
  510. out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
  511. }
  512. return (0);
  513. }
  514. /*
  515. * Adjust quota limits, and start/stop timers accordingly.
  516. */
  517. STATIC int
  518. xfs_qm_scall_setqlim(
  519. xfs_mount_t *mp,
  520. xfs_dqid_t id,
  521. uint type,
  522. fs_disk_quota_t *newlim)
  523. {
  524. xfs_disk_dquot_t *ddq;
  525. xfs_dquot_t *dqp;
  526. xfs_trans_t *tp;
  527. int error;
  528. xfs_qcnt_t hard, soft;
  529. if (!capable(CAP_SYS_ADMIN))
  530. return XFS_ERROR(EPERM);
  531. if ((newlim->d_fieldmask &
  532. (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
  533. return (0);
  534. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
  535. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
  536. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  537. xfs_trans_cancel(tp, 0);
  538. return (error);
  539. }
  540. /*
  541. * We don't want to race with a quotaoff so take the quotaoff lock.
  542. * (We don't hold an inode lock, so there's nothing else to stop
  543. * a quotaoff from happening). (XXXThis doesn't currently happen
  544. * because we take the vfslock before calling xfs_qm_sysent).
  545. */
  546. mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
  547. /*
  548. * Get the dquot (locked), and join it to the transaction.
  549. * Allocate the dquot if this doesn't exist.
  550. */
  551. if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
  552. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  553. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  554. ASSERT(error != ENOENT);
  555. return (error);
  556. }
  557. xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
  558. xfs_trans_dqjoin(tp, dqp);
  559. ddq = &dqp->q_core;
  560. /*
  561. * Make sure that hardlimits are >= soft limits before changing.
  562. */
  563. hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
  564. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
  565. be64_to_cpu(ddq->d_blk_hardlimit);
  566. soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
  567. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
  568. be64_to_cpu(ddq->d_blk_softlimit);
  569. if (hard == 0 || hard >= soft) {
  570. ddq->d_blk_hardlimit = cpu_to_be64(hard);
  571. ddq->d_blk_softlimit = cpu_to_be64(soft);
  572. if (id == 0) {
  573. mp->m_quotainfo->qi_bhardlimit = hard;
  574. mp->m_quotainfo->qi_bsoftlimit = soft;
  575. }
  576. } else {
  577. qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
  578. }
  579. hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
  580. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
  581. be64_to_cpu(ddq->d_rtb_hardlimit);
  582. soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
  583. (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
  584. be64_to_cpu(ddq->d_rtb_softlimit);
  585. if (hard == 0 || hard >= soft) {
  586. ddq->d_rtb_hardlimit = cpu_to_be64(hard);
  587. ddq->d_rtb_softlimit = cpu_to_be64(soft);
  588. if (id == 0) {
  589. mp->m_quotainfo->qi_rtbhardlimit = hard;
  590. mp->m_quotainfo->qi_rtbsoftlimit = soft;
  591. }
  592. } else {
  593. qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
  594. }
  595. hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
  596. (xfs_qcnt_t) newlim->d_ino_hardlimit :
  597. be64_to_cpu(ddq->d_ino_hardlimit);
  598. soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
  599. (xfs_qcnt_t) newlim->d_ino_softlimit :
  600. be64_to_cpu(ddq->d_ino_softlimit);
  601. if (hard == 0 || hard >= soft) {
  602. ddq->d_ino_hardlimit = cpu_to_be64(hard);
  603. ddq->d_ino_softlimit = cpu_to_be64(soft);
  604. if (id == 0) {
  605. mp->m_quotainfo->qi_ihardlimit = hard;
  606. mp->m_quotainfo->qi_isoftlimit = soft;
  607. }
  608. } else {
  609. qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
  610. }
  611. /*
  612. * Update warnings counter(s) if requested
  613. */
  614. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  615. ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
  616. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  617. ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
  618. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  619. ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
  620. if (id == 0) {
  621. /*
  622. * Timelimits for the super user set the relative time
  623. * the other users can be over quota for this file system.
  624. * If it is zero a default is used. Ditto for the default
  625. * soft and hard limit values (already done, above), and
  626. * for warnings.
  627. */
  628. if (newlim->d_fieldmask & FS_DQ_BTIMER) {
  629. mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
  630. ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
  631. }
  632. if (newlim->d_fieldmask & FS_DQ_ITIMER) {
  633. mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
  634. ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
  635. }
  636. if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
  637. mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
  638. ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
  639. }
  640. if (newlim->d_fieldmask & FS_DQ_BWARNS)
  641. mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
  642. if (newlim->d_fieldmask & FS_DQ_IWARNS)
  643. mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
  644. if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
  645. mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
  646. } else {
  647. /*
  648. * If the user is now over quota, start the timelimit.
  649. * The user will not be 'warned'.
  650. * Note that we keep the timers ticking, whether enforcement
  651. * is on or off. We don't really want to bother with iterating
  652. * over all ondisk dquots and turning the timers on/off.
  653. */
  654. xfs_qm_adjust_dqtimers(mp, ddq);
  655. }
  656. dqp->dq_flags |= XFS_DQ_DIRTY;
  657. xfs_trans_log_dquot(tp, dqp);
  658. xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
  659. xfs_trans_commit(tp, 0);
  660. xfs_qm_dqprint(dqp);
  661. xfs_qm_dqrele(dqp);
  662. mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
  663. return (0);
  664. }
  665. STATIC int
  666. xfs_qm_scall_getquota(
  667. xfs_mount_t *mp,
  668. xfs_dqid_t id,
  669. uint type,
  670. fs_disk_quota_t *out)
  671. {
  672. xfs_dquot_t *dqp;
  673. int error;
  674. /*
  675. * Try to get the dquot. We don't want it allocated on disk, so
  676. * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
  677. * exist, we'll get ENOENT back.
  678. */
  679. if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
  680. return (error);
  681. }
  682. xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
  683. /*
  684. * If everything's NULL, this dquot doesn't quite exist as far as
  685. * our utility programs are concerned.
  686. */
  687. if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
  688. xfs_qm_dqput(dqp);
  689. return XFS_ERROR(ENOENT);
  690. }
  691. /* xfs_qm_dqprint(dqp); */
  692. /*
  693. * Convert the disk dquot to the exportable format
  694. */
  695. xfs_qm_export_dquot(mp, &dqp->q_core, out);
  696. xfs_qm_dqput(dqp);
  697. return (error ? XFS_ERROR(EFAULT) : 0);
  698. }
  699. STATIC int
  700. xfs_qm_log_quotaoff_end(
  701. xfs_mount_t *mp,
  702. xfs_qoff_logitem_t *startqoff,
  703. uint flags)
  704. {
  705. xfs_trans_t *tp;
  706. int error;
  707. xfs_qoff_logitem_t *qoffi;
  708. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
  709. if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
  710. 0, 0, XFS_DEFAULT_LOG_COUNT))) {
  711. xfs_trans_cancel(tp, 0);
  712. return (error);
  713. }
  714. qoffi = xfs_trans_get_qoff_item(tp, startqoff,
  715. flags & XFS_ALL_QUOTA_ACCT);
  716. xfs_trans_log_quotaoff_item(tp, qoffi);
  717. /*
  718. * We have to make sure that the transaction is secure on disk before we
  719. * return and actually stop quota accounting. So, make it synchronous.
  720. * We don't care about quotoff's performance.
  721. */
  722. xfs_trans_set_sync(tp);
  723. error = xfs_trans_commit(tp, 0);
  724. return (error);
  725. }
  726. STATIC int
  727. xfs_qm_log_quotaoff(
  728. xfs_mount_t *mp,
  729. xfs_qoff_logitem_t **qoffstartp,
  730. uint flags)
  731. {
  732. xfs_trans_t *tp;
  733. int error;
  734. unsigned long s;
  735. xfs_qoff_logitem_t *qoffi=NULL;
  736. uint oldsbqflag=0;
  737. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
  738. if ((error = xfs_trans_reserve(tp, 0,
  739. sizeof(xfs_qoff_logitem_t) * 2 +
  740. mp->m_sb.sb_sectsize + 128,
  741. 0,
  742. 0,
  743. XFS_DEFAULT_LOG_COUNT))) {
  744. goto error0;
  745. }
  746. qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
  747. xfs_trans_log_quotaoff_item(tp, qoffi);
  748. s = XFS_SB_LOCK(mp);
  749. oldsbqflag = mp->m_sb.sb_qflags;
  750. mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
  751. XFS_SB_UNLOCK(mp, s);
  752. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  753. /*
  754. * We have to make sure that the transaction is secure on disk before we
  755. * return and actually stop quota accounting. So, make it synchronous.
  756. * We don't care about quotoff's performance.
  757. */
  758. xfs_trans_set_sync(tp);
  759. error = xfs_trans_commit(tp, 0);
  760. error0:
  761. if (error) {
  762. xfs_trans_cancel(tp, 0);
  763. /*
  764. * No one else is modifying sb_qflags, so this is OK.
  765. * We still hold the quotaofflock.
  766. */
  767. s = XFS_SB_LOCK(mp);
  768. mp->m_sb.sb_qflags = oldsbqflag;
  769. XFS_SB_UNLOCK(mp, s);
  770. }
  771. *qoffstartp = qoffi;
  772. return (error);
  773. }
  774. /*
  775. * Translate an internal style on-disk-dquot to the exportable format.
  776. * The main differences are that the counters/limits are all in Basic
  777. * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
  778. * to be converted to the native endianness.
  779. */
  780. STATIC void
  781. xfs_qm_export_dquot(
  782. xfs_mount_t *mp,
  783. xfs_disk_dquot_t *src,
  784. struct fs_disk_quota *dst)
  785. {
  786. memset(dst, 0, sizeof(*dst));
  787. dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
  788. dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
  789. dst->d_id = be32_to_cpu(src->d_id);
  790. dst->d_blk_hardlimit =
  791. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
  792. dst->d_blk_softlimit =
  793. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
  794. dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
  795. dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
  796. dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
  797. dst->d_icount = be64_to_cpu(src->d_icount);
  798. dst->d_btimer = be32_to_cpu(src->d_btimer);
  799. dst->d_itimer = be32_to_cpu(src->d_itimer);
  800. dst->d_iwarns = be16_to_cpu(src->d_iwarns);
  801. dst->d_bwarns = be16_to_cpu(src->d_bwarns);
  802. dst->d_rtb_hardlimit =
  803. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
  804. dst->d_rtb_softlimit =
  805. XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
  806. dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
  807. dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
  808. dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
  809. /*
  810. * Internally, we don't reset all the timers when quota enforcement
  811. * gets turned off. No need to confuse the user level code,
  812. * so return zeroes in that case.
  813. */
  814. if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
  815. (!XFS_IS_OQUOTA_ENFORCED(mp) &&
  816. (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
  817. dst->d_btimer = 0;
  818. dst->d_itimer = 0;
  819. dst->d_rtbtimer = 0;
  820. }
  821. #ifdef DEBUG
  822. if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
  823. (XFS_IS_OQUOTA_ENFORCED(mp) &&
  824. (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
  825. 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 */