xfs_qm_bhv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) 2000-2006 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 "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_clnt.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_btree.h"
  42. #include "xfs_bmap.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_qm.h"
  52. #define MNTOPT_QUOTA "quota" /* disk quotas (user) */
  53. #define MNTOPT_NOQUOTA "noquota" /* no quotas */
  54. #define MNTOPT_USRQUOTA "usrquota" /* user quota enabled */
  55. #define MNTOPT_GRPQUOTA "grpquota" /* group quota enabled */
  56. #define MNTOPT_PRJQUOTA "prjquota" /* project quota enabled */
  57. #define MNTOPT_UQUOTA "uquota" /* user quota (IRIX variant) */
  58. #define MNTOPT_GQUOTA "gquota" /* group quota (IRIX variant) */
  59. #define MNTOPT_PQUOTA "pquota" /* project quota (IRIX variant) */
  60. #define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
  61. #define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
  62. #define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
  63. #define MNTOPT_QUOTANOENF "qnoenforce" /* same as uqnoenforce */
  64. STATIC int
  65. xfs_qm_parseargs(
  66. struct bhv_desc *bhv,
  67. char *options,
  68. struct xfs_mount_args *args,
  69. int update)
  70. {
  71. size_t length;
  72. char *local_options = options;
  73. char *this_char;
  74. int error;
  75. int referenced = update;
  76. while ((this_char = strsep(&local_options, ",")) != NULL) {
  77. length = strlen(this_char);
  78. if (local_options)
  79. length++;
  80. if (!strcmp(this_char, MNTOPT_NOQUOTA)) {
  81. args->flags &= ~(XFSMNT_UQUOTAENF|XFSMNT_UQUOTA);
  82. args->flags &= ~(XFSMNT_GQUOTAENF|XFSMNT_GQUOTA);
  83. referenced = update;
  84. } else if (!strcmp(this_char, MNTOPT_QUOTA) ||
  85. !strcmp(this_char, MNTOPT_UQUOTA) ||
  86. !strcmp(this_char, MNTOPT_USRQUOTA)) {
  87. args->flags |= XFSMNT_UQUOTA | XFSMNT_UQUOTAENF;
  88. referenced = 1;
  89. } else if (!strcmp(this_char, MNTOPT_QUOTANOENF) ||
  90. !strcmp(this_char, MNTOPT_UQUOTANOENF)) {
  91. args->flags |= XFSMNT_UQUOTA;
  92. args->flags &= ~XFSMNT_UQUOTAENF;
  93. referenced = 1;
  94. } else if (!strcmp(this_char, MNTOPT_PQUOTA) ||
  95. !strcmp(this_char, MNTOPT_PRJQUOTA)) {
  96. args->flags |= XFSMNT_PQUOTA | XFSMNT_PQUOTAENF;
  97. referenced = 1;
  98. } else if (!strcmp(this_char, MNTOPT_PQUOTANOENF)) {
  99. args->flags |= XFSMNT_PQUOTA;
  100. args->flags &= ~XFSMNT_PQUOTAENF;
  101. referenced = 1;
  102. } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
  103. !strcmp(this_char, MNTOPT_GRPQUOTA)) {
  104. args->flags |= XFSMNT_GQUOTA | XFSMNT_GQUOTAENF;
  105. referenced = 1;
  106. } else if (!strcmp(this_char, MNTOPT_GQUOTANOENF)) {
  107. args->flags |= XFSMNT_GQUOTA;
  108. args->flags &= ~XFSMNT_GQUOTAENF;
  109. referenced = 1;
  110. } else {
  111. if (local_options)
  112. *(local_options-1) = ',';
  113. continue;
  114. }
  115. while (length--)
  116. *this_char++ = ',';
  117. }
  118. if ((args->flags & XFSMNT_GQUOTA) && (args->flags & XFSMNT_PQUOTA)) {
  119. cmn_err(CE_WARN,
  120. "XFS: cannot mount with both project and group quota");
  121. return XFS_ERROR(EINVAL);
  122. }
  123. error = bhv_next_vfs_parseargs(BHV_NEXT(bhv), options, args, update);
  124. if (!error && !referenced)
  125. bhv_remove_vfsops(bhvtovfs(bhv), VFS_POSITION_QM);
  126. return error;
  127. }
  128. STATIC int
  129. xfs_qm_showargs(
  130. struct bhv_desc *bhv,
  131. struct seq_file *m)
  132. {
  133. struct bhv_vfs *vfsp = bhvtovfs(bhv);
  134. struct xfs_mount *mp = XFS_VFSTOM(vfsp);
  135. if (mp->m_qflags & XFS_UQUOTA_ACCT) {
  136. (mp->m_qflags & XFS_UQUOTA_ENFD) ?
  137. seq_puts(m, "," MNTOPT_USRQUOTA) :
  138. seq_puts(m, "," MNTOPT_UQUOTANOENF);
  139. }
  140. if (mp->m_qflags & XFS_PQUOTA_ACCT) {
  141. (mp->m_qflags & XFS_OQUOTA_ENFD) ?
  142. seq_puts(m, "," MNTOPT_PRJQUOTA) :
  143. seq_puts(m, "," MNTOPT_PQUOTANOENF);
  144. }
  145. if (mp->m_qflags & XFS_GQUOTA_ACCT) {
  146. (mp->m_qflags & XFS_OQUOTA_ENFD) ?
  147. seq_puts(m, "," MNTOPT_GRPQUOTA) :
  148. seq_puts(m, "," MNTOPT_GQUOTANOENF);
  149. }
  150. if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
  151. seq_puts(m, "," MNTOPT_NOQUOTA);
  152. return bhv_next_vfs_showargs(BHV_NEXT(bhv), m);
  153. }
  154. STATIC int
  155. xfs_qm_mount(
  156. struct bhv_desc *bhv,
  157. struct xfs_mount_args *args,
  158. struct cred *cr)
  159. {
  160. struct bhv_vfs *vfsp = bhvtovfs(bhv);
  161. struct xfs_mount *mp = XFS_VFSTOM(vfsp);
  162. if (args->flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA | XFSMNT_PQUOTA))
  163. xfs_qm_mount_quotainit(mp, args->flags);
  164. return bhv_next_vfs_mount(BHV_NEXT(bhv), args, cr);
  165. }
  166. /*
  167. * Directory tree accounting is implemented using project quotas, where
  168. * the project identifier is inherited from parent directories.
  169. * A statvfs (df, etc.) of a directory that is using project quota should
  170. * return a statvfs of the project, not the entire filesystem.
  171. * This makes such trees appear as if they are filesystems in themselves.
  172. */
  173. STATIC int
  174. xfs_qm_statvfs(
  175. struct bhv_desc *bhv,
  176. bhv_statvfs_t *statp,
  177. struct bhv_vnode *vnode)
  178. {
  179. xfs_mount_t *mp;
  180. xfs_inode_t *ip;
  181. xfs_dquot_t *dqp;
  182. xfs_disk_dquot_t *dp;
  183. __uint64_t limit;
  184. int error;
  185. error = bhv_next_vfs_statvfs(BHV_NEXT(bhv), statp, vnode);
  186. if (error || !vnode)
  187. return error;
  188. mp = xfs_vfstom(bhvtovfs(bhv));
  189. ip = xfs_vtoi(vnode);
  190. if (!(ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  191. return 0;
  192. if (!(mp->m_qflags & XFS_PQUOTA_ACCT))
  193. return 0;
  194. if (!(mp->m_qflags & XFS_OQUOTA_ENFD))
  195. return 0;
  196. if (xfs_qm_dqget(mp, NULL, ip->i_d.di_projid, XFS_DQ_PROJ, 0, &dqp))
  197. return 0;
  198. dp = &dqp->q_core;
  199. limit = dp->d_blk_softlimit ?
  200. be64_to_cpu(dp->d_blk_softlimit) :
  201. be64_to_cpu(dp->d_blk_hardlimit);
  202. if (limit && statp->f_blocks > limit) {
  203. statp->f_blocks = limit;
  204. statp->f_bfree =
  205. (statp->f_blocks > be64_to_cpu(dp->d_bcount)) ?
  206. (statp->f_blocks - be64_to_cpu(dp->d_bcount)) : 0;
  207. }
  208. limit = dp->d_ino_softlimit ?
  209. be64_to_cpu(dp->d_ino_softlimit) :
  210. be64_to_cpu(dp->d_ino_hardlimit);
  211. if (limit && statp->f_files > limit) {
  212. statp->f_files = limit;
  213. statp->f_ffree =
  214. (statp->f_files > be64_to_cpu(dp->d_icount)) ?
  215. (statp->f_ffree - be64_to_cpu(dp->d_icount)) : 0;
  216. }
  217. xfs_qm_dqput(dqp);
  218. return 0;
  219. }
  220. STATIC int
  221. xfs_qm_syncall(
  222. struct bhv_desc *bhv,
  223. int flags,
  224. cred_t *credp)
  225. {
  226. struct bhv_vfs *vfsp = bhvtovfs(bhv);
  227. struct xfs_mount *mp = XFS_VFSTOM(vfsp);
  228. int error;
  229. /*
  230. * Get the Quota Manager to flush the dquots.
  231. */
  232. if (XFS_IS_QUOTA_ON(mp)) {
  233. if ((error = xfs_qm_sync(mp, flags))) {
  234. /*
  235. * If we got an IO error, we will be shutting down.
  236. * So, there's nothing more for us to do here.
  237. */
  238. ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
  239. if (XFS_FORCED_SHUTDOWN(mp)) {
  240. return XFS_ERROR(error);
  241. }
  242. }
  243. }
  244. return bhv_next_vfs_sync(BHV_NEXT(bhv), flags, credp);
  245. }
  246. STATIC int
  247. xfs_qm_newmount(
  248. xfs_mount_t *mp,
  249. uint *needquotamount,
  250. uint *quotaflags)
  251. {
  252. uint quotaondisk;
  253. uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
  254. *quotaflags = 0;
  255. *needquotamount = B_FALSE;
  256. quotaondisk = XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
  257. (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
  258. if (quotaondisk) {
  259. uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
  260. pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
  261. gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
  262. }
  263. /*
  264. * If the device itself is read-only, we can't allow
  265. * the user to change the state of quota on the mount -
  266. * this would generate a transaction on the ro device,
  267. * which would lead to an I/O error and shutdown
  268. */
  269. if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
  270. (!uquotaondisk && XFS_IS_UQUOTA_ON(mp)) ||
  271. (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
  272. (!pquotaondisk && XFS_IS_PQUOTA_ON(mp)) ||
  273. (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
  274. (!gquotaondisk && XFS_IS_OQUOTA_ON(mp))) &&
  275. xfs_dev_is_read_only(mp, "changing quota state")) {
  276. cmn_err(CE_WARN,
  277. "XFS: please mount with%s%s%s%s.",
  278. (!quotaondisk ? "out quota" : ""),
  279. (uquotaondisk ? " usrquota" : ""),
  280. (pquotaondisk ? " prjquota" : ""),
  281. (gquotaondisk ? " grpquota" : ""));
  282. return XFS_ERROR(EPERM);
  283. }
  284. if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
  285. /*
  286. * Call mount_quotas at this point only if we won't have to do
  287. * a quotacheck.
  288. */
  289. if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
  290. /*
  291. * If an error occured, qm_mount_quotas code
  292. * has already disabled quotas. So, just finish
  293. * mounting, and get on with the boring life
  294. * without disk quotas.
  295. */
  296. xfs_qm_mount_quotas(mp, 0);
  297. } else {
  298. /*
  299. * Clear the quota flags, but remember them. This
  300. * is so that the quota code doesn't get invoked
  301. * before we're ready. This can happen when an
  302. * inode goes inactive and wants to free blocks,
  303. * or via xfs_log_mount_finish.
  304. */
  305. *needquotamount = B_TRUE;
  306. *quotaflags = mp->m_qflags;
  307. mp->m_qflags = 0;
  308. }
  309. }
  310. return 0;
  311. }
  312. STATIC int
  313. xfs_qm_endmount(
  314. xfs_mount_t *mp,
  315. uint needquotamount,
  316. uint quotaflags,
  317. int mfsi_flags)
  318. {
  319. if (needquotamount) {
  320. ASSERT(mp->m_qflags == 0);
  321. mp->m_qflags = quotaflags;
  322. xfs_qm_mount_quotas(mp, mfsi_flags);
  323. }
  324. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  325. if (! (XFS_IS_QUOTA_ON(mp)))
  326. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
  327. else
  328. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
  329. #endif
  330. #ifdef QUOTADEBUG
  331. if (XFS_IS_QUOTA_ON(mp) && xfs_qm_internalqcheck(mp))
  332. cmn_err(CE_WARN, "XFS: mount internalqcheck failed");
  333. #endif
  334. return 0;
  335. }
  336. STATIC void
  337. xfs_qm_dqrele_null(
  338. xfs_dquot_t *dq)
  339. {
  340. /*
  341. * Called from XFS, where we always check first for a NULL dquot.
  342. */
  343. if (!dq)
  344. return;
  345. xfs_qm_dqrele(dq);
  346. }
  347. STATIC struct xfs_qmops xfs_qmcore_xfs = {
  348. .xfs_qminit = xfs_qm_newmount,
  349. .xfs_qmdone = xfs_qm_unmount_quotadestroy,
  350. .xfs_qmmount = xfs_qm_endmount,
  351. .xfs_qmunmount = xfs_qm_unmount_quotas,
  352. .xfs_dqrele = xfs_qm_dqrele_null,
  353. .xfs_dqattach = xfs_qm_dqattach,
  354. .xfs_dqdetach = xfs_qm_dqdetach,
  355. .xfs_dqpurgeall = xfs_qm_dqpurge_all,
  356. .xfs_dqvopalloc = xfs_qm_vop_dqalloc,
  357. .xfs_dqvopcreate = xfs_qm_vop_dqattach_and_dqmod_newinode,
  358. .xfs_dqvoprename = xfs_qm_vop_rename_dqattach,
  359. .xfs_dqvopchown = xfs_qm_vop_chown,
  360. .xfs_dqvopchownresv = xfs_qm_vop_chown_reserve,
  361. .xfs_dqtrxops = &xfs_trans_dquot_ops,
  362. };
  363. struct bhv_module_vfsops xfs_qmops = { {
  364. BHV_IDENTITY_INIT(VFS_BHV_QM, VFS_POSITION_QM),
  365. .vfs_parseargs = xfs_qm_parseargs,
  366. .vfs_showargs = xfs_qm_showargs,
  367. .vfs_mount = xfs_qm_mount,
  368. .vfs_statvfs = xfs_qm_statvfs,
  369. .vfs_sync = xfs_qm_syncall,
  370. .vfs_quotactl = xfs_qm_quotactl, },
  371. };
  372. void __init
  373. xfs_qm_init(void)
  374. {
  375. static char message[] __initdata =
  376. KERN_INFO "SGI XFS Quota Management subsystem\n";
  377. printk(message);
  378. mutex_init(&xfs_Gqm_lock);
  379. vfs_bhv_set_custom(&xfs_qmops, &xfs_qmcore_xfs);
  380. xfs_qm_init_procfs();
  381. }
  382. void __exit
  383. xfs_qm_exit(void)
  384. {
  385. vfs_bhv_clr_custom(&xfs_qmops);
  386. xfs_qm_cleanup_procfs();
  387. if (qm_dqzone)
  388. kmem_zone_destroy(qm_dqzone);
  389. if (qm_dqtrxzone)
  390. kmem_zone_destroy(qm_dqtrxzone);
  391. }