xfs_qm_syscalls.c 39 KB

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