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