xfs_qm_syscalls.c 41 KB

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