xfs_qm_syscalls.c 39 KB

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