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);
66 if (sfi->magic != SMFS_FILE_MAGIC)
69 if (filp->f_flags & O_APPEND)
70 msg.pos = filp->f_dentry->d_inode->i_size;
72 pre_smfs_inode(filp->f_dentry->d_inode, cache_inode);
74 SMFS_PRE_HOOK(filp->f_dentry->d_inode, HOOK_WRITE, &msg);
76 if (ppos != &(filp->f_pos)) {
77 cache_ppos = &msg.pos;
79 cache_ppos = &sfi->c_file->f_pos;
84 rc = cache_inode->i_fop->write(sfi->c_file, buf, count,
87 SMFS_POST_HOOK(filp->f_dentry->d_inode, HOOK_WRITE, &msg, rc);
89 post_smfs_inode(filp->f_dentry->d_inode, cache_inode);
91 duplicate_file(filp, sfi->c_file);
96 int smfs_ioctl(struct inode * inode, struct file * filp,
97 unsigned int cmd, unsigned long arg)
99 struct inode *cache_inode = I2CI(filp->f_dentry->d_inode);
100 struct smfs_file_info *sfi;
105 LASSERT(cache_inode);
106 LASSERT(cache_inode->i_fop->ioctl);
109 if (sfi->magic != SMFS_FILE_MAGIC)
112 pre_smfs_inode(inode, cache_inode);
114 rc = cache_inode->i_fop->ioctl(cache_inode, sfi->c_file, cmd, arg);
116 post_smfs_inode(inode, cache_inode);
117 duplicate_file(filp, sfi->c_file);
122 static ssize_t smfs_read(struct file *filp, char *buf,
123 size_t count, loff_t *ppos)
125 struct inode *cache_inode = I2CI(filp->f_dentry->d_inode);
126 struct smfs_file_info *sfi;
128 loff_t *cache_ppos = NULL;
133 LASSERT(cache_inode);
134 LASSERT(cache_inode->i_fop->read);
137 if (sfi->magic != SMFS_FILE_MAGIC)
140 if (ppos != &(filp->f_pos)) {
141 cache_ppos = &tmp_ppos;
143 cache_ppos = &sfi->c_file->f_pos;
147 pre_smfs_inode(filp->f_dentry->d_inode, cache_inode);
149 rc = cache_inode->i_fop->read(sfi->c_file, buf, count, cache_ppos);
152 post_smfs_inode(filp->f_dentry->d_inode, cache_inode);
153 duplicate_file(filp, sfi->c_file);
158 static loff_t smfs_llseek(struct file *file, loff_t offset, int origin)
160 struct inode *cache_inode = I2CI(file->f_dentry->d_inode);
161 struct smfs_file_info *sfi;
166 LASSERT(cache_inode);
167 LASSERT(cache_inode->i_fop->llseek);
170 if (sfi->magic != SMFS_FILE_MAGIC)
173 pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
175 rc = cache_inode->i_fop->llseek(sfi->c_file, offset, origin);
177 post_smfs_inode(file->f_dentry->d_inode, cache_inode);
178 duplicate_file(file, sfi->c_file);
183 static int smfs_mmap(struct file *file, struct vm_area_struct *vma)
185 struct inode *inode = file->f_dentry->d_inode;
186 struct smfs_file_info *sfi;
187 struct inode *cache_inode = I2CI(inode);
191 LASSERT(cache_inode);
194 if (sfi->magic != SMFS_FILE_MAGIC)
197 if (cache_inode->i_mapping == &cache_inode->i_data)
198 inode->i_mapping = cache_inode->i_mapping;
200 pre_smfs_inode(inode, cache_inode);
202 rc = cache_inode->i_fop->mmap(sfi->c_file, vma);
204 post_smfs_inode(inode, cache_inode);
205 duplicate_file(file, sfi->c_file);
210 static int smfs_init_cache_file(struct inode *inode, struct file *filp)
212 struct smfs_file_info *sfi = NULL;
213 struct file *cache_filp = NULL;
214 struct dentry *cache_dentry = NULL;
218 OBD_ALLOC(sfi, sizeof(struct smfs_file_info));
222 cache_filp = get_empty_filp();
228 sfi->magic = SMFS_FILE_MAGIC;
230 cache_dentry = pre_smfs_dentry(NULL, I2CI(inode), filp->f_dentry);
236 cache_filp->f_vfsmnt = filp->f_vfsmnt;
237 cache_filp->f_dentry = cache_dentry;
238 duplicate_file(cache_filp, filp);
239 sfi->c_file = cache_filp;
241 if (filp->private_data != NULL)
244 filp->private_data = sfi;
249 OBD_FREE(sfi, sizeof(struct smfs_file_info));
251 put_filp(cache_filp);
255 static int smfs_cleanup_cache_file(struct file *filp)
257 struct smfs_file_info *sfi = NULL;
266 post_smfs_dentry(sfi->c_file->f_dentry);
268 put_filp(sfi->c_file);
270 OBD_FREE(sfi, sizeof(struct smfs_file_info));
272 filp->private_data = NULL;
277 int smfs_open(struct inode *inode, struct file *filp)
279 struct inode *cache_inode = I2CI(inode);
284 LASSERT(cache_inode);
286 if ((rc = smfs_init_cache_file(inode, filp)))
288 //it possible that backstore fs has no open(),
289 //but we need it to init cache filp
290 if (cache_inode->i_fop->open) {
291 rc = cache_inode->i_fop->open(cache_inode, F2CF(filp));
292 duplicate_file(filp, F2CF(filp));
298 int smfs_release(struct inode *inode, struct file *filp)
300 struct inode *cache_inode = I2CI(inode);
301 struct file *cache_file = NULL;
302 struct smfs_file_info *sfi = NULL;
307 LASSERT(cache_inode);
311 if (sfi->magic != SMFS_FILE_MAGIC)
313 cache_file = sfi->c_file;
316 if (cache_inode->i_fop->release)
317 rc = cache_inode->i_fop->release(cache_inode, cache_file);
319 post_smfs_inode(inode, cache_inode);
321 smfs_cleanup_cache_file(filp);
326 int smfs_fsync(struct file *file, struct dentry *dentry, int datasync)
328 struct smfs_file_info *sfi = NULL;
329 struct dentry *cache_dentry = NULL;
330 struct file *cache_file = NULL;
331 struct inode *cache_inode = I2CI(dentry->d_inode);
336 LASSERT(cache_inode);
337 LASSERT(cache_inode->i_fop->fsync);
339 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
345 if (sfi->magic != SMFS_FILE_MAGIC)
347 cache_file = sfi->c_file;
350 pre_smfs_inode(dentry->d_inode, cache_inode);
352 rc = cache_inode->i_fop->fsync(cache_file,
353 cache_dentry, datasync);
355 post_smfs_inode(dentry->d_inode, cache_inode);
356 duplicate_file(file, cache_file);
357 post_smfs_dentry(cache_dentry);
362 struct file_operations smfs_file_fops = {
369 release: smfs_release,
373 static void smfs_truncate(struct inode *inode)
375 struct inode *cache_inode = I2CI(inode);
377 if (!cache_inode || !cache_inode->i_op->truncate)
380 pre_smfs_inode(inode, cache_inode);
382 cache_inode->i_op->truncate(cache_inode);
384 post_smfs_inode(inode, cache_inode);
389 int smfs_setattr(struct dentry *dentry, struct iattr *attr)
391 struct inode *cache_inode = I2CI(dentry->d_inode);
392 struct dentry *cache_dentry;
395 struct hook_setattr_msg msg = {
402 LASSERT(cache_inode);
403 LASSERT(cache_inode->i_op->setattr);
405 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
409 handle = smfs_trans_start(dentry->d_inode, FSFILT_OP_SETATTR, NULL);
410 if (IS_ERR(handle) ) {
415 pre_smfs_inode(dentry->d_inode, cache_inode);
417 SMFS_PRE_HOOK(dentry->d_inode, HOOK_SETATTR, &msg);
419 rc = cache_inode->i_op->setattr(cache_dentry, attr);
421 SMFS_POST_HOOK(dentry->d_inode, HOOK_SETATTR, &msg, rc);
423 post_smfs_inode(dentry->d_inode, cache_inode);
424 smfs_trans_commit(dentry->d_inode, handle, 0);
426 post_smfs_dentry(cache_dentry);
430 int smfs_setxattr(struct dentry *dentry, const char *name, const void *value,
431 size_t size, int flags)
433 struct inode *cache_inode = I2CI(dentry->d_inode);
434 struct dentry *cache_dentry = NULL;
439 LASSERT(cache_inode);
440 LASSERT(cache_inode->i_op->setxattr);
442 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
446 pre_smfs_inode(dentry->d_inode, cache_inode);
448 rc = cache_inode->i_op->setxattr(cache_dentry, name, value,
451 post_smfs_inode(dentry->d_inode, cache_inode);
452 post_smfs_dentry(cache_dentry);
457 int smfs_getxattr(struct dentry *dentry, const char *name, void *buffer,
460 struct inode *cache_inode = I2CI(dentry->d_inode);
461 struct dentry *cache_dentry;
466 LASSERT(cache_inode);
467 LASSERT(cache_inode->i_op->getxattr);
469 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
473 pre_smfs_inode(dentry->d_inode, cache_inode);
475 rc = cache_inode->i_op->getxattr(cache_dentry, name, buffer,
478 post_smfs_inode(dentry->d_inode, cache_inode);
479 post_smfs_dentry(cache_dentry);
484 ssize_t smfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
486 struct inode *cache_inode = I2CI(dentry->d_inode);
487 struct dentry *cache_dentry;
492 LASSERT(cache_inode);
493 LASSERT(cache_inode->i_op->listxattr);
495 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
499 pre_smfs_inode(dentry->d_inode, cache_inode);
501 rc = cache_inode->i_op->listxattr(cache_dentry, buffer, size);
503 post_smfs_inode(dentry->d_inode, cache_inode);
504 post_smfs_dentry(cache_dentry);
509 int smfs_removexattr(struct dentry *dentry, const char *name)
511 struct inode *cache_inode = I2CI(dentry->d_inode);
512 struct dentry *cache_dentry;
517 LASSERT(cache_inode);
518 LASSERT(cache_inode->i_op->removexattr);
520 cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
524 pre_smfs_inode(dentry->d_inode, cache_inode);
526 rc = cache_inode->i_op->removexattr(cache_dentry, name);
528 post_smfs_inode(dentry->d_inode, cache_inode);
529 post_smfs_dentry(cache_dentry);
534 int smfs_permission(struct inode *inode, int mask, struct nameidata *nd)
536 struct inode *cache_inode = I2CI(inode);
541 LASSERT(cache_inode);
542 LASSERT(cache_inode->i_op->permission);
544 pre_smfs_inode(inode, cache_inode);
546 rc = cache_inode->i_op->permission(cache_inode, mask, nd);
548 post_smfs_inode(inode, cache_inode);
553 struct inode_operations smfs_file_iops = {
554 .truncate = smfs_truncate, /* BKL held */
555 .setattr = smfs_setattr, /* BKL held */
556 .setxattr = smfs_setxattr,
557 .getxattr = smfs_getxattr,
558 .listxattr = smfs_listxattr,
559 .removexattr = smfs_removexattr,
560 .permission = smfs_permission,
563 struct inode_operations smfs_special_iops = {
564 .setattr = smfs_setattr, /* BKL held */
565 .setxattr = smfs_setxattr,
566 .getxattr = smfs_getxattr,
567 .listxattr = smfs_listxattr,
568 .removexattr = smfs_removexattr,
569 .permission = smfs_permission,