1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/smfs/cache_space.c
5 * A library of functions to manage cache space based on ARC
6 * (modified LRU) replacement algorithm.
8 * Copyright (c) 2004 Cluster File Systems, Inc.
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DEBUG_SUBSYSTEM S_SM
27 #include <linux/lustre_log.h>
28 #include <linux/lustre_fsfilt.h>
29 #include <linux/lustre_smfs.h>
31 #include "smfs_internal.h"
33 struct cache_purge_param {
34 int nfract; /* Percentage of cache dirty to activate cpurge */
35 int ndirty; /* Maximum number of objects to write out per
37 int interval; /* jiffies delay between cache purge */
38 int nfract_sync;/* Percentage of cache dirty to activate
39 cpurge synchronously */
40 int nfract_stop_cpurge; /* Percentage of cache dirty to stop cpurge */
41 } cf_prm = {30, 512, 600 * HZ, 60, 20};
43 static struct cache_purge_queue smfs_cpq;
44 static struct cache_purge_queue *cpq = &smfs_cpq;
46 int cache_space_hook_init(struct super_block *sb)
48 struct smfs_super_info *smfs_info = S2SMI(sb);
50 SMFS_SET_CACHE_HOOK(smfs_info);
53 int cache_space_hook_exit(struct super_block *sb)
55 struct smfs_super_info *smfs_info = S2SMI(sb);
57 SMFS_CLEAN_CACHE_HOOK(smfs_info);
61 static int cache_leaf_node(struct dentry *dentry, __u64 *active_entry)
63 struct inode *inode = dentry->d_inode;
65 if (S_ISDIR(inode->i_mode)) {
66 if (inode->i_nlink != 2)
68 if (!strncmp(dentry->d_name.name, "lost+found", dentry->d_name.len))
70 LASSERT(active_entry != NULL);
71 get_active_entry(inode, active_entry);
72 return(*active_entry > 0 ? 0 : 1);
74 if (inode->i_nlink != 1)
76 if (!strncmp(dentry->d_name.name, KML_LOG_NAME, dentry->d_name.len) ||
77 !strncmp(dentry->d_name.name, CACHE_LRU_LOG, dentry->d_name.len))
82 static int cache_pre_leaf_node(struct dentry *dentry, __u64 *active_entry, int op)
84 if (((op == 0 && dentry->d_inode->i_nlink == 0) ||
85 (op == 1 && dentry->d_inode->i_nlink == 2)) &&
86 strncmp(dentry->d_name.name, KML_LOG_NAME, dentry->d_name.len) &&
87 strncmp(dentry->d_name.name, CACHE_LRU_LOG, dentry->d_name.len))
89 else if ((op == 2 && dentry->d_inode->i_nlink == 0) ||
90 (op == 3 && dentry->d_inode->i_nlink == 3)) {
91 LASSERT(active_entry != NULL);
92 get_active_entry(dentry->d_inode, active_entry);
93 return(*active_entry > 0 ? 0 : 1);
98 static int set_lru_logcookie(struct inode *inode, void *handle,
99 struct llog_cookie *logcookie)
101 struct fsfilt_operations *fsops = I2CSB(inode)->sm_fsfilt;
103 rc = fsops->fs_set_xattr(inode, handle, XATTR_SMFS_CACHE_LOGCOOKIE,
104 logcookie, sizeof(*logcookie));
107 static int get_lru_logcookie(struct inode *inode, struct llog_cookie *logcookie)
109 struct fsfilt_operations *fsops = I2CSB(inode)->sm_fsfilt;
111 rc = fsops->fs_get_xattr(inode, XATTR_SMFS_CACHE_LOGCOOKIE,
112 logcookie, sizeof(*logcookie));
116 static int try2purge_from_cache(struct ll_fid cfid, struct ll_fid pfid)
118 struct inode *inode, *parent;
119 struct super_block *sb = cpq->cpq_sb;
120 //struct llog_cookie logcookie;
121 __u32 hoard_priority = 0;
125 inode = iget(sb, cfid.id);
127 CERROR("not existent inode: "LPX64"/%u\n",
128 cfid.id, cfid.generation);
131 parent = iget(sb, pfid.id);
132 if (IS_ERR(parent)) {
133 CERROR("not existent inode: "LPX64"/%u\n",
134 pfid.id, pfid.generation);
139 CWARN("inode/parent %lu:%lu on the lru list\n",
140 inode->i_ino, parent->i_ino);
142 rc = get_hoard_priority(inode, &hoard_priority);
143 if (hoard_priority) {
144 CWARN("inode %lu set hoard\n", inode->i_ino);
147 if (atomic_read(&inode->i_count) > 1 || (inode->i_state & I_DIRTY)) {
148 CWARN("inode %lu is busy\n", inode->i_ino);
158 static int cache_lru_get_rec_cb(struct llog_handle *llh,
159 struct llog_rec_hdr *rec, void *data)
161 struct llog_lru_rec *llr;
162 int count = *(int *)data, rc = 0;
165 if (!(le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
166 CERROR("log is not plain\n");
169 if (rec->lrh_type != CACHE_LRU_REC) {
170 CERROR("log record type error\n");
174 llr = (struct llog_lru_rec *)rec;
176 if (try2purge_from_cache(llr->llr_cfid, llr->llr_pfid)==1){
177 CDEBUG(D_INODE, "purge ino/gen "LPX64"/%u from cache\n",
178 llr->llr_cfid.id, llr->llr_cfid.generation);
181 rc = LLOG_PROC_BREAK;
182 *(int *)data = count;
188 static int cpurge_stop(void)
190 struct fsfilt_operations *fsops = S2SMI(cpq->cpq_sb)->sm_fsfilt;
191 struct obd_statfs osfs;
194 rc = fsops->fs_statfs(cpq->cpq_sb, &osfs);
197 free = osfs.os_bfree * 100;
198 if (free < cf_prm.nfract_stop_cpurge * osfs.os_blocks)
203 static int cache_balance_state(void)
205 struct fsfilt_operations *fsops = S2SMI(cpq->cpq_sb)->sm_fsfilt;
206 struct obd_statfs osfs;
209 rc = fsops->fs_statfs(cpq->cpq_sb, &osfs);
212 free = (osfs.os_blocks - osfs.os_bfree) * 100;
213 if (free > cf_prm.nfract * osfs.os_blocks) {
214 if (free < cf_prm.nfract_sync)
221 void wakeup_cpurge(void)
223 wake_up(&cpq->cpq_waitq);
226 /* walk the lru llog to purge count number of objects */
227 static int purge_some_cache(int *count)
232 rc = llog_cat_process(cpq->cpq_loghandle,
233 (llog_cb_t)cache_lru_get_rec_cb,
236 CDEBUG(D_INODE, "no enough objects available\n");
241 #define CFLUSH_NR 512
242 static void check_cache_space(void)
244 int state = cache_balance_state();
253 int count = CFLUSH_NR;
254 purge_some_cache(&count);
258 void cache_space_pre(struct inode *inode, int op)
262 /* FIXME have not used op */
266 static int cache_space_hook_lru(struct inode *inode, struct inode *parent,
267 void *handle, int op, int flags)
269 struct fsfilt_operations *fsops = S2SMI(cpq->cpq_sb)->sm_fsfilt;
270 struct llog_ctxt *ctxt = cpq->cpq_loghandle->lgh_ctxt;
271 struct llog_lru_rec *llr = NULL;
272 struct llog_cookie *logcookie = NULL;
273 int cookie_size = sizeof(struct llog_cookie);
277 LASSERT(ctxt != NULL);
279 if (op & ~(CACHE_SPACE_DELETE | CACHE_SPACE_INSERT |CACHE_SPACE_COMMIT))
282 OBD_ALLOC(logcookie, cookie_size);
284 GOTO(out, rc = -ENOMEM);
286 if (op & CACHE_SPACE_DELETE) {
287 rc = get_lru_logcookie(inode, logcookie);
291 if (logcookie->lgc_lgl.lgl_oid == 0) {
292 CWARN("inode %lu/%u is not in lru list\n",
293 inode->i_ino, inode->i_generation);
294 GOTO(insert, rc = -ENOENT);
296 if (flags && llog_cat_half_bottom(logcookie, ctxt->loc_handle))
299 rc = llog_cancel(ctxt, 1, logcookie, 0, NULL);
301 memset(logcookie, 0, cookie_size);
302 rc = set_lru_logcookie(inode, handle, logcookie);
306 CERROR("failed at llog_cancel: %d\n", rc);
312 if (op & CACHE_SPACE_INSERT) {
313 LASSERT(parent != NULL);
314 OBD_ALLOC(llr, sizeof(*llr));
316 GOTO(out, rc = -ENOMEM);
318 llr->llr_hdr.lrh_len = llr->llr_tail.lrt_len = sizeof(*llr);
319 llr->llr_hdr.lrh_type = CACHE_LRU_REC;
320 llr->llr_cfid.id = inode->i_ino;
321 llr->llr_cfid.generation = inode->i_generation;
322 llr->llr_cfid.f_type = inode->i_mode & S_IFMT;
323 llr->llr_pfid.id = parent->i_ino;
324 llr->llr_pfid.generation = parent->i_generation;
325 llr->llr_pfid.f_type = parent->i_mode & S_IFMT;
327 rc = llog_add(ctxt, &llr->llr_hdr, NULL, logcookie, 1,
330 CERROR("failed at llog_add: %d\n", rc);
333 rc = set_lru_logcookie(inode, handle, logcookie);
336 if (op & CACHE_SPACE_COMMIT) {
338 err = fsops->fs_commit(inode->i_sb, inode, handle, 0);
340 CERROR("error committing transaction: %d\n", err);
348 OBD_FREE(logcookie, cookie_size);
350 OBD_FREE(llr, sizeof(*llr));
354 static int cache_purge_thread(void *args)
357 struct l_wait_info lwi = LWI_TIMEOUT(cf_prm.interval * HZ, NULL, NULL);
361 kportal_daemonize("wb_cache_purge");
363 SIGNAL_MASK_LOCK(current, flags);
364 sigfillset(¤t->blocked);
366 SIGNAL_MASK_UNLOCK(current, flags);
369 complete(&cpq->cpq_comp);
372 int ndirty = cf_prm.ndirty;
374 purge_some_cache(&ndirty);
375 if (ndirty > 0 || cpurge_stop())
376 l_wait_event(cpq->cpq_waitq,
377 cpq->cpq_flags & SVC_STOPPING,
379 if (cpq->cpq_flags & SVC_STOPPING) {
380 cpq->cpq_flags &= ~SVC_STOPPING;
385 cpq->cpq_flags = SVC_STOPPED;
386 complete(&cpq->cpq_comp);
390 int cache_space_hook_setup(struct super_block *sb)
392 struct llog_ctxt *ctxt;
396 /* first to initialize the cache lru catalog on local fs */
397 rc = llog_catalog_setup(&ctxt, CACHE_LRU_LOG,
399 S2SMI(sb)->smsi_ctxt,
400 S2SMI(sb)->sm_fsfilt,
401 S2SMI(sb)->smsi_logs_dir,
402 S2SMI(sb)->smsi_objects_dir);
404 CERROR("failed to initialize cache lru list catalog %d\n", rc);
408 cpq->cpq_loghandle = ctxt->loc_handle;
410 /* start cache purge daemon, only one daemon now */
411 init_waitqueue_head(&cpq->cpq_waitq);
412 init_completion(&cpq->cpq_comp);
415 rc = kernel_thread(cache_purge_thread, NULL, CLONE_VM | CLONE_FILES);
417 CERROR("cannot start thread: %d\n", rc);
420 wait_for_completion(&cpq->cpq_comp);
424 llog_catalog_cleanup(ctxt);
425 OBD_FREE(ctxt, sizeof(*ctxt));
429 int cache_space_hook_cleanup(void)
431 struct llog_ctxt *ctxt;
435 init_completion(&cpq->cpq_comp);
436 cpq->cpq_flags = SVC_STOPPING;
437 wake_up(&cpq->cpq_waitq);
438 wait_for_completion(&cpq->cpq_comp);
440 ctxt = cpq->cpq_loghandle->lgh_ctxt;
441 rc = llog_catalog_cleanup(ctxt);
442 OBD_FREE(ctxt, sizeof(*ctxt));
444 CERROR("failed to clean up cache lru list catalog %d\n", rc);
449 static int cache_space_hook_create(void *handle, struct inode *dir,
450 struct dentry *dentry, struct inode *new_dir,
451 struct dentry *new_dentry)
453 __u64 active_entry = 0;
456 LASSERT(cache_leaf_node(dentry, NULL));
457 rc = cache_space_hook_lru(dentry->d_inode, dir, handle,
458 CACHE_SPACE_INSERT, 0);
461 if (cache_leaf_node(dentry->d_parent, &active_entry)) {
462 rc = cache_space_hook_lru(dir,NULL,handle,CACHE_SPACE_DELETE,0);
467 rc = get_active_entry(dir, &active_entry);
470 rc = set_active_entry(dir, &active_entry, handle);
473 static int cache_space_hook_lookup(void *handle, struct inode *dir,
474 struct dentry *dentry, struct inode *new_dir,
475 struct dentry *new_dentry)
480 if (cache_leaf_node(dentry, &active_entry))
481 rc = cache_space_hook_lru(dentry->d_inode, dir, handle,
482 CACHE_SPACE_DELETE | CACHE_SPACE_INSERT,1);
485 static int cache_space_hook_link(void *handle, struct inode *dir,
486 struct dentry *dentry, struct inode *new_dir,
487 struct dentry *new_dentry)
489 __u64 active_entry = 0;
492 if (cache_pre_leaf_node(dentry, NULL, 1)) {
493 rc = cache_space_hook_lru(dentry->d_inode, NULL,
494 handle, CACHE_SPACE_DELETE, 0);
499 if (cache_leaf_node(dentry->d_parent, &active_entry)) {
500 rc = cache_space_hook_lru(dir,NULL,handle,CACHE_SPACE_DELETE,0);
506 rc = get_active_entry(dir, &active_entry);
509 rc = set_active_entry(dir, &active_entry, handle);
512 static int cache_space_hook_unlink(void *handle, struct inode *dir,
513 struct dentry *dentry, struct inode *new_dir,
514 struct dentry *new_dentry)
519 if (cache_pre_leaf_node(dentry, NULL, 0))
520 rc = cache_space_hook_lru(dentry->d_inode, NULL,
521 handle, CACHE_SPACE_DELETE, 0);
522 else if (cache_leaf_node(dentry, NULL))
523 rc = cache_space_hook_lru(dentry->d_inode, dir,
524 handle, CACHE_SPACE_INSERT,0);
528 rc = get_active_entry(dir, &active_entry);
531 rc = set_active_entry(dir, &active_entry, handle);
532 if (!rc && cache_leaf_node(dentry->d_parent, &active_entry))
533 rc = cache_space_hook_lru(dir,
534 dentry->d_parent->d_parent->d_inode,
535 handle, CACHE_SPACE_INSERT, 0);
538 static int cache_space_hook_mkdir(void *handle, struct inode *dir,
539 struct dentry *dentry, struct inode *new_dir,
540 struct dentry *new_dentry)
545 LASSERT(cache_leaf_node(dentry, &active_entry));
546 rc = cache_space_hook_lru(dentry->d_inode, dir, handle,
547 CACHE_SPACE_INSERT,0);
549 if (!rc && cache_pre_leaf_node(dentry->d_parent, &active_entry, 3))
550 rc = cache_space_hook_lru(dir,NULL,handle,CACHE_SPACE_DELETE,0);
553 static int cache_space_hook_rmdir(void *handle, struct inode *dir,
554 struct dentry *dentry, struct inode *new_dir,
555 struct dentry *new_dentry)
560 LASSERT(cache_pre_leaf_node(dentry, &active_entry, 2));
561 rc = cache_space_hook_lru(dentry->d_inode, NULL, handle,
562 CACHE_SPACE_DELETE, 0);
564 if (!rc && cache_leaf_node(dentry->d_parent, &active_entry))
565 rc = cache_space_hook_lru(dir,
566 dentry->d_parent->d_parent->d_inode,
567 handle, CACHE_SPACE_INSERT, 0);
570 static int cache_space_hook_rename(void *handle, struct inode *old_dir,
571 struct dentry *old_dentry, struct inode *new_dir,
572 struct dentry *new_dentry)
577 if (new_dentry->d_inode) {
578 if (cache_pre_leaf_node(new_dentry, NULL, 0))
579 rc = cache_space_hook_lru(new_dentry->d_inode, NULL,
580 handle, CACHE_SPACE_DELETE,0);
581 else if (cache_leaf_node(new_dentry, NULL))
582 rc = cache_space_hook_lru(new_dentry->d_inode,
584 CACHE_SPACE_INSERT,0);
587 if (rc || old_dir == new_dir)
590 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
591 if (cache_leaf_node(new_dentry->d_parent, &active_entry)) {
592 rc = cache_space_hook_lru(new_dir, NULL, handle,
593 CACHE_SPACE_DELETE, 0);
598 rc = get_active_entry(new_dir, &active_entry);
601 rc = set_active_entry(new_dir, &active_entry, handle);
604 rc = get_active_entry(old_dir, &active_entry);
607 rc = set_active_entry(old_dir, &active_entry, handle);
608 } else if (cache_pre_leaf_node(new_dentry->d_parent, &active_entry, 3))
609 rc = cache_space_hook_lru(new_dir, NULL, handle,
610 CACHE_SPACE_DELETE, 0);
612 if (!rc && cache_leaf_node(old_dentry->d_parent, &active_entry))
613 rc = cache_space_hook_lru(old_dir,
614 old_dentry->d_parent->d_parent->d_inode,
615 handle, CACHE_SPACE_INSERT, 0);
619 typedef int (*cache_hook_op)(void *handle, struct inode *old_dir,
620 struct dentry *old_dentry, struct inode *new_dir,
621 struct dentry *new_dentry);
622 static cache_hook_op cache_space_hook_ops[CACHE_HOOK_MAX + 1] = {
623 [CACHE_HOOK_CREATE] cache_space_hook_create,
624 [CACHE_HOOK_LOOKUP] cache_space_hook_lookup,
625 [CACHE_HOOK_LINK] cache_space_hook_link,
626 [CACHE_HOOK_UNLINK] cache_space_hook_unlink,
627 [CACHE_HOOK_SYMLINK] cache_space_hook_create,
628 [CACHE_HOOK_MKDIR] cache_space_hook_mkdir,
629 [CACHE_HOOK_RMDIR] cache_space_hook_rmdir,
630 [CACHE_HOOK_MKNOD] cache_space_hook_create,
631 [CACHE_HOOK_RENAME] cache_space_hook_rename,
634 int cache_space_post(int op, void *handle, struct inode *old_dir,
635 struct dentry *old_dentry, struct inode *new_dir,
636 struct dentry *new_dentry)
641 LASSERT(op <= CACHE_HOOK_MAX && cache_space_hook_ops[op] != NULL);
643 rc = cache_space_hook_ops[op](handle, old_dir, old_dentry,
644 new_dir, new_dentry);