Whamcloud - gitweb
fixes:
[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         ENTRY;
50
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);
55
56                 if (!expired) {
57                         capa = &ocapa->c_capa;
58                         expiry = expiry_to_jiffies(capa->lc_expiry -
59                                                    capa_pre_expiry(capa));
60                         if (time_before(expiry, ll_capa_timer.expires) ||
61                             !timer_pending(&ll_capa_timer)) {
62                                 mod_timer(&ll_capa_timer, expiry);
63                                 CDEBUG(D_INFO,"ll_capa_timer new expiry: %lu\n",
64                                        expiry);
65                         }
66                 }
67         }
68         spin_unlock(&capa_lock);
69
70         RETURN(expired);
71 }
72
73 static int inline ll_capa_check_stop(void)
74 {
75         return (capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
76 }
77
78 static int ll_renew_capa(struct obd_capa *ocapa)
79 {
80         struct ptlrpc_request *req = NULL;
81         /* no need to lock, no one else will touch it */
82         struct inode *inode = ocapa->c_inode;
83         struct obd_export *md_exp = ll_i2mdexp(inode);
84         struct ll_inode_info *lli = ll_i2info(inode);
85         __u64 valid = OBD_MD_CAPA;
86         int rc;
87         ENTRY;
88
89         if (capa_expired(&ocapa->c_capa))
90                 RETURN(-ESTALE);
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         int rc;
102         ENTRY;
103
104         {
105                 char name[sizeof(current->comm)];
106                 snprintf(name, sizeof(name) - 1, "ll_capa");
107                 kportal_daemonize(name);
108         }
109
110         SIGNAL_MASK_LOCK(current, flags);
111         sigfillset(&current->blocked);
112         RECALC_SIGPENDING;
113         SIGNAL_MASK_UNLOCK(current, flags);
114
115         /*
116          * letting starting function know, that we are ready and control may be
117          * returned.
118          */
119         capa_thread.t_flags = SVC_RUNNING;
120         complete(&ctl->ctl_starting);
121
122         while (1) {
123                 struct l_wait_info lwi = { 0 };
124                 struct obd_capa *ocapa, *tmp, *next = NULL, tcapa;
125                 unsigned long expiry, sleep = CAPA_PRE_EXPIRY;
126
127                 l_wait_event(capa_thread.t_ctl_waitq,
128                              (have_expired_capa() || ll_capa_check_stop()),
129                              &lwi);
130
131                 if (ll_capa_check_stop())
132                         break;
133
134                 spin_lock(&capa_lock);
135                 list_for_each_entry_safe(ocapa, tmp, ll_capa_list, c_list) {
136                         if (ocapa->c_capa.lc_flags & CAPA_FL_SHORT)
137                                 sleep = CAPA_PRE_EXPIRY_SHORT;
138
139                         if (ocapa->c_capa.lc_op == CAPA_TRUNC)
140                                 continue;
141
142                         if (__capa_is_to_expire(ocapa)) {
143                                 /* copy capa in case it's deleted */
144                                 tcapa = *ocapa;
145                                 spin_unlock(&capa_lock);
146
147                                 rc = ll_renew_capa(&tcapa);
148                                 if (rc)
149                                         capa_put(ocapa);
150
151                                 spin_lock(&capa_lock);
152                         } else {
153                                 next = ocapa;
154                                 break;
155                         }
156                 }
157
158                 if (next) {
159                         struct lustre_capa *capa = &next->c_capa;
160
161                         expiry = expiry_to_jiffies(capa->lc_expiry -
162                                                    capa_pre_expiry(capa));
163                         if (time_before(expiry, ll_capa_timer.expires) ||
164                             !timer_pending(&ll_capa_timer)) {
165                                 mod_timer(&ll_capa_timer, expiry);
166                                 CDEBUG(D_INFO,"ll_capa_timer new expiry: %lu\n",
167                                        expiry);
168                         }
169                 }
170                 spin_unlock(&capa_lock);
171
172                 /* wait ll_renew_capa finish */
173                 set_current_state(TASK_INTERRUPTIBLE);
174                 schedule_timeout(sleep * HZ);
175         }
176
177         capa_thread.t_flags = SVC_STOPPED;
178
179         /* this is SMP-safe way to finish thread. */
180         complete_and_exit(&ctl->ctl_finishing, 0);
181         EXIT;
182 }
183
184 /* just wake up, others are handled by ll_capa_thread */
185 void ll_capa_timer_callback(unsigned long unused)
186 {
187         ENTRY;
188         wake_up(&capa_thread.t_ctl_waitq);
189         EXIT;
190 }
191
192 int ll_capa_thread_start(void)
193 {
194         int rc;
195         ENTRY;
196
197         LASSERT(capa_thread.t_flags == 0);
198         init_completion(&ll_capa_ctl.ctl_starting);
199         init_completion(&ll_capa_ctl.ctl_finishing);
200         init_waitqueue_head(&capa_thread.t_ctl_waitq);
201
202         rc = kernel_thread(ll_capa_thread, &ll_capa_ctl,
203                            (CLONE_VM | CLONE_FILES));
204         if (rc < 0) {
205                 CERROR("cannot start expired capa thread, "
206                        "err = %d\n", rc);
207                 RETURN(rc);
208         }
209         wait_for_completion(&ll_capa_ctl.ctl_starting);
210         LASSERT(capa_thread.t_flags == SVC_RUNNING);
211         RETURN(0);
212 }
213
214 void ll_capa_thread_stop(void)
215 {
216         ENTRY;
217
218         capa_thread.t_flags = SVC_STOPPING;
219         wake_up(&capa_thread.t_ctl_waitq);
220         wait_for_completion(&ll_capa_ctl.ctl_finishing);
221         LASSERT(capa_thread.t_flags == SVC_STOPPED);
222         capa_thread.t_flags = 0;
223
224         EXIT;
225 }
226
227 int ll_set_capa(struct inode *inode, struct lookup_intent *it)
228 {
229         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
230         struct mds_body *body;
231         struct lustre_capa *capa;
232         struct obd_capa *ocapa;
233         struct ll_inode_info *lli = ll_i2info(inode);
234         unsigned long expiry;
235
236         if (!S_ISREG(inode->i_mode))
237                 return 0;
238
239         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
240         LASSERT(body != NULL);          /* reply already checked out */
241         LASSERT_REPSWABBED(req, 1);     /* and swabbed down */
242
243         if (!(body->valid & OBD_MD_CAPA))
244                 return 0;
245
246         ENTRY;
247
248         capa = lustre_msg_buf(req->rq_repmsg, 7, sizeof (*capa));
249         LASSERT(capa != NULL);          /* reply already checked out */
250         LASSERT_REPSWABBED(req, 7);     /* and swabbed down */
251
252         ocapa = capa_renew(capa, CLIENT_CAPA);
253         if (!ocapa)
254                 RETURN(-ENOMEM);
255
256         spin_lock(&capa_lock);
257         ocapa->c_inode = inode;
258         ocapa->c_handle = body->handle;
259         spin_unlock(&capa_lock);
260
261         spin_lock(&lli->lli_lock);
262         /* in case it was linked to lli_capas already */
263         if (list_empty(&ocapa->c_lli_list))
264                 list_add(&ocapa->c_lli_list, &lli->lli_capas);
265         spin_unlock(&lli->lli_lock);
266
267         expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
268
269         spin_lock(&capa_lock);
270         if (time_before(expiry, ll_capa_timer.expires) ||
271             !timer_pending(&ll_capa_timer)) {
272                 mod_timer(&ll_capa_timer, expiry);
273                 CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
274         }
275         spin_unlock(&capa_lock);
276
277         RETURN(0);
278 }
279
280 int ll_set_trunc_capa(struct ptlrpc_request *req, int offset, struct inode *inode)
281 {
282         struct mds_body *body;
283         struct obd_capa *ocapa;
284         struct lustre_capa *capa;
285         struct ll_inode_info *lli = ll_i2info(inode);
286
287         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
288         if (!body)
289                 return -ENOMEM;
290
291         if (!(body->valid & OBD_MD_CAPA))
292                 return 0;
293
294         ENTRY;
295         capa = (struct lustre_capa *)lustre_swab_repbuf(req, offset + 1,
296                                         sizeof(*capa), lustre_swab_lustre_capa);
297         if (!capa)
298                 RETURN(-ENOMEM);        
299
300         ocapa = capa_renew(capa, CLIENT_CAPA);
301         if (!ocapa)
302                 RETURN(-ENOMEM);
303
304         spin_lock(&lli->lli_lock);
305         /* in case it was linked to lli_capas already */
306         if (list_empty(&ocapa->c_lli_list))
307                 list_add(&ocapa->c_lli_list, &lli->lli_capas);
308         spin_unlock(&lli->lli_lock);
309
310         RETURN(0);
311 }
312
313 struct obd_capa *ll_get_capa(struct inode *inode, uid_t uid, int op)
314 {
315         struct ll_inode_info *lli = ll_i2info(inode);
316         struct obd_capa *ocapa, *tmp;
317         ENTRY;
318
319         list_for_each_entry_safe(ocapa, tmp, &lli->lli_capas, c_lli_list) {
320                 if (ocapa->c_capa.lc_ruid != uid)
321                         continue;
322                 if (ocapa->c_capa.lc_op != op)
323                         continue;
324
325                 RETURN(ocapa);
326         }
327         
328         RETURN(NULL);
329 }