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_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. /*
  173. * Directory tree accounting is implemented using project quotas, where
  174. * the project identifier is inherited from parent directories.
  175. * A statvfs (df, etc.) of a directory that is using project quota should
  176. * return a statvfs of the project, not the entire filesystem.
  177. * This makes such trees appear as if they are filesystems in themselves.
  178. */
  179. STATIC int
  180. xfs_qm_statvfs(
  181. struct bhv_desc *bhv,
  182. xfs_statfs_t *statp,
  183. struct vnode *vnode)
  184. {
  185. xfs_mount_t *mp;
  186. xfs_inode_t *ip;
  187. xfs_dquot_t *dqp;
  188. xfs_disk_dquot_t *dp;
  189. __uint64_t limit;
  190. int error;
  191. error = PVFS_STATVFS(BHV_NEXT(bhv), statp, vnode);
  192. if (error || !vnode)
  193. return error;
  194. mp = XFS_BHVTOM(bhv);
  195. ip = xfs_vtoi(vnode);
  196. if (!(ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  197. return 0;
  198. if (!(mp->m_qflags & XFS_PQUOTA_ACCT))
  199. return 0;
  200. if (!(mp->m_qflags & XFS_OQUOTA_ENFD))
  201. return 0;
  202. if (xfs_qm_dqget(mp, NULL, ip->i_d.di_projid, XFS_DQ_PROJ, 0, &dqp))
  203. return 0;
  204. dp = &dqp->q_core;
  205. limit = dp->d_blk_softlimit ? dp->d_blk_softlimit : dp->d_blk_hardlimit;
  206. if (limit && statp->f_blocks > limit) {
  207. statp->f_blocks = limit;
  208. statp->f_bfree = (statp->f_blocks > dp->d_bcount) ?
  209. (statp->f_blocks - dp->d_bcount) : 0;
  210. }
  211. limit = dp->d_ino_softlimit ? dp->d_ino_softlimit : dp->d_ino_hardlimit;
  212. if (limit && statp->f_files > limit) {
  213. statp->f_files = limit;
  214. statp->f_ffree = (statp->f_files > dp->d_icount) ?
  215. (statp->f_ffree - 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 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. PVFS_SYNC(BHV_NEXT(bhv), flags, credp, error);
  245. return error;
  246. }
  247. STATIC int
  248. xfs_qm_newmount(
  249. xfs_mount_t *mp,
  250. uint *needquotamount,
  251. uint *quotaflags)
  252. {
  253. uint quotaondisk;
  254. uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
  255. *quotaflags = 0;
  256. *needquotamount = B_FALSE;
  257. quotaondisk = XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
  258. (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
  259. if (quotaondisk) {
  260. uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
  261. pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
  262. gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
  263. }
  264. /*
  265. * If the device itself is read-only, we can't allow
  266. * the user to change the state of quota on the mount -
  267. * this would generate a transaction on the ro device,
  268. * which would lead to an I/O error and shutdown
  269. */
  270. if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
  271. (!uquotaondisk && XFS_IS_UQUOTA_ON(mp)) ||
  272. (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
  273. (!pquotaondisk && XFS_IS_PQUOTA_ON(mp)) ||
  274. (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
  275. (!gquotaondisk && XFS_IS_OQUOTA_ON(mp))) &&
  276. xfs_dev_is_read_only(mp, "changing quota state")) {
  277. cmn_err(CE_WARN,
  278. "XFS: please mount with%s%s%s%s.",
  279. (!quotaondisk ? "out quota" : ""),
  280. (uquotaondisk ? " usrquota" : ""),
  281. (pquotaondisk ? " prjquota" : ""),
  282. (gquotaondisk ? " grpquota" : ""));
  283. return XFS_ERROR(EPERM);
  284. }
  285. if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
  286. /*
  287. * Call mount_quotas at this point only if we won't have to do
  288. * a quotacheck.
  289. */
  290. if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
  291. /*
  292. * If an error occured, qm_mount_quotas code
  293. * has already disabled quotas. So, just finish
  294. * mounting, and get on with the boring life
  295. * without disk quotas.
  296. */
  297. xfs_qm_mount_quotas(mp, 0);
  298. } else {
  299. /*
  300. * Clear the quota flags, but remember them. This
  301. * is so that the quota code doesn't get invoked
  302. * before we're ready. This can happen when an
  303. * inode goes inactive and wants to free blocks,
  304. * or via xfs_log_mount_finish.
  305. */
  306. *needquotamount = B_TRUE;
  307. *quotaflags = mp->m_qflags;
  308. mp->m_qflags = 0;
  309. }
  310. }
  311. return 0;
  312. }
  313. STATIC int
  314. xfs_qm_endmount(
  315. xfs_mount_t *mp,
  316. uint needquotamount,
  317. uint quotaflags,
  318. int mfsi_flags)
  319. {
  320. if (needquotamount) {
  321. ASSERT(mp->m_qflags == 0);
  322. mp->m_qflags = quotaflags;
  323. xfs_qm_mount_quotas(mp, mfsi_flags);
  324. }
  325. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  326. if (! (XFS_IS_QUOTA_ON(mp)))
  327. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
  328. else
  329. xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
  330. #endif
  331. #ifdef QUOTADEBUG
  332. if (XFS_IS_QUOTA_ON(mp) && xfs_qm_internalqcheck(mp))
  333. cmn_err(CE_WARN, "XFS: mount internalqcheck failed");
  334. #endif
  335. return 0;
  336. }
  337. STATIC void
  338. xfs_qm_dqrele_null(
  339. xfs_dquot_t *dq)
  340. {
  341. /*
  342. * Called from XFS, where we always check first for a NULL dquot.
  343. */
  344. if (!dq)
  345. return;
  346. xfs_qm_dqrele(dq);
  347. }
  348. STATIC struct xfs_qmops xfs_qmcore_xfs = {
  349. .xfs_qminit = xfs_qm_newmount,
  350. .xfs_qmdone = xfs_qm_unmount_quotadestroy,
  351. .xfs_qmmount = xfs_qm_endmount,
  352. .xfs_qmunmount = xfs_qm_unmount_quotas,
  353. .xfs_dqrele = xfs_qm_dqrele_null,
  354. .xfs_dqattach = xfs_qm_dqattach,
  355. .xfs_dqdetach = xfs_qm_dqdetach,
  356. .xfs_dqpurgeall = xfs_qm_dqpurge_all,
  357. .xfs_dqvopalloc = xfs_qm_vop_dqalloc,
  358. .xfs_dqvopcreate = xfs_qm_vop_dqattach_and_dqmod_newinode,
  359. .xfs_dqvoprename = xfs_qm_vop_rename_dqattach,
  360. .xfs_dqvopchown = xfs_qm_vop_chown,
  361. .xfs_dqvopchownresv = xfs_qm_vop_chown_reserve,
  362. .xfs_dqtrxops = &xfs_trans_dquot_ops,
  363. };
  364. struct bhv_vfsops xfs_qmops = { {
  365. BHV_IDENTITY_INIT(VFS_BHV_QM, VFS_POSITION_QM),
  366. .vfs_parseargs = xfs_qm_parseargs,
  367. .vfs_showargs = xfs_qm_showargs,
  368. .vfs_mount = xfs_qm_mount,
  369. .vfs_statvfs = xfs_qm_statvfs,
  370. .vfs_sync = xfs_qm_syncall,
  371. .vfs_quotactl = xfs_qm_quotactl, },
  372. };
  373. void __init
  374. xfs_qm_init(void)
  375. {
  376. static char message[] __initdata =
  377. KERN_INFO "SGI XFS Quota Management subsystem\n";
  378. printk(message);
  379. mutex_init(&xfs_Gqm_lock);
  380. vfs_bhv_set_custom(&xfs_qmops, &xfs_qmcore_xfs);
  381. xfs_qm_init_procfs();
  382. }
  383. void __exit
  384. xfs_qm_exit(void)
  385. {
  386. vfs_bhv_clr_custom(&xfs_qmops);
  387. xfs_qm_cleanup_procfs();
  388. if (qm_dqzone)
  389. kmem_zone_destroy(qm_dqzone);
  390. if (qm_dqtrxzone)
  391. kmem_zone_destroy(qm_dqtrxzone);
  392. }