xfs_qm_syscalls.c 39 KB

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