1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Special file handling for Lustre.
6 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7 * Author: Wang Di <wangdi@clusterfs.com>
8 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DEBUG_SUBSYSTEM S_LLITE
27 #include <linux/lustre_dlm.h>
28 #include <linux/lustre_lite.h>
29 #include <linux/pagemap.h>
30 #include <linux/file.h>
31 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
32 #include <linux/lustre_compat25.h>
35 #include "llite_internal.h"
40 static struct file_operations **get_save_fops(struct file* filp, int mode)
42 struct inode *inode = filp->f_dentry->d_inode;
43 struct ll_inode_info *lli = ll_i2info(inode);
45 if (mode == INODE_OPS) {
46 return &(lli->ll_save_ifop);
47 } else if (mode == FILE_OPS) {
48 if (S_ISFIFO(inode->i_mode)) {
49 switch (filp->f_mode) {
51 return &(lli->ll_save_ffop);
53 return &(lli->ll_save_wfop);
55 return &(lli->ll_save_wrfop);
60 return &(lli->ll_save_ffop);
62 CERROR("invalid special file ops %d\n", mode);
68 static void save_fops(struct file *filp, struct inode *inode,
69 struct file_operations *sfops)
71 if (sfops != filp->f_op) {
72 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
75 if (S_ISCHR(inode->i_mode))
76 filp->f_op = &ll_special_chr_file_fops;
77 else if (S_ISFIFO(inode->i_mode))
78 filp->f_op = &ll_special_fifo_file_fops;
82 static ssize_t ll_special_file_read(struct file *filp, char *buf,
83 size_t count, loff_t *ppos)
85 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
88 if (pfop && *pfop && (*pfop)->read)
89 rc = (*pfop)->read(filp, buf, count, ppos);
94 static ssize_t ll_special_file_write(struct file *filp, const char *buf,
95 size_t count, loff_t *ppos)
97 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
100 if (pfop && *pfop && (*pfop)->write)
101 rc = (*pfop)->write(filp, buf, count, ppos);
106 static int ll_special_file_ioctl(struct inode *inode, struct file *filp,
107 unsigned int cmd, unsigned long arg)
109 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
112 if (pfop && *pfop && (*pfop)->ioctl) {
113 struct file_operations *sfops = filp->f_op;
115 rc = (*pfop)->ioctl(inode, filp, cmd, arg);
116 save_fops(filp, inode, sfops);
121 static loff_t ll_special_file_seek(struct file *filp, loff_t offset, int origin)
123 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
126 if (pfop && *pfop && (*pfop)->llseek)
127 rc = (*pfop)->llseek(filp, offset, origin);
129 rc = default_llseek(filp, offset, origin);
135 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
137 static unsigned int ll_special_file_poll(struct file *filp,
138 struct poll_table_struct *poll_table)
140 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
141 int rc = DEFAULT_POLLMASK;
143 if (pfop && *pfop && (*pfop)->poll)
144 rc = (*pfop)->poll(filp, poll_table);
149 static int ll_special_file_open(struct inode *inode, struct file *filp)
151 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
154 if (pfop && *pfop && (*pfop)->open)
155 rc = (*pfop)->open(inode, filp);
160 static ssize_t ll_special_read(struct file *filp, char *buf, size_t count,
163 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
166 if (pfop && *pfop && (*pfop)->read)
167 rc = (*pfop)->read(filp, buf, count, ppos);
172 static ssize_t ll_special_write(struct file *filp, const char *buf,
173 size_t count, loff_t *ppos)
175 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
178 if (pfop && *pfop && (*pfop)->write)
179 rc = (*pfop)->write(filp, buf, count, ppos);
184 static int ll_special_ioctl(struct inode *inode, struct file *filp,
185 unsigned int cmd, unsigned long arg)
187 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
190 if (pfop && *pfop && (*pfop)->ioctl) {
191 struct file_operations *sfops = filp->f_op;
193 rc = (*pfop)->ioctl(inode, filp, cmd, arg);
195 /* sometimes, file_operations will be changed in ioctl */
196 save_fops(filp, inode, sfops);
202 static int ll_special_mmap(struct file * filp, struct vm_area_struct * vma)
204 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
207 if (pfop && *pfop && (*pfop)->mmap)
208 rc = (*pfop)->mmap(filp, vma);
213 static loff_t ll_special_seek(struct file *filp, loff_t offset, int origin)
215 struct file_operations** pfop = get_save_fops (filp, INODE_OPS);
218 if (pfop && *pfop && (*pfop)->llseek)
219 rc = (*pfop)->llseek(filp, offset, origin);
221 rc = default_llseek(filp, offset, origin);
226 static int ll_special_fsync(struct file *filp, struct dentry *dentry, int data)
228 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
231 if (pfop && *pfop && (*pfop)->fsync)
232 rc = (*pfop)->fsync(filp, dentry, data);
237 static int ll_special_file_fasync(int fd, struct file *filp, int on)
239 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
242 if (pfop && *pfop && (*pfop)->fasync)
243 rc = (*pfop)->fasync(fd, filp, on);
248 static int ll_special_release_internal(struct inode *inode, struct file *filp,
251 struct file_operations **pfop = get_save_fops(filp, mode);
252 struct ll_sb_info *sbi = ll_i2sbi(inode);
257 if ((*pfop)->release)
258 rc = (*pfop)->release(inode, filp);
262 lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
264 err = ll_mdc_close(sbi->ll_mdc_exp, inode, filp);
271 static int ll_special_open(struct inode *inode, struct file *filp)
273 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
274 struct file_operations *sfops = filp->f_op;
275 struct ptlrpc_request *req;
276 struct lookup_intent *it;
277 int rc = -EINVAL, err;
283 rc = (*pfop)->open(inode, filp);
285 /* sometimes file_operations will be changed in open */
286 save_fops(filp, inode, sfops);
290 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
294 err = ll_local_open(filp, it);
296 CERROR("error opening special file: rc %d\n", rc);
297 ll_mdc_close(ll_i2sbi(inode)->ll_mdc_exp, inode, filp);
299 if (pfop && *pfop && (*pfop)->release)
300 (*pfop)->release(inode, filp);
305 req = it->d.lustre.it_data;
307 ptlrpc_req_finished(req);
312 static int ll_special_release(struct inode *inode, struct file *filp)
314 return ll_special_release_internal(inode, filp, INODE_OPS);
317 static int ll_special_file_release(struct inode *inode, struct file *filp)
319 return ll_special_release_internal(inode, filp, FILE_OPS);
322 struct inode_operations ll_special_inode_operations = {
323 .setattr_raw = ll_setattr_raw,
324 .setattr = ll_setattr,
325 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
326 .getattr_it = ll_getattr,
328 .revalidate_it = ll_inode_revalidate_it,
332 struct file_operations ll_special_chr_inode_fops = {
333 .owner = THIS_MODULE,
334 .open = ll_special_open,
337 struct file_operations ll_special_blk_inode_fops = {
338 .owner = THIS_MODULE,
339 .read = ll_special_read,
340 .write = ll_special_write,
341 .ioctl = ll_special_ioctl,
342 .open = ll_special_open,
343 .release = ll_special_release,
344 .mmap = ll_special_mmap,
345 .llseek = ll_special_seek,
346 .fsync = ll_special_fsync,
349 struct file_operations ll_special_fifo_inode_fops = {
350 .owner = THIS_MODULE,
351 .open = ll_special_open,
354 struct file_operations ll_special_sock_inode_fops = {
355 .owner = THIS_MODULE,
356 .open = ll_special_open
359 struct file_operations ll_special_chr_file_fops = {
360 .owner = THIS_MODULE,
361 .llseek = ll_special_file_seek,
362 .read = ll_special_file_read,
363 .write = ll_special_file_write,
364 .poll = ll_special_file_poll,
365 .ioctl = ll_special_file_ioctl,
366 .open = ll_special_file_open,
367 .release = ll_special_file_release,
368 .fasync = ll_special_file_fasync,
371 struct file_operations ll_special_fifo_file_fops = {
372 .owner = THIS_MODULE,
373 .llseek = ll_special_file_seek,
374 .read = ll_special_file_read,
375 .write = ll_special_file_write,
376 .poll = ll_special_file_poll,
377 .ioctl = ll_special_file_ioctl,
378 .open = ll_special_file_open,
379 .release = ll_special_file_release,