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         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 = 0;
81         int rc;
82         ENTRY;
83
84         valid |= OBD_MD_CAPA;
85
86         rc = md_getattr(md_exp, &lli->lli_id, valid, NULL, NULL, 0,
87                         0, ocapa, &req);
88         if (rc < 0)
89                 CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
90         RETURN(rc);
91 }
92
93 static int ll_capa_thread(void *arg)
94 {
95         struct thread_ctl *ctl = arg;
96         unsigned long flags;
97         ENTRY;
98
99         {
100                 char name[sizeof(current->comm)];
101                 snprintf(name, sizeof(name) - 1, "ll_capa");
102                 kportal_daemonize(name);
103         }
104
105         SIGNAL_MASK_LOCK(current, flags);
106         sigfillset(&current->blocked);
107         RECALC_SIGPENDING;
108         SIGNAL_MASK_UNLOCK(current, flags);
109
110         /*
111          * letting starting function know, that we are ready and control may be
112          * returned.
113          */
114         capa_thread.t_flags = SVC_RUNNING;
115         complete(&ctl->ctl_starting);
116
117         while (1) {
118                 struct l_wait_info lwi = { 0 };
119                 struct obd_capa *ocapa, *next = NULL;
120                 unsigned long expiry, sleep = CAPA_PRE_EXPIRY;
121
122                 l_wait_event(capa_thread.t_ctl_waitq,
123                              (have_expired_capa() || ll_capa_check_stop()),
124                              &lwi);
125
126                 spin_lock(&capa_lock);
127                 list_for_each_entry(ocapa, ll_capa_list, c_list) {
128                         if (__capa_is_to_expire(ocapa)) {
129                                 /* get capa in case it's deleted */
130                                 __capa_get(ocapa);
131
132                                 spin_unlock(&capa_lock);
133                                 ll_renew_capa(ocapa);
134                                 capa_put(ocapa, CLIENT_CAPA);
135                                 spin_lock(&capa_lock);
136                         } else {
137                                 next = ocapa;
138                                 break;
139                         }
140                 }
141                 if (next) {
142                         expiry = expiry_to_jiffies(next->c_capa.lc_expiry);
143                         mod_timer(&ll_capa_timer, expiry);
144                         CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
145                         if (next->c_capa.lc_flags & CAPA_FL_NOROUND)
146                                 sleep = CAPA_PRE_EXPIRY_NOROUND;
147                 }
148                 spin_unlock(&capa_lock);
149
150                 if (ll_capa_check_stop())
151                         break;
152
153                 /* wait ll_renew_capa finish */
154                 set_current_state(TASK_INTERRUPTIBLE);
155                 schedule_timeout(sleep * HZ);
156         }
157
158         capa_thread.t_flags = SVC_STOPPED;
159
160         /* this is SMP-safe way to finish thread. */
161         complete_and_exit(&ctl->ctl_finishing, 0);
162         EXIT;
163 }
164
165 /* just wake up, others are handled by ll_capa_thread */
166 void ll_capa_timer_callback(unsigned long unused)
167 {
168         ENTRY;
169         wake_up(&capa_thread.t_ctl_waitq);
170         EXIT;
171 }
172
173 int ll_capa_thread_start(void)
174 {
175         int rc;
176         ENTRY;
177
178         LASSERT(capa_thread.t_flags == 0);
179         init_completion(&ll_capa_ctl.ctl_starting);
180         init_completion(&ll_capa_ctl.ctl_finishing);
181         init_waitqueue_head(&capa_thread.t_ctl_waitq);
182
183         rc = kernel_thread(ll_capa_thread, &ll_capa_ctl,
184                            (CLONE_VM | CLONE_FILES));
185         if (rc < 0) {
186                 CERROR("cannot start expired capa thread, "
187                        "err = %d\n", rc);
188                 RETURN(rc);
189         }
190         wait_for_completion(&ll_capa_ctl.ctl_starting);
191         LASSERT(capa_thread.t_flags == SVC_RUNNING);
192         RETURN(0);
193 }
194
195 void ll_capa_thread_stop(void)
196 {
197         ENTRY;
198
199         capa_thread.t_flags = SVC_STOPPING;
200         wake_up(&capa_thread.t_ctl_waitq);
201         wait_for_completion(&ll_capa_ctl.ctl_finishing);
202         LASSERT(capa_thread.t_flags == SVC_STOPPED);
203         capa_thread.t_flags = 0;
204
205         EXIT;
206 }
207
208 int ll_set_och_capa(struct inode *inode, struct lookup_intent *it,
209                     struct obd_client_handle *och)
210 {
211         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
212         struct ll_inode_info *lli = ll_i2info(inode);
213         struct mds_body *body;
214         struct lustre_capa *capa;
215         __u64 mdsid = lli->lli_id.li_fid.lf_group;
216         unsigned long ino = lli->lli_id.li_stc.u.e3s.l3s_ino;
217         int capa_op = (it->it_flags & MAY_WRITE) ? MAY_WRITE : MAY_READ;
218         unsigned long expiry;
219         int rc = 0;
220         ENTRY;
221
222         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof (*body));
223         LASSERT(body != NULL);          /* reply already checked out */
224         LASSERT_REPSWABBED(req, 1);     /* and swabbed down */
225
226         capa = lustre_msg_buf(req->rq_repmsg, 7, sizeof (*capa));
227         LASSERT(capa != NULL);          /* reply already checked out */
228         LASSERT_REPSWABBED(req, 7);     /* and swabbed down */
229
230         och->och_capa = capa_get(current->uid, capa_op, mdsid, ino,
231                                  CLIENT_CAPA, capa, inode, &body->handle);
232         if (!och->och_capa)
233                 rc = -ENOMEM;
234
235         expiry = expiry_to_jiffies(capa->lc_expiry - capa_pre_expiry(capa));
236
237         spin_lock(&capa_lock);
238         if (time_before(expiry, ll_capa_timer.expires) ||
239             !timer_pending(&ll_capa_timer)) {
240                 mod_timer(&ll_capa_timer, expiry);
241                 CDEBUG(D_INFO, "ll_capa_timer new expiry: %lu\n", expiry);
242         }
243         spin_unlock(&capa_lock);
244
245         RETURN(rc);
246 }
247