xfs_qm_bhv.c 11 KB

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