Whamcloud - gitweb
c7e97c1b095ffac30ca549e7a62654b6b5d70d40
[fs/lustre-release.git] / lustre / include / lustre_quota.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef _LUSTRE_QUOTA_H
5 #define _LUSTRE_QUOTA_H
6
7 #if defined(__linux__)
8 #include <linux/lustre_quota.h>
9 #elif defined(__APPLE__)
10 #include <darwin/lustre_quota.h>
11 #elif defined(__WINNT__)
12 #include <winnt/lustre_quota.h>
13 #else
14 #error Unsupported operating system.
15 #endif
16
17 #include <lustre/lustre_idl.h>
18 #include <lustre_net.h>
19 #include <lvfs.h>
20
21 struct obd_device;
22 struct client_obd;
23
24 #ifndef NR_DQHASH
25 #define NR_DQHASH 45
26 #endif
27
28 #ifdef HAVE_QUOTA_SUPPORT
29
30 #ifdef __KERNEL__
31
32 /* structures to access admin quotafile */
33 struct lustre_mem_dqinfo {
34         unsigned int dqi_bgrace;
35         unsigned int dqi_igrace;
36         unsigned long dqi_flags;
37         unsigned int dqi_blocks;
38         unsigned int dqi_free_blk;
39         unsigned int dqi_free_entry;
40 };
41
42 struct lustre_quota_info {
43         struct file *qi_files[MAXQUOTAS];
44         struct lustre_mem_dqinfo qi_info[MAXQUOTAS];
45 };
46
47 #define DQ_STATUS_AVAIL         0x0     /* Available dquot */
48 #define DQ_STATUS_SET           0x01    /* Sombody is setting dquot */
49 #define DQ_STATUS_RECOVERY      0x02    /* dquot is in recovery */
50
51 struct lustre_dquot {
52         /* Hash list in memory, protect by dquot_hash_lock */
53         struct list_head dq_hash;
54         /* Protect the data in lustre_dquot */
55         struct semaphore dq_sem;
56         /* Use count */
57         int dq_refcnt;
58         /* Pointer of quota info it belongs to */
59         struct lustre_quota_info *dq_info;
60         
61         loff_t dq_off;                  /* Offset of dquot on disk */
62         unsigned int dq_id;             /* ID this applies to (uid, gid) */
63         int dq_type;                    /* Type fo quota (USRQUOTA, GRPQUOUTA) */
64         unsigned short dq_status;       /* See DQ_STATUS_ */
65         unsigned long dq_flags;         /* See DQ_ in quota.h */
66         struct mem_dqblk dq_dqb;        /* Diskquota usage */
67 };
68
69 struct dquot_id {
70         struct list_head        di_link;
71         __u32                   di_id;
72 };
73
74 #define QFILE_CHK               1
75 #define QFILE_RD_INFO           2
76 #define QFILE_WR_INFO           3
77 #define QFILE_INIT_INFO         4
78 #define QFILE_RD_DQUOT          5
79 #define QFILE_WR_DQUOT          6
80
81 /* admin quotafile operations */
82 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
83 int lustre_check_quota_file(struct lustre_quota_info *lqi, int type);
84 int lustre_read_quota_info(struct lustre_quota_info *lqi, int type);
85 int lustre_write_quota_info(struct lustre_quota_info *lqi, int type);
86 int lustre_read_dquot(struct lustre_dquot *dquot);
87 int lustre_commit_dquot(struct lustre_dquot *dquot);
88 int lustre_init_quota_info(struct lustre_quota_info *lqi, int type);
89 int lustre_get_qids(struct file *file, struct inode *inode, int type, 
90                     struct list_head *list);
91 #else
92
93 #ifndef DQ_FAKE_B
94 #define DQ_FAKE_B       6
95 #endif
96
97 static inline int lustre_check_quota_file(struct lustre_quota_info *lqi,
98                                           int type)
99 {
100         return 0;
101 }
102 static inline int lustre_read_quota_info(struct lustre_quota_info *lqi,
103                                          int type)
104 {
105         return 0;
106 }
107 static inline int lustre_write_quota_info(struct lustre_quota_info *lqi,
108                                           int type)
109 {
110         return 0;
111 }
112 static inline int lustre_read_dquot(struct lustre_dquot *dquot)
113 {
114         return 0;
115 }
116 static inline int lustre_commit_dquot(struct lustre_dquot *dquot)
117 {
118         return 0;
119 }
120 static inline int lustre_init_quota_info(struct lustre_quota_info *lqi,
121                                          int type)
122 {
123         return 0;
124 }
125 #endif  /* KERNEL_VERSION(2,5,0) */
126
127 #define LL_DQUOT_OFF(sb)    DQUOT_OFF(sb)
128
129 typedef int (*dqacq_handler_t) (struct obd_device * obd, struct qunit_data * qd,
130                                 int opc);
131 struct lustre_quota_ctxt {
132         struct super_block *lqc_sb;     /* superblock this applies to */
133         struct obd_import *lqc_import;  /* import used to send dqacq/dqrel RPC */
134         dqacq_handler_t lqc_handler;    /* dqacq/dqrel RPC handler, only for quota master */ 
135         unsigned long lqc_recovery:1,   /* Doing recovery */ 
136                       lqc_atype:2,      /* Turn on user/group quota at setup automatically, 
137                                          * 0: none, 1: user quota, 2: group quota, 3: both */
138                       lqc_status:1;     /* Quota status. 0:Off, 1:On */
139         spinlock_t    lqc_lock;         /* guard lqc_imp_valid now */
140         unsigned long lqc_iunit_sz;     /* Unit size of file quota */
141         unsigned long lqc_itune_sz;     /* Trigger dqacq when available file quota less than
142                                          * this value, trigger dqrel when available file quota
143                                          * more than this value + 1 iunit */
144         unsigned long lqc_bunit_sz;     /* Unit size of block quota */
145         unsigned long lqc_btune_sz;     /* See comment of lqc_itune_sz */
146         unsigned long lqc_limit_sz;     /* When remaining quota on ost is less 
147                                          * than this value, ost will request
148                                          * quota from mds */
149 };
150
151 #else
152
153 struct lustre_quota_info {
154 };
155
156 struct lustre_quota_ctxt {
157 };
158
159 #endif  /* !__KERNEL__ */
160
161 #else
162
163 #define LL_DQUOT_OFF(sb) do {} while(0)
164
165 struct lustre_quota_info {
166 };
167
168 struct lustre_quota_ctxt {
169 };
170
171 #endif /* !HAVE_QUOTA_SUPPORT */
172
173 /* If the (quota limit < qunit * slave count), the slave which can't
174  * acquire qunit should set it's local limit as MIN_QLIMIT */
175 #define MIN_QLIMIT      1
176
177 struct quotacheck_thread_args {
178         struct obd_export   *qta_exp;   /* obd export */
179         struct obd_quotactl  qta_oqctl; /* obd_quotactl args */
180         struct super_block  *qta_sb;    /* obd super block */
181         atomic_t            *qta_sem;   /* obt_quotachecking */
182 };
183
184 typedef struct {
185         int (*quota_init) (void);
186         int (*quota_exit) (void);
187         int (*quota_setup) (struct obd_device *);
188         int (*quota_cleanup) (struct obd_device *);
189         /* For quota master, close admin quota files */
190         int (*quota_fs_cleanup) (struct obd_device *);
191         int (*quota_ctl) (struct obd_export *, struct obd_quotactl *);
192         int (*quota_check) (struct obd_export *, struct obd_quotactl *);
193         int (*quota_recovery) (struct obd_device *);
194         
195         /* For quota master/slave, adjust quota limit after fs operation */
196         int (*quota_adjust) (struct obd_device *, unsigned int[], 
197                              unsigned int[], int, int); 
198         
199         /* For quota slave, set import, trigger quota recovery */
200         int (*quota_setinfo) (struct obd_export *, struct obd_device *);
201         
202         /* For quota slave, clear import when relative import is invalid */
203         int (*quota_clearinfo) (struct obd_export *, struct obd_device *);
204         
205         /* For quota slave, set proper thread resoure capability */
206         int (*quota_enforce) (struct obd_device *, unsigned int);
207         
208         /* For quota slave, check whether specified uid/gid is over quota */
209         int (*quota_getflag) (struct obd_device *, struct obdo *);
210         
211         /* For quota slave, acquire/release quota from master if needed */
212         int (*quota_acquire) (struct obd_device *, unsigned int, unsigned int);
213
214         /* For quota slave, check whether specified uid/gid's remaining quota
215          * can finish a write rpc */
216         int (*quota_chkquota) (struct obd_device *, unsigned int, unsigned int,
217                                int);
218         
219         /* For quota client, poll if the quota check done */
220         int (*quota_poll_check) (struct obd_export *, struct if_quotacheck *);
221         
222         /* For quota client, check whether specified uid/gid is over quota */
223         int (*quota_chkdq) (struct client_obd *, unsigned int, unsigned int);
224         
225         /* For quota client, set over quota flag for specifed uid/gid */
226         int (*quota_setdq) (struct client_obd *, unsigned int, unsigned int,
227                             obd_flag, obd_flag);
228 } quota_interface_t;
229
230 #define Q_COPY(out, in, member) (out)->member = (in)->member
231
232 #define QUOTA_OP(interface, op) interface->quota_ ## op         
233
234 #define QUOTA_CHECK_OP(interface, op)                           \
235 do {                                                            \
236         if (!interface)                                         \
237                 RETURN(0);                                      \
238         if (!QUOTA_OP(interface, op)) {                         \
239                 CERROR("no quota operation: " #op "\n");        \
240                 RETURN(-EOPNOTSUPP);                            \
241         }                                                       \
242 } while(0)
243
244 static inline int lquota_init(quota_interface_t *interface)
245 {
246         int rc;
247         ENTRY;
248         
249         QUOTA_CHECK_OP(interface, init);
250         rc = QUOTA_OP(interface, init)();
251         RETURN(rc);
252 }
253
254 static inline int lquota_exit(quota_interface_t *interface) 
255 {
256         int rc;
257         ENTRY;
258         
259         QUOTA_CHECK_OP(interface, exit);
260         rc = QUOTA_OP(interface, exit)();
261         RETURN(rc);
262 }
263
264 static inline int lquota_setup(quota_interface_t *interface,
265                                struct obd_device *obd)
266 {
267         int rc;
268         ENTRY;
269         
270         QUOTA_CHECK_OP(interface, setup);
271         rc = QUOTA_OP(interface, setup)(obd);
272         RETURN(rc);
273 }
274
275 static inline int lquota_cleanup(quota_interface_t *interface,
276                                  struct obd_device *obd) 
277 {
278         int rc;
279         ENTRY;
280         
281         QUOTA_CHECK_OP(interface, cleanup);
282         rc = QUOTA_OP(interface, cleanup)(obd);
283         RETURN(rc);
284 }
285
286 static inline int lquota_fs_cleanup(quota_interface_t *interface,
287                                     struct obd_device *obd)
288 {
289         int rc;
290         ENTRY;
291         
292         QUOTA_CHECK_OP(interface, fs_cleanup);
293         rc = QUOTA_OP(interface, fs_cleanup)(obd);
294         RETURN(rc);
295 }
296
297 static inline int lquota_recovery(quota_interface_t *interface,
298                                   struct obd_device *obd) 
299 {        
300         int rc;
301         ENTRY;
302         
303         QUOTA_CHECK_OP(interface, recovery);
304         rc = QUOTA_OP(interface, recovery)(obd);
305         RETURN(rc);
306 }
307
308 static inline int lquota_adjust(quota_interface_t *interface,
309                                 struct obd_device *obd, 
310                                 unsigned int qcids[], 
311                                 unsigned int qpids[], 
312                                 int rc, int opc) 
313 {
314         int ret;
315         ENTRY;
316         
317         QUOTA_CHECK_OP(interface, adjust);
318         ret = QUOTA_OP(interface, adjust)(obd, qcids, qpids, rc, opc);
319         RETURN(ret);
320 }
321
322 static inline int lquota_chkdq(quota_interface_t *interface,
323                                struct client_obd *cli,
324                                unsigned int uid, unsigned int gid)
325 {
326         int rc;
327         ENTRY;
328         
329         QUOTA_CHECK_OP(interface, chkdq);
330         rc = QUOTA_OP(interface, chkdq)(cli, uid, gid);
331         RETURN(rc);
332 }
333
334 static inline int lquota_setdq(quota_interface_t *interface,
335                                struct client_obd *cli,
336                                unsigned int uid, unsigned int gid,
337                                obd_flag valid, obd_flag flags)
338 {
339         int rc;
340         ENTRY;
341         
342         QUOTA_CHECK_OP(interface, setdq);
343         rc = QUOTA_OP(interface, setdq)(cli, uid, gid, valid, flags);
344         RETURN(rc);
345 }
346
347 static inline int lquota_poll_check(quota_interface_t *interface,
348                                     struct obd_export *exp,
349                                     struct if_quotacheck *qchk)
350 {
351         int rc;
352         ENTRY;
353         
354         QUOTA_CHECK_OP(interface, poll_check);
355         rc = QUOTA_OP(interface, poll_check)(exp, qchk);
356         RETURN(rc);
357 }
358
359        
360 static inline int lquota_setinfo(quota_interface_t *interface,
361                                  struct obd_export *exp, 
362                                  struct obd_device *obd) 
363 {
364         int rc;
365         ENTRY;
366
367         QUOTA_CHECK_OP(interface, setinfo);
368         rc = QUOTA_OP(interface, setinfo)(exp, obd);
369         RETURN(rc);
370 }
371
372 static inline int lquota_clearinfo(quota_interface_t *interface,
373                                    struct obd_export *exp, 
374                                    struct obd_device *obd) 
375 {
376         int rc;
377         ENTRY;
378
379         QUOTA_CHECK_OP(interface, clearinfo);
380         rc = QUOTA_OP(interface, clearinfo)(exp, obd);
381         RETURN(rc);
382 }
383
384 static inline int lquota_enforce(quota_interface_t *interface, 
385                                  struct obd_device *obd,
386                                  unsigned int ignore)
387 {
388         int rc;
389         ENTRY;
390
391         QUOTA_CHECK_OP(interface, enforce);
392         rc = QUOTA_OP(interface, enforce)(obd, ignore);
393         RETURN(rc);
394 }
395
396 static inline int lquota_getflag(quota_interface_t *interface,
397                                  struct obd_device *obd, struct obdo *oa)
398 {
399         int rc;
400         ENTRY;
401
402         QUOTA_CHECK_OP(interface, getflag);
403         rc = QUOTA_OP(interface, getflag)(obd, oa);
404         RETURN(rc);
405 }
406         
407 static inline int lquota_acquire(quota_interface_t *interface,
408                                  struct obd_device *obd, 
409                                  unsigned int uid, unsigned int gid)
410 {
411         int rc;
412         ENTRY;
413
414         QUOTA_CHECK_OP(interface, acquire);
415         rc = QUOTA_OP(interface, acquire)(obd, uid, gid);
416         RETURN(rc);
417 }
418
419 static inline int lquota_chkquota(quota_interface_t *interface,
420                                   struct obd_device *obd,
421                                   unsigned int uid, unsigned int gid,
422                                   int npage)
423 {
424         int rc;
425         ENTRY;
426         
427         QUOTA_CHECK_OP(interface, chkquota);
428         rc = QUOTA_OP(interface, chkquota)(obd, uid, gid, npage);
429         RETURN(rc);
430 }
431
432 int lprocfs_rd_bunit(char *page, char **start, off_t off, int count, 
433                      int *eof, void *data);
434 int lprocfs_rd_iunit(char *page, char **start, off_t off, int count, 
435                      int *eof, void *data);
436 int lprocfs_wr_bunit(struct file *file, const char *buffer,
437                      unsigned long count, void *data);
438 int lprocfs_wr_iunit(struct file *file, const char *buffer,
439                      unsigned long count, void *data);
440 int lprocfs_rd_btune(char *page, char **start, off_t off, int count, 
441                      int *eof, void *data);
442 int lprocfs_rd_itune(char *page, char **start, off_t off, int count, 
443                      int *eof, void *data);
444 int lprocfs_wr_btune(struct file *file, const char *buffer,
445                      unsigned long count, void *data);
446 int lprocfs_wr_itune(struct file *file, const char *buffer,
447                      unsigned long count, void *data);
448 int lprocfs_rd_type(char *page, char **start, off_t off, int count, 
449                     int *eof, void *data);
450 int lprocfs_wr_type(struct file *file, const char *buffer,
451                     unsigned long count, void *data);
452 int lprocfs_filter_rd_limit(char *page, char **start, off_t off, int count, 
453                             int *eof, void *data);
454 int lprocfs_filter_wr_limit(struct file *file, const char *buffer,
455                             unsigned long count, void *data);
456
457
458 #ifndef __KERNEL__
459 extern quota_interface_t osc_quota_interface;
460 extern quota_interface_t mdc_quota_interface;
461 extern quota_interface_t lov_quota_interface;
462 #endif
463
464 #endif /* _LUSTRE_QUOTA_H */