xfs_qm_bhv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 "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_dir.h"
  28. #include "xfs_dir2.h"
  29. #include "xfs_alloc.h"
  30. #include "xfs_dmapi.h"
  31. #include "xfs_quota.h"
  32. #include "xfs_mount.h"
  33. #include "xfs_bmap_btree.h"
  34. #include "xfs_alloc_btree.h"
  35. #include "xfs_ialloc_btree.h"
  36. #include "xfs_dir_sf.h"
  37. #include "xfs_dir2_sf.h"
  38. #include "xfs_attr_sf.h"
  39. #include "xfs_dinode.h"
  40. #include "xfs_inode.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_itable.h"
  43. #include "xfs_btree.h"
  44. #include "xfs_bmap.h"
  45. #include "xfs_rtalloc.h"
  46. #include "xfs_error.h"
  47. #include "xfs_rw.h"
  48. #include "xfs_acl.h"
  49. #include "xfs_cap.h"
  50. #include "xfs_mac.h"
  51. #include "xfs_attr.h"
  52. #include "xfs_buf_item.h"
  53. #include "xfs_qm.h"
  54. #define MNTOPT_QUOTA "quota" /* disk quotas (user) */
  55. #define MNTOPT_NOQUOTA "noquota" /* no quotas */
  56. #define MNTOPT_USRQUOTA "usrquota" /* user quota enabled */
  57. #define MNTOPT_GRPQUOTA "grpquota" /* group quota enabled */
  58. #define MNTOPT_PRJQUOTA "prjquota" /* project quota enabled */
  59. #define MNTOPT_UQUOTA "uquota" /* user quota (IRIX variant) */
  60. #define MNTOPT_GQUOTA "gquota" /* group quota (IRIX variant) */
  61. #define MNTOPT_PQUOTA "pquota" /* project quota (IRIX variant) */
  62. #define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
  63. #define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
  64. #define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
  65. #define MNTOPT_QUOTANOENF "qnoenforce" /* same as uqnoenforce */
  66. STATIC int
  67. xfs_qm_parseargs(
  68. struct bhv_desc *bhv,
  69. char *options,
  70. struct xfs_mount_args *args,
  71. int update)
  72. {
  73. size_t length;
  74. char *local_options = options;
  75. char *this_char;
  76. int error;
  77. int referenced = update;
  78. while ((this_char = strsep(&local_options, ",")) != NULL) {
  79. length = strlen(this_char);
  80. if (local_options)
  81. length++;
  82. if (!strcmp(this_char, MNTOPT_NOQUOTA)) {
  83. args->flags &= ~(XFSMNT_UQUOTAENF|XFSMNT_UQUOTA);
  84. args->flags &= ~(XFSMNT_GQUOTAENF|XFSMNT_GQUOTA);
  85. referenced = update;
  86. } else if (!strcmp(this_char, MNTOPT_QUOTA) ||
  87. !strcmp(this_char, MNTOPT_UQUOTA) ||
  88. !strcmp(this_char, MNTOPT_USRQUOTA)) {
  89. args->flags |= XFSMNT_UQUOTA | XFSMNT_UQUOTAENF;
  90. referenced = 1;
  91. } else if (!strcmp(this_char, MNTOPT_QUOTANOENF) ||
  92. !strcmp(this_char, MNTOPT_UQUOTANOENF)) {
  93. args->flags |= XFSMNT_UQUOTA;
  94. args->flags &= ~XFSMNT_UQUOTAENF;
  95. referenced = 1;
  96. } else if (!strcmp(this_char, MNTOPT_PQUOTA) ||
  97. !strcmp(this_char, MNTOPT_PRJQUOTA)) {
  98. args->flags |= XFSMNT_PQUOTA | XFSMNT_PQUOTAENF;
  99. referenced = 1;
  100. } else if (!strcmp(this_char, MNTOPT_PQUOTANOENF)) {
  101. args->flags |= XFSMNT_PQUOTA;
  102. args->flags &= ~XFSMNT_PQUOTAENF;
  103. referenced = 1;
  104. } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
  105. !strcmp(this_char, MNTOPT_GRPQUOTA)) {
  106. args->flags |= XFSMNT_GQUOTA | XFSMNT_GQUOTAENF;
  107. referenced = 1;
  108. } else if (!strcmp(this_char, MNTOPT_GQUOTANOENF)) {
  109. args->flags |= XFSMNT_GQUOTA;
  110. args->flags &= ~XFSMNT_GQUOTAENF;
  111. referenced = 1;
  112. } else {
  113. if (local_options)
  114. *(local_options-1) = ',';
  115. continue;
  116. }
  117. while (length--)
  118. *this_char++ = ',';
  119. }
  120. if ((args->flags & XFSMNT_GQUOTA) && (args->flags & XFSMNT_PQUOTA)) {
  121. cmn_err(CE_WARN,
  122. "XFS: cannot mount with both project and group quota");
  123. return XFS_ERROR(EINVAL);
  124. }
  125. PVFS_PARSEARGS(BHV_NEXT(bhv), options, args, update, error);
  126. if (!error && !referenced)
  127. bhv_remove_vfsops(bhvtovfs(bhv), VFS_POSITION_QM);
  128. return error;
  129. }
  130. STATIC int
  131. xfs_qm_showargs(
  132. struct bhv_desc *bhv,
  133. struct seq_file *m)
  134. {
  135. struct vfs *vfsp = bhvtovfs(bhv);
  136. struct xfs_mount *mp = XFS_VFSTOM(vfsp);
  137. int error;
  138. if (mp->m_qflags & XFS_UQUOTA_ACCT) {
  139. (mp->m_qflags & XFS_UQUOTA_ENFD) ?
  140. seq_puts(m, "," MNTOPT_USRQUOTA) :
  141. seq_puts(m, "," MNTOPT_UQUOTANOENF);
  142. }
  143. if (mp->m_qflags & XFS_PQUOTA_ACCT) {
  144. (mp->m_qflags & XFS_OQUOTA_ENFD) ?
  145. seq_puts(m, "," MNTOPT_PRJQUOTA) :
  146. seq_puts(m, "," MNTOPT_PQUOTANOENF);
  147. }
  148. if (mp->m_qflags & XFS_GQUOTA_ACCT) {
  149. (mp->m_qflags & XFS_OQUOTA_ENFD) ?
  150. seq_puts(m, "," MNTOPT_GRPQUOTA) :
  151. seq_puts(m, "," MNTOPT_GQUOTANOENF);
  152. }
  153. if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
  154. seq_puts(m, "," MNTOPT_NOQUOTA);
  155. PVFS_SHOWARGS(BHV_NEXT(bhv), m, error);
  156. return error;
  157. }
  158. STATIC int
  159. xfs_qm_mount(
  160. struct bhv_desc *bhv,
  161. struct xfs_mount_args *args,
  162. struct cred *cr)
  163. {
  164. struct vfs *vfsp = bhvtovfs(bhv);
  165. struct xfs_mount *mp = XFS_VFSTOM(vfsp);
  166. int error;
  167. if (args->flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA | XFSMNT_PQUOTA))
  168. xfs_qm_mount_quotainit(mp, args->flags);
  169. PVFS_MOUNT(BHV_NEXT(bhv), args, cr, error);
  170. return error;
  171. }
  172. STATIC int
  173. xfs_qm_syncall(
  174. struct bhv_desc *bhv,
  175. int flags,
  176. cred_t *credp)
  177. {
  178. struct vfs *vfsp = bhvtovfs(bhv);
  179. struct xfs_mount *mp = XFS_VFSTOM(vfsp);
  180. int error;
  181. /*
  182. * Get the Quota Manager to flush the dquots.
  183. */
  184. if (XFS_IS_QUOTA_ON(mp)) {
  185. if ((error = xfs_qm_sync(mp, flags))) {
  186. /*
  187. * If we got an IO error, we will be shutting down.
  188. * So, there's nothing more for us to do here.
  189. */
  190. ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
  191. if (XFS_FORCED_SHUTDOWN(mp)) {
  192. return XFS_ERROR(error);
  193. }
  194. }
  195. }
  196. PVFS_SYNC(BHV_NEXT(bhv), flags, credp, error);
  197. return error;
  198. }
  199. STATIC int
  200. xfs_qm_newmount(
  201. xfs_mount_t *mp,
  202. uint *needquotamount,
  203. uint *quotaflags)
  204. {
  205. uint quotaondisk;
  206. uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
  207. *quotaflags = 0;
  208. *needquotamount = B_FALSE;
  209. quotaondisk = XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
  210. (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
  211. if (quotaondisk) {
  212. uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
  213. pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
  214. gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
  215. }
  216. /*
  217. * If the device itself is read-only, we can't allow
  218. * the user to change the state of quota on the mount -
  219. * this would generate a transaction on the ro device,
  220. * which would lead to an I/O error and shutdown
  221. */
  222. if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
  223. (!uquotaondisk && XFS_IS_UQUOTA_ON(mp)) ||
  224. (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
  225. (!pquotaondisk && XFS_IS_PQUOTA_ON(mp)) ||
  226. (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
  227. (!gquotaondisk && XFS_IS_OQUOTA_ON(mp))) &&
  228. xfs_dev_is_read_only(mp, "changing quota state")) {
  229. cmn_err(CE_WARN,
  230. "XFS: please mount with%s%s%s%s.",
  231. (!quotaondisk ? "out quota" : ""),
  232. (uquotaondisk ? " usrquota" : ""),
  233. (pquotaondisk ? " prjquota" : ""),
  234. (gquotaondisk ? " grpquota" : ""));
  235. return XFS_ERROR(EPERM);
  236. }
  237. if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
  238. /*
  239. * Call mount_quotas at this point only if we won't have to do
  240. * a quotacheck.
  241. */
  242. if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
  243. /*
  244. * If an error occured, qm_mount_quotas code
  245. * has already disabled quotas. So, just finish
  246. * mounting, and get on with the boring life
  247. * without disk quotas.
  248. */
  249. xfs_qm_mount_quotas(mp, 0);
  250. } else {
  251. /*
  252. * Clear the quota flags, but remember them. This
  253. * is so that the quota code doesn't get invoked
  254. * before we're ready. This can happen when an
  255. * inode goes inactive and wants to free blocks,
  256. * or via xfs_log_mount_finish.
  257. */
  258. *needquotamount = B_TRUE;
  259. *quotaflags = mp->m_qflags;
  260. mp->m_qflags = 0;
  261. }
  262. }
  263. return 0;
  264. }
  265. STATIC int
  266. xfs_qm_endmount(
  267. xfs_mount_t *mp,
  268. uint needquotamount,
  269. uint quotaflags,
  270. int mfsi_flags)
  271. {
  272. if (needquotamount) {
  273. ASSERT(mp->m_qflags == 0);
  274. mp->m_qflags = quotaflags;
  275. xfs_qm_mount_quotas(mp, mfsi_flags);
  276. }
  277. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  278. if (! (XFS_IS_QUOTA_ON(mp)))
  279. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
  280. else
  281. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
  282. #endif
  283. #ifdef QUOTADEBUG
  284. if (XFS_IS_QUOTA_ON(mp) && xfs_qm_internalqcheck(mp))
  285. cmn_err(CE_WARN, "XFS: mount internalqcheck failed");
  286. #endif
  287. return 0;
  288. }
  289. STATIC void
  290. xfs_qm_dqrele_null(
  291. xfs_dquot_t *dq)
  292. {
  293. /*
  294. * Called from XFS, where we always check first for a NULL dquot.
  295. */
  296. if (!dq)
  297. return;
  298. xfs_qm_dqrele(dq);
  299. }
  300. STATIC struct xfs_qmops xfs_qmcore_xfs = {
  301. .xfs_qminit = xfs_qm_newmount,
  302. .xfs_qmdone = xfs_qm_unmount_quotadestroy,
  303. .xfs_qmmount = xfs_qm_endmount,
  304. .xfs_qmunmount = xfs_qm_unmount_quotas,
  305. .xfs_dqrele = xfs_qm_dqrele_null,
  306. .xfs_dqattach = xfs_qm_dqattach,
  307. .xfs_dqdetach = xfs_qm_dqdetach,
  308. .xfs_dqpurgeall = xfs_qm_dqpurge_all,
  309. .xfs_dqvopalloc = xfs_qm_vop_dqalloc,
  310. .xfs_dqvopcreate = xfs_qm_vop_dqattach_and_dqmod_newinode,
  311. .xfs_dqvoprename = xfs_qm_vop_rename_dqattach,
  312. .xfs_dqvopchown = xfs_qm_vop_chown,
  313. .xfs_dqvopchownresv = xfs_qm_vop_chown_reserve,
  314. .xfs_dqtrxops = &xfs_trans_dquot_ops,
  315. };
  316. struct bhv_vfsops xfs_qmops = { {
  317. BHV_IDENTITY_INIT(VFS_BHV_QM, VFS_POSITION_QM),
  318. .vfs_parseargs = xfs_qm_parseargs,
  319. .vfs_showargs = xfs_qm_showargs,
  320. .vfs_mount = xfs_qm_mount,
  321. .vfs_sync = xfs_qm_syncall,
  322. .vfs_quotactl = xfs_qm_quotactl, },
  323. };
  324. void __init
  325. xfs_qm_init(void)
  326. {
  327. static char message[] __initdata =
  328. KERN_INFO "SGI XFS Quota Management subsystem\n";
  329. printk(message);
  330. mutex_init(&xfs_Gqm_lock);
  331. vfs_bhv_set_custom(&xfs_qmops, &xfs_qmcore_xfs);
  332. xfs_qm_init_procfs();
  333. }
  334. void __exit
  335. xfs_qm_exit(void)
  336. {
  337. vfs_bhv_clr_custom(&xfs_qmops);
  338. xfs_qm_cleanup_procfs();
  339. if (qm_dqzone)
  340. kmem_cache_destroy(qm_dqzone);
  341. if (qm_dqtrxzone)
  342. kmem_cache_destroy(qm_dqtrxzone);
  343. }