xfs_qm_bhv.c 12 KB

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