xfs_qm_syscalls.c 40 KB

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