quota.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/list.h>
  35. #include <linux/mutex.h>
  36. #include <linux/rwsem.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/wait.h>
  39. #include <linux/percpu_counter.h>
  40. #include <linux/dqblk_xfs.h>
  41. #include <linux/dqblk_v1.h>
  42. #include <linux/dqblk_v2.h>
  43. #include <linux/atomic.h>
  44. #include <linux/uidgid.h>
  45. #include <linux/projid.h>
  46. #include <uapi/linux/quota.h>
  47. #undef USRQUOTA
  48. #undef GRPQUOTA
  49. enum quota_type {
  50. USRQUOTA = 0, /* element used for user quotas */
  51. GRPQUOTA = 1, /* element used for group quotas */
  52. PRJQUOTA = 2, /* element used for project quotas */
  53. };
  54. typedef __kernel_uid32_t qid_t; /* Type in which we store ids in memory */
  55. typedef long long qsize_t; /* Type in which we store sizes */
  56. struct kqid { /* Type in which we store the quota identifier */
  57. union {
  58. kuid_t uid;
  59. kgid_t gid;
  60. kprojid_t projid;
  61. };
  62. enum quota_type type; /* USRQUOTA (uid) or GRPQUOTA (gid) or PRJQUOTA (projid) */
  63. };
  64. extern bool qid_eq(struct kqid left, struct kqid right);
  65. extern bool qid_lt(struct kqid left, struct kqid right);
  66. extern qid_t from_kqid(struct user_namespace *to, struct kqid qid);
  67. extern qid_t from_kqid_munged(struct user_namespace *to, struct kqid qid);
  68. extern bool qid_valid(struct kqid qid);
  69. /**
  70. * make_kqid - Map a user-namespace, type, qid tuple into a kqid.
  71. * @from: User namespace that the qid is in
  72. * @type: The type of quota
  73. * @qid: Quota identifier
  74. *
  75. * Maps a user-namespace, type qid tuple into a kernel internal
  76. * kqid, and returns that kqid.
  77. *
  78. * When there is no mapping defined for the user-namespace, type,
  79. * qid tuple an invalid kqid is returned. Callers are expected to
  80. * test for and handle handle invalid kqids being returned.
  81. * Invalid kqids may be tested for using qid_valid().
  82. */
  83. static inline struct kqid make_kqid(struct user_namespace *from,
  84. enum quota_type type, qid_t qid)
  85. {
  86. struct kqid kqid;
  87. kqid.type = type;
  88. switch (type) {
  89. case USRQUOTA:
  90. kqid.uid = make_kuid(from, qid);
  91. break;
  92. case GRPQUOTA:
  93. kqid.gid = make_kgid(from, qid);
  94. break;
  95. case PRJQUOTA:
  96. kqid.projid = make_kprojid(from, qid);
  97. break;
  98. default:
  99. BUG();
  100. }
  101. return kqid;
  102. }
  103. /**
  104. * make_kqid_invalid - Explicitly make an invalid kqid
  105. * @type: The type of quota identifier
  106. *
  107. * Returns an invalid kqid with the specified type.
  108. */
  109. static inline struct kqid make_kqid_invalid(enum quota_type type)
  110. {
  111. struct kqid kqid;
  112. kqid.type = type;
  113. switch (type) {
  114. case USRQUOTA:
  115. kqid.uid = INVALID_UID;
  116. break;
  117. case GRPQUOTA:
  118. kqid.gid = INVALID_GID;
  119. break;
  120. case PRJQUOTA:
  121. kqid.projid = INVALID_PROJID;
  122. break;
  123. default:
  124. BUG();
  125. }
  126. return kqid;
  127. }
  128. /**
  129. * make_kqid_uid - Make a kqid from a kuid
  130. * @uid: The kuid to make the quota identifier from
  131. */
  132. static inline struct kqid make_kqid_uid(kuid_t uid)
  133. {
  134. struct kqid kqid;
  135. kqid.type = USRQUOTA;
  136. kqid.uid = uid;
  137. return kqid;
  138. }
  139. /**
  140. * make_kqid_gid - Make a kqid from a kgid
  141. * @gid: The kgid to make the quota identifier from
  142. */
  143. static inline struct kqid make_kqid_gid(kgid_t gid)
  144. {
  145. struct kqid kqid;
  146. kqid.type = GRPQUOTA;
  147. kqid.gid = gid;
  148. return kqid;
  149. }
  150. /**
  151. * make_kqid_projid - Make a kqid from a projid
  152. * @projid: The kprojid to make the quota identifier from
  153. */
  154. static inline struct kqid make_kqid_projid(kprojid_t projid)
  155. {
  156. struct kqid kqid;
  157. kqid.type = PRJQUOTA;
  158. kqid.projid = projid;
  159. return kqid;
  160. }
  161. extern spinlock_t dq_data_lock;
  162. /* Maximal numbers of writes for quota operation (insert/delete/update)
  163. * (over VFS all formats) */
  164. #define DQUOT_INIT_ALLOC max(V1_INIT_ALLOC, V2_INIT_ALLOC)
  165. #define DQUOT_INIT_REWRITE max(V1_INIT_REWRITE, V2_INIT_REWRITE)
  166. #define DQUOT_DEL_ALLOC max(V1_DEL_ALLOC, V2_DEL_ALLOC)
  167. #define DQUOT_DEL_REWRITE max(V1_DEL_REWRITE, V2_DEL_REWRITE)
  168. /*
  169. * Data for one user/group kept in memory
  170. */
  171. struct mem_dqblk {
  172. qsize_t dqb_bhardlimit; /* absolute limit on disk blks alloc */
  173. qsize_t dqb_bsoftlimit; /* preferred limit on disk blks */
  174. qsize_t dqb_curspace; /* current used space */
  175. qsize_t dqb_rsvspace; /* current reserved space for delalloc*/
  176. qsize_t dqb_ihardlimit; /* absolute limit on allocated inodes */
  177. qsize_t dqb_isoftlimit; /* preferred inode limit */
  178. qsize_t dqb_curinodes; /* current # allocated inodes */
  179. time_t dqb_btime; /* time limit for excessive disk use */
  180. time_t dqb_itime; /* time limit for excessive inode use */
  181. };
  182. /*
  183. * Data for one quotafile kept in memory
  184. */
  185. struct quota_format_type;
  186. struct mem_dqinfo {
  187. struct quota_format_type *dqi_format;
  188. int dqi_fmt_id; /* Id of the dqi_format - used when turning
  189. * quotas on after remount RW */
  190. struct list_head dqi_dirty_list; /* List of dirty dquots */
  191. unsigned long dqi_flags;
  192. unsigned int dqi_bgrace;
  193. unsigned int dqi_igrace;
  194. qsize_t dqi_maxblimit;
  195. qsize_t dqi_maxilimit;
  196. void *dqi_priv;
  197. };
  198. struct super_block;
  199. #define DQF_MASK 0xffff /* Mask for format specific flags */
  200. #define DQF_GETINFO_MASK 0x1ffff /* Mask for flags passed to userspace */
  201. #define DQF_SETINFO_MASK 0xffff /* Mask for flags modifiable from userspace */
  202. #define DQF_SYS_FILE_B 16
  203. #define DQF_SYS_FILE (1 << DQF_SYS_FILE_B) /* Quota file stored as system file */
  204. #define DQF_INFO_DIRTY_B 31
  205. #define DQF_INFO_DIRTY (1 << DQF_INFO_DIRTY_B) /* Is info dirty? */
  206. extern void mark_info_dirty(struct super_block *sb, int type);
  207. static inline int info_dirty(struct mem_dqinfo *info)
  208. {
  209. return test_bit(DQF_INFO_DIRTY_B, &info->dqi_flags);
  210. }
  211. enum {
  212. DQST_LOOKUPS,
  213. DQST_DROPS,
  214. DQST_READS,
  215. DQST_WRITES,
  216. DQST_CACHE_HITS,
  217. DQST_ALLOC_DQUOTS,
  218. DQST_FREE_DQUOTS,
  219. DQST_SYNCS,
  220. _DQST_DQSTAT_LAST
  221. };
  222. struct dqstats {
  223. int stat[_DQST_DQSTAT_LAST];
  224. struct percpu_counter counter[_DQST_DQSTAT_LAST];
  225. };
  226. extern struct dqstats *dqstats_pcpu;
  227. extern struct dqstats dqstats;
  228. static inline void dqstats_inc(unsigned int type)
  229. {
  230. percpu_counter_inc(&dqstats.counter[type]);
  231. }
  232. static inline void dqstats_dec(unsigned int type)
  233. {
  234. percpu_counter_dec(&dqstats.counter[type]);
  235. }
  236. #define DQ_MOD_B 0 /* dquot modified since read */
  237. #define DQ_BLKS_B 1 /* uid/gid has been warned about blk limit */
  238. #define DQ_INODES_B 2 /* uid/gid has been warned about inode limit */
  239. #define DQ_FAKE_B 3 /* no limits only usage */
  240. #define DQ_READ_B 4 /* dquot was read into memory */
  241. #define DQ_ACTIVE_B 5 /* dquot is active (dquot_release not called) */
  242. #define DQ_LASTSET_B 6 /* Following 6 bits (see QIF_) are reserved\
  243. * for the mask of entries set via SETQUOTA\
  244. * quotactl. They are set under dq_data_lock\
  245. * and the quota format handling dquot can\
  246. * clear them when it sees fit. */
  247. struct dquot {
  248. struct hlist_node dq_hash; /* Hash list in memory */
  249. struct list_head dq_inuse; /* List of all quotas */
  250. struct list_head dq_free; /* Free list element */
  251. struct list_head dq_dirty; /* List of dirty dquots */
  252. struct mutex dq_lock; /* dquot IO lock */
  253. atomic_t dq_count; /* Use count */
  254. wait_queue_head_t dq_wait_unused; /* Wait queue for dquot to become unused */
  255. struct super_block *dq_sb; /* superblock this applies to */
  256. struct kqid dq_id; /* ID this applies to (uid, gid, projid) */
  257. loff_t dq_off; /* Offset of dquot on disk */
  258. unsigned long dq_flags; /* See DQ_* */
  259. struct mem_dqblk dq_dqb; /* Diskquota usage */
  260. };
  261. /* Operations which must be implemented by each quota format */
  262. struct quota_format_ops {
  263. int (*check_quota_file)(struct super_block *sb, int type); /* Detect whether file is in our format */
  264. int (*read_file_info)(struct super_block *sb, int type); /* Read main info about file - called on quotaon() */
  265. int (*write_file_info)(struct super_block *sb, int type); /* Write main info about file */
  266. int (*free_file_info)(struct super_block *sb, int type); /* Called on quotaoff() */
  267. int (*read_dqblk)(struct dquot *dquot); /* Read structure for one user */
  268. int (*commit_dqblk)(struct dquot *dquot); /* Write structure for one user */
  269. int (*release_dqblk)(struct dquot *dquot); /* Called when last reference to dquot is being dropped */
  270. };
  271. /* Operations working with dquots */
  272. struct dquot_operations {
  273. int (*write_dquot) (struct dquot *); /* Ordinary dquot write */
  274. struct dquot *(*alloc_dquot)(struct super_block *, int); /* Allocate memory for new dquot */
  275. void (*destroy_dquot)(struct dquot *); /* Free memory for dquot */
  276. int (*acquire_dquot) (struct dquot *); /* Quota is going to be created on disk */
  277. int (*release_dquot) (struct dquot *); /* Quota is going to be deleted from disk */
  278. int (*mark_dirty) (struct dquot *); /* Dquot is marked dirty */
  279. int (*write_info) (struct super_block *, int); /* Write of quota "superblock" */
  280. /* get reserved quota for delayed alloc, value returned is managed by
  281. * quota code only */
  282. qsize_t *(*get_reserved_space) (struct inode *);
  283. };
  284. struct path;
  285. /* Operations handling requests from userspace */
  286. struct quotactl_ops {
  287. int (*quota_on)(struct super_block *, int, int, struct path *);
  288. int (*quota_on_meta)(struct super_block *, int, int);
  289. int (*quota_off)(struct super_block *, int);
  290. int (*quota_sync)(struct super_block *, int);
  291. int (*get_info)(struct super_block *, int, struct if_dqinfo *);
  292. int (*set_info)(struct super_block *, int, struct if_dqinfo *);
  293. int (*get_dqblk)(struct super_block *, struct kqid, struct fs_disk_quota *);
  294. int (*set_dqblk)(struct super_block *, struct kqid, struct fs_disk_quota *);
  295. int (*get_xstate)(struct super_block *, struct fs_quota_stat *);
  296. int (*set_xstate)(struct super_block *, unsigned int, int);
  297. };
  298. struct quota_format_type {
  299. int qf_fmt_id; /* Quota format id */
  300. const struct quota_format_ops *qf_ops; /* Operations of format */
  301. struct module *qf_owner; /* Module implementing quota format */
  302. struct quota_format_type *qf_next;
  303. };
  304. /* Quota state flags - they actually come in two flavors - for users and groups */
  305. enum {
  306. _DQUOT_USAGE_ENABLED = 0, /* Track disk usage for users */
  307. _DQUOT_LIMITS_ENABLED, /* Enforce quota limits for users */
  308. _DQUOT_SUSPENDED, /* User diskquotas are off, but
  309. * we have necessary info in
  310. * memory to turn them on */
  311. _DQUOT_STATE_FLAGS
  312. };
  313. #define DQUOT_USAGE_ENABLED (1 << _DQUOT_USAGE_ENABLED)
  314. #define DQUOT_LIMITS_ENABLED (1 << _DQUOT_LIMITS_ENABLED)
  315. #define DQUOT_SUSPENDED (1 << _DQUOT_SUSPENDED)
  316. #define DQUOT_STATE_FLAGS (DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED | \
  317. DQUOT_SUSPENDED)
  318. /* Other quota flags */
  319. #define DQUOT_STATE_LAST (_DQUOT_STATE_FLAGS * MAXQUOTAS)
  320. #define DQUOT_QUOTA_SYS_FILE (1 << DQUOT_STATE_LAST)
  321. /* Quota file is a special
  322. * system file and user cannot
  323. * touch it. Filesystem is
  324. * responsible for setting
  325. * S_NOQUOTA, S_NOATIME flags
  326. */
  327. #define DQUOT_NEGATIVE_USAGE (1 << (DQUOT_STATE_LAST + 1))
  328. /* Allow negative quota usage */
  329. static inline unsigned int dquot_state_flag(unsigned int flags, int type)
  330. {
  331. return flags << _DQUOT_STATE_FLAGS * type;
  332. }
  333. static inline unsigned int dquot_generic_flag(unsigned int flags, int type)
  334. {
  335. return (flags >> _DQUOT_STATE_FLAGS * type) & DQUOT_STATE_FLAGS;
  336. }
  337. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  338. extern void quota_send_warning(struct kqid qid, dev_t dev,
  339. const char warntype);
  340. #else
  341. static inline void quota_send_warning(struct kqid qid, dev_t dev,
  342. const char warntype)
  343. {
  344. return;
  345. }
  346. #endif /* CONFIG_QUOTA_NETLINK_INTERFACE */
  347. struct quota_info {
  348. unsigned int flags; /* Flags for diskquotas on this device */
  349. struct mutex dqio_mutex; /* lock device while I/O in progress */
  350. struct mutex dqonoff_mutex; /* Serialize quotaon & quotaoff */
  351. struct rw_semaphore dqptr_sem; /* serialize ops using quota_info struct, pointers from inode to dquots */
  352. struct inode *files[MAXQUOTAS]; /* inodes of quotafiles */
  353. struct mem_dqinfo info[MAXQUOTAS]; /* Information for each quota type */
  354. const struct quota_format_ops *ops[MAXQUOTAS]; /* Operations for each type */
  355. };
  356. int register_quota_format(struct quota_format_type *fmt);
  357. void unregister_quota_format(struct quota_format_type *fmt);
  358. struct quota_module_name {
  359. int qm_fmt_id;
  360. char *qm_mod_name;
  361. };
  362. #define INIT_QUOTA_MODULE_NAMES {\
  363. {QFMT_VFS_OLD, "quota_v1"},\
  364. {QFMT_VFS_V0, "quota_v2"},\
  365. {QFMT_VFS_V1, "quota_v2"},\
  366. {0, NULL}}
  367. #endif /* _QUOTA_ */