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 ptlrpc_thread ll_capa_thread;
36 static struct list_head *ll_capa_list = &capa_list[CLIENT_CAPA];
37 static struct thread_ctl {
38 struct completion ctl_starting;
39 struct completion ctl_finishing;
42 static inline int have_expired_capa(void)
44 struct obd_capa *ocapa;
48 spin_lock(&capa_lock);
49 if (!list_empty(ll_capa_list)) {
50 ocapa = list_entry(ll_capa_list->next, struct obd_capa, c_list);
52 expired = __capa_is_to_expire(ocapa);
54 spin_unlock(&capa_lock);
59 static int inline ll_capa_check_stop(void)
61 return (ll_capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
64 static int ll_renew_capa(struct obd_capa *ocapa)
66 struct ptlrpc_request *req = NULL;
67 /* no need to lock, no one else will touch it */
68 struct inode *inode = ocapa->c_inode;
69 struct obd_export *md_exp = ll_i2mdexp(inode);
70 struct ll_inode_info *lli = ll_i2info(inode);
77 rc = md_getattr(md_exp, &lli->lli_id, valid, NULL, NULL, 0,
80 CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
84 static int ll_capa_thread_main(void *arg)
86 struct thread_ctl *ctl = arg;
91 char name[sizeof(current->comm)];
92 snprintf(name, sizeof(name) - 1, "ll_capa");
93 kportal_daemonize(name);
96 SIGNAL_MASK_LOCK(current, flags);
97 sigfillset(¤t->blocked);
99 SIGNAL_MASK_UNLOCK(current, flags);
102 * letting starting function know, that we are ready and control may be
105 ll_capa_thread.t_flags = SVC_RUNNING;
106 complete(&ctl->ctl_starting);
109 struct l_wait_info lwi = { 0 };
110 struct obd_capa *ocapa, *next = NULL;
111 int sleep = CAPA_PRE_EXPIRY;
113 l_wait_event(ll_capa_thread.t_ctl_waitq,
114 (have_expired_capa() || ll_capa_check_stop()),
117 spin_lock(&capa_lock);
118 list_for_each_entry(ocapa, ll_capa_list, c_list) {
119 if (__capa_is_to_expire(ocapa)) {
120 /* get capa in case it's deleted */
123 spin_unlock(&capa_lock);
124 ll_renew_capa(ocapa);
125 capa_put(ocapa, CLIENT_CAPA);
126 spin_lock(&capa_lock);
133 mod_timer(&ll_capa_timer,
134 expiry_to_jiffies(next->c_capa.lc_expiry));
135 if (next->c_capa.lc_flags & CAPA_FL_NOROUND)
136 sleep = CAPA_PRE_EXPIRY_NOROUND;
138 spin_unlock(&capa_lock);
140 if (ll_capa_check_stop())
143 /* wait ll_renew_capa finish */
144 set_current_state(TASK_INTERRUPTIBLE);
145 schedule_timeout(sleep * HZ);
148 ll_capa_thread.t_flags = SVC_STOPPED;
150 /* this is SMP-safe way to finish thread. */
151 complete_and_exit(&ctl->ctl_finishing, 0);
155 /* just wake up, others are handled by ll_capa_thread */
156 void ll_capa_timer_callback(unsigned long unused)
159 wake_up(&ll_capa_thread.t_ctl_waitq);
163 int ll_capa_start_thread(void)
168 LASSERT(ll_capa_thread.t_flags == 0);
169 init_completion(&ll_capa_ctl.ctl_starting);
170 init_completion(&ll_capa_ctl.ctl_finishing);
171 init_waitqueue_head(&ll_capa_thread.t_ctl_waitq);
173 rc = kernel_thread(ll_capa_thread_main, &ll_capa_ctl,
174 (CLONE_VM | CLONE_FILES));
176 CERROR("cannot start expired capa thread, "
180 wait_for_completion(&ll_capa_ctl.ctl_starting);
181 LASSERT(ll_capa_thread.t_flags == SVC_RUNNING);
185 void ll_capa_stop_thread(void)
189 ll_capa_thread.t_flags = SVC_STOPPING;
190 wake_up(&ll_capa_thread.t_ctl_waitq);
191 wait_for_completion(&ll_capa_ctl.ctl_finishing);
192 LASSERT(ll_capa_thread.t_flags == SVC_STOPPED);
193 ll_capa_thread.t_flags = 0;
198 int ll_set_och_capa(struct inode *inode, struct lookup_intent *it,
199 struct obd_client_handle *och)
201 struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
202 struct ll_inode_info *lli = ll_i2info(inode);
203 struct mds_body *body;
204 struct lustre_capa *capa;
205 __u64 mdsid = lli->lli_id.li_fid.lf_group;
206 unsigned long ino = lli->lli_id.li_stc.u.e3s.l3s_ino;
207 int capa_op = (it->it_flags & MAY_WRITE) ? MAY_WRITE : MAY_READ;
208 unsigned long expiry;
212 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
213 LASSERT(body != NULL); /* reply already checked out */
214 LASSERT_REPSWABBED(req, 1); /* and swabbed down */
216 capa = lustre_msg_buf(req->rq_repmsg, 7, sizeof (*capa));
217 LASSERT(capa != NULL); /* reply already checked out */
218 LASSERT_REPSWABBED(req, 7); /* and swabbed down */
220 och->och_capa = capa_get(current->uid, capa_op, mdsid, ino,
221 CLIENT_CAPA, capa, inode, &body->handle);
225 expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
226 if (time_before(expiry, ll_capa_timer.expires) ||
227 !timer_pending(&ll_capa_timer))
228 mod_timer(&ll_capa_timer, expiry);