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