quota.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright (c) 1982, 1986 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Robert Elz at The University of Melbourne.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #ifndef _LINUX_QUOTA_
  33. #define _LINUX_QUOTA_
  34. #include <linux/errno.h>
  35. #include <linux/types.h>
  36. #define __DQUOT_VERSION__ "dquot_6.5.2"
  37. #define MAXQUOTAS 2
  38. #define USRQUOTA 0 /* element used for user quotas */
  39. #define GRPQUOTA 1 /* element used for group quotas */
  40. /*
  41. * Definitions for the default names of the quotas files.
  42. */
  43. #define INITQFNAMES { \
  44. "user", /* USRQUOTA */ \
  45. "group", /* GRPQUOTA */ \
  46. "undefined", \
  47. };
  48. /*
  49. * Command definitions for the 'quotactl' system call.
  50. * The commands are broken into a main command defined below
  51. * and a subcommand that is used to convey the type of
  52. * quota that is being manipulated (see above).
  53. */
  54. #define SUBCMDMASK 0x00ff
  55. #define SUBCMDSHIFT 8
  56. #define QCMD(cmd, type) (((cmd) << SUBCMDSHIFT) | ((type) & SUBCMDMASK))
  57. #define Q_SYNC 0x800001 /* sync disk copy of a filesystems quotas */
  58. #define Q_QUOTAON 0x800002 /* turn quotas on */
  59. #define Q_QUOTAOFF 0x800003 /* turn quotas off */
  60. #define Q_GETFMT 0x800004 /* get quota format used on given filesystem */
  61. #define Q_GETINFO 0x800005 /* get information about quota files */
  62. #define Q_SETINFO 0x800006 /* set information about quota files */
  63. #define Q_GETQUOTA 0x800007 /* get user quota structure */
  64. #define Q_SETQUOTA 0x800008 /* set user quota structure */
  65. /* Quota format type IDs */
  66. #define QFMT_VFS_OLD 1
  67. #define QFMT_VFS_V0 2
  68. #define QFMT_OCFS2 3
  69. #define QFMT_VFS_V1 4
  70. /* Size of block in which space limits are passed through the quota
  71. * interface */
  72. #define QIF_DQBLKSIZE_BITS 10
  73. #define QIF_DQBLKSIZE (1 << QIF_DQBLKSIZE_BITS)
  74. /*
  75. * Quota structure used for communication with userspace via quotactl
  76. * Following flags are used to specify which fields are valid
  77. */
  78. enum {
  79. QIF_BLIMITS_B = 0,
  80. QIF_SPACE_B,
  81. QIF_ILIMITS_B,
  82. QIF_INODES_B,
  83. QIF_BTIME_B,
  84. QIF_ITIME_B,
  85. };
  86. #define QIF_BLIMITS (1 << QIF_BLIMITS_B)
  87. #define QIF_SPACE (1 << QIF_SPACE_B)
  88. #define QIF_ILIMITS (1 << QIF_ILIMITS_B)
  89. #define QIF_INODES (1 << QIF_INODES_B)
  90. #define QIF_BTIME (1 << QIF_BTIME_B)
  91. #define QIF_ITIME (1 << QIF_ITIME_B)
  92. #define QIF_LIMITS (QIF_BLIMITS | QIF_ILIMITS)
  93. #define QIF_USAGE (QIF_SPACE | QIF_INODES)
  94. #define QIF_TIMES (QIF_BTIME | QIF_ITIME)
  95. #define QIF_ALL (QIF_LIMITS | QIF_USAGE | QIF_TIMES)
  96. struct if_dqblk {
  97. __u64 dqb_bhardlimit;
  98. __u64 dqb_bsoftlimit;
  99. __u64 dqb_curspace;
  100. __u64 dqb_ihardlimit;
  101. __u64 dqb_isoftlimit;
  102. __u64 dqb_curinodes;
  103. __u64 dqb_btime;
  104. __u64 dqb_itime;
  105. __u32 dqb_valid;
  106. };
  107. /*
  108. * Structure used for setting quota information about file via quotactl
  109. * Following flags are used to specify which fields are valid
  110. */
  111. #define IIF_BGRACE 1
  112. #define IIF_IGRACE 2
  113. #define IIF_FLAGS 4
  114. #define IIF_ALL (IIF_BGRACE | IIF_IGRACE | IIF_FLAGS)
  115. struct if_dqinfo {
  116. __u64 dqi_bgrace;
  117. __u64 dqi_igrace;
  118. __u32 dqi_flags;
  119. __u32 dqi_valid;
  120. };
  121. /*
  122. * Definitions for quota netlink interface
  123. */
  124. #define QUOTA_NL_NOWARN 0
  125. #define QUOTA_NL_IHARDWARN 1 /* Inode hardlimit reached */
  126. #define QUOTA_NL_ISOFTLONGWARN 2 /* Inode grace time expired */
  127. #define QUOTA_NL_ISOFTWARN 3 /* Inode softlimit reached */
  128. #define QUOTA_NL_BHARDWARN 4 /* Block hardlimit reached */
  129. #define QUOTA_NL_BSOFTLONGWARN 5 /* Block grace time expired */
  130. #define QUOTA_NL_BSOFTWARN 6 /* Block softlimit reached */
  131. #define QUOTA_NL_IHARDBELOW 7 /* Usage got below inode hardlimit */
  132. #define QUOTA_NL_ISOFTBELOW 8 /* Usage got below inode softlimit */
  133. #define QUOTA_NL_BHARDBELOW 9 /* Usage got below block hardlimit */
  134. #define QUOTA_NL_BSOFTBELOW 10 /* Usage got below block softlimit */
  135. enum {
  136. QUOTA_NL_C_UNSPEC,
  137. QUOTA_NL_C_WARNING,
  138. __QUOTA_NL_C_MAX,
  139. };
  140. #define QUOTA_NL_C_MAX (__QUOTA_NL_C_MAX - 1)
  141. enum {
  142. QUOTA_NL_A_UNSPEC,
  143. QUOTA_NL_A_QTYPE,
  144. QUOTA_NL_A_EXCESS_ID,
  145. QUOTA_NL_A_WARNING,
  146. QUOTA_NL_A_DEV_MAJOR,
  147. QUOTA_NL_A_DEV_MINOR,
  148. QUOTA_NL_A_CAUSED_ID,
  149. __QUOTA_NL_A_MAX,
  150. };
  151. #define QUOTA_NL_A_MAX (__QUOTA_NL_A_MAX - 1)
  152. #ifdef __KERNEL__
  153. #include <linux/list.h>
  154. #include <linux/mutex.h>
  155. #include <linux/rwsem.h>
  156. #include <linux/spinlock.h>
  157. #include <linux/wait.h>
  158. #include <linux/dqblk_xfs.h>
  159. #include <linux/dqblk_v1.h>
  160. #include <linux/dqblk_v2.h>
  161. #include <asm/atomic.h>
  162. typedef __kernel_uid32_t qid_t; /* Type in which we store ids in memory */
  163. typedef long long qsize_t; /* Type in which we store sizes */
  164. extern spinlock_t dq_data_lock;
  165. /* Maximal numbers of writes for quota operation (insert/delete/update)
  166. * (over VFS all formats) */
  167. #define DQUOT_INIT_ALLOC max(V1_INIT_ALLOC, V2_INIT_ALLOC)
  168. #define DQUOT_INIT_REWRITE max(V1_INIT_REWRITE, V2_INIT_REWRITE)
  169. #define DQUOT_DEL_ALLOC max(V1_DEL_ALLOC, V2_DEL_ALLOC)
  170. #define DQUOT_DEL_REWRITE max(V1_DEL_REWRITE, V2_DEL_REWRITE)
  171. /*
  172. * Data for one user/group kept in memory
  173. */
  174. struct mem_dqblk {
  175. qsize_t dqb_bhardlimit; /* absolute limit on disk blks alloc */
  176. qsize_t dqb_bsoftlimit; /* preferred limit on disk blks */
  177. qsize_t dqb_curspace; /* current used space */
  178. qsize_t dqb_rsvspace; /* current reserved space for delalloc*/
  179. qsize_t dqb_ihardlimit; /* absolute limit on allocated inodes */
  180. qsize_t dqb_isoftlimit; /* preferred inode limit */
  181. qsize_t dqb_curinodes; /* current # allocated inodes */
  182. time_t dqb_btime; /* time limit for excessive disk use */
  183. time_t dqb_itime; /* time limit for excessive inode use */
  184. };
  185. /*
  186. * Data for one quotafile kept in memory
  187. */
  188. struct quota_format_type;
  189. struct mem_dqinfo {
  190. struct quota_format_type *dqi_format;
  191. int dqi_fmt_id; /* Id of the dqi_format - used when turning
  192. * quotas on after remount RW */
  193. struct list_head dqi_dirty_list; /* List of dirty dquots */
  194. unsigned long dqi_flags;
  195. unsigned int dqi_bgrace;
  196. unsigned int dqi_igrace;
  197. qsize_t dqi_maxblimit;
  198. qsize_t dqi_maxilimit;
  199. void *dqi_priv;
  200. };
  201. struct super_block;
  202. #define DQF_MASK 0xffff /* Mask for format specific flags */
  203. #define DQF_INFO_DIRTY_B 16
  204. #define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B) /* Is info dirty? */
  205. extern void mark_info_dirty(struct super_block *sb, int type);
  206. static inline int info_dirty(struct mem_dqinfo *info)
  207. {
  208. return test_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
  209. }
  210. struct dqstats {
  211. int lookups;
  212. int drops;
  213. int reads;
  214. int writes;
  215. int cache_hits;
  216. int allocated_dquots;
  217. int free_dquots;
  218. int syncs;
  219. };
  220. extern struct dqstats dqstats;
  221. #define DQ_MOD_B 0 /* dquot modified since read */
  222. #define DQ_BLKS_B 1 /* uid/gid has been warned about blk limit */
  223. #define DQ_INODES_B 2 /* uid/gid has been warned about inode limit */
  224. #define DQ_FAKE_B 3 /* no limits only usage */
  225. #define DQ_READ_B 4 /* dquot was read into memory */
  226. #define DQ_ACTIVE_B 5 /* dquot is active (dquot_release not called) */
  227. #define DQ_LASTSET_B 6 /* Following 6 bits (see QIF_) are reserved\
  228. * for the mask of entries set via SETQUOTA\
  229. * quotactl. They are set under dq_data_lock\
  230. * and the quota format handling dquot can\
  231. * clear them when it sees fit. */
  232. struct dquot {
  233. struct hlist_node dq_hash; /* Hash list in memory */
  234. struct list_head dq_inuse; /* List of all quotas */
  235. struct list_head dq_free; /* Free list element */
  236. struct list_head dq_dirty; /* List of dirty dquots */
  237. struct mutex dq_lock; /* dquot IO lock */
  238. atomic_t dq_count; /* Use count */
  239. wait_queue_head_t dq_wait_unused; /* Wait queue for dquot to become unused */
  240. struct super_block *dq_sb; /* superblock this applies to */
  241. unsigned int dq_id; /* ID this applies to (uid, gid) */
  242. loff_t dq_off; /* Offset of dquot on disk */
  243. unsigned long dq_flags; /* See DQ_* */
  244. short dq_type; /* Type of quota */
  245. struct mem_dqblk dq_dqb; /* Diskquota usage */
  246. };
  247. #define QUOTA_OK 0
  248. #define NO_QUOTA 1
  249. /* Operations which must be implemented by each quota format */
  250. struct quota_format_ops {
  251. int (*check_quota_file)(struct super_block *sb, int type); /* Detect whether file is in our format */
  252. int (*read_file_info)(struct super_block *sb, int type); /* Read main info about file - called on quotaon() */
  253. int (*write_file_info)(struct super_block *sb, int type); /* Write main info about file */
  254. int (*free_file_info)(struct super_block *sb, int type); /* Called on quotaoff() */
  255. int (*read_dqblk)(struct dquot *dquot); /* Read structure for one user */
  256. int (*commit_dqblk)(struct dquot *dquot); /* Write structure for one user */
  257. int (*release_dqblk)(struct dquot *dquot); /* Called when last reference to dquot is being dropped */
  258. };
  259. /* Operations working with dquots */
  260. struct dquot_operations {
  261. int (*initialize) (struct inode *, int);
  262. int (*drop) (struct inode *);
  263. int (*alloc_space) (struct inode *, qsize_t, int);
  264. int (*alloc_inode) (const struct inode *, qsize_t);
  265. int (*free_space) (struct inode *, qsize_t);
  266. int (*free_inode) (const struct inode *, qsize_t);
  267. int (*transfer) (struct inode *, struct iattr *);
  268. int (*write_dquot) (struct dquot *); /* Ordinary dquot write */
  269. struct dquot *(*alloc_dquot)(struct super_block *, int); /* Allocate memory for new dquot */
  270. void (*destroy_dquot)(struct dquot *); /* Free memory for dquot */
  271. int (*acquire_dquot) (struct dquot *); /* Quota is going to be created on disk */
  272. int (*release_dquot) (struct dquot *); /* Quota is going to be deleted from disk */
  273. int (*mark_dirty) (struct dquot *); /* Dquot is marked dirty */
  274. int (*write_info) (struct super_block *, int); /* Write of quota "superblock" */
  275. /* reserve quota for delayed block allocation */
  276. int (*reserve_space) (struct inode *, qsize_t, int);
  277. /* claim reserved quota for delayed alloc */
  278. int (*claim_space) (struct inode *, qsize_t);
  279. /* release rsved quota for delayed alloc */
  280. void (*release_rsv) (struct inode *, qsize_t);
  281. /* get reserved quota for delayed alloc, value returned is managed by
  282. * quota code only */
  283. qsize_t *(*get_reserved_space) (struct inode *);
  284. };
  285. /* Operations handling requests from userspace */
  286. struct quotactl_ops {
  287. int (*quota_on)(struct super_block *, int, int, char *, int);
  288. int (*quota_off)(struct super_block *, int, int);
  289. int (*quota_sync)(struct super_block *, int, int);
  290. int (*get_info)(struct super_block *, int, struct if_dqinfo *);
  291. int (*set_info)(struct super_block *, int, struct if_dqinfo *);
  292. int (*get_dqblk)(struct super_block *, int, qid_t, struct if_dqblk *);
  293. int (*set_dqblk)(struct super_block *, int, qid_t, struct if_dqblk *);
  294. int (*get_xstate)(struct super_block *, struct fs_quota_stat *);
  295. int (*set_xstate)(struct super_block *, unsigned int, int);
  296. int (*get_xquota)(struct super_block *, int, qid_t, struct fs_disk_quota *);
  297. int (*set_xquota)(struct super_block *, int, qid_t, struct fs_disk_quota *);
  298. };
  299. struct quota_format_type {
  300. int qf_fmt_id; /* Quota format id */
  301. const struct quota_format_ops *qf_ops; /* Operations of format */
  302. struct module *qf_owner; /* Module implementing quota format */
  303. struct quota_format_type *qf_next;
  304. };
  305. /* Quota state flags - they actually come in two flavors - for users and groups */
  306. enum {
  307. _DQUOT_USAGE_ENABLED = 0, /* Track disk usage for users */
  308. _DQUOT_LIMITS_ENABLED, /* Enforce quota limits for users */
  309. _DQUOT_SUSPENDED, /* User diskquotas are off, but
  310. * we have necessary info in
  311. * memory to turn them on */
  312. _DQUOT_STATE_FLAGS
  313. };
  314. #define DQUOT_USAGE_ENABLED (1 << _DQUOT_USAGE_ENABLED)
  315. #define DQUOT_LIMITS_ENABLED (1 << _DQUOT_LIMITS_ENABLED)
  316. #define DQUOT_SUSPENDED (1 << _DQUOT_SUSPENDED)
  317. #define DQUOT_STATE_FLAGS (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED | \
  318. DQUOT_SUSPENDED)
  319. /* Other quota flags */
  320. #define DQUOT_STATE_LAST (_DQUOT_STATE_FLAGS * MAXQUOTAS)
  321. #define DQUOT_QUOTA_SYS_FILE (1 << DQUOT_STATE_LAST)
  322. /* Quota file is a special
  323. * system file and user cannot
  324. * touch it. Filesystem is
  325. * responsible for setting
  326. * S_NOQUOTA, S_NOATIME flags
  327. */
  328. #define DQUOT_NEGATIVE_USAGE (1 << (DQUOT_STATE_LAST + 1))
  329. /* Allow negative quota usage */
  330. static inline unsigned int dquot_state_flag(unsigned int flags, int type)
  331. {
  332. return flags << _DQUOT_STATE_FLAGS * type;
  333. }
  334. static inline unsigned int dquot_generic_flag(unsigned int flags, int type)
  335. {
  336. return (flags >> _DQUOT_STATE_FLAGS * type) & DQUOT_STATE_FLAGS;
  337. }
  338. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  339. extern void quota_send_warning(short type, unsigned int id, dev_t dev,
  340. const char warntype);
  341. #else
  342. static inline void quota_send_warning(short type, unsigned int id, dev_t dev,
  343. const char warntype)
  344. {
  345. return;
  346. }
  347. #endif /* CONFIG_QUOTA_NETLINK_INTERFACE */
  348. struct quota_info {
  349. unsigned int flags; /* Flags for diskquotas on this device */
  350. struct mutex dqio_mutex; /* lock device while I/O in progress */
  351. struct mutex dqonoff_mutex; /* Serialize quotaon & quotaoff */
  352. struct rw_semaphore dqptr_sem; /* serialize ops using quota_info struct, pointers from inode to dquots */
  353. struct inode *files[MAXQUOTAS]; /* inodes of quotafiles */
  354. struct mem_dqinfo info[MAXQUOTAS]; /* Information for each quota type */
  355. const struct quota_format_ops *ops[MAXQUOTAS]; /* Operations for each type */
  356. };
  357. int register_quota_format(struct quota_format_type *fmt);
  358. void unregister_quota_format(struct quota_format_type *fmt);
  359. struct quota_module_name {
  360. int qm_fmt_id;
  361. char *qm_mod_name;
  362. };
  363. #define INIT_QUOTA_MODULE_NAMES {\
  364. {QFMT_VFS_OLD, "quota_v1"},\
  365. {QFMT_VFS_V0, "quota_v2"},\
  366. {0, NULL}}
  367. #else
  368. # /* nodep */ include <sys/cdefs.h>
  369. __BEGIN_DECLS
  370. long quotactl __P ((unsigned int, const char *, int, caddr_t));
  371. __END_DECLS
  372. #endif /* __KERNEL__ */
  373. #endif /* _QUOTA_ */