xfs_qm_syscalls.c 39 KB

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