Whamcloud - gitweb
revert part of last change.
[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                 if (!expired) {
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",
63                                        expiry);
64                         }
65                 }
66         }
67         spin_unlock(&capa_lock);
68
69         RETURN(expired);
70 }
71
72 static int inline ll_capa_check_stop(void)
73 {
74         return (capa_thread.t_flags & SVC_STOPPING) ? 1: 0;
75 }
76
77 static int ll_renew_capa(struct obd_capa *ocapa)
78 {
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;
85         int rc;
86         ENTRY;
87
88         if (capa_expired(&ocapa->c_capa))
89                 RETURN(-ESTALE);
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
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                                 inode = igrab(ocapa->c_inode);
144                                 if (inode == NULL)
145                                         continue;
146
147                                 tcapa = *ocapa;
148                                 spin_unlock(&capa_lock);
149
150                                 rc = ll_renew_capa(&tcapa);
151                                 iput(inode);
152
153                                 if (rc)
154                                         capa_put(ocapa);
155
156                                 spin_lock(&capa_lock);
157                         } else {
158                                 next = ocapa;
159                                 break;
160                         }
161                 }
162
163                 if (next) {
164                         struct lustre_capa *capa = &next->c_capa;
165
166                         expiry = expiry_to_jiffies(capa->lc_expiry -
167                                                    capa_pre_expiry(capa));
168                         if (time_before(expiry, ll_capa_timer.expires) ||
169                             !timer_pending(&ll_capa_timer)) {
170                                 mod_timer(&ll_capa_timer, expiry);
171                                 CDEBUG(D_INFO,"ll_capa_timer new expiry: %lu\n",
172                                        expiry);
173                         }
174                 }
175                 spin_unlock(&capa_lock);
176
177                 /* wait ll_renew_capa finish */
178                 set_current_state(TASK_INTERRUPTIBLE);
179                 schedule_timeout(sleep * HZ);
180         }
181
182         capa_thread.t_flags = SVC_STOPPED;
183
184         /* this is SMP-safe way to finish thread. */
185         complete_and_exit(&ctl->ctl_finishing, 0);
186         EXIT;
187 }
188
189 /* just wake up, others are handled by ll_capa_thread */
190 void ll_capa_timer_callback(unsigned long unused)
191 {
192         ENTRY;
193         wake_up(&capa_thread.t_ctl_waitq);
194         EXIT;
195 }
196
197 int ll_capa_thread_start(void)
198 {
199         int rc;
200         ENTRY;
201
202         LASSERT(capa_thread.t_flags == 0);
203         init_completion(&ll_capa_ctl.ctl_starting);
204         init_completion(&ll_capa_ctl.ctl_finishing);
205         init_waitqueue_head(&capa_thread.t_ctl_waitq);
206
207         rc = kernel_thread(ll_capa_thread, &ll_capa_ctl,
208                            (CLONE_VM | CLONE_FILES));
209         if (rc < 0) {
210                 CERROR("cannot start expired capa thread, "
211                        "err = %d\n", rc);
212                 RETURN(rc);
213         }
214         wait_for_completion(&ll_capa_ctl.ctl_starting);
215         LASSERT(capa_thread.t_flags == SVC_RUNNING);
216         RETURN(0);
217 }
218
219 void ll_capa_thread_stop(void)
220 {
221         ENTRY;
222
223         capa_thread.t_flags = SVC_STOPPING;
224         wake_up(&capa_thread.t_ctl_waitq);
225         wait_for_completion(&ll_capa_ctl.ctl_finishing);
226         LASSERT(capa_thread.t_flags == SVC_STOPPED);
227         capa_thread.t_flags = 0;
228
229         EXIT;
230 }
231
232 int ll_set_capa(struct inode *inode, struct lookup_intent *it,
233                 struct obd_client_handle *och)
234 {
235         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
236         struct mds_body *body;
237         struct lustre_capa *capa;
238         struct obd_capa *ocapa;
239         struct ll_inode_info *lli = ll_i2info(inode);
240         unsigned long expiry;
241
242         if (!S_ISREG(inode->i_mode))
243                 return 0;
244
245         /* GNS code path will have no req */
246         if (!req)
247                 return 0;
248
249         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
250         LASSERT(body != NULL);          /* reply already checked out */
251         LASSERT_REPSWABBED(req, 1);     /* and swabbed down */
252
253         if (!(body->valid & OBD_MD_CAPA))
254                 return 0;
255
256         ENTRY;
257
258         capa = lustre_msg_buf(req->rq_repmsg, 7, sizeof (*capa));
259         LASSERT(capa != NULL);          /* reply already checked out */
260         LASSERT_REPSWABBED(req, 7);     /* and swabbed down */
261
262         ocapa = capa_renew(capa, CLIENT_CAPA);
263         if (!ocapa)
264                 RETURN(-ENOMEM);
265
266         spin_lock(&capa_lock);
267         ocapa->c_inode = inode;
268         ocapa->c_handle = och->och_fh;
269         spin_unlock(&capa_lock);
270
271         spin_lock(&lli->lli_lock);
272         /* in case it was linked to lli_capas already */
273         if (list_empty(&ocapa->c_lli_list))
274                 list_add(&ocapa->c_lli_list, &lli->lli_capas);
275         spin_unlock(&lli->lli_lock);
276
277         expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
278
279         spin_lock(&capa_lock);
280         if (time_before(expiry, ll_capa_timer.expires) ||
281             !timer_pending(&ll_capa_timer)) {
282                 mod_timer(&ll_capa_timer, expiry);
283                 CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
284         }
285         spin_unlock(&capa_lock);
286
287         RETURN(0);
288 }
289
290 int ll_set_trunc_capa(struct ptlrpc_request *req, int offset, struct inode *inode)
291 {
292         struct mds_body *body;
293         struct obd_capa *ocapa;
294         struct lustre_capa *capa;
295         struct ll_inode_info *lli = ll_i2info(inode);
296
297         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
298         if (!body)
299                 return -ENOMEM;
300
301         if (!(body->valid & OBD_MD_CAPA))
302                 return 0;
303
304         ENTRY;
305         capa = (struct lustre_capa *)lustre_swab_repbuf(req, offset + 1,
306                                         sizeof(*capa), lustre_swab_lustre_capa);
307         if (!capa)
308                 RETURN(-ENOMEM);        
309
310         ocapa = capa_renew(capa, CLIENT_CAPA);
311         if (!ocapa)
312                 RETURN(-ENOMEM);
313
314         spin_lock(&lli->lli_lock);
315         /* in case it was linked to lli_capas already */
316         if (list_empty(&ocapa->c_lli_list))
317                 list_add(&ocapa->c_lli_list, &lli->lli_capas);
318         spin_unlock(&lli->lli_lock);
319
320         RETURN(0);
321 }
322
323 struct obd_capa *ll_get_capa(struct inode *inode, uid_t uid, int op)
324 {
325         struct ll_inode_info *lli = ll_i2info(inode);
326         struct obd_capa *ocapa, *tmp;
327         ENTRY;
328
329         list_for_each_entry_safe(ocapa, tmp, &lli->lli_capas, c_lli_list) {
330                 if (ocapa->c_capa.lc_ruid != uid)
331                         continue;
332                 if (ocapa->c_capa.lc_op != op)
333                         continue;
334
335                 RETURN(ocapa);
336         }
337         
338         RETURN(NULL);
339 }