2 * linux/kernel/vserver/dlimit.c
4 * Virtual Server: Context Disk Limits
6 * Copyright (C) 2004-2007 Herbert Pƶtzl
8 * V0.01 initial version
9 * V0.02 compat32 splitup
14 // #include <linux/mnt_namespace.h>
15 #include <linux/namei.h>
16 #include <linux/statfs.h>
17 #include <linux/compat.h>
18 #include <linux/vserver/switch.h>
19 #include <linux/vs_context.h>
20 #include <linux/vs_tag.h>
21 #include <linux/vs_dlimit.h>
22 #include <linux/vserver/dlimit_cmd.h>
24 #include <asm/errno.h>
25 #include <asm/uaccess.h>
29 * allocate an initialized dl_info struct
30 * doesn't make it visible (hash) */
32 static struct dl_info *__alloc_dl_info(struct super_block *sb, tag_t tag)
34 struct dl_info *new = NULL;
36 vxdprintk(VXD_CBIT(dlim, 5),
37 "alloc_dl_info(%p,%d)*", sb, tag);
39 /* would this benefit from a slab cache? */
40 new = kmalloc(sizeof(struct dl_info), GFP_KERNEL);
44 memset (new, 0, sizeof(struct dl_info));
47 INIT_RCU_HEAD(&new->dl_rcu);
48 INIT_HLIST_NODE(&new->dl_hlist);
49 spin_lock_init(&new->dl_lock);
50 atomic_set(&new->dl_refcnt, 0);
51 atomic_set(&new->dl_usecnt, 0);
53 /* rest of init goes here */
55 vxdprintk(VXD_CBIT(dlim, 4),
56 "alloc_dl_info(%p,%d) = %p", sb, tag, new);
60 /* __dealloc_dl_info()
62 * final disposal of dl_info */
64 static void __dealloc_dl_info(struct dl_info *dli)
66 vxdprintk(VXD_CBIT(dlim, 4),
67 "dealloc_dl_info(%p)", dli);
69 dli->dl_hlist.next = LIST_POISON1;
73 BUG_ON(atomic_read(&dli->dl_usecnt));
74 BUG_ON(atomic_read(&dli->dl_refcnt));
80 /* hash table for dl_info hash */
82 #define DL_HASH_SIZE 13
84 struct hlist_head dl_info_hash[DL_HASH_SIZE];
86 static spinlock_t dl_info_hash_lock = SPIN_LOCK_UNLOCKED;
89 static inline unsigned int __hashval(struct super_block *sb, tag_t tag)
91 return ((tag ^ (unsigned long)sb) % DL_HASH_SIZE);
98 * add the dli to the global hash table
99 * requires the hash_lock to be held */
101 static inline void __hash_dl_info(struct dl_info *dli)
103 struct hlist_head *head;
105 vxdprintk(VXD_CBIT(dlim, 6),
106 "__hash_dl_info: %p[#%d]", dli, dli->dl_tag);
108 head = &dl_info_hash[__hashval(dli->dl_sb, dli->dl_tag)];
109 hlist_add_head_rcu(&dli->dl_hlist, head);
112 /* __unhash_dl_info()
114 * remove the dli from the global hash table
115 * requires the hash_lock to be held */
117 static inline void __unhash_dl_info(struct dl_info *dli)
119 vxdprintk(VXD_CBIT(dlim, 6),
120 "__unhash_dl_info: %p[#%d]", dli, dli->dl_tag);
121 hlist_del_rcu(&dli->dl_hlist);
126 /* __lookup_dl_info()
128 * requires the rcu_read_lock()
129 * doesn't increment the dl_refcnt */
131 static inline struct dl_info *__lookup_dl_info(struct super_block *sb, tag_t tag)
133 struct hlist_head *head = &dl_info_hash[__hashval(sb, tag)];
134 struct hlist_node *pos;
137 hlist_for_each_entry_rcu(dli, pos, head, dl_hlist) {
139 if (dli->dl_tag == tag && dli->dl_sb == sb) {
147 struct dl_info *locate_dl_info(struct super_block *sb, tag_t tag)
152 dli = get_dl_info(__lookup_dl_info(sb, tag));
153 vxdprintk(VXD_CBIT(dlim, 7),
154 "locate_dl_info(%p,#%d) = %p", sb, tag, dli);
159 void rcu_free_dl_info(struct rcu_head *head)
161 struct dl_info *dli = container_of(head, struct dl_info, dl_rcu);
164 BUG_ON(!dli || !head);
166 usecnt = atomic_read(&dli->dl_usecnt);
169 refcnt = atomic_read(&dli->dl_refcnt);
172 vxdprintk(VXD_CBIT(dlim, 3),
173 "rcu_free_dl_info(%p)", dli);
175 __dealloc_dl_info(dli);
177 printk("!!! rcu didn't free\n");
183 static int do_addrem_dlimit(uint32_t id, const char __user *name,
184 uint32_t flags, int add)
189 ret = user_path_walk_link(name, &nd);
191 struct super_block *sb;
195 if (!nd.dentry->d_inode)
197 if (!(sb = nd.dentry->d_inode->i_sb))
201 dli = __alloc_dl_info(sb, id);
202 spin_lock(&dl_info_hash_lock);
205 if (__lookup_dl_info(sb, id))
210 spin_lock(&dl_info_hash_lock);
211 dli = __lookup_dl_info(sb, id);
216 __unhash_dl_info(dli);
220 spin_unlock(&dl_info_hash_lock);
222 __dealloc_dl_info(dli);
229 int vc_add_dlimit(uint32_t id, void __user *data)
231 struct vcmd_ctx_dlimit_base_v0 vc_data;
233 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
236 return do_addrem_dlimit(id, vc_data.name, vc_data.flags, 1);
239 int vc_rem_dlimit(uint32_t id, void __user *data)
241 struct vcmd_ctx_dlimit_base_v0 vc_data;
243 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
246 return do_addrem_dlimit(id, vc_data.name, vc_data.flags, 0);
251 int vc_add_dlimit_x32(uint32_t id, void __user *data)
253 struct vcmd_ctx_dlimit_base_v0_x32 vc_data;
255 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
258 return do_addrem_dlimit(id,
259 compat_ptr(vc_data.name_ptr), vc_data.flags, 1);
262 int vc_rem_dlimit_x32(uint32_t id, void __user *data)
264 struct vcmd_ctx_dlimit_base_v0_x32 vc_data;
266 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
269 return do_addrem_dlimit(id,
270 compat_ptr(vc_data.name_ptr), vc_data.flags, 0);
273 #endif /* CONFIG_COMPAT */
277 int do_set_dlimit(uint32_t id, const char __user *name,
278 uint32_t space_used, uint32_t space_total,
279 uint32_t inodes_used, uint32_t inodes_total,
280 uint32_t reserved, uint32_t flags)
285 ret = user_path_walk_link(name, &nd);
287 struct super_block *sb;
291 if (!nd.dentry->d_inode)
293 if (!(sb = nd.dentry->d_inode->i_sb))
295 if ((reserved != CDLIM_KEEP &&
297 (inodes_used != CDLIM_KEEP &&
298 inodes_used > inodes_total) ||
299 (space_used != CDLIM_KEEP &&
300 space_used > space_total))
304 dli = locate_dl_info(sb, id);
308 spin_lock(&dli->dl_lock);
310 if (inodes_used != CDLIM_KEEP)
311 dli->dl_inodes_used = inodes_used;
312 if (inodes_total != CDLIM_KEEP)
313 dli->dl_inodes_total = inodes_total;
314 if (space_used != CDLIM_KEEP) {
315 dli->dl_space_used = space_used;
316 dli->dl_space_used <<= 10;
318 if (space_total == CDLIM_INFINITY)
319 dli->dl_space_total = DLIM_INFINITY;
320 else if (space_total != CDLIM_KEEP) {
321 dli->dl_space_total = space_total;
322 dli->dl_space_total <<= 10;
324 if (reserved != CDLIM_KEEP)
325 dli->dl_nrlmult = (1 << 10) * (100 - reserved) / 100;
327 spin_unlock(&dli->dl_lock);
338 int vc_set_dlimit(uint32_t id, void __user *data)
340 struct vcmd_ctx_dlimit_v0 vc_data;
342 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
345 return do_set_dlimit(id, vc_data.name,
346 vc_data.space_used, vc_data.space_total,
347 vc_data.inodes_used, vc_data.inodes_total,
348 vc_data.reserved, vc_data.flags);
353 int vc_set_dlimit_x32(uint32_t id, void __user *data)
355 struct vcmd_ctx_dlimit_v0_x32 vc_data;
357 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
360 return do_set_dlimit(id, compat_ptr(vc_data.name_ptr),
361 vc_data.space_used, vc_data.space_total,
362 vc_data.inodes_used, vc_data.inodes_total,
363 vc_data.reserved, vc_data.flags);
366 #endif /* CONFIG_COMPAT */
370 int do_get_dlimit(uint32_t id, const char __user *name,
371 uint32_t *space_used, uint32_t *space_total,
372 uint32_t *inodes_used, uint32_t *inodes_total,
373 uint32_t *reserved, uint32_t *flags)
378 ret = user_path_walk_link(name, &nd);
380 struct super_block *sb;
384 if (!nd.dentry->d_inode)
386 if (!(sb = nd.dentry->d_inode->i_sb))
390 dli = locate_dl_info(sb, id);
394 spin_lock(&dli->dl_lock);
395 *inodes_used = dli->dl_inodes_used;
396 *inodes_total = dli->dl_inodes_total;
397 *space_used = dli->dl_space_used >> 10;
398 if (dli->dl_space_total == DLIM_INFINITY)
399 *space_total = CDLIM_INFINITY;
401 *space_total = dli->dl_space_total >> 10;
403 *reserved = 100 - ((dli->dl_nrlmult * 100 + 512) >> 10);
404 spin_unlock(&dli->dl_lock);
417 int vc_get_dlimit(uint32_t id, void __user *data)
419 struct vcmd_ctx_dlimit_v0 vc_data;
422 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
425 ret = do_get_dlimit(id, vc_data.name,
426 &vc_data.space_used, &vc_data.space_total,
427 &vc_data.inodes_used, &vc_data.inodes_total,
428 &vc_data.reserved, &vc_data.flags);
432 if (copy_to_user(data, &vc_data, sizeof(vc_data)))
439 int vc_get_dlimit_x32(uint32_t id, void __user *data)
441 struct vcmd_ctx_dlimit_v0_x32 vc_data;
444 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
447 ret = do_get_dlimit(id, compat_ptr(vc_data.name_ptr),
448 &vc_data.space_used, &vc_data.space_total,
449 &vc_data.inodes_used, &vc_data.inodes_total,
450 &vc_data.reserved, &vc_data.flags);
454 if (copy_to_user(data, &vc_data, sizeof(vc_data)))
459 #endif /* CONFIG_COMPAT */
462 void vx_vsi_statfs(struct super_block *sb, struct kstatfs *buf)
465 __u64 blimit, bfree, bavail;
468 dli = locate_dl_info(sb, dx_current_tag());
472 spin_lock(&dli->dl_lock);
473 if (dli->dl_inodes_total == (unsigned long)DLIM_INFINITY)
476 /* reduce max inodes available to limit */
477 if (buf->f_files > dli->dl_inodes_total)
478 buf->f_files = dli->dl_inodes_total;
480 ifree = dli->dl_inodes_total - dli->dl_inodes_used;
481 /* reduce free inodes to min */
482 if (ifree < buf->f_ffree)
483 buf->f_ffree = ifree;
486 if (dli->dl_space_total == DLIM_INFINITY)
489 blimit = dli->dl_space_total >> sb->s_blocksize_bits;
491 if (dli->dl_space_total < dli->dl_space_used)
494 bfree = (dli->dl_space_total - dli->dl_space_used)
495 >> sb->s_blocksize_bits;
497 bavail = ((dli->dl_space_total >> 10) * dli->dl_nrlmult);
498 if (bavail < dli->dl_space_used)
501 bavail = (bavail - dli->dl_space_used)
502 >> sb->s_blocksize_bits;
504 /* reduce max space available to limit */
505 if (buf->f_blocks > blimit)
506 buf->f_blocks = blimit;
508 /* reduce free space to min */
509 if (bfree < buf->f_bfree)
510 buf->f_bfree = bfree;
512 /* reduce avail space to min */
513 if (bavail < buf->f_bavail)
514 buf->f_bavail = bavail;
517 spin_unlock(&dli->dl_lock);
523 #include <linux/module.h>
525 EXPORT_SYMBOL_GPL(locate_dl_info);
526 EXPORT_SYMBOL_GPL(rcu_free_dl_info);