1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre filesystem abstraction routines
7 * Copyright (C) 2004 Cluster File Systems, Inc.
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DEBUG_SUBSYSTEM S_SM
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/string.h>
30 #include <linux/slab.h>
31 #include <linux/stat.h>
32 #include <linux/unistd.h>
33 #include <linux/pagemap.h>
34 #include <linux/file.h>
36 #include <linux/obd_class.h>
37 #include <linux/obd_support.h>
38 #include <linux/lustre_lib.h>
39 #include <linux/lustre_idl.h>
40 #include <linux/lustre_fsfilt.h>
41 #include <linux/lustre_smfs.h>
42 #include <linux/lustre_snap.h>
44 #include "smfs_internal.h"
46 static ssize_t smfs_write(struct file *filp, const char *buf, size_t count,
49 struct inode *cache_inode = I2CI(filp->f_dentry->d_inode);
50 struct smfs_file_info *sfi;
51 loff_t *cache_ppos = NULL;
53 struct hook_write_msg msg = {
54 .dentry = filp->f_dentry,
62 LASSERT(cache_inode->i_fop->write);
65 LASSERT(sfi->magic == SMFS_FILE_MAGIC);
67 if (filp->f_flags & O_APPEND)
68 msg.pos = filp->f_dentry->d_inode->i_size;
70 pre_smfs_file(filp, &msg.pos, &cache_ppos);
71 SMFS_PRE_HOOK(filp->f_dentry->d_inode, HOOK_WRITE, &msg);
73 rc = cache_inode->i_fop->write(sfi->c_file, buf, count,
76 SMFS_POST_HOOK(filp->f_dentry->d_inode, HOOK_WRITE, &msg, rc);
83 int smfs_ioctl(struct inode * inode, struct file * filp,
84 unsigned int cmd, unsigned long arg)
86 struct inode *cache_inode = I2CI(filp->f_dentry->d_inode);
87 struct smfs_file_info *sfi;
93 LASSERT(cache_inode->i_fop->ioctl);
96 if (sfi->magic != SMFS_FILE_MAGIC)
99 pre_smfs_inode(inode, cache_inode);
101 rc = cache_inode->i_fop->ioctl(cache_inode, sfi->c_file, cmd, arg);
103 post_smfs_inode(inode, cache_inode);
104 duplicate_file(filp, sfi->c_file);
109 static ssize_t smfs_read(struct file *filp, char *buf,
110 size_t count, loff_t *ppos)
112 struct inode *cache_inode = I2CI(filp->f_dentry->d_inode);
113 struct smfs_file_info *sfi;
114 loff_t *cache_ppos = NULL;
119 LASSERT(cache_inode);
120 LASSERT(cache_inode->i_fop->read);
123 if (sfi->magic != SMFS_FILE_MAGIC)
126 pre_smfs_file(filp, ppos, &cache_ppos);
127 rc = cache_inode->i_fop->read(sfi->c_file, buf, count, cache_ppos);
128 post_smfs_file(filp);
133 static loff_t smfs_llseek(struct file *file, loff_t offset, int origin)
135 struct inode *cache_inode = I2CI(file->f_dentry->d_inode);
136 struct smfs_file_info *sfi;
141 LASSERT(cache_inode);
142 LASSERT(cache_inode->i_fop->llseek);
145 if (sfi->magic != SMFS_FILE_MAGIC)
148 pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
150 rc = cache_inode->i_fop->llseek(sfi->c_file, offset, origin);
152 post_smfs_inode(file->f_dentry->d_inode, cache_inode);
153 duplicate_file(file, sfi->c_file);
158 static int smfs_mmap(struct file *file, struct vm_area_struct *vma)
160 struct inode *inode = file->f_dentry->d_inode;
161 struct smfs_file_info *sfi;
162 struct inode *cache_inode = I2CI(inode);
166 LASSERT(cache_inode);
169 if (sfi->magic != SMFS_FILE_MAGIC)
172 if (cache_inode->i_mapping == &cache_inode->i_data)
173 inode->i_mapping = cache_inode->i_mapping;
175 pre_smfs_inode(inode, cache_inode);
177 rc = cache_inode->i_fop->mmap(sfi->c_file, vma);
179 post_smfs_inode(inode, cache_inode);
180 duplicate_file(file, sfi->c_file);
185 static int smfs_init_cache_file(struct inode *inode, struct file *filp)
187 struct smfs_file_info *sfi = NULL;
188 struct file *cache_filp = NULL;
189 struct dentry *cache_dentry = NULL;
193 OBD_ALLOC(sfi, sizeof(struct smfs_file_info));
197 cache_filp = get_empty_filp();
203 sfi->magic = SMFS_FILE_MAGIC;
205 cache_dentry = pre_smfs_dentry(NULL, I2CI(inode), filp->f_dentry);
211 cache_filp->f_vfsmnt = filp->f_vfsmnt;
212 cache_filp->f_dentry = cache_dentry;
213 duplicate_file(cache_filp, filp);
214 sfi->c_file = cache_filp;
216 if (filp->private_data != NULL)
219 filp->private_data = sfi;
224 OBD_FREE(sfi, sizeof(struct smfs_file_info));
226 put_filp(cache_filp);
230 static int smfs_cleanup_cache_file(struct file *filp)
232 struct smfs_file_info *sfi = NULL;
241 post_smfs_dentry(sfi->c_file->f_dentry);
243 put_filp(sfi->c_file);
245 OBD_FREE(sfi, sizeof(struct smfs_file_info));
247 filp->private_data = NULL;
252 int smfs_open(struct inode *inode, struct file *filp)
254 struct inode *cache_inode = I2CI(inode);
259 LASSERT(cache_inode);
261 if ((rc = smfs_init_cache_file(inode, filp)))
263 //it possible that backstore fs has no open(),
264 //but we need it to init cache filp
265 if (cache_inode->i_fop->open) {
266 rc = cache_inode->i_fop->open(cache_inode, F2CF(filp));
267 duplicate_file(filp, F2CF(filp));
273 int smfs_release(struct inode *inode, struct file *filp)
275 struct inode *cache_inode = I2CI(inode);
276 struct file *cache_file = NULL;
277 struct smfs_file_info *sfi = NULL;
282 LASSERT(cache_inode);
286 if (sfi->magic != SMFS_FILE_MAGIC)
288 cache_file = sfi->c_file;
291 if (cache_inode->i_fop->release)
292 rc = cache_inode->i_fop->release(cache_inode, cache_file);
294 post_smfs_inode(inode, cache_inode);
296 smfs_cleanup_cache_file(filp);
301 int smfs_fsync(struct file *file, struct dentry *dentry, int datasync)
303 struct smfs_file_info *sfi = NULL;
304 struct dentry *cache_dentry = NULL;
305 struct file *cache_file = NULL;
306 struct inode *cache_inode = I2CI(dentry->d_inode);
311 LASSERT(cache_inode);
312 LASSERT(cache_inode->i_fop->fsync);
314 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
320 if (sfi->magic != SMFS_FILE_MAGIC)
322 cache_file = sfi->c_file;
325 pre_smfs_inode(dentry->d_inode, cache_inode);
327 rc = cache_inode->i_fop->fsync(cache_file,
328 cache_dentry, datasync);
330 post_smfs_inode(dentry->d_inode, cache_inode);
331 duplicate_file(file, cache_file);
332 post_smfs_dentry(cache_dentry);
337 struct file_operations smfs_file_fops = {
344 release: smfs_release,
348 static void smfs_truncate(struct inode *inode)
350 struct inode *cache_inode = I2CI(inode);
352 if (!cache_inode || !cache_inode->i_op->truncate)
355 pre_smfs_inode(inode, cache_inode);
357 cache_inode->i_op->truncate(cache_inode);
359 post_smfs_inode(inode, cache_inode);
364 int smfs_setattr(struct dentry *dentry, struct iattr *attr)
366 struct inode *cache_inode = I2CI(dentry->d_inode);
367 struct dentry *cache_dentry;
370 struct hook_attr_msg msg = {
377 LASSERT(cache_inode);
378 LASSERT(cache_inode->i_op->setattr);
380 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
384 handle = smfs_trans_start(dentry->d_inode, FSFILT_OP_SETATTR, NULL);
385 if (IS_ERR(handle) ) {
390 pre_smfs_inode(dentry->d_inode, cache_inode);
392 SMFS_PRE_HOOK(dentry->d_inode, HOOK_SETATTR, &msg);
394 rc = cache_inode->i_op->setattr(cache_dentry, attr);
396 SMFS_POST_HOOK(dentry->d_inode, HOOK_SETATTR, &msg, rc);
398 post_smfs_inode(dentry->d_inode, cache_inode);
399 smfs_trans_commit(dentry->d_inode, handle, 0);
401 post_smfs_dentry(cache_dentry);
405 int smfs_setxattr(struct dentry *dentry, const char *name,
406 const void *value, size_t size, int flags)
408 struct inode *cache_inode = I2CI(dentry->d_inode);
409 struct dentry *cache_dentry = NULL;
410 struct hook_xattr_msg msg = {
417 LASSERT(cache_inode);
418 LASSERT(cache_inode->i_op->setxattr);
420 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
424 pre_smfs_inode(dentry->d_inode, cache_inode);
425 SMFS_PRE_HOOK(dentry->d_inode, HOOK_SETXATTR, &msg);
427 rc = cache_inode->i_op->setxattr(cache_dentry, name, value,
430 SMFS_POST_HOOK(dentry->d_inode, HOOK_SETXATTR, &msg, rc);
432 post_smfs_inode(dentry->d_inode, cache_inode);
433 post_smfs_dentry(cache_dentry);
438 int smfs_getxattr(struct dentry *dentry, const char *name,
439 void *buffer, size_t size)
441 struct inode *cache_inode = I2CI(dentry->d_inode);
442 struct dentry *cache_dentry;
443 struct hook_xattr_msg msg = {
450 LASSERT(cache_inode);
451 LASSERT(cache_inode->i_op->getxattr);
453 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
457 pre_smfs_inode(dentry->d_inode, cache_inode);
458 SMFS_PRE_HOOK(dentry->d_inode, HOOK_GETXATTR, &msg);
459 rc = cache_inode->i_op->getxattr(cache_dentry, name, buffer,
461 SMFS_POST_HOOK(dentry->d_inode, HOOK_GETXATTR, &msg, rc);
462 post_smfs_inode(dentry->d_inode, cache_inode);
463 post_smfs_dentry(cache_dentry);
468 ssize_t smfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
470 struct inode *cache_inode = I2CI(dentry->d_inode);
471 struct dentry *cache_dentry;
473 struct hook_xattr_msg msg = {
479 LASSERT(cache_inode);
480 LASSERT(cache_inode->i_op->listxattr);
482 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
486 pre_smfs_inode(dentry->d_inode, cache_inode);
487 SMFS_PRE_HOOK(dentry->d_inode, HOOK_LISTXATTR, &msg);
489 rc = cache_inode->i_op->listxattr(cache_dentry, buffer, size);
491 SMFS_POST_HOOK(dentry->d_inode, HOOK_LISTXATTR, &msg, rc);
492 post_smfs_inode(dentry->d_inode, cache_inode);
493 post_smfs_dentry(cache_dentry);
498 int smfs_removexattr(struct dentry *dentry, const char *name)
500 struct inode *cache_inode = I2CI(dentry->d_inode);
501 struct dentry *cache_dentry;
503 struct hook_xattr_msg msg = {
508 LASSERT(cache_inode);
509 LASSERT(cache_inode->i_op->removexattr);
511 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
515 pre_smfs_inode(dentry->d_inode, cache_inode);
516 SMFS_PRE_HOOK(dentry->d_inode, HOOK_REMOVEXATTR, &msg);
518 rc = cache_inode->i_op->removexattr(cache_dentry, name);
520 SMFS_POST_HOOK(dentry->d_inode, HOOK_REMOVEXATTR, &msg, rc);
521 post_smfs_inode(dentry->d_inode, cache_inode);
522 post_smfs_dentry(cache_dentry);
527 int smfs_permission(struct inode *inode, int mask, struct nameidata *nd)
529 struct inode *cache_inode = I2CI(inode);
534 LASSERT(cache_inode);
535 LASSERT(cache_inode->i_op->permission);
537 pre_smfs_inode(inode, cache_inode);
539 rc = cache_inode->i_op->permission(cache_inode, mask, nd);
541 post_smfs_inode(inode, cache_inode);
546 struct inode_operations smfs_file_iops = {
547 .truncate = smfs_truncate, /* BKL held */
548 .setattr = smfs_setattr, /* BKL held */
549 .setxattr = smfs_setxattr,
550 .getxattr = smfs_getxattr,
551 .listxattr = smfs_listxattr,
552 .removexattr = smfs_removexattr,
553 .permission = smfs_permission,
556 struct inode_operations smfs_special_iops = {
557 .setattr = smfs_setattr, /* BKL held */
558 .setxattr = smfs_setxattr,
559 .getxattr = smfs_getxattr,
560 .listxattr = smfs_listxattr,
561 .removexattr = smfs_removexattr,
562 .permission = smfs_permission,