1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004, 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 <linux/lustre_lite.h>
33 #include "llite_internal.h"
35 static struct list_head *ll_capa_list = &capa_list[CLIENT_CAPA];
36 static struct ptlrpc_thread capa_thread;
38 static struct thread_ctl {
39 struct completion ctl_starting;
40 struct completion ctl_finishing;
43 static inline int have_expired_capa(void)
45 struct obd_capa *ocapa;
50 spin_lock(&capa_lock);
51 if (!list_empty(ll_capa_list)) {
52 ocapa = list_entry(ll_capa_list->next, struct obd_capa, c_list);
54 expired = __capa_is_to_expire(ocapa);
55 if (!expired && !timer_pending(&ll_capa_timer)) {
56 /* the expired capa has been put, so set the timer to
57 * the expired of the next capa */
58 expiry = expiry_to_jiffies(ocapa->c_capa.lc_expiry);
59 mod_timer(&ll_capa_timer, expiry);
60 CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
63 spin_unlock(&capa_lock);
68 static int inline ll_capa_check_stop(void)
70 return (capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
73 static int ll_renew_capa(struct obd_capa *ocapa)
75 struct ptlrpc_request *req = NULL;
76 /* no need to lock, no one else will touch it */
77 struct inode *inode = ocapa->c_inode;
78 struct obd_export *md_exp = ll_i2mdexp(inode);
79 struct ll_inode_info *lli = ll_i2info(inode);
86 rc = md_getattr(md_exp, &lli->lli_id, valid, NULL, NULL, 0,
89 CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
93 static int ll_capa_thread(void *arg)
95 struct thread_ctl *ctl = arg;
100 char name[sizeof(current->comm)];
101 snprintf(name, sizeof(name) - 1, "ll_capa");
102 kportal_daemonize(name);
105 SIGNAL_MASK_LOCK(current, flags);
106 sigfillset(¤t->blocked);
108 SIGNAL_MASK_UNLOCK(current, flags);
111 * letting starting function know, that we are ready and control may be
114 capa_thread.t_flags = SVC_RUNNING;
115 complete(&ctl->ctl_starting);
118 struct l_wait_info lwi = { 0 };
119 struct obd_capa *ocapa, *next = NULL;
120 unsigned long expiry, sleep = CAPA_PRE_EXPIRY;
122 l_wait_event(capa_thread.t_ctl_waitq,
123 (have_expired_capa() || ll_capa_check_stop()),
126 spin_lock(&capa_lock);
127 list_for_each_entry(ocapa, ll_capa_list, c_list) {
128 if (__capa_is_to_expire(ocapa)) {
129 /* get capa in case it's deleted */
132 spin_unlock(&capa_lock);
133 ll_renew_capa(ocapa);
134 capa_put(ocapa, CLIENT_CAPA);
135 spin_lock(&capa_lock);
142 expiry = expiry_to_jiffies(next->c_capa.lc_expiry);
143 mod_timer(&ll_capa_timer, expiry);
144 CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
145 if (next->c_capa.lc_flags & CAPA_FL_NOROUND)
146 sleep = CAPA_PRE_EXPIRY_NOROUND;
148 spin_unlock(&capa_lock);
150 if (ll_capa_check_stop())
153 /* wait ll_renew_capa finish */
154 set_current_state(TASK_INTERRUPTIBLE);
155 schedule_timeout(sleep * HZ);
158 capa_thread.t_flags = SVC_STOPPED;
160 /* this is SMP-safe way to finish thread. */
161 complete_and_exit(&ctl->ctl_finishing, 0);
165 /* just wake up, others are handled by ll_capa_thread */
166 void ll_capa_timer_callback(unsigned long unused)
169 wake_up(&capa_thread.t_ctl_waitq);
173 int ll_capa_thread_start(void)
178 LASSERT(capa_thread.t_flags == 0);
179 init_completion(&ll_capa_ctl.ctl_starting);
180 init_completion(&ll_capa_ctl.ctl_finishing);
181 init_waitqueue_head(&capa_thread.t_ctl_waitq);
183 rc = kernel_thread(ll_capa_thread, &ll_capa_ctl,
184 (CLONE_VM | CLONE_FILES));
186 CERROR("cannot start expired capa thread, "
190 wait_for_completion(&ll_capa_ctl.ctl_starting);
191 LASSERT(capa_thread.t_flags == SVC_RUNNING);
195 void ll_capa_thread_stop(void)
199 capa_thread.t_flags = SVC_STOPPING;
200 wake_up(&capa_thread.t_ctl_waitq);
201 wait_for_completion(&ll_capa_ctl.ctl_finishing);
202 LASSERT(capa_thread.t_flags == SVC_STOPPED);
203 capa_thread.t_flags = 0;
208 int ll_set_och_capa(struct inode *inode, struct lookup_intent *it,
209 struct obd_client_handle *och)
211 struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
212 struct ll_inode_info *lli = ll_i2info(inode);
213 struct mds_body *body;
214 struct lustre_capa *capa;
215 __u64 mdsid = lli->lli_id.li_fid.lf_group;
216 unsigned long ino = lli->lli_id.li_stc.u.e3s.l3s_ino;
217 int capa_op = (it->it_flags & MAY_WRITE) ? MAY_WRITE : MAY_READ;
218 unsigned long expiry;
222 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
223 LASSERT(body != NULL); /* reply already checked out */
224 LASSERT_REPSWABBED(req, 1); /* and swabbed down */
226 capa = lustre_msg_buf(req->rq_repmsg, 7, sizeof (*capa));
227 LASSERT(capa != NULL); /* reply already checked out */
228 LASSERT_REPSWABBED(req, 7); /* and swabbed down */
230 och->och_capa = capa_get(current->uid, capa_op, mdsid, ino,
231 CLIENT_CAPA, capa, inode, &body->handle);
235 DEBUG_CAPA(D_INFO, capa, "ll_set_och_capa");
237 expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
239 spin_lock(&capa_lock);
240 if (time_before(expiry, ll_capa_timer.expires) ||
241 !timer_pending(&ll_capa_timer)) {
242 mod_timer(&ll_capa_timer, expiry);
243 CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
245 spin_unlock(&capa_lock);