4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lvfs/lvfs_linux.c
38 * Author: Andreas Dilger <adilger@clusterfs.com>
42 # define EXPORT_SYMTAB
45 #define DEBUG_SUBSYSTEM S_FILTER
47 #include <linux/version.h>
49 #include <asm/unistd.h>
50 #include <linux/slab.h>
51 #include <linux/pagemap.h>
52 #include <linux/quotaops.h>
53 #include <linux/version.h>
54 #include <libcfs/libcfs.h>
55 #include <lustre_fsfilt.h>
57 #include <linux/module.h>
58 #include <linux/init.h>
59 #include <linux/lustre_compat25.h>
61 #include "lvfs_internal.h"
64 #include <lustre_lib.h>
65 #include <lustre_quota.h>
67 __u64 obd_max_pages = 0;
68 __u64 obd_max_alloc = 0;
69 struct lprocfs_stats *obd_memory = NULL;
70 cfs_spinlock_t obd_updatemax_lock = CFS_SPIN_LOCK_UNLOCKED;
71 /* refine later and change to seqlock or simlar from libcfs */
73 /* Debugging check only needed during development */
75 # define ASSERT_CTXT_MAGIC(magic) LASSERT((magic) == OBD_RUN_CTXT_MAGIC)
76 # define ASSERT_NOT_KERNEL_CTXT(msg) LASSERTF(!segment_eq(get_fs(), get_ds()),\
78 # define ASSERT_KERNEL_CTXT(msg) LASSERTF(segment_eq(get_fs(), get_ds()), msg)
80 # define ASSERT_CTXT_MAGIC(magic) do {} while(0)
81 # define ASSERT_NOT_KERNEL_CTXT(msg) do {} while(0)
82 # define ASSERT_KERNEL_CTXT(msg) do {} while(0)
85 static void push_group_info(struct lvfs_run_ctxt *save,
86 struct group_info *ginfo)
89 save->ngroups = current_ngroups;
94 save->group_info = current_cred()->group_info;
95 if ((cred = prepare_creds())) {
96 cred->group_info = ginfo;
103 static void pop_group_info(struct lvfs_run_ctxt *save,
104 struct group_info *ginfo)
107 current_ngroups = save->ngroups;
111 if ((cred = prepare_creds())) {
112 cred->group_info = save->group_info;
115 task_unlock(current);
119 /* push / pop to root of obd store */
120 void push_ctxt(struct lvfs_run_ctxt *save, struct lvfs_run_ctxt *new_ctx,
121 struct lvfs_ucred *uc)
123 //ASSERT_NOT_KERNEL_CTXT("already in kernel context!\n");
124 ASSERT_CTXT_MAGIC(new_ctx->magic);
125 OBD_SET_CTXT_MAGIC(save);
128 LASSERT(cfs_atomic_read(&cfs_fs_pwd(current->fs)->d_count));
129 LASSERT(cfs_atomic_read(&new_ctx->pwd->d_count));
130 save->pwd = dget(cfs_fs_pwd(current->fs));
131 save->pwdmnt = mntget(cfs_fs_mnt(current->fs));
132 save->luc.luc_umask = cfs_curproc_umask();
133 save->ngroups = current_cred()->group_info->ngroups;
136 LASSERT(save->pwdmnt);
137 LASSERT(new_ctx->pwd);
138 LASSERT(new_ctx->pwdmnt);
142 save->luc.luc_uid = current_uid();
143 save->luc.luc_gid = current_gid();
144 save->luc.luc_fsuid = current_fsuid();
145 save->luc.luc_fsgid = current_fsgid();
146 save->luc.luc_cap = current_cap();
148 if ((cred = prepare_creds())) {
149 cred->uid = uc->luc_uid;
150 cred->gid = uc->luc_gid;
151 cred->fsuid = uc->luc_fsuid;
152 cred->fsgid = uc->luc_fsgid;
153 cred->cap_effective = uc->luc_cap;
157 push_group_info(save,
159 uc->luc_identity ? uc->luc_identity->mi_ginfo :
162 current->fs->umask = 0; /* umask already applied on client */
164 ll_set_fs_pwd(current->fs, new_ctx->pwdmnt, new_ctx->pwd);
166 EXPORT_SYMBOL(push_ctxt);
168 void pop_ctxt(struct lvfs_run_ctxt *saved, struct lvfs_run_ctxt *new_ctx,
169 struct lvfs_ucred *uc)
171 ASSERT_CTXT_MAGIC(saved->magic);
172 ASSERT_KERNEL_CTXT("popping non-kernel context!\n");
174 LASSERTF(cfs_fs_pwd(current->fs) == new_ctx->pwd, "%p != %p\n",
175 cfs_fs_pwd(current->fs), new_ctx->pwd);
176 LASSERTF(cfs_fs_mnt(current->fs) == new_ctx->pwdmnt, "%p != %p\n",
177 cfs_fs_mnt(current->fs), new_ctx->pwdmnt);
180 ll_set_fs_pwd(current->fs, saved->pwdmnt, saved->pwd);
183 mntput(saved->pwdmnt);
184 current->fs->umask = saved->luc.luc_umask;
187 if ((cred = prepare_creds())) {
188 cred->uid = saved->luc.luc_uid;
189 cred->gid = saved->luc.luc_gid;
190 cred->fsuid = saved->luc.luc_fsuid;
191 cred->fsgid = saved->luc.luc_fsgid;
192 cred->cap_effective = saved->luc.luc_cap;
196 pop_group_info(saved,
198 uc->luc_identity ? uc->luc_identity->mi_ginfo :
202 EXPORT_SYMBOL(pop_ctxt);
204 /* utility to make a file */
205 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode, int fix)
207 struct dentry *dchild;
211 // ASSERT_KERNEL_CTXT("kernel doing mknod outside kernel context\n");
212 CDEBUG(D_INODE, "creating file %.*s\n", (int)strlen(name), name);
214 dchild = ll_lookup_one_len(name, dir, strlen(name));
216 GOTO(out_up, dchild);
218 if (dchild->d_inode) {
219 int old_mode = dchild->d_inode->i_mode;
220 if (!S_ISREG(old_mode))
221 GOTO(out_err, err = -EEXIST);
223 /* Fixup file permissions if necessary */
224 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
225 CWARN("fixing permissions on %s from %o to %o\n",
226 name, old_mode, mode);
227 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
228 (old_mode & ~S_IALLUGO);
229 mark_inode_dirty(dchild->d_inode);
231 GOTO(out_up, dchild);
234 err = ll_vfs_create(dir->d_inode, dchild, (mode & ~S_IFMT) | S_IFREG,
243 dchild = ERR_PTR(err);
247 EXPORT_SYMBOL(simple_mknod);
249 /* utility to make a directory */
250 struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
251 const char *name, int mode, int fix)
253 struct dentry *dchild;
257 // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
258 CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
259 dchild = ll_lookup_one_len(name, dir, strlen(name));
261 GOTO(out_up, dchild);
263 if (dchild->d_inode) {
264 int old_mode = dchild->d_inode->i_mode;
265 if (!S_ISDIR(old_mode)) {
266 CERROR("found %s (%lu/%u) is mode %o\n", name,
267 dchild->d_inode->i_ino,
268 dchild->d_inode->i_generation, old_mode);
269 GOTO(out_err, err = -ENOTDIR);
272 /* Fixup directory permissions if necessary */
273 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
275 "fixing permissions on %s from %o to %o\n",
276 name, old_mode, mode);
277 dchild->d_inode->i_mode = (mode & S_IALLUGO) |
278 (old_mode & ~S_IALLUGO);
279 mark_inode_dirty(dchild->d_inode);
281 GOTO(out_up, dchild);
284 err = ll_vfs_mkdir(dir->d_inode, dchild, mnt, mode);
292 dchild = ERR_PTR(err);
296 EXPORT_SYMBOL(simple_mkdir);
298 /* utility to rename a file */
299 int lustre_rename(struct dentry *dir, struct vfsmount *mnt,
300 char *oldname, char *newname)
302 struct dentry *dchild_old, *dchild_new;
306 ASSERT_KERNEL_CTXT("kernel doing rename outside kernel context\n");
307 CDEBUG(D_INODE, "renaming file %.*s to %.*s\n",
308 (int)strlen(oldname), oldname, (int)strlen(newname), newname);
310 dchild_old = ll_lookup_one_len(oldname, dir, strlen(oldname));
311 if (IS_ERR(dchild_old))
312 RETURN(PTR_ERR(dchild_old));
314 if (!dchild_old->d_inode)
315 GOTO(put_old, err = -ENOENT);
317 dchild_new = ll_lookup_one_len(newname, dir, strlen(newname));
318 if (IS_ERR(dchild_new))
319 GOTO(put_old, err = PTR_ERR(dchild_new));
321 err = ll_vfs_rename(dir->d_inode, dchild_old, mnt,
322 dir->d_inode, dchild_new, mnt);
329 EXPORT_SYMBOL(lustre_rename);
332 * Read a file from within kernel context. Prior to calling this
333 * function we should already have done a push_ctxt().
335 int lustre_fread(struct file *file, void *buf, int len, loff_t *off)
337 ASSERT_KERNEL_CTXT("kernel doing read outside kernel context\n");
338 if (!file || !file->f_op || !file->f_op->read || !off)
341 return file->f_op->read(file, buf, len, off);
343 EXPORT_SYMBOL(lustre_fread);
346 * Write a file from within kernel context. Prior to calling this
347 * function we should already have done a push_ctxt().
349 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off)
352 ASSERT_KERNEL_CTXT("kernel doing write outside kernel context\n");
360 if (!file->f_op->write)
363 RETURN(file->f_op->write(file, buf, len, off));
365 EXPORT_SYMBOL(lustre_fwrite);
368 * Sync a file from within kernel context. Prior to calling this
369 * function we should already have done a push_ctxt().
371 int lustre_fsync(struct file *file)
374 ASSERT_KERNEL_CTXT("kernel doing sync outside kernel context\n");
375 if (!file || !file->f_op || !file->f_op->fsync)
378 RETURN(cfs_do_fsync(file, 0));
380 EXPORT_SYMBOL(lustre_fsync);
382 /* Note: dput(dchild) will be called if there is an error */
383 struct l_file *l_dentry_open(struct lvfs_run_ctxt *ctxt, struct l_dentry *de,
386 mntget(ctxt->pwdmnt);
387 return ll_dentry_open(de, ctxt->pwdmnt, flags, current_cred());
389 EXPORT_SYMBOL(l_dentry_open);
391 static int l_filldir(void *__buf, const char *name, int namlen, loff_t offset,
392 u64 ino, unsigned int d_type)
394 struct l_linux_dirent *dirent;
395 struct l_readdir_callback *buf = (struct l_readdir_callback *)__buf;
397 dirent = buf->lrc_dirent;
399 dirent->lld_off = offset;
401 OBD_ALLOC(dirent, sizeof(*dirent));
406 cfs_list_add_tail(&dirent->lld_list, buf->lrc_list);
408 buf->lrc_dirent = dirent;
409 dirent->lld_ino = ino;
410 LASSERT(sizeof(dirent->lld_name) >= namlen + 1);
411 memcpy(dirent->lld_name, name, namlen);
416 long l_readdir(struct file *file, cfs_list_t *dentry_list)
418 struct l_linux_dirent *lastdirent;
419 struct l_readdir_callback buf;
422 buf.lrc_dirent = NULL;
423 buf.lrc_list = dentry_list;
425 error = vfs_readdir(file, l_filldir, &buf);
429 lastdirent = buf.lrc_dirent;
431 lastdirent->lld_off = file->f_pos;
435 EXPORT_SYMBOL(l_readdir);
437 int l_notify_change(struct vfsmount *mnt, struct dentry *dchild,
438 struct iattr *newattrs)
442 LOCK_INODE_MUTEX(dchild->d_inode);
443 #ifdef HAVE_SECURITY_PLUG
444 rc = notify_change(dchild, mnt, newattrs);
446 rc = notify_change(dchild, newattrs);
448 UNLOCK_INODE_MUTEX(dchild->d_inode);
451 EXPORT_SYMBOL(l_notify_change);
453 /* utility to truncate a file */
454 int simple_truncate(struct dentry *dir, struct vfsmount *mnt,
455 char *name, loff_t length)
457 struct dentry *dchild;
458 struct iattr newattrs;
462 CDEBUG(D_INODE, "truncating file %.*s to %lld\n", (int)strlen(name),
463 name, (long long)length);
464 dchild = ll_lookup_one_len(name, dir, strlen(name));
466 GOTO(out, err = PTR_ERR(dchild));
468 if (dchild->d_inode) {
469 int old_mode = dchild->d_inode->i_mode;
470 if (S_ISDIR(old_mode)) {
471 CERROR("found %s (%lu/%u) is mode %o\n", name,
472 dchild->d_inode->i_ino,
473 dchild->d_inode->i_generation, old_mode);
474 GOTO(out_dput, err = -EISDIR);
477 newattrs.ia_size = length;
478 newattrs.ia_valid = ATTR_SIZE;
479 err = l_notify_change(mnt, dchild, &newattrs);
487 EXPORT_SYMBOL(simple_truncate);
489 int __lvfs_set_rdonly(lvfs_sbdev_type dev, lvfs_sbdev_type jdev)
491 #ifdef HAVE_DEV_SET_RDONLY
492 if (jdev && (jdev != dev)) {
493 CDEBUG(D_IOCTL | D_HA, "set journal dev %lx rdonly\n",
495 dev_set_rdonly(jdev);
497 CDEBUG(D_IOCTL | D_HA, "set dev %lx rdonly\n", (long)dev);
502 CERROR("DEV %lx CANNOT BE SET READONLY\n", (long)dev);
507 EXPORT_SYMBOL(__lvfs_set_rdonly);
509 int lvfs_check_rdonly(lvfs_sbdev_type dev)
511 #ifdef HAVE_DEV_SET_RDONLY
512 return dev_check_rdonly(dev);
517 EXPORT_SYMBOL(lvfs_check_rdonly);
519 int lvfs_check_io_health(struct obd_device *obd, struct file *file)
521 char *write_page = NULL;
526 OBD_ALLOC(write_page, CFS_PAGE_SIZE);
530 rc = fsfilt_write_record(obd, file, write_page, CFS_PAGE_SIZE, &offset, 1);
532 OBD_FREE(write_page, CFS_PAGE_SIZE);
534 CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
537 EXPORT_SYMBOL(lvfs_check_io_health);
539 void obd_update_maxusage()
543 max1 = obd_pages_sum();
544 max2 = obd_memory_sum();
546 cfs_spin_lock(&obd_updatemax_lock);
547 if (max1 > obd_max_pages)
548 obd_max_pages = max1;
549 if (max2 > obd_max_alloc)
550 obd_max_alloc = max2;
551 cfs_spin_unlock(&obd_updatemax_lock);
555 __u64 obd_memory_max(void)
559 cfs_spin_lock(&obd_updatemax_lock);
561 cfs_spin_unlock(&obd_updatemax_lock);
566 __u64 obd_pages_max(void)
570 cfs_spin_lock(&obd_updatemax_lock);
572 cfs_spin_unlock(&obd_updatemax_lock);
577 EXPORT_SYMBOL(obd_update_maxusage);
578 EXPORT_SYMBOL(obd_pages_max);
579 EXPORT_SYMBOL(obd_memory_max);
580 EXPORT_SYMBOL(obd_memory);
583 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
584 enum lprocfs_fields_flags field)
592 centry = cfs_atomic_read(&lc->lc_cntl.la_entry);
595 case LPROCFS_FIELDS_FLAGS_CONFIG:
598 case LPROCFS_FIELDS_FLAGS_SUM:
599 ret = lc->lc_sum + lc->lc_sum_irq;
601 case LPROCFS_FIELDS_FLAGS_MIN:
604 case LPROCFS_FIELDS_FLAGS_MAX:
607 case LPROCFS_FIELDS_FLAGS_AVG:
608 ret = (lc->lc_max - lc->lc_min)/2;
610 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
611 ret = lc->lc_sumsquare;
613 case LPROCFS_FIELDS_FLAGS_COUNT:
619 } while (centry != cfs_atomic_read(&lc->lc_cntl.la_entry) &&
620 centry != cfs_atomic_read(&lc->lc_cntl.la_exit));
624 EXPORT_SYMBOL(lprocfs_read_helper);
627 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
628 MODULE_DESCRIPTION("Lustre VFS Filesystem Helper v0.1");
629 MODULE_LICENSE("GPL");