1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/llite/llite_capa.c
38 * Author: Lai Siyao <lsy@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <linux/version.h>
45 #include <asm/uaccess.h>
46 #include <linux/file.h>
47 #include <linux/kmod.h>
49 #include <lustre_lite.h>
50 #include "llite_internal.h"
52 /* for obd_capa.c_list, client capa might stay in three places:
55 * 3. stand alone: just allocated.
58 /* capas for oss writeback and those failed to renew */
59 static CFS_LIST_HEAD(ll_idle_capas);
60 static struct ptlrpc_thread ll_capa_thread;
61 static cfs_list_t *ll_capa_list = &capa_list[CAPA_SITE_CLIENT];
63 /* llite capa renewal timer */
64 struct timer_list ll_capa_timer;
65 /* for debug: indicate whether capa on llite is enabled or not */
66 static cfs_atomic_t ll_capa_debug = CFS_ATOMIC_INIT(0);
67 static unsigned long long ll_capa_renewed = 0;
68 static unsigned long long ll_capa_renewal_noent = 0;
69 static unsigned long long ll_capa_renewal_failed = 0;
70 static unsigned long long ll_capa_renewal_retries = 0;
72 static inline void update_capa_timer(struct obd_capa *ocapa, cfs_time_t expiry)
74 if (cfs_time_before(expiry, ll_capa_timer.expires) ||
75 !timer_pending(&ll_capa_timer)) {
76 mod_timer(&ll_capa_timer, expiry);
77 DEBUG_CAPA(D_SEC, &ocapa->c_capa,
78 "ll_capa_timer update: %lu/%lu by", expiry, jiffies);
82 static inline cfs_time_t capa_renewal_time(struct obd_capa *ocapa)
84 return cfs_time_sub(ocapa->c_expiry,
85 cfs_time_seconds(ocapa->c_capa.lc_timeout) / 2);
88 static inline int capa_is_to_expire(struct obd_capa *ocapa)
90 return cfs_time_beforeq(capa_renewal_time(ocapa), cfs_time_current());
93 static inline int have_expired_capa(void)
95 struct obd_capa *ocapa = NULL;
98 /* if ll_capa_list has client capa to expire or ll_idle_capas has
99 * expired capa, return 1.
101 cfs_spin_lock(&capa_lock);
102 if (!cfs_list_empty(ll_capa_list)) {
103 ocapa = cfs_list_entry(ll_capa_list->next, struct obd_capa,
105 expired = capa_is_to_expire(ocapa);
107 update_capa_timer(ocapa, capa_renewal_time(ocapa));
108 } else if (!cfs_list_empty(&ll_idle_capas)) {
109 ocapa = cfs_list_entry(ll_idle_capas.next, struct obd_capa,
111 expired = capa_is_expired(ocapa);
113 update_capa_timer(ocapa, ocapa->c_expiry);
115 cfs_spin_unlock(&capa_lock);
118 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "expired");
122 static inline int ll_capa_check_stop(void)
124 return (ll_capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
127 static void sort_add_capa(struct obd_capa *ocapa, cfs_list_t *head)
129 struct obd_capa *tmp;
130 cfs_list_t *before = NULL;
132 /* TODO: client capa is sorted by expiry, this could be optimized */
133 cfs_list_for_each_entry_reverse(tmp, head, c_list) {
134 if (cfs_time_aftereq(ocapa->c_expiry, tmp->c_expiry)) {
135 before = &tmp->c_list;
140 LASSERT(&ocapa->c_list != before);
141 cfs_list_add(&ocapa->c_list, before ?: head);
144 static inline int obd_capa_open_count(struct obd_capa *oc)
146 struct ll_inode_info *lli = ll_i2info(oc->u.cli.inode);
147 return cfs_atomic_read(&lli->lli_open_count);
150 static void ll_delete_capa(struct obd_capa *ocapa)
152 struct ll_inode_info *lli = ll_i2info(ocapa->u.cli.inode);
154 if (capa_for_mds(&ocapa->c_capa)) {
155 LASSERT(lli->lli_mds_capa == ocapa);
156 lli->lli_mds_capa = NULL;
157 } else if (capa_for_oss(&ocapa->c_capa)) {
158 cfs_list_del_init(&ocapa->u.cli.lli_list);
161 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free client");
162 cfs_list_del_init(&ocapa->c_list);
163 capa_count[CAPA_SITE_CLIENT]--;
164 /* release the ref when alloc */
168 /* three places where client capa is deleted:
169 * 1. capa_thread_main(), main place to delete expired capa.
170 * 2. ll_clear_inode_capas() in ll_clear_inode().
171 * 3. ll_truncate_free_capa() delete truncate capa explicitly in ll_truncate().
173 static int capa_thread_main(void *unused)
175 struct obd_capa *ocapa, *tmp, *next;
176 struct inode *inode = NULL;
177 struct l_wait_info lwi = { 0 };
181 cfs_daemonize("ll_capa");
183 ll_capa_thread.t_flags = SVC_RUNNING;
184 cfs_waitq_signal(&ll_capa_thread.t_ctl_waitq);
187 l_wait_event(ll_capa_thread.t_ctl_waitq,
188 (ll_capa_check_stop() || have_expired_capa()),
191 if (ll_capa_check_stop())
196 cfs_spin_lock(&capa_lock);
197 cfs_list_for_each_entry_safe(ocapa, tmp, ll_capa_list, c_list) {
198 LASSERT(ocapa->c_capa.lc_opc != CAPA_OPC_OSS_TRUNC);
200 if (!capa_is_to_expire(ocapa)) {
205 cfs_list_del_init(&ocapa->c_list);
207 /* for MDS capability, only renew those which belong to
208 * dir, or its inode is opened, or client holds LOOKUP
211 if (capa_for_mds(&ocapa->c_capa) &&
212 !S_ISDIR(ocapa->u.cli.inode->i_mode) &&
213 obd_capa_open_count(ocapa) == 0 &&
214 !ll_have_md_lock(ocapa->u.cli.inode,
215 MDS_INODELOCK_LOOKUP)) {
216 DEBUG_CAPA(D_SEC, &ocapa->c_capa,
218 sort_add_capa(ocapa, &ll_idle_capas);
222 /* for OSS capability, only renew those whose inode is
225 if (capa_for_oss(&ocapa->c_capa) &&
226 obd_capa_open_count(ocapa) == 0) {
227 /* oss capa with open count == 0 won't renew,
228 * move to idle list */
229 sort_add_capa(ocapa, &ll_idle_capas);
233 /* NB iput() is in ll_update_capa() */
234 inode = igrab(ocapa->u.cli.inode);
236 DEBUG_CAPA(D_ERROR, &ocapa->c_capa,
243 cfs_spin_unlock(&capa_lock);
244 rc = md_renew_capa(ll_i2mdexp(inode), ocapa,
246 cfs_spin_lock(&capa_lock);
248 DEBUG_CAPA(D_ERROR, &ocapa->c_capa,
249 "renew failed: %d", rc);
250 ll_capa_renewal_failed++;
255 update_capa_timer(next, capa_renewal_time(next));
257 cfs_list_for_each_entry_safe(ocapa, tmp, &ll_idle_capas,
259 if (!capa_is_expired(ocapa)) {
261 update_capa_timer(ocapa, ocapa->c_expiry);
265 if (cfs_atomic_read(&ocapa->c_refc) > 1) {
266 DEBUG_CAPA(D_SEC, &ocapa->c_capa,
267 "expired(c_refc %d), don't release",
268 cfs_atomic_read(&ocapa->c_refc));
269 /* don't try to renew any more */
270 cfs_list_del_init(&ocapa->c_list);
274 /* expired capa is released. */
275 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "release expired");
276 ll_delete_capa(ocapa);
279 cfs_spin_unlock(&capa_lock);
282 ll_capa_thread.t_flags = SVC_STOPPED;
283 cfs_waitq_signal(&ll_capa_thread.t_ctl_waitq);
287 void ll_capa_timer_callback(unsigned long unused)
289 cfs_waitq_signal(&ll_capa_thread.t_ctl_waitq);
292 int ll_capa_thread_start(void)
297 cfs_waitq_init(&ll_capa_thread.t_ctl_waitq);
299 rc = cfs_kernel_thread(capa_thread_main, NULL, 0);
301 CERROR("cannot start expired capa thread: rc %d\n", rc);
304 cfs_wait_event(ll_capa_thread.t_ctl_waitq,
305 ll_capa_thread.t_flags & SVC_RUNNING);
310 void ll_capa_thread_stop(void)
312 ll_capa_thread.t_flags = SVC_STOPPING;
313 cfs_waitq_signal(&ll_capa_thread.t_ctl_waitq);
314 cfs_wait_event(ll_capa_thread.t_ctl_waitq,
315 ll_capa_thread.t_flags & SVC_STOPPED);
318 struct obd_capa *ll_osscapa_get(struct inode *inode, __u64 opc)
320 struct ll_inode_info *lli = ll_i2info(inode);
321 struct obd_capa *ocapa;
326 if ((ll_i2sbi(inode)->ll_flags & LL_SBI_OSS_CAPA) == 0)
329 LASSERT(opc == CAPA_OPC_OSS_WRITE || opc == CAPA_OPC_OSS_RW ||
330 opc == CAPA_OPC_OSS_TRUNC);
332 cfs_spin_lock(&capa_lock);
333 cfs_list_for_each_entry(ocapa, &lli->lli_oss_capas, u.cli.lli_list) {
334 if (capa_is_expired(ocapa))
336 if ((opc & CAPA_OPC_OSS_WRITE) &&
337 capa_opc_supported(&ocapa->c_capa, CAPA_OPC_OSS_WRITE)) {
340 } else if ((opc & CAPA_OPC_OSS_READ) &&
341 capa_opc_supported(&ocapa->c_capa,
342 CAPA_OPC_OSS_READ)) {
345 } else if ((opc & CAPA_OPC_OSS_TRUNC) &&
346 capa_opc_supported(&ocapa->c_capa, opc)) {
353 LASSERT(lu_fid_eq(capa_fid(&ocapa->c_capa),
354 ll_inode2fid(inode)));
355 LASSERT(ocapa->c_site == CAPA_SITE_CLIENT);
359 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found client");
363 if (cfs_atomic_read(&ll_capa_debug)) {
364 CERROR("no capability for "DFID" opc "LPX64"\n",
365 PFID(&lli->lli_fid), opc);
366 cfs_atomic_set(&ll_capa_debug, 0);
369 cfs_spin_unlock(&capa_lock);
373 EXPORT_SYMBOL(ll_osscapa_get);
375 struct obd_capa *ll_mdscapa_get(struct inode *inode)
377 struct ll_inode_info *lli = ll_i2info(inode);
378 struct obd_capa *ocapa;
381 LASSERT(inode != NULL);
383 if ((ll_i2sbi(inode)->ll_flags & LL_SBI_MDS_CAPA) == 0)
386 cfs_spin_lock(&capa_lock);
387 ocapa = capa_get(lli->lli_mds_capa);
388 cfs_spin_unlock(&capa_lock);
389 if (!ocapa && cfs_atomic_read(&ll_capa_debug)) {
390 CERROR("no mds capability for "DFID"\n", PFID(&lli->lli_fid));
391 cfs_atomic_set(&ll_capa_debug, 0);
397 static struct obd_capa *do_add_mds_capa(struct inode *inode,
398 struct obd_capa *ocapa)
400 struct ll_inode_info *lli = ll_i2info(inode);
401 struct obd_capa *old = lli->lli_mds_capa;
402 struct lustre_capa *capa = &ocapa->c_capa;
405 ocapa->u.cli.inode = inode;
406 lli->lli_mds_capa = ocapa;
407 capa_count[CAPA_SITE_CLIENT]++;
409 DEBUG_CAPA(D_SEC, capa, "add MDS");
411 cfs_spin_lock(&old->c_lock);
413 cfs_spin_unlock(&old->c_lock);
415 DEBUG_CAPA(D_SEC, capa, "update MDS");
423 static struct obd_capa *do_lookup_oss_capa(struct inode *inode, int opc)
425 struct ll_inode_info *lli = ll_i2info(inode);
426 struct obd_capa *ocapa;
428 /* inside capa_lock */
429 cfs_list_for_each_entry(ocapa, &lli->lli_oss_capas, u.cli.lli_list) {
430 if ((capa_opc(&ocapa->c_capa) & opc) != opc)
433 LASSERT(lu_fid_eq(capa_fid(&ocapa->c_capa),
434 ll_inode2fid(inode)));
435 LASSERT(ocapa->c_site == CAPA_SITE_CLIENT);
437 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found client");
444 static inline void inode_add_oss_capa(struct inode *inode,
445 struct obd_capa *ocapa)
447 struct ll_inode_info *lli = ll_i2info(inode);
448 struct obd_capa *tmp;
449 cfs_list_t *next = NULL;
451 /* capa is sorted in lli_oss_capas so lookup can always find the
453 cfs_list_for_each_entry(tmp, &lli->lli_oss_capas, u.cli.lli_list) {
454 if (cfs_time_after(ocapa->c_expiry, tmp->c_expiry)) {
455 next = &tmp->u.cli.lli_list;
459 LASSERT(&ocapa->u.cli.lli_list != next);
460 cfs_list_move_tail(&ocapa->u.cli.lli_list, next ?: &lli->lli_oss_capas);
463 static struct obd_capa *do_add_oss_capa(struct inode *inode,
464 struct obd_capa *ocapa)
466 struct obd_capa *old;
467 struct lustre_capa *capa = &ocapa->c_capa;
469 LASSERTF(S_ISREG(inode->i_mode),
470 "inode has oss capa, but not regular file, mode: %d\n",
473 /* FIXME: can't replace it so easily with fine-grained opc */
474 old = do_lookup_oss_capa(inode, capa_opc(capa) & CAPA_OPC_OSS_ONLY);
476 ocapa->u.cli.inode = inode;
477 CFS_INIT_LIST_HEAD(&ocapa->u.cli.lli_list);
478 capa_count[CAPA_SITE_CLIENT]++;
480 DEBUG_CAPA(D_SEC, capa, "add OSS");
482 cfs_spin_lock(&old->c_lock);
484 cfs_spin_unlock(&old->c_lock);
486 DEBUG_CAPA(D_SEC, capa, "update OSS");
492 inode_add_oss_capa(inode, ocapa);
496 struct obd_capa *ll_add_capa(struct inode *inode, struct obd_capa *ocapa)
498 cfs_spin_lock(&capa_lock);
499 ocapa = capa_for_mds(&ocapa->c_capa) ? do_add_mds_capa(inode, ocapa) :
500 do_add_oss_capa(inode, ocapa);
502 /* truncate capa won't renew */
503 if (ocapa->c_capa.lc_opc != CAPA_OPC_OSS_TRUNC) {
504 set_capa_expiry(ocapa);
505 cfs_list_del_init(&ocapa->c_list);
506 sort_add_capa(ocapa, ll_capa_list);
508 update_capa_timer(ocapa, capa_renewal_time(ocapa));
511 cfs_spin_unlock(&capa_lock);
513 cfs_atomic_set(&ll_capa_debug, 1);
517 static inline void delay_capa_renew(struct obd_capa *oc, cfs_time_t delay)
519 /* NB: set a fake expiry for this capa to prevent it renew too soon */
520 oc->c_expiry = cfs_time_add(oc->c_expiry, cfs_time_seconds(delay));
523 int ll_update_capa(struct obd_capa *ocapa, struct lustre_capa *capa)
525 struct inode *inode = ocapa->u.cli.inode;
534 cfs_spin_lock(&capa_lock);
536 DEBUG_CAPA(D_SEC, &ocapa->c_capa,
537 "renewal canceled because object removed");
538 ll_capa_renewal_noent++;
540 ll_capa_renewal_failed++;
542 /* failed capa won't be renewed any longer, but if -EIO,
543 * client might be doing recovery, retry in 2 min. */
544 if (rc == -EIO && !capa_is_expired(ocapa)) {
545 delay_capa_renew(ocapa, 120);
546 DEBUG_CAPA(D_ERROR, &ocapa->c_capa,
547 "renewal failed: -EIO, retry in 2 mins");
548 ll_capa_renewal_retries++;
551 DEBUG_CAPA(D_ERROR, &ocapa->c_capa,
552 "renewal failed(rc: %d) for", rc);
556 cfs_list_del_init(&ocapa->c_list);
557 sort_add_capa(ocapa, &ll_idle_capas);
558 cfs_spin_unlock(&capa_lock);
565 cfs_spin_lock(&ocapa->c_lock);
566 LASSERT(!memcmp(&ocapa->c_capa, capa,
567 offsetof(struct lustre_capa, lc_opc)));
568 ocapa->c_capa = *capa;
569 set_capa_expiry(ocapa);
570 cfs_spin_unlock(&ocapa->c_lock);
572 cfs_spin_lock(&capa_lock);
573 if (capa_for_oss(capa))
574 inode_add_oss_capa(inode, ocapa);
575 DEBUG_CAPA(D_SEC, capa, "renew");
578 cfs_list_del_init(&ocapa->c_list);
579 sort_add_capa(ocapa, ll_capa_list);
580 update_capa_timer(ocapa, capa_renewal_time(ocapa));
581 cfs_spin_unlock(&capa_lock);
588 void ll_capa_open(struct inode *inode)
590 struct ll_inode_info *lli = ll_i2info(inode);
592 if ((ll_i2sbi(inode)->ll_flags & (LL_SBI_MDS_CAPA | LL_SBI_OSS_CAPA))
596 if (!S_ISREG(inode->i_mode))
599 cfs_atomic_inc(&lli->lli_open_count);
602 void ll_capa_close(struct inode *inode)
604 struct ll_inode_info *lli = ll_i2info(inode);
606 if ((ll_i2sbi(inode)->ll_flags & (LL_SBI_MDS_CAPA | LL_SBI_OSS_CAPA))
610 if (!S_ISREG(inode->i_mode))
613 cfs_atomic_dec(&lli->lli_open_count);
616 /* delete CAPA_OPC_OSS_TRUNC only */
617 void ll_truncate_free_capa(struct obd_capa *ocapa)
622 LASSERT(ocapa->c_capa.lc_opc & CAPA_OPC_OSS_TRUNC);
623 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free truncate");
625 /* release ref when find */
627 if (likely(ocapa->c_capa.lc_opc == CAPA_OPC_OSS_TRUNC)) {
628 cfs_spin_lock(&capa_lock);
629 ll_delete_capa(ocapa);
630 cfs_spin_unlock(&capa_lock);
634 void ll_clear_inode_capas(struct inode *inode)
636 struct ll_inode_info *lli = ll_i2info(inode);
637 struct obd_capa *ocapa, *tmp;
639 cfs_spin_lock(&capa_lock);
640 ocapa = lli->lli_mds_capa;
642 ll_delete_capa(ocapa);
644 cfs_list_for_each_entry_safe(ocapa, tmp, &lli->lli_oss_capas,
646 ll_delete_capa(ocapa);
647 cfs_spin_unlock(&capa_lock);
650 void ll_print_capa_stat(struct ll_sb_info *sbi)
652 if (sbi->ll_flags & (LL_SBI_MDS_CAPA | LL_SBI_OSS_CAPA))
653 LCONSOLE_INFO("Fid capabilities renewed: %llu\n"
654 "Fid capabilities renewal ENOENT: %llu\n"
655 "Fid capabilities failed to renew: %llu\n"
656 "Fid capabilities renewal retries: %llu\n",
657 ll_capa_renewed, ll_capa_renewal_noent,
658 ll_capa_renewal_failed, ll_capa_renewal_retries);