1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_LLITE
26 #include <linux/version.h>
27 #include <asm/uaccess.h>
28 #include <linux/file.h>
29 #include <linux/kmod.h>
31 #include <linux/lustre_lite.h>
32 #include "llite_internal.h"
34 /* After roughly how long should we remove an inactive mount? */
35 #define GNS_MOUNT_TIMEOUT 120
36 /* How often should the GNS timer look for mounts to cleanup? */
39 int ll_finish_gns(struct ll_sb_info *sbi)
41 down(&sbi->ll_gns_sem);
42 if (sbi->ll_gns_state != LL_GNS_STATE_MOUNTING) {
44 CERROR("FINISH_GNS called on mount which was not expecting "
49 sbi->ll_gns_state = LL_GNS_STATE_FINISHED;
51 complete(&sbi->ll_gns_completion);
56 /* Pass exactly one (1) page in; when this function returns "page" will point
57 * somewhere into the middle of the page. */
58 int fill_page_with_path(struct dentry *dentry, struct vfsmount *mnt,
61 char *path = *pagep, *p;
63 path[PAGE_SIZE - 1] = '\0';
64 p = path + PAGE_SIZE - 1;
67 if (p - path < dentry->d_name.len + 1)
69 if (dentry->d_name.name[0] != '/') {
70 p -= dentry->d_name.len;
71 memcpy(p, dentry->d_name.name, dentry->d_name.len);
76 dentry = dentry->d_parent;
77 if (dentry->d_parent == dentry) {
78 if (mnt->mnt_parent == mnt)
79 break; /* finished walking up */
82 while (dentry->d_parent == dentry &&
83 follow_up(&mnt, &dentry))
93 int ll_dir_process_mount_object(struct dentry *dentry, struct vfsmount *mnt)
95 struct ll_sb_info *sbi;
96 struct file *mntinfo_fd = NULL;
97 struct page *datapage = NULL, *pathpage;
98 struct address_space *mapping;
99 struct ll_dentry_data *lld = dentry->d_fsdata;
100 struct dentry *dchild, *tmp_dentry;
101 struct vfsmount *tmp_mnt;
102 char *p, *path, *argv[4];
103 int stage = 0, rc = 0;
107 CERROR("suid directory found, but no vfsmount available.\n");
111 LASSERT(dentry->d_inode != NULL);
112 LASSERT(S_ISDIR(dentry->d_inode->i_mode));
113 LASSERT(lld != NULL);
114 sbi = ll_i2sbi(dentry->d_inode);
115 LASSERT(sbi != NULL);
117 down(&sbi->ll_gns_sem);
118 if (sbi->ll_gns_state == LL_GNS_STATE_MOUNTING) {
119 up(&sbi->ll_gns_sem);
120 wait_for_completion(&sbi->ll_gns_completion);
121 if (d_mountpoint(dentry))
125 if (sbi->ll_gns_state == LL_GNS_STATE_FINISHED) {
126 /* we lost a race; just return */
127 up(&sbi->ll_gns_sem);
128 if (d_mountpoint(dentry))
132 LASSERT(sbi->ll_gns_state == LL_GNS_STATE_IDLE);
133 sbi->ll_gns_state = LL_GNS_STATE_MOUNTING;
134 up(&sbi->ll_gns_sem);
136 /* We need to build an absolute pathname to pass to mount */
137 pathpage = alloc_pages(GFP_HIGHUSER, 0);
138 if (pathpage == NULL)
139 GOTO(cleanup, rc = -ENOMEM);
140 path = kmap(pathpage);
141 LASSERT(path != NULL);
143 fill_page_with_path(dentry, mnt, &path);
145 dchild = lookup_one_len(".mntinfo", dentry, strlen(".mntinfo"));
146 if (dchild == NULL || IS_ERR(dchild)) {
147 CERROR("Directory %*s is setuid, but without a mount object.\n",
148 dentry->d_name.len, dentry->d_name.name);
149 GOTO(cleanup, rc = -1);
154 mntinfo_fd = dentry_open(dchild, mnt, 0);
155 if (IS_ERR(mntinfo_fd)) {
158 GOTO(cleanup, rc = PTR_ERR(mntinfo_fd));
162 if (mntinfo_fd->f_dentry->d_inode->i_size > PAGE_SIZE) {
163 CERROR("Mount object file is too big (%Ld)\n",
164 mntinfo_fd->f_dentry->d_inode->i_size);
165 GOTO(cleanup, rc = -1);
167 mapping = mntinfo_fd->f_dentry->d_inode->i_mapping;
168 datapage = read_cache_page(mapping, 0,
169 (filler_t *)mapping->a_ops->readpage,
171 if (IS_ERR(datapage))
172 GOTO(cleanup, rc = PTR_ERR(datapage));
178 p[PAGE_SIZE - 1] = '\0';
183 argv[0] = "/usr/lib/lustre/gns-upcall.sh";
187 rc = USERMODEHELPER(argv[0], argv, NULL);
190 CERROR("GNS mount failed: %d\n", rc);
194 wait_for_completion(&sbi->ll_gns_completion);
195 LASSERT(sbi->ll_gns_state == LL_GNS_STATE_FINISHED);
197 if (d_mountpoint(dentry)) {
198 /* successful follow_down will mntput and dput */
199 tmp_mnt = mntget(mnt);
200 tmp_dentry = dget(dentry);
201 rc = follow_down(&tmp_mnt, &tmp_dentry);
203 struct ll_sb_info *sbi = ll_s2sbi(dentry->d_sb);
204 spin_lock(&dcache_lock);
205 LASSERT(list_empty(&tmp_mnt->mnt_lustre_list));
206 list_add_tail(&tmp_mnt->mnt_lustre_list,
208 spin_unlock(&dcache_lock);
210 tmp_mnt->mnt_last_used = jiffies;
220 CERROR("Woke up from GNS mount, but no mountpoint in place.\n");
229 page_cache_release(datapage);
231 if (mntinfo_fd != NULL)
235 __free_pages(pathpage, 0);
237 down(&sbi->ll_gns_sem);
238 sbi->ll_gns_state = LL_GNS_STATE_IDLE;
239 up(&sbi->ll_gns_sem);
244 /* If timeout == 1, only remove the mounts which are properly aged.
246 * If timeout == 0, we are unmounting -- remove them all. */
247 int ll_gns_umount_all(struct ll_sb_info *sbi, int timeout)
249 struct list_head kill_list = LIST_HEAD_INIT(kill_list);
250 struct page *page = NULL;
251 char *kpage = NULL, *path;
256 page = alloc_pages(GFP_HIGHUSER, 0);
260 LASSERT(kpage != NULL);
263 spin_lock(&dcache_lock);
264 list_splice_init(&sbi->ll_mnt_list, &kill_list);
266 /* Walk the list in reverse order, and put them on the front of the
267 * sbi list each iteration; this avoids list-ordering problems if we
268 * race with another gns-mounting thread */
269 while (!list_empty(&kill_list)) {
270 struct vfsmount *mnt =
271 list_entry(kill_list.prev, struct vfsmount,
274 list_del_init(&mnt->mnt_lustre_list);
275 list_add(&mnt->mnt_lustre_list, &sbi->ll_mnt_list);
278 jiffies - mnt->mnt_last_used < GNS_MOUNT_TIMEOUT * HZ) {
282 spin_unlock(&dcache_lock);
284 CDEBUG(D_INODE, "unmounting mnt %p from sbi %p\n", mnt, sbi);
286 rc = do_umount(mnt, 0);
287 if (rc != 0 && page != NULL) {
290 rc2 = fill_page_with_path(mnt->mnt_root, mnt, &path);
291 CERROR("GNS umount(%s): %d\n", rc2 == 0 ? path : "",
295 spin_lock(&dcache_lock);
297 spin_unlock(&dcache_lock);
301 __free_pages(page, 0);
306 static struct list_head gns_sbi_list = LIST_HEAD_INIT(gns_sbi_list);
307 static struct semaphore gns_sem;
308 static struct ptlrpc_thread gns_thread;
310 void ll_gns_timer_callback(unsigned long data)
312 struct ll_sb_info *sbi = (void *)data;
316 if (list_empty(&sbi->ll_gns_sbi_head))
317 list_add(&sbi->ll_gns_sbi_head, &gns_sbi_list);
319 wake_up(&gns_thread.t_ctl_waitq);
320 mod_timer(&sbi->ll_gns_timer, jiffies + GNS_TICK * HZ);
323 static int gns_check_event(void)
327 rc = !list_empty(&gns_sbi_list);
332 static int ll_gns_thread_main(void *arg)
338 char name[sizeof(current->comm)];
339 snprintf(name, sizeof(name) - 1, "ll_gns");
340 kportal_daemonize(name);
342 SIGNAL_MASK_LOCK(current, flags);
343 sigfillset(¤t->blocked);
345 SIGNAL_MASK_UNLOCK(current, flags);
347 gns_thread.t_flags = SVC_RUNNING;
348 wake_up(&gns_thread.t_ctl_waitq);
350 while ((gns_thread.t_flags & SVC_STOPPING) == 0) {
351 struct l_wait_info lwi = { 0 };
353 l_wait_event(gns_thread.t_ctl_waitq, gns_check_event() ||
354 gns_thread.t_flags & SVC_STOPPING, &lwi);
357 while (!list_empty(&gns_sbi_list)) {
358 struct ll_sb_info *sbi =
359 list_entry(gns_sbi_list.prev, struct ll_sb_info,
361 list_del_init(&sbi->ll_gns_sbi_head);
362 ll_gns_umount_all(sbi, 1);
367 gns_thread.t_flags = SVC_STOPPED;
368 wake_up(&gns_thread.t_ctl_waitq);
373 void ll_gns_add_timer(struct ll_sb_info *sbi)
375 mod_timer(&sbi->ll_gns_timer, jiffies + GNS_TICK * HZ);
378 void ll_gns_del_timer(struct ll_sb_info *sbi)
380 del_timer(&sbi->ll_gns_timer);
383 int ll_gns_start_thread(void)
385 struct l_wait_info lwi = { 0 };
388 LASSERT(gns_thread.t_flags == 0);
389 sema_init(&gns_sem, 1);
391 init_waitqueue_head(&gns_thread.t_ctl_waitq);
392 rc = kernel_thread(ll_gns_thread_main, NULL, CLONE_VM | CLONE_FILES);
394 CERROR("cannot start thread: %d\n", rc);
397 l_wait_event(gns_thread.t_ctl_waitq, gns_thread.t_flags & SVC_RUNNING,
402 void ll_gns_stop_thread(void)
404 struct l_wait_info lwi = { 0 };
407 gns_thread.t_flags = SVC_STOPPING;
410 wake_up(&gns_thread.t_ctl_waitq);
411 l_wait_event(gns_thread.t_ctl_waitq, gns_thread.t_flags & SVC_STOPPED,
413 gns_thread.t_flags = 0;