xfs_qm_bhv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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_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. /*
  213. * Clear the quotaflags in memory and in the superblock.
  214. */
  215. void
  216. xfs_mount_reset_sbqflags(
  217. xfs_mount_t *mp)
  218. {
  219. xfs_trans_t *tp;
  220. unsigned long s;
  221. mp->m_qflags = 0;
  222. /*
  223. * It is OK to look at sb_qflags here in mount path,
  224. * without SB_LOCK.
  225. */
  226. if (mp->m_sb.sb_qflags == 0)
  227. return;
  228. s = XFS_SB_LOCK(mp);
  229. mp->m_sb.sb_qflags = 0;
  230. XFS_SB_UNLOCK(mp, s);
  231. /*
  232. * if the fs is readonly, let the incore superblock run
  233. * with quotas off but don't flush the update out to disk
  234. */
  235. if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
  236. return;
  237. #ifdef QUOTADEBUG
  238. xfs_fs_cmn_err(CE_NOTE, mp, "Writing superblock quota changes");
  239. #endif
  240. tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
  241. if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  242. XFS_DEFAULT_LOG_COUNT)) {
  243. xfs_trans_cancel(tp, 0);
  244. xfs_fs_cmn_err(CE_ALERT, mp,
  245. "xfs_mount_reset_sbqflags: Superblock update failed!");
  246. return;
  247. }
  248. xfs_mod_sb(tp, XFS_SB_QFLAGS);
  249. xfs_trans_commit(tp, 0, NULL);
  250. }
  251. STATIC int
  252. xfs_qm_newmount(
  253. xfs_mount_t *mp,
  254. uint *needquotamount,
  255. uint *quotaflags)
  256. {
  257. uint quotaondisk;
  258. uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
  259. *quotaflags = 0;
  260. *needquotamount = B_FALSE;
  261. quotaondisk = XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
  262. (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
  263. if (quotaondisk) {
  264. uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
  265. pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
  266. gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
  267. }
  268. /*
  269. * If the device itself is read-only, we can't allow
  270. * the user to change the state of quota on the mount -
  271. * this would generate a transaction on the ro device,
  272. * which would lead to an I/O error and shutdown
  273. */
  274. if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
  275. (!uquotaondisk && XFS_IS_UQUOTA_ON(mp)) ||
  276. (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
  277. (!pquotaondisk && XFS_IS_PQUOTA_ON(mp)) ||
  278. (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
  279. (!gquotaondisk && XFS_IS_OQUOTA_ON(mp))) &&
  280. xfs_dev_is_read_only(mp, "changing quota state")) {
  281. cmn_err(CE_WARN,
  282. "XFS: please mount with%s%s%s%s.",
  283. (!quotaondisk ? "out quota" : ""),
  284. (uquotaondisk ? " usrquota" : ""),
  285. (pquotaondisk ? " prjquota" : ""),
  286. (gquotaondisk ? " grpquota" : ""));
  287. return XFS_ERROR(EPERM);
  288. }
  289. if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
  290. /*
  291. * Call mount_quotas at this point only if we won't have to do
  292. * a quotacheck.
  293. */
  294. if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
  295. /*
  296. * If an error occured, qm_mount_quotas code
  297. * has already disabled quotas. So, just finish
  298. * mounting, and get on with the boring life
  299. * without disk quotas.
  300. */
  301. xfs_qm_mount_quotas(mp, 0);
  302. } else {
  303. /*
  304. * Clear the quota flags, but remember them. This
  305. * is so that the quota code doesn't get invoked
  306. * before we're ready. This can happen when an
  307. * inode goes inactive and wants to free blocks,
  308. * or via xfs_log_mount_finish.
  309. */
  310. *needquotamount = B_TRUE;
  311. *quotaflags = mp->m_qflags;
  312. mp->m_qflags = 0;
  313. }
  314. }
  315. return 0;
  316. }
  317. STATIC int
  318. xfs_qm_endmount(
  319. xfs_mount_t *mp,
  320. uint needquotamount,
  321. uint quotaflags,
  322. int mfsi_flags)
  323. {
  324. if (needquotamount) {
  325. ASSERT(mp->m_qflags == 0);
  326. mp->m_qflags = quotaflags;
  327. xfs_qm_mount_quotas(mp, mfsi_flags);
  328. }
  329. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  330. if (! (XFS_IS_QUOTA_ON(mp)))
  331. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
  332. else
  333. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
  334. #endif
  335. #ifdef QUOTADEBUG
  336. if (XFS_IS_QUOTA_ON(mp) && xfs_qm_internalqcheck(mp))
  337. cmn_err(CE_WARN, "XFS: mount internalqcheck failed");
  338. #endif
  339. return 0;
  340. }
  341. STATIC void
  342. xfs_qm_dqrele_null(
  343. xfs_dquot_t *dq)
  344. {
  345. /*
  346. * Called from XFS, where we always check first for a NULL dquot.
  347. */
  348. if (!dq)
  349. return;
  350. xfs_qm_dqrele(dq);
  351. }
  352. STATIC struct xfs_qmops xfs_qmcore_xfs = {
  353. .xfs_qminit = xfs_qm_newmount,
  354. .xfs_qmdone = xfs_qm_unmount_quotadestroy,
  355. .xfs_qmmount = xfs_qm_endmount,
  356. .xfs_qmunmount = xfs_qm_unmount_quotas,
  357. .xfs_dqrele = xfs_qm_dqrele_null,
  358. .xfs_dqattach = xfs_qm_dqattach,
  359. .xfs_dqdetach = xfs_qm_dqdetach,
  360. .xfs_dqpurgeall = xfs_qm_dqpurge_all,
  361. .xfs_dqvopalloc = xfs_qm_vop_dqalloc,
  362. .xfs_dqvopcreate = xfs_qm_vop_dqattach_and_dqmod_newinode,
  363. .xfs_dqvoprename = xfs_qm_vop_rename_dqattach,
  364. .xfs_dqvopchown = xfs_qm_vop_chown,
  365. .xfs_dqvopchownresv = xfs_qm_vop_chown_reserve,
  366. .xfs_dqtrxops = &xfs_trans_dquot_ops,
  367. };
  368. struct bhv_vfsops xfs_qmops = { {
  369. BHV_IDENTITY_INIT(VFS_BHV_QM, VFS_POSITION_QM),
  370. .vfs_parseargs = xfs_qm_parseargs,
  371. .vfs_showargs = xfs_qm_showargs,
  372. .vfs_mount = xfs_qm_mount,
  373. .vfs_sync = xfs_qm_syncall,
  374. .vfs_quotactl = xfs_qm_quotactl, },
  375. };
  376. void __init
  377. xfs_qm_init(void)
  378. {
  379. static char message[] __initdata =
  380. KERN_INFO "SGI XFS Quota Management subsystem\n";
  381. printk(message);
  382. mutex_init(&xfs_Gqm_lock, MUTEX_DEFAULT, "xfs_qmlock");
  383. vfs_bhv_set_custom(&xfs_qmops, &xfs_qmcore_xfs);
  384. xfs_qm_init_procfs();
  385. }
  386. void __exit
  387. xfs_qm_exit(void)
  388. {
  389. vfs_bhv_clr_custom(&xfs_qmops);
  390. xfs_qm_cleanup_procfs();
  391. if (qm_dqzone)
  392. kmem_cache_destroy(qm_dqzone);
  393. if (qm_dqtrxzone)
  394. kmem_cache_destroy(qm_dqtrxzone);
  395. }