Home | History | Annotate | Download | only in quota
      1 /** quotaio.h
      2  *
      3  * Header of IO operations for quota utilities
      4  * Jan Kara <jack (at) suse.cz>
      5  */
      6 
      7 #ifndef GUARD_QUOTAIO_H
      8 #define GUARD_QUOTAIO_H
      9 
     10 #include <limits.h>
     11 #include <sys/types.h>
     12 #include <sys/stat.h>
     13 
     14 #include "ext2fs/ext2fs.h"
     15 #include "dqblk_v2.h"
     16 
     17 typedef int64_t qsize_t;	/* Type in which we store size limitations */
     18 
     19 #define MAXQUOTAS 2
     20 #define USRQUOTA 0
     21 #define GRPQUOTA 1
     22 
     23 /*
     24  * Definitions of magics and versions of current quota files
     25  */
     26 #define INITQMAGICS {\
     27 	0xd9c01f11,	/* USRQUOTA */\
     28 	0xd9c01927	/* GRPQUOTA */\
     29 }
     30 
     31 /* Size of blocks in which are counted size limits in generic utility parts */
     32 #define QUOTABLOCK_BITS 10
     33 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
     34 #define toqb(x) (((x) + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS)
     35 
     36 /* Quota format type IDs */
     37 #define	QFMT_VFS_OLD 1
     38 #define	QFMT_VFS_V0 2
     39 #define	QFMT_VFS_V1 4
     40 
     41 /*
     42  * The following constants define the default amount of time given a user
     43  * before the soft limits are treated as hard limits (usually resulting
     44  * in an allocation failure). The timer is started when the user crosses
     45  * their soft limit, it is reset when they go below their soft limit.
     46  */
     47 #define MAX_IQ_TIME  604800	/* (7*24*60*60) 1 week */
     48 #define MAX_DQ_TIME  604800	/* (7*24*60*60) 1 week */
     49 
     50 #define IOFL_INFODIRTY	0x01	/* Did info change? */
     51 
     52 struct quotafile_ops;
     53 
     54 /* Generic information about quotafile */
     55 struct util_dqinfo {
     56 	time_t dqi_bgrace;	/* Block grace time for given quotafile */
     57 	time_t dqi_igrace;	/* Inode grace time for given quotafile */
     58 	union {
     59 		struct v2_mem_dqinfo v2_mdqi;
     60 	} u;			/* Format specific info about quotafile */
     61 };
     62 
     63 struct quota_file {
     64 	ext2_filsys fs;
     65 	ext2_ino_t ino;
     66 	ext2_file_t e2_file;
     67 };
     68 
     69 /* Structure for one opened quota file */
     70 struct quota_handle {
     71 	int qh_type;		/* Type of quotafile */
     72 	int qh_fmt;		/* Quotafile format */
     73 	int qh_io_flags;	/* IO flags for file */
     74 	struct quota_file qh_qf;
     75 	unsigned int (*e2fs_read)(struct quota_file *qf, ext2_loff_t offset,
     76 			 void *buf, unsigned int size);
     77 	unsigned int (*e2fs_write)(struct quota_file *qf, ext2_loff_t offset,
     78 			  void *buf, unsigned int size);
     79 	struct quotafile_ops *qh_ops;	/* Operations on quotafile */
     80 	struct util_dqinfo qh_info;	/* Generic quotafile info */
     81 };
     82 
     83 /* Utility quota block */
     84 struct util_dqblk {
     85 	qsize_t dqb_ihardlimit;
     86 	qsize_t dqb_isoftlimit;
     87 	qsize_t dqb_curinodes;
     88 	qsize_t dqb_bhardlimit;
     89 	qsize_t dqb_bsoftlimit;
     90 	qsize_t dqb_curspace;
     91 	time_t dqb_btime;
     92 	time_t dqb_itime;
     93 	union {
     94 		struct v2_mem_dqblk v2_mdqb;
     95 	} u;			/* Format specific dquot information */
     96 };
     97 
     98 /* Structure for one loaded quota */
     99 struct dquot {
    100 	struct dquot *dq_next;	/* Pointer to next dquot in the list */
    101 	qid_t dq_id;		/* ID dquot belongs to */
    102 	int dq_flags;		/* Some flags for utils */
    103 	struct quota_handle *dq_h;	/* Handle of quotafile for this dquot */
    104 	struct util_dqblk dq_dqb;	/* Parsed data of dquot */
    105 };
    106 
    107 /* Structure of quotafile operations */
    108 struct quotafile_ops {
    109 	/* Check whether quotafile is in our format */
    110 	int (*check_file) (struct quota_handle *h, int type, int fmt);
    111 	/* Open quotafile */
    112 	int (*init_io) (struct quota_handle *h);
    113 	/* Create new quotafile */
    114 	int (*new_io) (struct quota_handle *h);
    115 	/* Write all changes and close quotafile */
    116 	int (*end_io) (struct quota_handle *h);
    117 	/* Write info about quotafile */
    118 	int (*write_info) (struct quota_handle *h);
    119 	/* Read dquot into memory */
    120 	struct dquot *(*read_dquot) (struct quota_handle *h, qid_t id);
    121 	/* Write given dquot to disk */
    122 	int (*commit_dquot) (struct dquot *dquot);
    123 	/* Scan quotafile and call callback on every structure */
    124 	int (*scan_dquots) (struct quota_handle *h,
    125 			    int (*process_dquot) (struct dquot *dquot,
    126 						  void *data),
    127 			    void *data);
    128 	/* Function to print format specific file information */
    129 	int (*report) (struct quota_handle *h, int verbose);
    130 };
    131 
    132 /* This might go into a special header file but that sounds a bit silly... */
    133 extern struct quotafile_ops quotafile_ops_meta;
    134 
    135 /* Open existing quotafile of given type (and verify its format) on given
    136  * filesystem. */
    137 errcode_t quota_file_open(struct quota_handle *h, ext2_filsys fs,
    138 			  ext2_ino_t qf_ino, int type, int fmt, int flags);
    139 
    140 
    141 /* Create new quotafile of specified format on given filesystem */
    142 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
    143 			    int type, int fmt);
    144 
    145 /* Close quotafile */
    146 errcode_t quota_file_close(struct quota_handle *h);
    147 
    148 /* Get empty quota structure */
    149 struct dquot *get_empty_dquot(void);
    150 
    151 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino);
    152 
    153 const char *type2name(int type);
    154 
    155 void update_grace_times(struct dquot *q);
    156 
    157 /* size for the buffer returned by quota_get_qf_name(); must be greater
    158    than maxlen of extensions[] and fmtnames[] (plus 2) found in quotaio.c */
    159 #define QUOTA_NAME_LEN 16
    160 
    161 const char *quota_get_qf_name(int type, int fmt, char *buf);
    162 const char *quota_get_qf_path(const char *mntpt, int qtype, int fmt,
    163 			      char *path_buf, size_t path_buf_size);
    164 
    165 #endif /* GUARD_QUOTAIO_H */
    166