Whamcloud - gitweb
change error msg to warning.
[fs/lustre-release.git] / lustre / llite / llite_capa.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004, 2005 Cluster File Systems, Inc.
5  *
6  * Author: Lai Siyao <lsy@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
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.
13  *
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.
18  *
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.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/fs.h>
27 #include <linux/version.h>
28 #include <asm/uaccess.h>
29 #include <linux/file.h>
30 #include <linux/kmod.h>
31
32 #include <linux/lustre_lite.h>
33 #include "llite_internal.h"
34
35 static struct list_head *ll_capa_list = &capa_list[CLIENT_CAPA];
36 static struct ptlrpc_thread capa_thread;
37
38 static struct thread_ctl {
39         struct completion ctl_starting;
40         struct completion ctl_finishing;
41 } ll_capa_ctl;
42
43 static inline int have_expired_capa(void)
44 {
45         struct obd_capa *ocapa;
46         int expired = 0;
47         unsigned long expiry;
48         ENTRY;
49
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);
53
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);
61                 }
62         }
63         spin_unlock(&capa_lock);
64
65         RETURN(expired);
66 }
67
68 static int inline ll_capa_check_stop(void)
69 {
70         return (capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
71 }
72
73 static int ll_renew_capa(struct obd_capa *ocapa)
74 {
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);
80         __u64 valid = OBD_MD_CAPA;
81         int rc;
82         ENTRY;
83
84         if (capa_expired(&ocapa->c_capa)) {
85                 /* this is the second time try to renew since the last
86                  * renewal failed, it means on one is openning it and
87                  * should be put now. */
88                 capa_put(ocapa);
89                 RETURN(0);
90         }
91
92         rc = md_getattr(md_exp, &lli->lli_id, valid, NULL, NULL, 0,
93                         0, ocapa, &req);
94         RETURN(rc);
95 }
96
97 static int ll_capa_thread(void *arg)
98 {
99         struct thread_ctl *ctl = arg;
100         unsigned long flags;
101         ENTRY;
102
103         {
104                 char name[sizeof(current->comm)];
105                 snprintf(name, sizeof(name) - 1, "ll_capa");
106                 kportal_daemonize(name);
107         }
108
109         SIGNAL_MASK_LOCK(current, flags);
110         sigfillset(&current->blocked);
111         RECALC_SIGPENDING;
112         SIGNAL_MASK_UNLOCK(current, flags);
113
114         /*
115          * letting starting function know, that we are ready and control may be
116          * returned.
117          */
118         capa_thread.t_flags = SVC_RUNNING;
119         complete(&ctl->ctl_starting);
120
121         while (1) {
122                 struct l_wait_info lwi = { 0 };
123                 struct obd_capa *ocapa, *next = NULL, tcapa;
124                 unsigned long expiry, sleep = CAPA_PRE_EXPIRY;
125
126                 l_wait_event(capa_thread.t_ctl_waitq,
127                              (have_expired_capa() || ll_capa_check_stop()),
128                              &lwi);
129
130                 if (ll_capa_check_stop())
131                         break;
132
133                 spin_lock(&capa_lock);
134                 list_for_each_entry(ocapa, ll_capa_list, c_list) {
135                         if (ocapa->c_capa.lc_op == CAPA_TRUNC)
136                                 continue;
137
138                         if (__capa_is_to_expire(ocapa)) {
139                                 /* copy capa in case it's deleted */
140                                 tcapa = *ocapa;
141                                 spin_unlock(&capa_lock);
142
143                                 ll_renew_capa(&tcapa);
144
145                                 spin_lock(&capa_lock);
146                         } else {
147                                 next = ocapa;
148                                 break;
149                         }
150                 }
151                 if (next) {
152                         expiry = expiry_to_jiffies(next->c_capa.lc_expiry);
153                         mod_timer(&ll_capa_timer, expiry);
154                         CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
155                         if (next->c_capa.lc_flags & CAPA_FL_NOROUND)
156                                 sleep = CAPA_PRE_EXPIRY_NOROUND;
157                 }
158                 spin_unlock(&capa_lock);
159
160                 /* wait ll_renew_capa finish */
161                 set_current_state(TASK_INTERRUPTIBLE);
162                 schedule_timeout(sleep * HZ);
163         }
164
165         capa_thread.t_flags = SVC_STOPPED;
166
167         /* this is SMP-safe way to finish thread. */
168         complete_and_exit(&ctl->ctl_finishing, 0);
169         EXIT;
170 }
171
172 /* just wake up, others are handled by ll_capa_thread */
173 void ll_capa_timer_callback(unsigned long unused)
174 {
175         ENTRY;
176         wake_up(&capa_thread.t_ctl_waitq);
177         EXIT;
178 }
179
180 int ll_capa_thread_start(void)
181 {
182         int rc;
183         ENTRY;
184
185         LASSERT(capa_thread.t_flags == 0);
186         init_completion(&ll_capa_ctl.ctl_starting);
187         init_completion(&ll_capa_ctl.ctl_finishing);
188         init_waitqueue_head(&capa_thread.t_ctl_waitq);
189
190         rc = kernel_thread(ll_capa_thread, &ll_capa_ctl,
191                            (CLONE_VM | CLONE_FILES));
192         if (rc < 0) {
193                 CERROR("cannot start expired capa thread, "
194                        "err = %d\n", rc);
195                 RETURN(rc);
196         }
197         wait_for_completion(&ll_capa_ctl.ctl_starting);
198         LASSERT(capa_thread.t_flags == SVC_RUNNING);
199         RETURN(0);
200 }
201
202 void ll_capa_thread_stop(void)
203 {
204         ENTRY;
205
206         capa_thread.t_flags = SVC_STOPPING;
207         wake_up(&capa_thread.t_ctl_waitq);
208         wait_for_completion(&ll_capa_ctl.ctl_finishing);
209         LASSERT(capa_thread.t_flags == SVC_STOPPED);
210         capa_thread.t_flags = 0;
211
212         EXIT;
213 }
214
215 int ll_set_capa(struct inode *inode, struct lookup_intent *it)
216 {
217         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
218         struct mds_body *body;
219         struct lustre_capa *capa;
220         struct obd_capa *ocapa;
221         struct ll_inode_info *lli = ll_i2info(inode);
222         unsigned long expiry;
223
224         if (!S_ISREG(inode->i_mode))
225                 return 0;
226
227         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
228         LASSERT(body != NULL);          /* reply already checked out */
229         LASSERT_REPSWABBED(req, 1);     /* and swabbed down */
230
231         if (!(body->valid & OBD_MD_CAPA))
232                 return 0;
233
234         ENTRY;
235
236         capa = lustre_msg_buf(req->rq_repmsg, 7, sizeof (*capa));
237         LASSERT(capa != NULL);          /* reply already checked out */
238         LASSERT_REPSWABBED(req, 7);     /* and swabbed down */
239
240         ocapa = capa_renew(capa, CLIENT_CAPA);
241         if (!ocapa)
242                 RETURN(-ENOMEM);
243
244         spin_lock(&capa_lock);
245         ocapa->c_inode = inode;
246         ocapa->c_handle = body->handle;
247         spin_unlock(&capa_lock);
248
249         spin_lock(&lli->lli_lock);
250         /* in case it was linked to lli_capas already */
251         if (list_empty(&ocapa->c_lli_list))
252                 list_add(&ocapa->c_lli_list, &lli->lli_capas);
253         spin_unlock(&lli->lli_lock);
254
255         expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
256
257         spin_lock(&capa_lock);
258         if (time_before(expiry, ll_capa_timer.expires) ||
259             !timer_pending(&ll_capa_timer)) {
260                 mod_timer(&ll_capa_timer, expiry);
261                 CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
262         }
263         spin_unlock(&capa_lock);
264
265         RETURN(0);
266 }
267
268 int ll_set_trunc_capa(struct ptlrpc_request *req, int offset, struct inode *inode)
269 {
270         struct mds_body *body;
271         struct obd_capa *ocapa;
272         struct lustre_capa *capa;
273         struct ll_inode_info *lli = ll_i2info(inode);
274
275         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
276         if (!body)
277                 return -ENOMEM;
278
279         if (!(body->valid & OBD_MD_CAPA))
280                 return 0;
281
282         ENTRY;
283         capa = (struct lustre_capa *)lustre_swab_repbuf(req, offset + 1,
284                                         sizeof(*capa), lustre_swab_lustre_capa);
285         if (!capa)
286                 RETURN(-ENOMEM);        
287
288         ocapa = capa_renew(capa, CLIENT_CAPA);
289         if (!ocapa)
290                 RETURN(-ENOMEM);
291
292         spin_lock(&lli->lli_lock);
293         /* in case it was linked to lli_capas already */
294         if (list_empty(&ocapa->c_lli_list))
295                 list_add(&ocapa->c_lli_list, &lli->lli_capas);
296         spin_unlock(&lli->lli_lock);
297
298         RETURN(0);
299 }
300
301 struct obd_capa *ll_get_capa(struct inode *inode, uid_t uid, int op)
302 {
303         struct ll_inode_info *lli = ll_i2info(inode);
304         struct obd_capa *ocapa, *tmp;
305         ENTRY;
306
307         list_for_each_entry_safe(ocapa, tmp, &lli->lli_capas, c_lli_list) {
308                 if (ocapa->c_capa.lc_ruid != uid)
309                         continue;
310                 if (ocapa->c_capa.lc_op != op)
311                         continue;
312
313                 RETURN(ocapa);
314         }
315         
316         RETURN(NULL);
317 }