Whamcloud - gitweb
05c7fe2ea7d15a5cf42b946d44378867627ddf02
[fs/lustre-release.git] / lustre / llite / llite_gns.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: Phil Schwan <phil@clusterfs.com>
7  * Author: Oleg Drokin <green@clusterfs.com>
8  * Author: Yury Umanets <yury@clusterfs.com>
9  * Review: Nikita Danilov <nikita@clusterfs.com>
10  *
11  *   This file is part of Lustre, http://www.lustre.org.
12  *
13  *   Lustre is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   Lustre is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with Lustre; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #define DEBUG_SUBSYSTEM S_LLITE
28
29 #include <linux/fs.h>
30 #include <linux/version.h>
31 #include <asm/uaccess.h>
32 #include <linux/file.h>
33 #include <linux/kmod.h>
34
35 #include <linux/lustre_lite.h>
36 #include "llite_internal.h"
37
38 static struct list_head gns_sbi_list = LIST_HEAD_INIT(gns_sbi_list);
39 static spinlock_t gns_lock = SPIN_LOCK_UNLOCKED;
40 static struct ptlrpc_thread gns_thread;
41 static struct ll_gns_ctl gns_ctl;
42
43 /*
44  * waits until passed dentry gets mountpoint or timeout and attempts are
45  * exhausted. Returns 1 if dentry became mountpoint and 0 otherwise.
46  */
47 static int
48 ll_gns_wait_for_mount(struct dentry *dentry,
49                       int timeout, int tries)
50 {
51         struct l_wait_info lwi;
52         struct ll_sb_info *sbi;
53         ENTRY;
54
55         LASSERT(dentry != NULL);
56         LASSERT(!IS_ERR(dentry));
57         sbi = ll_s2sbi(dentry->d_sb);
58         
59         lwi = LWI_TIMEOUT(timeout * HZ, NULL, NULL);
60         for (; !d_mountpoint(dentry) && tries > 0; tries--)
61                 l_wait_event(sbi->ll_gns_waitq, d_mountpoint(dentry), &lwi);
62
63         if (d_mountpoint(dentry)) {
64                 spin_lock(&sbi->ll_gns_lock);
65                 sbi->ll_gns_state = LL_GNS_FINISHED;
66                 spin_unlock(&sbi->ll_gns_lock);
67                 RETURN(0);
68         }
69         RETURN(-ETIME);
70 }
71
72 /*
73  * tries to mount the mount object under passed @dentry. In the case of success
74  * @dentry will become mount point and 0 will be returned. Error code will be
75  * returned otherwise.
76  */
77 int
78 ll_gns_mount_object(struct dentry *dentry, struct vfsmount *mnt)
79 {
80         struct ll_dentry_data *lld = dentry->d_fsdata;
81         char *path, *pathpage, *datapage, *argv[4];
82         struct file *mntinfo_fd = NULL;
83         int cleanup_phase = 0, rc = 0;
84         struct ll_sb_info *sbi;
85         struct dentry *dchild;
86         ENTRY;
87
88         if (mnt == NULL) {
89                 CERROR("suid directory found, but no "
90                        "vfsmount available.\n");
91                 RETURN(-EINVAL);
92         }
93
94         CDEBUG(D_INODE, "mounting dentry %p\n", dentry);
95
96         LASSERT(dentry->d_inode != NULL);
97         LASSERT(S_ISDIR(dentry->d_inode->i_mode));
98         LASSERT(lld != NULL);
99         
100         sbi = ll_i2sbi(dentry->d_inode);
101         LASSERT(sbi != NULL);
102
103         /* 
104          * another thead is in progress or just finished mounting the
105          * dentry. Handling that.
106          */
107         spin_lock(&sbi->ll_gns_lock);
108         if (sbi->ll_gns_state == LL_GNS_MOUNTING ||
109             sbi->ll_gns_state == LL_GNS_FINISHED) {
110                 spin_unlock(&sbi->ll_gns_lock);
111                 CDEBUG(D_INODE, "GNS is in progress now, throwing "
112                        "-ERESTARTSYS to restart syscall and let "
113                        "it finish.\n");
114                 RETURN(-ERESTARTSYS);
115         }
116         LASSERT(sbi->ll_gns_state == LL_GNS_IDLE);
117
118         /* mounting started */
119         sbi->ll_gns_state = LL_GNS_MOUNTING;
120         spin_unlock(&sbi->ll_gns_lock);
121
122         /* we need to build an absolute pathname to pass to mount */
123         pathpage = (char *)__get_free_page(GFP_KERNEL);
124         if (!pathpage)
125                 GOTO(cleanup, rc = -ENOMEM);
126         cleanup_phase = 1;
127
128         /* getting @dentry path stored in @pathpage. */
129         path = d_path(dentry, mnt, pathpage, PAGE_SIZE);
130         if (IS_ERR(path)) {
131                 CERROR("can't build mount object path, err %d\n",
132                        (int)PTR_ERR(dchild));
133                 GOTO(cleanup, rc = PTR_ERR(dchild));
134         }
135
136         /* synchronizing with possible /proc/fs/...write */
137         down(&sbi->ll_gns_sem);
138         
139         /* 
140          * mount object name is taken from sbi, where it is set in mount time or
141          * via /proc/fs... tunable. It may be ".mntinfo" or so.
142          */
143         dchild = lookup_one_len(sbi->ll_gns_oname, dentry,
144                                 strlen(sbi->ll_gns_oname));
145         up(&sbi->ll_gns_sem);
146
147         cleanup_phase = 2;
148         
149         if (IS_ERR(dchild)) {
150                 rc = PTR_ERR(dchild);
151                 
152                 if (rc == -ERESTARTSYS) {
153                         CDEBUG(D_INODE, "possible endless loop is detected "
154                                "due to mount object is directory marked by "
155                                "SUID bit.\n");
156                         GOTO(cleanup, rc = -ELOOP);
157                 }
158
159                 CERROR("can't find mount object %*s/%*s err = %d.\n",
160                        (int)dentry->d_name.len, dentry->d_name.name,
161                        strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
162                        rc);
163                 GOTO(cleanup, rc);
164         }
165
166         /* mount object is not found */
167         if (!dchild->d_inode)
168                 GOTO(cleanup, rc = -ENOENT);
169
170         /* check if found child is regular file */
171         if (!S_ISREG(dchild->d_inode->i_mode))
172                 GOTO(cleanup, rc = -EOPNOTSUPP);
173
174         mntget(mnt);
175
176         /* ok, mount object if found, opening it. */
177         mntinfo_fd = dentry_open(dchild, mnt, 0);
178         if (IS_ERR(mntinfo_fd)) {
179                 CERROR("can't open mount object %*s/%*s err = %d.\n",
180                        (int)dentry->d_name.len, dentry->d_name.name,
181                        strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
182                        (int)PTR_ERR(mntinfo_fd));
183                 mntput(mnt);
184                 GOTO(cleanup, rc = PTR_ERR(mntinfo_fd));
185         }
186         cleanup_phase = 3;
187
188         if (mntinfo_fd->f_dentry->d_inode->i_size > PAGE_SIZE) {
189                 CERROR("mount object %*s/%*s is too big (%Ld)\n",
190                        (int)dentry->d_name.len, dentry->d_name.name,
191                        strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
192                        mntinfo_fd->f_dentry->d_inode->i_size);
193                 GOTO(cleanup, rc = -EFBIG);
194         }
195
196         datapage = (char *)__get_free_page(GFP_KERNEL);
197         if (!datapage)
198                 GOTO(cleanup, rc = -ENOMEM);
199
200         cleanup_phase = 4;
201         
202         /* read data from mount object. */
203         rc = kernel_read(mntinfo_fd, 0, datapage, PAGE_SIZE);
204         if (rc < 0) {
205                 CERROR("can't read mount object %*s/%*s data, err %d\n",
206                        (int)dentry->d_name.len, dentry->d_name.name,
207                        strlen(sbi->ll_gns_oname), sbi->ll_gns_oname,
208                        rc);
209                 GOTO(cleanup, rc);
210         }
211
212         datapage[PAGE_SIZE - 1] = '\0';
213
214         fput(mntinfo_fd);
215         mntinfo_fd = NULL;
216         dchild = NULL;
217
218         /* synchronizing with possible /proc/fs/...write */
219         down(&sbi->ll_gns_sem);
220
221         /*
222          * upcall is initialized in mount time or via /proc/fs/... tuneable and
223          * may be /usr/lib/lustre/gns-upcall.sh
224          */
225         argv[0] = sbi->ll_gns_upcall;
226         argv[1] = datapage;
227         argv[2] = path;
228         argv[3] = NULL;
229         
230         up(&sbi->ll_gns_sem);
231
232         rc = USERMODEHELPER(argv[0], argv, NULL);
233         if (rc) {
234                 CERROR("failed to call GNS upcall %s, err = %d\n",
235                        sbi->ll_gns_upcall, rc);
236                 GOTO(cleanup, rc);
237         }
238
239         /*
240          * wait for mount completion. This is actually not need, because
241          * USERMODEHELPER() returns only when usermode process finishes. But we
242          * doing this just for case USERMODEHELPER() semantics will be changed
243          * or usermode upcall program will start mounting in backgound and
244          * return instantly. --umka
245          */
246         rc = ll_gns_wait_for_mount(dentry, 1, GNS_WAIT_ATTEMPTS);
247         complete_all(&sbi->ll_gns_mount_finished);
248         if (rc == 0) {
249                 struct dentry *rdentry;
250                 struct vfsmount *rmnt;
251                 
252                 /* mount is successful */
253                 LASSERT(sbi->ll_gns_state == LL_GNS_FINISHED);
254
255                 rmnt = mntget(mnt);
256                 rdentry = dget(dentry);
257                 
258                 if (follow_down(&rmnt, &rdentry)) {
259                         /* 
260                          * registering new mount in GNS mounts list and thus
261                          * make it accessible from GNS control thread.
262                          */
263                         spin_lock(&dcache_lock);
264                         LASSERT(list_empty(&rmnt->mnt_lustre_list));
265                         list_add_tail(&rmnt->mnt_lustre_list,
266                                       &sbi->ll_mnt_list);
267                         spin_unlock(&dcache_lock);
268                         rmnt->mnt_last_used = jiffies;
269                         mntput(rmnt);
270                         dput(rdentry);
271                 } else {
272                         mntput(mnt);
273                         dput(dentry);
274                 }
275         } else {
276                 CERROR("usermode upcall %s failed to mount %s, err %d\n",
277                        sbi->ll_gns_upcall, path, rc);
278         }
279                 
280         EXIT;
281 cleanup:
282         switch (cleanup_phase) {
283         case 4:
284                 free_page((unsigned long)datapage);
285         case 3:
286                 if (mntinfo_fd != NULL)
287                         fput(mntinfo_fd);
288         case 2:
289                 if (dchild != NULL)
290                         dput(dchild);
291         case 1:
292                 free_page((unsigned long)pathpage);
293                 
294                 /* 
295                  * waking up all waiters after gns state is set to
296                  * LL_GNS_MOUNTING
297                  */
298                 complete_all(&sbi->ll_gns_mount_finished);
299         case 0:
300                 spin_lock(&sbi->ll_gns_lock);
301                 sbi->ll_gns_state = LL_GNS_IDLE;
302                 spin_unlock(&sbi->ll_gns_lock);
303         }
304         return rc;
305 }
306
307 /* tries to umount passed @mnt. */
308 int ll_gns_umount_object(struct vfsmount *mnt)
309 {
310         int rc = 0;
311         ENTRY;
312         
313         CDEBUG(D_INODE, "unmounting mnt %p\n", mnt);
314         rc = do_umount(mnt, 0);
315         if (rc) {
316                 CDEBUG(D_INODE, "can't umount 0x%p, err = %d\n",
317                        mnt, rc);
318         }
319         
320         RETURN(rc);
321 }
322
323 int ll_gns_check_mounts(struct ll_sb_info *sbi, int flags)
324 {
325         struct list_head check_list = LIST_HEAD_INIT(check_list);
326         struct vfsmount *mnt;
327         unsigned long pass;
328         ENTRY;
329
330         spin_lock(&dcache_lock);
331         list_splice_init(&sbi->ll_mnt_list, &check_list);
332
333         /*
334          * walk the list in reverse order, and put them on the front of the sbi
335          * list each iteration; this avoids list-ordering problems if we race
336          * with another gns-mounting thread.
337          */
338         while (!list_empty(&check_list)) {
339                 mnt = list_entry(check_list.prev,
340                                  struct vfsmount,
341                                  mnt_lustre_list);
342
343                 mntget(mnt);
344
345                 list_del_init(&mnt->mnt_lustre_list);
346
347                 list_add(&mnt->mnt_lustre_list,
348                          &sbi->ll_mnt_list);
349
350                 /* check for timeout if needed */
351                 pass = jiffies - mnt->mnt_last_used;
352                 
353                 if (flags == LL_GNS_CHECK &&
354                     pass < sbi->ll_gns_timeout * HZ)
355                 {
356                         mntput(mnt);
357                         continue;
358                 }
359                 spin_unlock(&dcache_lock);
360
361                 /* umounting @mnt */
362                 ll_gns_umount_object(mnt);
363
364                 mntput(mnt);
365                 spin_lock(&dcache_lock);
366         }
367         spin_unlock(&dcache_lock);
368         RETURN(0);
369 }
370
371 /*
372  * GNS timer callback function. It restarts gns timer and wakes up GNS control
373  * thread to process mounts list.
374  */
375 void ll_gns_timer_callback(unsigned long data)
376 {
377         struct ll_sb_info *sbi = (void *)data;
378         ENTRY;
379
380         spin_lock(&gns_lock);
381         if (list_empty(&sbi->ll_gns_sbi_head))
382                 list_add(&sbi->ll_gns_sbi_head, &gns_sbi_list);
383         spin_unlock(&gns_lock);
384         
385         wake_up(&gns_thread.t_ctl_waitq);
386         mod_timer(&sbi->ll_gns_timer,
387                   jiffies + sbi->ll_gns_tick * HZ);
388 }
389
390 /* this function checks if something new happened to exist in gns list. */
391 static int inline ll_gns_check_event(void)
392 {
393         int rc;
394         
395         spin_lock(&gns_lock);
396         rc = !list_empty(&gns_sbi_list);
397         spin_unlock(&gns_lock);
398
399         return rc;
400 }
401
402 /* should we stop GNS control thread? */
403 static int inline ll_gns_check_stop(void)
404 {
405         mb();
406         return (gns_thread.t_flags & SVC_STOPPING) ? 1 : 0;
407 }
408
409 /* GNS control thread function. */
410 static int ll_gns_thread_main(void *arg)
411 {
412         struct ll_gns_ctl *ctl = arg;
413         unsigned long flags;
414         ENTRY;
415
416         {
417                 char name[sizeof(current->comm)];
418                 snprintf(name, sizeof(name) - 1, "ll_gns");
419                 kportal_daemonize(name);
420         }
421         
422         SIGNAL_MASK_LOCK(current, flags);
423         sigfillset(&current->blocked);
424         RECALC_SIGPENDING;
425         SIGNAL_MASK_UNLOCK(current, flags);
426
427         /*
428          * letting starting function know, that we are ready and control may be
429          * returned.
430          */
431         gns_thread.t_flags = SVC_RUNNING;
432         complete(&ctl->gc_starting);
433
434         while (!ll_gns_check_stop()) {
435                 struct l_wait_info lwi = { 0 };
436
437                 l_wait_event(gns_thread.t_ctl_waitq,
438                              (ll_gns_check_event() ||
439                               ll_gns_check_stop()), &lwi);
440                 
441                 spin_lock(&gns_lock);
442                 while (!list_empty(&gns_sbi_list)) {
443                         struct ll_sb_info *sbi;
444
445                         sbi = list_entry(gns_sbi_list.prev,
446                                          struct ll_sb_info,
447                                          ll_gns_sbi_head);
448                         
449                         list_del_init(&sbi->ll_gns_sbi_head);
450                         spin_unlock(&gns_lock);
451                         ll_gns_check_mounts(sbi, LL_GNS_CHECK);
452                         spin_lock(&gns_lock);
453                 }
454                 spin_unlock(&gns_lock);
455         }
456
457         EXIT;
458         gns_thread.t_flags = SVC_STOPPED;
459
460         /* this is SMP-safe way to finish thread. */
461         complete_and_exit(&ctl->gc_finishing, 0);
462 }
463
464 void ll_gns_add_timer(struct ll_sb_info *sbi)
465 {
466         mod_timer(&sbi->ll_gns_timer,
467                   jiffies + sbi->ll_gns_tick * HZ);
468 }
469
470 void ll_gns_del_timer(struct ll_sb_info *sbi)
471 {
472         del_timer(&sbi->ll_gns_timer);
473 }
474
475 /*
476  * starts GNS control thread and waits for a signal it is up and work may be
477  * continued.
478  */
479 int ll_gns_start_thread(void)
480 {
481         int rc;
482         ENTRY;
483
484         LASSERT(gns_thread.t_flags == 0);
485         init_completion(&gns_ctl.gc_starting);
486         init_completion(&gns_ctl.gc_finishing);
487         init_waitqueue_head(&gns_thread.t_ctl_waitq);
488         
489         rc = kernel_thread(ll_gns_thread_main, &gns_ctl,
490                            (CLONE_VM | CLONE_FILES));
491         if (rc < 0) {
492                 CERROR("cannot start GNS control thread, "
493                        "err = %d\n", rc);
494                 RETURN(rc);
495         }
496         wait_for_completion(&gns_ctl.gc_starting);
497         LASSERT(gns_thread.t_flags == SVC_RUNNING);
498         RETURN(0);
499 }
500
501 /* stops GNS control thread and waits its actual stop. */
502 void ll_gns_stop_thread(void)
503 {
504         ENTRY;
505         gns_thread.t_flags = SVC_STOPPING;
506         wake_up(&gns_thread.t_ctl_waitq);
507         wait_for_completion(&gns_ctl.gc_finishing);
508         LASSERT(gns_thread.t_flags == SVC_STOPPED);
509         gns_thread.t_flags = 0;
510         EXIT;
511 }