xfs_qm_syscalls.c 39 KB

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