1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2005 Cluster File Systems, Inc.
6 * Author: Lai Siyao <lsy@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LLITE
27 #include <linux/version.h>
28 #include <asm/uaccess.h>
29 #include <linux/file.h>
30 #include <linux/kmod.h>
32 #include <lustre_lite.h>
33 #include "llite_internal.h"
35 /* for obd_capa.c_list, client capa might stay in three places:
38 * 3. stand alone: just allocated.
41 /* capas for oss writeback and those failed to renew */
42 static LIST_HEAD(ll_idle_capas);
43 static struct ptlrpc_thread ll_capa_thread;
44 static struct list_head *ll_capa_list = &capa_list[CAPA_SITE_CLIENT];
46 /* llite capa renewal timer */
47 struct timer_list ll_capa_timer;
48 /* for debug: indicate whether capa on llite is enabled or not */
49 static atomic_t ll_capa_debug = ATOMIC_INIT(0);
50 static unsigned long long ll_capa_renewed = 0;
51 static unsigned long long ll_capa_renewal_noent = 0;
52 static unsigned long long ll_capa_renewal_failed = 0;
53 static unsigned long long ll_capa_renewal_retries = 0;
55 static inline void update_capa_timer(struct obd_capa *ocapa, cfs_time_t expiry)
57 if (time_before(expiry, ll_capa_timer.expires) ||
58 !timer_pending(&ll_capa_timer)) {
59 mod_timer(&ll_capa_timer, expiry);
60 DEBUG_CAPA(D_SEC, &ocapa->c_capa,
61 "ll_capa_timer update: %lu/%lu by", expiry, jiffies);
65 static inline cfs_time_t capa_renewal_time(struct obd_capa *ocapa)
67 return cfs_time_sub(ocapa->c_expiry,
68 cfs_time_seconds(ocapa->c_capa.lc_timeout) / 2);
71 static inline int capa_is_to_expire(struct obd_capa *ocapa)
73 return cfs_time_beforeq(capa_renewal_time(ocapa), cfs_time_current());
76 static inline int have_expired_capa(void)
78 struct obd_capa *ocapa = NULL;
81 /* if ll_capa_list has client capa to expire or ll_idle_capas has
82 * expired capa, return 1.
84 spin_lock(&capa_lock);
85 if (!list_empty(ll_capa_list)) {
86 ocapa = list_entry(ll_capa_list->next, struct obd_capa, c_list);
87 expired = capa_is_to_expire(ocapa);
89 update_capa_timer(ocapa, capa_renewal_time(ocapa));
90 } else if (!list_empty(&ll_idle_capas)) {
91 ocapa = list_entry(ll_idle_capas.next, struct obd_capa, c_list);
92 expired = capa_is_expired(ocapa);
94 update_capa_timer(ocapa, ocapa->c_expiry);
96 spin_unlock(&capa_lock);
99 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "expired");
103 static inline int ll_capa_check_stop(void)
105 return (ll_capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
108 static void sort_add_capa(struct obd_capa *ocapa, struct list_head *head)
110 struct obd_capa *tmp;
111 struct list_head *before = NULL;
113 /* TODO: client capa is sorted by expiry, this could be optimized */
114 list_for_each_entry_reverse(tmp, head, c_list) {
115 if (cfs_time_aftereq(ocapa->c_expiry, tmp->c_expiry)) {
116 before = &tmp->c_list;
121 LASSERT(&ocapa->c_list != before);
122 list_add(&ocapa->c_list, before ?: head);
125 static inline int obd_capa_open_count(struct obd_capa *oc)
127 struct ll_inode_info *lli = ll_i2info(oc->u.cli.inode);
128 return atomic_read(&lli->lli_open_count);
131 static void ll_delete_capa(struct obd_capa *ocapa)
133 struct ll_inode_info *lli = ll_i2info(ocapa->u.cli.inode);
135 if (capa_for_mds(&ocapa->c_capa)) {
136 LASSERT(lli->lli_mds_capa == ocapa);
137 lli->lli_mds_capa = NULL;
138 } else if (capa_for_oss(&ocapa->c_capa)) {
139 list_del_init(&ocapa->u.cli.lli_list);
142 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free client");
143 list_del(&ocapa->c_list);
144 capa_count[CAPA_SITE_CLIENT]--;
148 /* three places where client capa is deleted:
149 * 1. capa_thread_main(), main place to delete expired capa.
150 * 2. ll_clear_inode_capas() in ll_clear_inode().
151 * 3. ll_truncate_free_capa() delete truncate capa explicitly in ll_truncate().
153 static int capa_thread_main(void *unused)
155 struct obd_capa *ocapa, *tmp, *next;
156 struct inode *inode = NULL;
157 struct l_wait_info lwi = { 0 };
161 cfs_daemonize("ll_capa");
163 ll_capa_thread.t_flags = SVC_RUNNING;
164 wake_up(&ll_capa_thread.t_ctl_waitq);
167 l_wait_event(ll_capa_thread.t_ctl_waitq,
168 (ll_capa_check_stop() || have_expired_capa()),
171 if (ll_capa_check_stop())
176 spin_lock(&capa_lock);
177 list_for_each_entry_safe(ocapa, tmp, ll_capa_list, c_list) {
178 LASSERT(ocapa->c_capa.lc_opc != CAPA_OPC_OSS_TRUNC);
180 if (!capa_is_to_expire(ocapa)) {
185 list_del_init(&ocapa->c_list);
187 /* for MDS capability, only renew those which belong to
188 * dir, or its inode is opened, or client holds LOOKUP
191 if (capa_for_mds(&ocapa->c_capa) &&
192 !S_ISDIR(ocapa->u.cli.inode->i_mode) &&
193 obd_capa_open_count(ocapa) == 0 &&
194 !ll_have_md_lock(ocapa->u.cli.inode,
195 MDS_INODELOCK_LOOKUP)) {
196 DEBUG_CAPA(D_SEC, &ocapa->c_capa,
198 sort_add_capa(ocapa, &ll_idle_capas);
202 /* for OSS capability, only renew those whose inode is
205 if (capa_for_oss(&ocapa->c_capa) &&
206 obd_capa_open_count(ocapa) == 0) {
207 /* oss capa with open count == 0 won't renew,
208 * move to idle list */
209 sort_add_capa(ocapa, &ll_idle_capas);
213 /* NB iput() is in ll_update_capa() */
214 inode = igrab(ocapa->u.cli.inode);
216 DEBUG_CAPA(D_ERROR, &ocapa->c_capa,
223 spin_unlock(&capa_lock);
225 rc = md_renew_capa(ll_i2mdexp(inode), ocapa,
227 spin_lock(&capa_lock);
229 DEBUG_CAPA(D_ERROR, &ocapa->c_capa,
230 "renew failed: %d", rc);
231 ll_capa_renewal_failed++;
236 update_capa_timer(next, capa_renewal_time(next));
238 list_for_each_entry_safe(ocapa, tmp, &ll_idle_capas, c_list) {
239 if (!capa_is_expired(ocapa)) {
241 update_capa_timer(ocapa, ocapa->c_expiry);
245 if (atomic_read(&ocapa->c_refc)) {
246 DEBUG_CAPA(D_SEC, &ocapa->c_capa,
247 "expired(c_refc %d), don't release",
248 atomic_read(&ocapa->c_refc));
249 /* don't try to renew any more */
250 list_del_init(&ocapa->c_list);
254 /* expired capa is released. */
255 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "release expired");
256 ll_delete_capa(ocapa);
259 spin_unlock(&capa_lock);
262 ll_capa_thread.t_flags = SVC_STOPPED;
263 wake_up(&ll_capa_thread.t_ctl_waitq);
267 void ll_capa_timer_callback(unsigned long unused)
269 wake_up(&ll_capa_thread.t_ctl_waitq);
272 int ll_capa_thread_start(void)
277 init_waitqueue_head(&ll_capa_thread.t_ctl_waitq);
279 rc = kernel_thread(capa_thread_main, NULL, 0);
281 CERROR("cannot start expired capa thread: rc %d\n", rc);
284 wait_event(ll_capa_thread.t_ctl_waitq,
285 ll_capa_thread.t_flags & SVC_RUNNING);
290 void ll_capa_thread_stop(void)
292 ll_capa_thread.t_flags = SVC_STOPPING;
293 wake_up(&ll_capa_thread.t_ctl_waitq);
294 wait_event(ll_capa_thread.t_ctl_waitq,
295 ll_capa_thread.t_flags & SVC_STOPPED);
298 static struct obd_capa *do_lookup_oss_capa(struct inode *inode, int opc)
300 struct ll_inode_info *lli = ll_i2info(inode);
301 struct obd_capa *ocapa;
303 /* inside capa_lock */
304 list_for_each_entry(ocapa, &lli->lli_oss_capas, u.cli.lli_list) {
305 if ((capa_opc(&ocapa->c_capa) & opc) != opc)
308 LASSERT(lu_fid_eq(capa_fid(&ocapa->c_capa),
309 ll_inode2fid(inode)));
310 LASSERT(ocapa->c_site == CAPA_SITE_CLIENT);
312 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found client");
319 struct obd_capa *ll_osscapa_get(struct inode *inode, __u64 opc)
321 struct ll_inode_info *lli = ll_i2info(inode);
322 struct obd_capa *ocapa;
325 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 spin_lock(&capa_lock);
333 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)) {
339 } else if ((opc & CAPA_OPC_OSS_READ) &&
340 capa_opc_supported(&ocapa->c_capa,
341 CAPA_OPC_OSS_READ)) {
343 } else if ((opc & CAPA_OPC_OSS_TRUNC) &&
344 capa_opc_supported(&ocapa->c_capa, opc)) {
350 LASSERT(lu_fid_eq(capa_fid(&ocapa->c_capa),
351 ll_inode2fid(inode)));
352 LASSERT(ocapa->c_site == CAPA_SITE_CLIENT);
356 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "found client");
360 if (atomic_read(&ll_capa_debug)) {
361 CERROR("no capability for "DFID" opc "LPX64"\n",
362 PFID(&lli->lli_fid), opc);
363 atomic_set(&ll_capa_debug, 0);
366 spin_unlock(&capa_lock);
371 struct obd_capa *ll_mdscapa_get(struct inode *inode)
373 struct ll_inode_info *lli = ll_i2info(inode);
374 struct obd_capa *ocapa;
377 LASSERT(inode != NULL);
379 if ((ll_i2sbi(inode)->ll_flags & LL_SBI_MDS_CAPA) == 0)
382 spin_lock(&capa_lock);
383 ocapa = capa_get(lli->lli_mds_capa);
384 spin_unlock(&capa_lock);
385 if (!ocapa && atomic_read(&ll_capa_debug)) {
386 CERROR("no mds capability for "DFID"\n", PFID(&lli->lli_fid));
387 atomic_set(&ll_capa_debug, 0);
393 static struct obd_capa *do_add_mds_capa(struct inode *inode,
394 struct obd_capa *ocapa)
396 struct ll_inode_info *lli = ll_i2info(inode);
397 struct obd_capa *old = lli->lli_mds_capa;
398 struct lustre_capa *capa = &ocapa->c_capa;
401 ocapa->u.cli.inode = inode;
402 lli->lli_mds_capa = ocapa;
403 capa_count[CAPA_SITE_CLIENT]++;
405 DEBUG_CAPA(D_SEC, capa, "add MDS");
407 spin_lock(&old->c_lock);
409 spin_unlock(&old->c_lock);
411 DEBUG_CAPA(D_SEC, capa, "update MDS");
419 static inline void inode_add_oss_capa(struct inode *inode,
420 struct obd_capa *ocapa)
422 struct ll_inode_info *lli = ll_i2info(inode);
423 struct obd_capa *tmp;
424 struct list_head *next = NULL;
426 /* capa is sorted in lli_oss_capas so lookup can always find the
428 list_for_each_entry(tmp, &lli->lli_oss_capas, u.cli.lli_list) {
429 if (cfs_time_after(ocapa->c_expiry, tmp->c_expiry)) {
430 next = &tmp->u.cli.lli_list;
434 LASSERT(&ocapa->u.cli.lli_list != next);
435 list_move_tail(&ocapa->u.cli.lli_list, next ?: &lli->lli_oss_capas);
438 static struct obd_capa *do_add_oss_capa(struct inode *inode,
439 struct obd_capa *ocapa)
441 struct obd_capa *old;
442 struct lustre_capa *capa = &ocapa->c_capa;
444 LASSERTF(S_ISREG(inode->i_mode),
445 "inode has oss capa, but not regular file, mode: %d\n",
448 /* FIXME: can't replace it so easily with fine-grained opc */
449 old = do_lookup_oss_capa(inode, capa_opc(capa) & CAPA_OPC_OSS_ONLY);
451 ocapa->u.cli.inode = inode;
452 INIT_LIST_HEAD(&ocapa->u.cli.lli_list);
453 capa_count[CAPA_SITE_CLIENT]++;
455 DEBUG_CAPA(D_SEC, capa, "add OSS");
457 spin_lock(&old->c_lock);
459 spin_unlock(&old->c_lock);
461 DEBUG_CAPA(D_SEC, capa, "update OSS");
467 inode_add_oss_capa(inode, ocapa);
471 struct obd_capa *ll_add_capa(struct inode *inode, struct obd_capa *ocapa)
473 spin_lock(&capa_lock);
474 ocapa = capa_for_mds(&ocapa->c_capa) ? do_add_mds_capa(inode, ocapa) :
475 do_add_oss_capa(inode, ocapa);
477 /* truncate capa won't renew */
478 if (ocapa->c_capa.lc_opc != CAPA_OPC_OSS_TRUNC) {
479 set_capa_expiry(ocapa);
480 list_del(&ocapa->c_list);
481 sort_add_capa(ocapa, ll_capa_list);
483 update_capa_timer(ocapa, capa_renewal_time(ocapa));
486 spin_unlock(&capa_lock);
488 atomic_set(&ll_capa_debug, 1);
492 static inline void delay_capa_renew(struct obd_capa *oc, cfs_time_t delay)
494 /* NB: set a fake expiry for this capa to prevent it renew too soon */
495 oc->c_expiry = cfs_time_add(oc->c_expiry, cfs_time_seconds(delay));
498 int ll_update_capa(struct obd_capa *ocapa, struct lustre_capa *capa)
500 struct inode *inode = ocapa->u.cli.inode;
509 spin_lock(&capa_lock);
511 DEBUG_CAPA(D_SEC, &ocapa->c_capa,
512 "renewal canceled because object removed");
513 ll_capa_renewal_noent++;
515 ll_capa_renewal_failed++;
517 /* failed capa won't be renewed any longer, but if -EIO,
518 * client might be doing recovery, retry in 2 min. */
519 if (rc == -EIO && !capa_is_expired(ocapa)) {
520 delay_capa_renew(ocapa, 120);
521 DEBUG_CAPA(D_ERROR, &ocapa->c_capa,
522 "renewal failed: -EIO, retry in 2 mins");
523 ll_capa_renewal_retries++;
526 DEBUG_CAPA(D_ERROR, &ocapa->c_capa,
527 "renewal failed(rc: %d) for", rc);
531 list_del(&ocapa->c_list);
532 sort_add_capa(ocapa, &ll_idle_capas);
533 spin_unlock(&capa_lock);
540 spin_lock(&ocapa->c_lock);
541 LASSERT(!memcmp(&ocapa->c_capa, capa,
542 offsetof(struct lustre_capa, lc_flags)));
543 ocapa->c_capa = *capa;
544 set_capa_expiry(ocapa);
545 spin_unlock(&ocapa->c_lock);
547 spin_lock(&capa_lock);
548 if (capa_for_oss(capa))
549 inode_add_oss_capa(inode, ocapa);
550 DEBUG_CAPA(D_SEC, capa, "renew");
553 list_del_init(&ocapa->c_list);
554 sort_add_capa(ocapa, ll_capa_list);
555 update_capa_timer(ocapa, capa_renewal_time(ocapa));
556 spin_unlock(&capa_lock);
563 void ll_capa_open(struct inode *inode)
565 struct ll_inode_info *lli = ll_i2info(inode);
567 if ((ll_i2sbi(inode)->ll_flags & (LL_SBI_MDS_CAPA | LL_SBI_OSS_CAPA))
571 if (!S_ISREG(inode->i_mode))
574 atomic_inc(&lli->lli_open_count);
577 void ll_capa_close(struct inode *inode)
579 struct ll_inode_info *lli = ll_i2info(inode);
581 if ((ll_i2sbi(inode)->ll_flags & (LL_SBI_MDS_CAPA | LL_SBI_OSS_CAPA))
585 if (!S_ISREG(inode->i_mode))
588 atomic_dec(&lli->lli_open_count);
591 /* delete CAPA_OPC_OSS_TRUNC only */
592 void ll_truncate_free_capa(struct obd_capa *ocapa)
597 LASSERT(ocapa->c_capa.lc_opc & CAPA_OPC_OSS_TRUNC);
598 DEBUG_CAPA(D_SEC, &ocapa->c_capa, "free truncate");
601 spin_lock(&capa_lock);
602 ll_delete_capa(ocapa);
603 spin_unlock(&capa_lock);
606 void ll_clear_inode_capas(struct inode *inode)
608 struct ll_inode_info *lli = ll_i2info(inode);
609 struct obd_capa *ocapa, *tmp;
611 spin_lock(&capa_lock);
612 ocapa = lli->lli_mds_capa;
614 ll_delete_capa(ocapa);
616 list_for_each_entry_safe(ocapa, tmp, &lli->lli_oss_capas,
618 ll_delete_capa(ocapa);
619 spin_unlock(&capa_lock);
622 void ll_print_capa_stat(struct ll_sb_info *sbi)
624 if (sbi->ll_flags & (LL_SBI_MDS_CAPA | LL_SBI_OSS_CAPA))
625 LCONSOLE_INFO("Fid capabilities renewed: %llu\n"
626 "Fid capabilities renewal ENOENT: %llu\n"
627 "Fid capabilities failed to renew: %llu\n"
628 "Fid capabilities renewal retries: %llu\n",
629 ll_capa_renewed, ll_capa_renewal_noent,
630 ll_capa_renewal_failed, ll_capa_renewal_retries);