xfs_qm_bhv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Copyright (c) 2000-2004 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_UQUOTA "uquota" /* user quota (IRIX variant) */
  72. #define MNTOPT_GQUOTA "gquota" /* group quota (IRIX variant) */
  73. #define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
  74. #define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
  75. #define MNTOPT_QUOTANOENF "qnoenforce" /* same as uqnoenforce */
  76. STATIC int
  77. xfs_qm_parseargs(
  78. struct bhv_desc *bhv,
  79. char *options,
  80. struct xfs_mount_args *args,
  81. int update)
  82. {
  83. size_t length;
  84. char *local_options = options;
  85. char *this_char;
  86. int error;
  87. int referenced = update;
  88. while ((this_char = strsep(&local_options, ",")) != NULL) {
  89. length = strlen(this_char);
  90. if (local_options)
  91. length++;
  92. if (!strcmp(this_char, MNTOPT_NOQUOTA)) {
  93. args->flags &= ~(XFSMNT_UQUOTAENF|XFSMNT_UQUOTA);
  94. args->flags &= ~(XFSMNT_GQUOTAENF|XFSMNT_GQUOTA);
  95. referenced = update;
  96. } else if (!strcmp(this_char, MNTOPT_QUOTA) ||
  97. !strcmp(this_char, MNTOPT_UQUOTA) ||
  98. !strcmp(this_char, MNTOPT_USRQUOTA)) {
  99. args->flags |= XFSMNT_UQUOTA | XFSMNT_UQUOTAENF;
  100. referenced = 1;
  101. } else if (!strcmp(this_char, MNTOPT_QUOTANOENF) ||
  102. !strcmp(this_char, MNTOPT_UQUOTANOENF)) {
  103. args->flags |= XFSMNT_UQUOTA;
  104. args->flags &= ~XFSMNT_UQUOTAENF;
  105. referenced = 1;
  106. } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
  107. !strcmp(this_char, MNTOPT_GRPQUOTA)) {
  108. args->flags |= XFSMNT_GQUOTA | XFSMNT_GQUOTAENF;
  109. referenced = 1;
  110. } else if (!strcmp(this_char, MNTOPT_GQUOTANOENF)) {
  111. args->flags |= XFSMNT_GQUOTA;
  112. args->flags &= ~XFSMNT_GQUOTAENF;
  113. referenced = 1;
  114. } else {
  115. if (local_options)
  116. *(local_options-1) = ',';
  117. continue;
  118. }
  119. while (length--)
  120. *this_char++ = ',';
  121. }
  122. PVFS_PARSEARGS(BHV_NEXT(bhv), options, args, update, error);
  123. if (!error && !referenced)
  124. bhv_remove_vfsops(bhvtovfs(bhv), VFS_POSITION_QM);
  125. return error;
  126. }
  127. STATIC int
  128. xfs_qm_showargs(
  129. struct bhv_desc *bhv,
  130. struct seq_file *m)
  131. {
  132. struct vfs *vfsp = bhvtovfs(bhv);
  133. struct xfs_mount *mp = XFS_VFSTOM(vfsp);
  134. int error;
  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_GQUOTA_ACCT) {
  141. (mp->m_qflags & XFS_GQUOTA_ENFD) ?
  142. seq_puts(m, "," MNTOPT_GRPQUOTA) :
  143. seq_puts(m, "," MNTOPT_GQUOTANOENF);
  144. }
  145. if (!(mp->m_qflags & (XFS_UQUOTA_ACCT|XFS_GQUOTA_ACCT)))
  146. seq_puts(m, "," MNTOPT_NOQUOTA);
  147. PVFS_SHOWARGS(BHV_NEXT(bhv), m, error);
  148. return error;
  149. }
  150. STATIC int
  151. xfs_qm_mount(
  152. struct bhv_desc *bhv,
  153. struct xfs_mount_args *args,
  154. struct cred *cr)
  155. {
  156. struct vfs *vfsp = bhvtovfs(bhv);
  157. struct xfs_mount *mp = XFS_VFSTOM(vfsp);
  158. int error;
  159. if (args->flags & (XFSMNT_UQUOTA | XFSMNT_GQUOTA))
  160. xfs_qm_mount_quotainit(mp, args->flags);
  161. PVFS_MOUNT(BHV_NEXT(bhv), args, cr, error);
  162. return error;
  163. }
  164. STATIC int
  165. xfs_qm_syncall(
  166. struct bhv_desc *bhv,
  167. int flags,
  168. cred_t *credp)
  169. {
  170. struct vfs *vfsp = bhvtovfs(bhv);
  171. struct xfs_mount *mp = XFS_VFSTOM(vfsp);
  172. int error;
  173. /*
  174. * Get the Quota Manager to flush the dquots.
  175. */
  176. if (XFS_IS_QUOTA_ON(mp)) {
  177. if ((error = xfs_qm_sync(mp, flags))) {
  178. /*
  179. * If we got an IO error, we will be shutting down.
  180. * So, there's nothing more for us to do here.
  181. */
  182. ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
  183. if (XFS_FORCED_SHUTDOWN(mp)) {
  184. return XFS_ERROR(error);
  185. }
  186. }
  187. }
  188. PVFS_SYNC(BHV_NEXT(bhv), flags, credp, error);
  189. return error;
  190. }
  191. /*
  192. * Clear the quotaflags in memory and in the superblock.
  193. */
  194. void
  195. xfs_mount_reset_sbqflags(
  196. xfs_mount_t *mp)
  197. {
  198. xfs_trans_t *tp;
  199. unsigned long s;
  200. mp->m_qflags = 0;
  201. /*
  202. * It is OK to look at sb_qflags here in mount path,
  203. * without SB_LOCK.
  204. */
  205. if (mp->m_sb.sb_qflags == 0)
  206. return;
  207. s = XFS_SB_LOCK(mp);
  208. mp->m_sb.sb_qflags = 0;
  209. XFS_SB_UNLOCK(mp, s);
  210. /*
  211. * if the fs is readonly, let the incore superblock run
  212. * with quotas off but don't flush the update out to disk
  213. */
  214. if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
  215. return;
  216. #ifdef QUOTADEBUG
  217. xfs_fs_cmn_err(CE_NOTE, mp, "Writing superblock quota changes");
  218. #endif
  219. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
  220. if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  221. XFS_DEFAULT_LOG_COUNT)) {
  222. xfs_trans_cancel(tp, 0);
  223. xfs_fs_cmn_err(CE_ALERT, mp,
  224. "xfs_mount_reset_sbqflags: Superblock update failed!");
  225. return;
  226. }
  227. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  228. xfs_trans_commit(tp, 0, NULL);
  229. }
  230. STATIC int
  231. xfs_qm_newmount(
  232. xfs_mount_t *mp,
  233. uint *needquotamount,
  234. uint *quotaflags)
  235. {
  236. uint quotaondisk;
  237. uint uquotaondisk = 0, gquotaondisk = 0;
  238. *quotaflags = 0;
  239. *needquotamount = B_FALSE;
  240. quotaondisk = XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
  241. mp->m_sb.sb_qflags & (XFS_UQUOTA_ACCT|XFS_GQUOTA_ACCT);
  242. if (quotaondisk) {
  243. uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
  244. gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
  245. }
  246. /*
  247. * If the device itself is read-only, we can't allow
  248. * the user to change the state of quota on the mount -
  249. * this would generate a transaction on the ro device,
  250. * which would lead to an I/O error and shutdown
  251. */
  252. if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
  253. (!uquotaondisk && XFS_IS_UQUOTA_ON(mp)) ||
  254. (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
  255. (!gquotaondisk && XFS_IS_GQUOTA_ON(mp))) &&
  256. xfs_dev_is_read_only(mp, "changing quota state")) {
  257. cmn_err(CE_WARN,
  258. "XFS: please mount with%s%s%s.",
  259. (!quotaondisk ? "out quota" : ""),
  260. (uquotaondisk ? " usrquota" : ""),
  261. (gquotaondisk ? " grpquota" : ""));
  262. return XFS_ERROR(EPERM);
  263. }
  264. if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
  265. /*
  266. * Call mount_quotas at this point only if we won't have to do
  267. * a quotacheck.
  268. */
  269. if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
  270. /*
  271. * If an error occured, qm_mount_quotas code
  272. * has already disabled quotas. So, just finish
  273. * mounting, and get on with the boring life
  274. * without disk quotas.
  275. */
  276. xfs_qm_mount_quotas(mp, 0);
  277. } else {
  278. /*
  279. * Clear the quota flags, but remember them. This
  280. * is so that the quota code doesn't get invoked
  281. * before we're ready. This can happen when an
  282. * inode goes inactive and wants to free blocks,
  283. * or via xfs_log_mount_finish.
  284. */
  285. *needquotamount = B_TRUE;
  286. *quotaflags = mp->m_qflags;
  287. mp->m_qflags = 0;
  288. }
  289. }
  290. return 0;
  291. }
  292. STATIC int
  293. xfs_qm_endmount(
  294. xfs_mount_t *mp,
  295. uint needquotamount,
  296. uint quotaflags,
  297. int mfsi_flags)
  298. {
  299. if (needquotamount) {
  300. ASSERT(mp->m_qflags == 0);
  301. mp->m_qflags = quotaflags;
  302. xfs_qm_mount_quotas(mp, mfsi_flags);
  303. }
  304. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  305. if (! (XFS_IS_QUOTA_ON(mp)))
  306. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
  307. else
  308. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
  309. #endif
  310. #ifdef QUOTADEBUG
  311. if (XFS_IS_QUOTA_ON(mp) && xfs_qm_internalqcheck(mp))
  312. cmn_err(CE_WARN, "XFS: mount internalqcheck failed");
  313. #endif
  314. return 0;
  315. }
  316. STATIC void
  317. xfs_qm_dqrele_null(
  318. xfs_dquot_t *dq)
  319. {
  320. /*
  321. * Called from XFS, where we always check first for a NULL dquot.
  322. */
  323. if (!dq)
  324. return;
  325. xfs_qm_dqrele(dq);
  326. }
  327. struct xfs_qmops xfs_qmcore_xfs = {
  328. .xfs_qminit = xfs_qm_newmount,
  329. .xfs_qmdone = xfs_qm_unmount_quotadestroy,
  330. .xfs_qmmount = xfs_qm_endmount,
  331. .xfs_qmunmount = xfs_qm_unmount_quotas,
  332. .xfs_dqrele = xfs_qm_dqrele_null,
  333. .xfs_dqattach = xfs_qm_dqattach,
  334. .xfs_dqdetach = xfs_qm_dqdetach,
  335. .xfs_dqpurgeall = xfs_qm_dqpurge_all,
  336. .xfs_dqvopalloc = xfs_qm_vop_dqalloc,
  337. .xfs_dqvopcreate = xfs_qm_vop_dqattach_and_dqmod_newinode,
  338. .xfs_dqvoprename = xfs_qm_vop_rename_dqattach,
  339. .xfs_dqvopchown = xfs_qm_vop_chown,
  340. .xfs_dqvopchownresv = xfs_qm_vop_chown_reserve,
  341. .xfs_dqtrxops = &xfs_trans_dquot_ops,
  342. };
  343. struct bhv_vfsops xfs_qmops = { {
  344. BHV_IDENTITY_INIT(VFS_BHV_QM, VFS_POSITION_QM),
  345. .vfs_parseargs = xfs_qm_parseargs,
  346. .vfs_showargs = xfs_qm_showargs,
  347. .vfs_mount = xfs_qm_mount,
  348. .vfs_sync = xfs_qm_syncall,
  349. .vfs_quotactl = xfs_qm_quotactl, },
  350. };
  351. void __init
  352. xfs_qm_init(void)
  353. {
  354. static char message[] __initdata =
  355. KERN_INFO "SGI XFS Quota Management subsystem\n";
  356. printk(message);
  357. mutex_init(&xfs_Gqm_lock, MUTEX_DEFAULT, "xfs_qmlock");
  358. vfs_bhv_set_custom(&xfs_qmops, &xfs_qmcore_xfs);
  359. xfs_qm_init_procfs();
  360. }
  361. void __exit
  362. xfs_qm_exit(void)
  363. {
  364. vfs_bhv_clr_custom(&xfs_qmops);
  365. xfs_qm_cleanup_procfs();
  366. if (qm_dqzone)
  367. kmem_cache_destroy(qm_dqzone);
  368. if (qm_dqtrxzone)
  369. kmem_cache_destroy(qm_dqtrxzone);
  370. }