Whamcloud - gitweb
- drop ll_capa_stat flag in order to preven flood
[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         struct lustre_capa *capa;
47         int expired = 0;
48         unsigned long expiry;
49         struct timeval tv;
50
51         ENTRY;
52
53         do_gettimeofday(&tv);
54         spin_lock(&capa_lock);
55         if (!list_empty(ll_capa_list)) {
56                 ocapa = list_entry(ll_capa_list->next, struct obd_capa, c_list);
57                 expired = __capa_is_to_expire(ocapa, &tv);
58                 if (!expired) {
59                         capa = &ocapa->c_capa;
60                         expiry = expiry_to_jiffies(capa->lc_expiry -
61                                                    capa_pre_expiry(capa));
62                         if (time_before(expiry, ll_capa_timer.expires) ||
63                             !timer_pending(&ll_capa_timer)) {
64                                 mod_timer(&ll_capa_timer, expiry);
65                                 CDEBUG(D_INFO,"ll_capa_timer new expiry: %lu\n",
66                                        expiry);
67                         }
68                 }
69         }
70         spin_unlock(&capa_lock);
71
72         RETURN(expired);
73 }
74
75 static int inline ll_capa_check_stop(void)
76 {
77         return (capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
78 }
79
80 static int ll_renew_capa(struct obd_capa *ocapa)
81 {
82         struct ptlrpc_request *req = NULL;
83         /* no need to lock, no one else will touch it */
84         struct inode *inode = ocapa->c_inode;
85         struct obd_export *md_exp = ll_i2mdexp(inode);
86         struct ll_inode_info *lli = ll_i2info(inode);
87         __u64 valid = OBD_MD_CAPA;
88         int rc;
89         ENTRY;
90
91         rc = md_getattr(md_exp, &lli->lli_id, valid, NULL, NULL, 0,
92                         0, ocapa, &req);
93         RETURN(rc);
94 }
95
96 static int ll_capa_thread(void *arg)
97 {
98         struct thread_ctl *ctl = arg;
99         unsigned long flags;
100         int rc;
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, tcapa, *tmp, *next = NULL;
124                 unsigned long expiry, sleep = CAPA_PRE_EXPIRY;
125                 struct inode *inode;
126                 struct timeval tv;
127
128                 l_wait_event(capa_thread.t_ctl_waitq,
129                              (have_expired_capa() || ll_capa_check_stop()),
130                              &lwi);
131
132                 if (ll_capa_check_stop())
133                         break;
134
135                 do_gettimeofday(&tv);
136                 spin_lock(&capa_lock);
137                 list_for_each_entry_safe(ocapa, tmp, ll_capa_list, c_list) {
138                         if (ocapa->c_capa.lc_flags & CAPA_FL_SHORT)
139                                 sleep = CAPA_PRE_EXPIRY_SHORT;
140
141                         if (ocapa->c_capa.lc_op == CAPA_TRUNC)
142                                 continue;
143
144                         if (capa_expired(&ocapa->c_capa)) {
145                                 capa_put_nolock(ocapa);
146                                 continue;
147                         }
148
149                         if (__capa_is_to_expire(ocapa, &tv)) {
150                                 inode = igrab(ocapa->c_inode);
151                                 if (inode == NULL) {
152                                         DEBUG_CAPA(D_ERROR, &ocapa->c_capa,
153                                                    "igrab failed for");
154                                         continue;
155                                 }
156
157                                 tcapa = *ocapa;
158                                 spin_unlock(&capa_lock);
159
160                                 rc = ll_renew_capa(&tcapa);
161                                 iput(inode);
162
163                                 spin_lock(&capa_lock);
164                         } else {
165                                 next = ocapa;
166                                 break;
167                         }
168                 }
169
170                 if (next) {
171                         struct lustre_capa *capa = &next->c_capa;
172
173                         expiry = expiry_to_jiffies(capa->lc_expiry -
174                                                    capa_pre_expiry(capa));
175                         if (time_before(expiry, ll_capa_timer.expires) ||
176                             !timer_pending(&ll_capa_timer)) {
177                                 mod_timer(&ll_capa_timer, expiry);
178                                 CDEBUG(D_INFO,"ll_capa_timer new expiry: %lu\n",
179                                        expiry);
180                         }
181                 }
182                 spin_unlock(&capa_lock);
183
184                 /* wait ll_renew_capa finish */
185                 set_current_state(TASK_INTERRUPTIBLE);
186                 schedule_timeout(sleep * HZ);
187         }
188
189         capa_thread.t_flags = SVC_STOPPED;
190
191         /* this is SMP-safe way to finish thread. */
192         complete_and_exit(&ctl->ctl_finishing, 0);
193         EXIT;
194 }
195
196 /* just wake up, others are handled by ll_capa_thread */
197 void ll_capa_timer_callback(unsigned long unused)
198 {
199         ENTRY;
200         wake_up(&capa_thread.t_ctl_waitq);
201         EXIT;
202 }
203
204 int ll_capa_thread_start(void)
205 {
206         int rc;
207         ENTRY;
208
209         LASSERT(capa_thread.t_flags == 0);
210         init_completion(&ll_capa_ctl.ctl_starting);
211         init_completion(&ll_capa_ctl.ctl_finishing);
212         init_waitqueue_head(&capa_thread.t_ctl_waitq);
213
214         rc = kernel_thread(ll_capa_thread, &ll_capa_ctl,
215                            (CLONE_VM | CLONE_FILES));
216         if (rc < 0) {
217                 CERROR("cannot start expired capa thread, "
218                        "err = %d\n", rc);
219                 RETURN(rc);
220         }
221         wait_for_completion(&ll_capa_ctl.ctl_starting);
222         LASSERT(capa_thread.t_flags == SVC_RUNNING);
223         RETURN(0);
224 }
225
226 void ll_capa_thread_stop(void)
227 {
228         ENTRY;
229
230         capa_thread.t_flags = SVC_STOPPING;
231         wake_up(&capa_thread.t_ctl_waitq);
232         wait_for_completion(&ll_capa_ctl.ctl_finishing);
233         LASSERT(capa_thread.t_flags == SVC_STOPPED);
234         capa_thread.t_flags = 0;
235
236         EXIT;
237 }
238
239 int ll_set_capa(struct inode *inode, struct lookup_intent *it,
240                 struct obd_client_handle *och)
241 {
242         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
243         struct mds_body *body;
244         struct lustre_capa *capa;
245         struct obd_capa *ocapa;
246         struct ll_inode_info *lli = ll_i2info(inode);
247         unsigned long expiry;
248
249         if (!S_ISREG(inode->i_mode))
250                 return 0;
251
252         /* GNS code path will have no req */
253         if (!req)
254                 return 0;
255
256         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
257         LASSERT(body != NULL);          /* reply already checked out */
258         LASSERT_REPSWABBED(req, 1);     /* and swabbed down */
259
260         if (!(body->valid & OBD_MD_CAPA)) {
261                 if (atomic_read(&ll_capa_stat)) {
262                         DEBUG_REQ(D_ERROR, req,
263                                   "no capa for (uid %u, op %d, "DLID4"\n",
264                                   (unsigned)current->uid, it->it_flags,
265                                   OLID4(&lli->lli_id));
266                         atomic_set(&ll_capa_stat, 0);
267                 }
268
269                 return 0;
270         }
271
272         ENTRY;
273
274         capa = lustre_msg_buf(req->rq_repmsg, 7, sizeof (*capa));
275         LASSERT(capa != NULL);          /* reply already checked out */
276         LASSERT_REPSWABBED(req, 7);     /* and swabbed down */
277
278         ocapa = capa_renew(capa, CLIENT_CAPA);
279         if (!ocapa)
280                 RETURN(-ENOMEM);
281
282         spin_lock(&capa_lock);
283         ocapa->c_inode = inode;
284         ocapa->c_handle = och->och_fh;
285         spin_unlock(&capa_lock);
286
287         spin_lock(&lli->lli_lock);
288         /* in case it was linked to lli_capas already */
289         if (list_empty(&ocapa->c_lli_list))
290                 list_add(&ocapa->c_lli_list, &lli->lli_capas);
291         spin_unlock(&lli->lli_lock);
292
293         expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
294
295         spin_lock(&capa_lock);
296         if (time_before(expiry, ll_capa_timer.expires) ||
297             !timer_pending(&ll_capa_timer)) {
298                 mod_timer(&ll_capa_timer, expiry);
299                 CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
300         }
301         spin_unlock(&capa_lock);
302
303         RETURN(0);
304 }
305
306 int ll_set_trunc_capa(struct ptlrpc_request *req, int offset, struct inode *inode)
307 {
308         struct mds_body *body;
309         struct obd_capa *ocapa;
310         struct lustre_capa *capa;
311         struct ll_inode_info *lli = ll_i2info(inode);
312
313         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
314         if (!body)
315                 return -ENOMEM;
316
317         if (!(body->valid & OBD_MD_CAPA))
318                 return 0;
319
320         ENTRY;
321         capa = (struct lustre_capa *)lustre_swab_repbuf(req, offset + 1,
322                                         sizeof(*capa), lustre_swab_lustre_capa);
323         if (!capa)
324                 RETURN(-ENOMEM);        
325
326         ocapa = capa_renew(capa, CLIENT_CAPA);
327         if (!ocapa)
328                 RETURN(-ENOMEM);
329
330         spin_lock(&lli->lli_lock);
331         /* in case it was linked to lli_capas already */
332         if (list_empty(&ocapa->c_lli_list))
333                 list_add(&ocapa->c_lli_list, &lli->lli_capas);
334         spin_unlock(&lli->lli_lock);
335
336         RETURN(0);
337 }
338
339 struct obd_capa *ll_get_capa(struct inode *inode, uid_t uid, int op)
340 {
341         struct ll_inode_info *lli = ll_i2info(inode);
342         struct obd_capa *ocapa, *tmp;
343         ENTRY;
344
345         list_for_each_entry_safe(ocapa, tmp, &lli->lli_capas, c_lli_list) {
346                 if (ocapa->c_capa.lc_ruid != uid)
347                         continue;
348                 if (ocapa->c_capa.lc_op != op)
349                         continue;
350
351                 RETURN(ocapa);
352         }
353
354         if (atomic_read(&ll_capa_stat)) {
355                 CDEBUG(D_ERROR, "can't find capa for (uid %u, op %d, mdsid "
356                        LPU64", ino %u igen %u) failed.\n",
357                        (unsigned)uid, op, id_group(&lli->lli_id),
358                        (unsigned)id_ino(&lli->lli_id), id_gen(&lli->lli_id));
359                 atomic_set(&ll_capa_stat, 0);
360         }
361         
362         RETURN(NULL);
363 }