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;
46 struct lustre_capa *capa;
51 spin_lock(&capa_lock);
52 if (!list_empty(ll_capa_list)) {
53 ocapa = list_entry(ll_capa_list->next, struct obd_capa, c_list);
54 expired = __capa_is_to_expire(ocapa);
56 capa = &ocapa->c_capa;
57 expiry = expiry_to_jiffies(capa->lc_expiry -
58 capa_pre_expiry(capa));
59 if (time_before(expiry, ll_capa_timer.expires) ||
60 !timer_pending(&ll_capa_timer)) {
61 mod_timer(&ll_capa_timer, expiry);
62 CDEBUG(D_INFO,"ll_capa_timer new expiry: %lu\n",
67 spin_unlock(&capa_lock);
72 static int inline ll_capa_check_stop(void)
74 return (capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
77 static int ll_renew_capa(struct obd_capa *ocapa)
79 struct ptlrpc_request *req = NULL;
80 /* no need to lock, no one else will touch it */
81 struct inode *inode = ocapa->c_inode;
82 struct obd_export *md_exp = ll_i2mdexp(inode);
83 struct ll_inode_info *lli = ll_i2info(inode);
84 __u64 valid = OBD_MD_CAPA;
88 if (capa_expired(&ocapa->c_capa))
91 rc = md_getattr(md_exp, &lli->lli_id, valid, NULL, NULL, 0,
96 static int ll_capa_thread(void *arg)
98 struct thread_ctl *ctl = arg;
104 char name[sizeof(current->comm)];
105 snprintf(name, sizeof(name) - 1, "ll_capa");
106 kportal_daemonize(name);
109 SIGNAL_MASK_LOCK(current, flags);
110 sigfillset(¤t->blocked);
112 SIGNAL_MASK_UNLOCK(current, flags);
115 * letting starting function know, that we are ready and control may be
118 capa_thread.t_flags = SVC_RUNNING;
119 complete(&ctl->ctl_starting);
122 struct l_wait_info lwi = { 0 };
123 struct obd_capa *ocapa, *tmp, *next = NULL, tcapa;
124 unsigned long expiry, sleep = CAPA_PRE_EXPIRY;
126 l_wait_event(capa_thread.t_ctl_waitq,
127 (have_expired_capa() || ll_capa_check_stop()),
130 if (ll_capa_check_stop())
133 spin_lock(&capa_lock);
134 list_for_each_entry_safe(ocapa, tmp, ll_capa_list, c_list) {
135 if (ocapa->c_capa.lc_flags & CAPA_FL_SHORT)
136 sleep = CAPA_PRE_EXPIRY_SHORT;
138 if (ocapa->c_capa.lc_op == CAPA_TRUNC)
141 if (__capa_is_to_expire(ocapa)) {
142 /* copy capa in case it's deleted */
144 spin_unlock(&capa_lock);
146 rc = ll_renew_capa(&tcapa);
150 spin_lock(&capa_lock);
158 struct lustre_capa *capa = &next->c_capa;
160 expiry = expiry_to_jiffies(capa->lc_expiry -
161 capa_pre_expiry(capa));
162 if (time_before(expiry, ll_capa_timer.expires) ||
163 !timer_pending(&ll_capa_timer)) {
164 mod_timer(&ll_capa_timer, expiry);
165 CDEBUG(D_INFO,"ll_capa_timer new expiry: %lu\n",
169 spin_unlock(&capa_lock);
171 /* wait ll_renew_capa finish */
172 set_current_state(TASK_INTERRUPTIBLE);
173 schedule_timeout(sleep * HZ);
176 capa_thread.t_flags = SVC_STOPPED;
178 /* this is SMP-safe way to finish thread. */
179 complete_and_exit(&ctl->ctl_finishing, 0);
183 /* just wake up, others are handled by ll_capa_thread */
184 void ll_capa_timer_callback(unsigned long unused)
187 wake_up(&capa_thread.t_ctl_waitq);
191 int ll_capa_thread_start(void)
196 LASSERT(capa_thread.t_flags == 0);
197 init_completion(&ll_capa_ctl.ctl_starting);
198 init_completion(&ll_capa_ctl.ctl_finishing);
199 init_waitqueue_head(&capa_thread.t_ctl_waitq);
201 rc = kernel_thread(ll_capa_thread, &ll_capa_ctl,
202 (CLONE_VM | CLONE_FILES));
204 CERROR("cannot start expired capa thread, "
208 wait_for_completion(&ll_capa_ctl.ctl_starting);
209 LASSERT(capa_thread.t_flags == SVC_RUNNING);
213 void ll_capa_thread_stop(void)
217 capa_thread.t_flags = SVC_STOPPING;
218 wake_up(&capa_thread.t_ctl_waitq);
219 wait_for_completion(&ll_capa_ctl.ctl_finishing);
220 LASSERT(capa_thread.t_flags == SVC_STOPPED);
221 capa_thread.t_flags = 0;
226 int ll_set_capa(struct inode *inode, struct lookup_intent *it)
228 struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
229 struct mds_body *body;
230 struct lustre_capa *capa;
231 struct obd_capa *ocapa;
232 struct ll_inode_info *lli = ll_i2info(inode);
233 unsigned long expiry;
235 if (!S_ISREG(inode->i_mode))
238 body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
239 LASSERT(body != NULL); /* reply already checked out */
240 LASSERT_REPSWABBED(req, 1); /* and swabbed down */
242 if (!(body->valid & OBD_MD_CAPA))
247 capa = lustre_msg_buf(req->rq_repmsg, 7, sizeof (*capa));
248 LASSERT(capa != NULL); /* reply already checked out */
249 LASSERT_REPSWABBED(req, 7); /* and swabbed down */
251 ocapa = capa_renew(capa, CLIENT_CAPA);
255 spin_lock(&capa_lock);
256 ocapa->c_inode = inode;
257 ocapa->c_handle = body->handle;
258 spin_unlock(&capa_lock);
260 spin_lock(&lli->lli_lock);
261 /* in case it was linked to lli_capas already */
262 if (list_empty(&ocapa->c_lli_list))
263 list_add(&ocapa->c_lli_list, &lli->lli_capas);
264 spin_unlock(&lli->lli_lock);
266 expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
268 spin_lock(&capa_lock);
269 if (time_before(expiry, ll_capa_timer.expires) ||
270 !timer_pending(&ll_capa_timer)) {
271 mod_timer(&ll_capa_timer, expiry);
272 CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
274 spin_unlock(&capa_lock);
279 int ll_set_trunc_capa(struct ptlrpc_request *req, int offset, struct inode *inode)
281 struct mds_body *body;
282 struct obd_capa *ocapa;
283 struct lustre_capa *capa;
284 struct ll_inode_info *lli = ll_i2info(inode);
286 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
290 if (!(body->valid & OBD_MD_CAPA))
294 capa = (struct lustre_capa *)lustre_swab_repbuf(req, offset + 1,
295 sizeof(*capa), lustre_swab_lustre_capa);
299 ocapa = capa_renew(capa, CLIENT_CAPA);
303 spin_lock(&lli->lli_lock);
304 /* in case it was linked to lli_capas already */
305 if (list_empty(&ocapa->c_lli_list))
306 list_add(&ocapa->c_lli_list, &lli->lli_capas);
307 spin_unlock(&lli->lli_lock);
312 struct obd_capa *ll_get_capa(struct inode *inode, uid_t uid, int op)
314 struct ll_inode_info *lli = ll_i2info(inode);
315 struct obd_capa *ocapa, *tmp;
318 list_for_each_entry_safe(ocapa, tmp, &lli->lli_capas, c_lli_list) {
319 if (ocapa->c_capa.lc_ruid != uid)
321 if (ocapa->c_capa.lc_op != op)