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 #define CACHE_HOOK "cache_hook"
47 int cache_space_pre_hook(struct inode *inode, struct dentry *dentry,
48 void *data1, void *data2, int op, void *handle)
53 if (smfs_cache_hook(inode)) {
55 handle = smfs_trans_start(inode, KML_CACHE_NOOP, NULL);
57 RETURN(PTR_ERR(handle));
60 cache_space_pre(inode, op);
65 int cache_space_post_hook(struct inode *inode, struct dentry *dentry,
66 void *data1, void *data2, int op, void *handle)
70 if (smfs_cache_hook(inode)) {
71 struct inode *new_inode = (struct inode*)data1;
72 struct dentry *new_dentry = (struct dentry*)data2;
73 LASSERT(handle != NULL);
74 rc = cache_space_post(op, handle, inode, dentry, new_inode,
80 int cache_space_hook_init(struct super_block *sb)
82 struct smfs_super_info *smfs_info = S2SMI(sb);
83 struct smfs_hook_ops *cache_hops;
87 cache_hops = smfs_alloc_hook_ops(CACHE_HOOK, cache_space_pre_hook,
88 cache_space_post_hook);
92 rc = smfs_register_hook_ops(sb, cache_hops);
94 smfs_free_hook_ops(cache_hops);
97 SMFS_SET_CACHE_HOOK(smfs_info);
102 int cache_space_hook_exit(struct super_block *sb)
104 struct smfs_super_info *smfs_info = S2SMI(sb);
105 struct smfs_hook_ops *cache_hops;
107 cache_hops = smfs_unregister_hook_ops(sb, CACHE_HOOK);
108 smfs_free_hook_ops(cache_hops);
110 SMFS_CLEAN_CACHE_HOOK(smfs_info);
114 static int cache_leaf_node(struct dentry *dentry, __u64 *active_entry)
116 struct inode *inode = dentry->d_inode;
118 if (S_ISDIR(inode->i_mode)) {
119 if (inode->i_nlink != 2)
121 if (!strncmp(dentry->d_name.name, "lost+found", dentry->d_name.len))
123 LASSERT(active_entry != NULL);
124 get_active_entry(inode, active_entry);
125 return(*active_entry > 0 ? 0 : 1);
127 if (inode->i_nlink != 1)
129 if (!strncmp(dentry->d_name.name, KML_LOG_NAME, dentry->d_name.len) ||
130 !strncmp(dentry->d_name.name, CACHE_LRU_LOG, dentry->d_name.len))
135 static int cache_pre_leaf_node(struct dentry *dentry, __u64 *active_entry, int op)
137 if (((op == 0 && dentry->d_inode->i_nlink == 0) ||
138 (op == 1 && dentry->d_inode->i_nlink == 2)) &&
139 strncmp(dentry->d_name.name, KML_LOG_NAME, dentry->d_name.len) &&
140 strncmp(dentry->d_name.name, CACHE_LRU_LOG, dentry->d_name.len))
142 else if ((op == 2 && dentry->d_inode->i_nlink == 0) ||
143 (op == 3 && dentry->d_inode->i_nlink == 3)) {
144 LASSERT(active_entry != NULL);
145 get_active_entry(dentry->d_inode, active_entry);
146 return(*active_entry > 0 ? 0 : 1);
151 static int set_lru_logcookie(struct inode *inode, void *handle,
152 struct llog_cookie *logcookie)
154 struct fsfilt_operations *fsops = I2CSB(inode)->sm_fsfilt;
156 rc = fsops->fs_set_xattr(inode, handle, XATTR_SMFS_CACHE_LOGCOOKIE,
157 logcookie, sizeof(*logcookie));
160 static int get_lru_logcookie(struct inode *inode, struct llog_cookie *logcookie)
162 struct fsfilt_operations *fsops = I2CSB(inode)->sm_fsfilt;
164 rc = fsops->fs_get_xattr(inode, XATTR_SMFS_CACHE_LOGCOOKIE,
165 logcookie, sizeof(*logcookie));
169 static int try2purge_from_cache(struct ll_fid cfid, struct ll_fid pfid)
171 struct inode *inode, *parent;
172 struct super_block *sb = cpq->cpq_sb;
173 //struct llog_cookie logcookie;
174 __u32 hoard_priority = 0;
178 inode = iget(sb, cfid.id);
180 CERROR("not existent inode: "LPX64"/%u\n",
181 cfid.id, cfid.generation);
184 parent = iget(sb, pfid.id);
185 if (IS_ERR(parent)) {
186 CERROR("not existent inode: "LPX64"/%u\n",
187 pfid.id, pfid.generation);
192 CWARN("inode/parent %lu:%lu on the lru list\n",
193 inode->i_ino, parent->i_ino);
195 rc = get_hoard_priority(inode, &hoard_priority);
196 if (hoard_priority) {
197 CWARN("inode %lu set hoard\n", inode->i_ino);
200 if (atomic_read(&inode->i_count) > 1 || (inode->i_state & I_DIRTY)) {
201 CWARN("inode %lu is busy\n", inode->i_ino);
211 static int cache_lru_get_rec_cb(struct llog_handle *llh,
212 struct llog_rec_hdr *rec, void *data)
214 struct llog_lru_rec *llr;
215 int count = *(int *)data, rc = 0;
218 if (!(le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
219 CERROR("log is not plain\n");
222 if (rec->lrh_type != CACHE_LRU_REC) {
223 CERROR("log record type error\n");
227 llr = (struct llog_lru_rec *)rec;
229 if (try2purge_from_cache(llr->llr_cfid, llr->llr_pfid)==1){
230 CDEBUG(D_INODE, "purge ino/gen "LPX64"/%u from cache\n",
231 llr->llr_cfid.id, llr->llr_cfid.generation);
234 rc = LLOG_PROC_BREAK;
235 *(int *)data = count;
241 static int cpurge_stop(void)
243 struct fsfilt_operations *fsops = S2SMI(cpq->cpq_sb)->sm_fsfilt;
244 struct obd_statfs osfs;
247 rc = fsops->fs_statfs(cpq->cpq_sb, &osfs);
250 free = osfs.os_bfree * 100;
251 if (free < cf_prm.nfract_stop_cpurge * osfs.os_blocks)
256 static int cache_balance_state(void)
258 struct fsfilt_operations *fsops = S2SMI(cpq->cpq_sb)->sm_fsfilt;
259 struct obd_statfs osfs;
262 rc = fsops->fs_statfs(cpq->cpq_sb, &osfs);
265 free = (osfs.os_blocks - osfs.os_bfree) * 100;
266 if (free > cf_prm.nfract * osfs.os_blocks) {
267 if (free < cf_prm.nfract_sync)
274 void wakeup_cpurge(void)
276 wake_up(&cpq->cpq_waitq);
279 /* walk the lru llog to purge count number of objects */
280 static int purge_some_cache(int *count)
285 rc = llog_cat_process(cpq->cpq_loghandle,
286 (llog_cb_t)cache_lru_get_rec_cb,
289 CDEBUG(D_INODE, "no enough objects available\n");
294 #define CFLUSH_NR 512
295 static void check_cache_space(void)
297 int state = cache_balance_state();
306 int count = CFLUSH_NR;
307 purge_some_cache(&count);
311 void cache_space_pre(struct inode *inode, int op)
315 /* FIXME have not used op */
319 static int cache_space_hook_lru(struct inode *inode, struct inode *parent,
320 void *handle, int op, int flags)
322 struct fsfilt_operations *fsops = S2SMI(cpq->cpq_sb)->sm_fsfilt;
323 struct llog_ctxt *ctxt = cpq->cpq_loghandle->lgh_ctxt;
324 struct llog_lru_rec *llr = NULL;
325 struct llog_cookie *logcookie = NULL;
326 int cookie_size = sizeof(struct llog_cookie);
330 LASSERT(ctxt != NULL);
332 if (op & ~(CACHE_SPACE_DELETE | CACHE_SPACE_INSERT |CACHE_SPACE_COMMIT))
335 OBD_ALLOC(logcookie, cookie_size);
337 GOTO(out, rc = -ENOMEM);
339 if (op & CACHE_SPACE_DELETE) {
340 rc = get_lru_logcookie(inode, logcookie);
344 if (logcookie->lgc_lgl.lgl_oid == 0) {
345 CWARN("inode %lu/%u is not in lru list\n",
346 inode->i_ino, inode->i_generation);
347 GOTO(insert, rc = -ENOENT);
349 if (flags && llog_cat_half_bottom(logcookie, ctxt->loc_handle))
352 rc = llog_cancel(ctxt, 1, logcookie, 0, NULL);
354 memset(logcookie, 0, cookie_size);
355 rc = set_lru_logcookie(inode, handle, logcookie);
359 CERROR("failed at llog_cancel: %d\n", rc);
365 if (op & CACHE_SPACE_INSERT) {
366 LASSERT(parent != NULL);
367 OBD_ALLOC(llr, sizeof(*llr));
369 GOTO(out, rc = -ENOMEM);
371 llr->llr_hdr.lrh_len = llr->llr_tail.lrt_len = sizeof(*llr);
372 llr->llr_hdr.lrh_type = CACHE_LRU_REC;
373 llr->llr_cfid.id = inode->i_ino;
374 llr->llr_cfid.generation = inode->i_generation;
375 llr->llr_cfid.f_type = inode->i_mode & S_IFMT;
376 llr->llr_pfid.id = parent->i_ino;
377 llr->llr_pfid.generation = parent->i_generation;
378 llr->llr_pfid.f_type = parent->i_mode & S_IFMT;
380 rc = llog_add(ctxt, &llr->llr_hdr, NULL, logcookie, 1,
383 CERROR("failed at llog_add: %d\n", rc);
386 rc = set_lru_logcookie(inode, handle, logcookie);
389 if (op & CACHE_SPACE_COMMIT) {
391 err = fsops->fs_commit(inode->i_sb, inode, handle, 0);
393 CERROR("error committing transaction: %d\n", err);
401 OBD_FREE(logcookie, cookie_size);
403 OBD_FREE(llr, sizeof(*llr));
407 static int cache_purge_thread(void *args)
410 struct l_wait_info lwi = LWI_TIMEOUT(cf_prm.interval * HZ, NULL, NULL);
414 kportal_daemonize("wb_cache_purge");
416 SIGNAL_MASK_LOCK(current, flags);
417 sigfillset(¤t->blocked);
419 SIGNAL_MASK_UNLOCK(current, flags);
422 complete(&cpq->cpq_comp);
425 int ndirty = cf_prm.ndirty;
427 purge_some_cache(&ndirty);
428 if (ndirty > 0 || cpurge_stop())
429 l_wait_event(cpq->cpq_waitq,
430 cpq->cpq_flags & SVC_STOPPING,
432 if (cpq->cpq_flags & SVC_STOPPING) {
433 cpq->cpq_flags &= ~SVC_STOPPING;
438 cpq->cpq_flags = SVC_STOPPED;
439 complete(&cpq->cpq_comp);
443 int cache_space_hook_setup(struct super_block *sb)
445 struct llog_ctxt *ctxt;
449 /* first to initialize the cache lru catalog on local fs */
450 rc = llog_catalog_setup(&ctxt, CACHE_LRU_LOG,
452 S2SMI(sb)->smsi_ctxt,
453 S2SMI(sb)->sm_fsfilt,
454 S2SMI(sb)->smsi_logs_dir,
455 S2SMI(sb)->smsi_objects_dir);
457 CERROR("failed to initialize cache lru list catalog %d\n", rc);
461 cpq->cpq_loghandle = ctxt->loc_handle;
463 /* start cache purge daemon, only one daemon now */
464 init_waitqueue_head(&cpq->cpq_waitq);
465 init_completion(&cpq->cpq_comp);
468 rc = kernel_thread(cache_purge_thread, NULL, CLONE_VM | CLONE_FILES);
470 CERROR("cannot start thread: %d\n", rc);
473 wait_for_completion(&cpq->cpq_comp);
477 llog_catalog_cleanup(ctxt);
478 OBD_FREE(ctxt, sizeof(*ctxt));
482 int cache_space_hook_cleanup(void)
484 struct llog_ctxt *ctxt;
488 init_completion(&cpq->cpq_comp);
489 cpq->cpq_flags = SVC_STOPPING;
490 wake_up(&cpq->cpq_waitq);
491 wait_for_completion(&cpq->cpq_comp);
493 ctxt = cpq->cpq_loghandle->lgh_ctxt;
494 rc = llog_catalog_cleanup(ctxt);
495 OBD_FREE(ctxt, sizeof(*ctxt));
497 CERROR("failed to clean up cache lru list catalog %d\n", rc);
502 static int cache_space_hook_create(void *handle, struct inode *dir,
503 struct dentry *dentry, struct inode *new_dir,
504 struct dentry *new_dentry)
506 __u64 active_entry = 0;
509 LASSERT(cache_leaf_node(dentry, NULL));
510 rc = cache_space_hook_lru(dentry->d_inode, dir, handle,
511 CACHE_SPACE_INSERT, 0);
514 if (cache_leaf_node(dentry->d_parent, &active_entry)) {
515 rc = cache_space_hook_lru(dir,NULL,handle,CACHE_SPACE_DELETE,0);
520 rc = get_active_entry(dir, &active_entry);
523 rc = set_active_entry(dir, &active_entry, handle);
526 static int cache_space_hook_lookup(void *handle, struct inode *dir,
527 struct dentry *dentry, struct inode *new_dir,
528 struct dentry *new_dentry)
533 if (cache_leaf_node(dentry, &active_entry))
534 rc = cache_space_hook_lru(dentry->d_inode, dir, handle,
535 CACHE_SPACE_DELETE | CACHE_SPACE_INSERT,1);
538 static int cache_space_hook_link(void *handle, struct inode *dir,
539 struct dentry *dentry, struct inode *new_dir,
540 struct dentry *new_dentry)
542 __u64 active_entry = 0;
545 if (cache_pre_leaf_node(dentry, NULL, 1)) {
546 rc = cache_space_hook_lru(dentry->d_inode, NULL,
547 handle, CACHE_SPACE_DELETE, 0);
552 if (cache_leaf_node(dentry->d_parent, &active_entry)) {
553 rc = cache_space_hook_lru(dir,NULL,handle,CACHE_SPACE_DELETE,0);
559 rc = get_active_entry(dir, &active_entry);
562 rc = set_active_entry(dir, &active_entry, handle);
565 static int cache_space_hook_unlink(void *handle, struct inode *dir,
566 struct dentry *dentry, struct inode *new_dir,
567 struct dentry *new_dentry)
572 if (cache_pre_leaf_node(dentry, NULL, 0))
573 rc = cache_space_hook_lru(dentry->d_inode, NULL,
574 handle, CACHE_SPACE_DELETE, 0);
575 else if (cache_leaf_node(dentry, NULL))
576 rc = cache_space_hook_lru(dentry->d_inode, dir,
577 handle, CACHE_SPACE_INSERT,0);
581 rc = get_active_entry(dir, &active_entry);
584 rc = set_active_entry(dir, &active_entry, handle);
585 if (!rc && cache_leaf_node(dentry->d_parent, &active_entry))
586 rc = cache_space_hook_lru(dir,
587 dentry->d_parent->d_parent->d_inode,
588 handle, CACHE_SPACE_INSERT, 0);
591 static int cache_space_hook_mkdir(void *handle, struct inode *dir,
592 struct dentry *dentry, struct inode *new_dir,
593 struct dentry *new_dentry)
598 LASSERT(cache_leaf_node(dentry, &active_entry));
599 rc = cache_space_hook_lru(dentry->d_inode, dir, handle,
600 CACHE_SPACE_INSERT,0);
602 if (!rc && cache_pre_leaf_node(dentry->d_parent, &active_entry, 3))
603 rc = cache_space_hook_lru(dir,NULL,handle,CACHE_SPACE_DELETE,0);
606 static int cache_space_hook_rmdir(void *handle, struct inode *dir,
607 struct dentry *dentry, struct inode *new_dir,
608 struct dentry *new_dentry)
613 LASSERT(cache_pre_leaf_node(dentry, &active_entry, 2));
614 rc = cache_space_hook_lru(dentry->d_inode, NULL, handle,
615 CACHE_SPACE_DELETE, 0);
617 if (!rc && cache_leaf_node(dentry->d_parent, &active_entry))
618 rc = cache_space_hook_lru(dir,
619 dentry->d_parent->d_parent->d_inode,
620 handle, CACHE_SPACE_INSERT, 0);
623 static int cache_space_hook_rename(void *handle, struct inode *old_dir,
624 struct dentry *old_dentry, struct inode *new_dir,
625 struct dentry *new_dentry)
630 if (new_dentry->d_inode) {
631 if (cache_pre_leaf_node(new_dentry, NULL, 0))
632 rc = cache_space_hook_lru(new_dentry->d_inode, NULL,
633 handle, CACHE_SPACE_DELETE,0);
634 else if (cache_leaf_node(new_dentry, NULL))
635 rc = cache_space_hook_lru(new_dentry->d_inode,
637 CACHE_SPACE_INSERT,0);
640 if (rc || old_dir == new_dir)
643 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
644 if (cache_leaf_node(new_dentry->d_parent, &active_entry)) {
645 rc = cache_space_hook_lru(new_dir, NULL, handle,
646 CACHE_SPACE_DELETE, 0);
651 rc = get_active_entry(new_dir, &active_entry);
654 rc = set_active_entry(new_dir, &active_entry, handle);
657 rc = get_active_entry(old_dir, &active_entry);
660 rc = set_active_entry(old_dir, &active_entry, handle);
661 } else if (cache_pre_leaf_node(new_dentry->d_parent, &active_entry, 3))
662 rc = cache_space_hook_lru(new_dir, NULL, handle,
663 CACHE_SPACE_DELETE, 0);
665 if (!rc && cache_leaf_node(old_dentry->d_parent, &active_entry))
666 rc = cache_space_hook_lru(old_dir,
667 old_dentry->d_parent->d_parent->d_inode,
668 handle, CACHE_SPACE_INSERT, 0);
672 typedef int (*cache_hook_op)(void *handle, struct inode *old_dir,
673 struct dentry *old_dentry, struct inode *new_dir,
674 struct dentry *new_dentry);
676 static cache_hook_op cache_space_hook_ops[HOOK_MAX + 1] = {
677 [HOOK_CREATE] cache_space_hook_create,
678 [HOOK_LOOKUP] cache_space_hook_lookup,
679 [HOOK_LINK] cache_space_hook_link,
680 [HOOK_UNLINK] cache_space_hook_unlink,
681 [HOOK_SYMLINK] cache_space_hook_create,
682 [HOOK_MKDIR] cache_space_hook_mkdir,
683 [HOOK_RMDIR] cache_space_hook_rmdir,
684 [HOOK_MKNOD] cache_space_hook_create,
685 [HOOK_RENAME] cache_space_hook_rename,
690 int cache_space_post(int op, void *handle, struct inode *old_dir,
691 struct dentry *old_dentry, struct inode *new_dir,
692 struct dentry *new_dentry)
697 LASSERT(op <= HOOK_MAX + 1);
699 if (cache_space_hook_ops[op])
700 rc = cache_space_hook_ops[op](handle, old_dir, old_dentry,
701 new_dir, new_dentry);