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;
80 CWARN("saved %p, replaced with %p\n", *pfop, filp->f_op);
82 CWARN("%p has owner %p\n", *pfop,(*pfop)->owner);
86 static ssize_t ll_special_file_read(struct file *filp, char *buf,
87 size_t count, loff_t *ppos)
89 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
92 if (pfop && *pfop && (*pfop)->read)
93 rc = (*pfop)->read(filp, buf, count, ppos);
98 static ssize_t ll_special_file_write(struct file *filp, const char *buf,
99 size_t count, loff_t *ppos)
101 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
104 if (pfop && *pfop && (*pfop)->write)
105 rc = (*pfop)->write(filp, buf, count, ppos);
110 static int ll_special_file_ioctl(struct inode *inode, struct file *filp,
111 unsigned int cmd, unsigned long arg)
113 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
116 if (pfop && *pfop && (*pfop)->ioctl) {
117 struct file_operations *sfops = filp->f_op;
119 rc = (*pfop)->ioctl(inode, filp, cmd, arg);
120 save_fops(filp, inode, sfops);
125 static loff_t ll_special_file_seek(struct file *filp, loff_t offset, int origin)
127 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
130 if (pfop && *pfop && (*pfop)->llseek)
131 rc = (*pfop)->llseek(filp, offset, origin);
133 rc = default_llseek(filp, offset, origin);
139 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
141 static unsigned int ll_special_file_poll(struct file *filp,
142 struct poll_table_struct *poll_table)
144 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
145 int rc = DEFAULT_POLLMASK;
147 if (pfop && *pfop && (*pfop)->poll)
148 rc = (*pfop)->poll(filp, poll_table);
153 static int ll_special_file_open(struct inode *inode, struct file *filp)
155 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
158 if (pfop && *pfop && (*pfop)->open)
159 rc = (*pfop)->open(inode, filp);
164 static ssize_t ll_special_read(struct file *filp, char *buf, size_t count,
167 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
170 if (pfop && *pfop && (*pfop)->read)
171 rc = (*pfop)->read(filp, buf, count, ppos);
176 static ssize_t ll_special_write(struct file *filp, const char *buf,
177 size_t count, loff_t *ppos)
179 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
182 if (pfop && *pfop && (*pfop)->write)
183 rc = (*pfop)->write(filp, buf, count, ppos);
188 static int ll_special_ioctl(struct inode *inode, struct file *filp,
189 unsigned int cmd, unsigned long arg)
191 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
194 if (pfop && *pfop && (*pfop)->ioctl) {
195 struct file_operations *sfops = filp->f_op;
197 rc = (*pfop)->ioctl(inode, filp, cmd, arg);
198 /* sometimes, file_operations will be changed in ioctl */
199 save_fops(filp, inode, sfops);
205 static int ll_special_mmap(struct file * filp, struct vm_area_struct * vma)
207 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
210 if (pfop && *pfop && (*pfop)->mmap)
211 rc = (*pfop)->mmap(filp, vma);
216 static loff_t ll_special_seek(struct file *filp, loff_t offset, int origin)
218 struct file_operations** pfop = get_save_fops (filp, INODE_OPS);
221 if (pfop && *pfop && (*pfop)->llseek)
222 rc = (*pfop)->llseek(filp, offset, origin);
224 rc = default_llseek(filp, offset, origin);
229 static int ll_special_fsync(struct file *filp, struct dentry *dentry, int data)
231 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
234 if (pfop && *pfop && (*pfop)->fsync)
235 rc = (*pfop)->fsync(filp, dentry, data);
240 static int ll_special_file_fasync(int fd, struct file *filp, int on)
242 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
245 if (pfop && *pfop && (*pfop)->fasync)
246 rc = (*pfop)->fasync(fd, filp, on);
251 static int ll_special_release_internal(struct inode *inode, struct file *filp,
254 struct file_operations **pfop = get_save_fops(filp, mode);
255 struct ll_sb_info *sbi = ll_i2sbi(inode);
260 if ((*pfop)->release)
261 rc = (*pfop)->release(inode, filp);
265 lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
267 err = ll_mdc_close(sbi->ll_mdc_exp, inode, filp);
274 static int ll_special_open(struct inode *inode, struct file *filp)
276 struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
277 struct file_operations *sfops = filp->f_op;
278 struct ptlrpc_request *req;
279 struct lookup_intent *it;
280 int rc = -EINVAL, err;
286 rc = (*pfop)->open(inode, filp);
288 /* sometimes file_operations will be changed in open */
289 save_fops(filp, inode, sfops);
293 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
297 err = ll_local_open(filp, it);
299 CERROR("error opening special file: rc %d", rc);
300 ll_mdc_close(ll_i2sbi(inode)->ll_mdc_exp, inode, filp);
302 if (pfop && *pfop && (*pfop)->release)
303 (*pfop)->release(inode, filp);
308 req = it->d.lustre.it_data;
310 ptlrpc_req_finished(req);
315 static int ll_special_release(struct inode *inode, struct file *filp)
317 return ll_special_release_internal(inode, filp, INODE_OPS);
320 static int ll_special_file_release(struct inode *inode, struct file *filp)
322 return ll_special_release_internal(inode, filp, FILE_OPS);
325 struct inode_operations ll_special_inode_operations = {
326 setattr_raw: ll_setattr_raw,
328 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
329 getattr_it: ll_getattr,
331 revalidate_it: ll_inode_revalidate_it,
335 struct file_operations ll_special_chr_inode_fops = {
337 open: ll_special_open,
340 struct file_operations ll_special_blk_inode_fops = {
342 read: ll_special_read,
343 write: ll_special_write,
344 ioctl: ll_special_ioctl,
345 open: ll_special_open,
346 release: ll_special_release,
347 mmap: ll_special_mmap,
348 llseek: ll_special_seek,
349 fsync: ll_special_fsync,
352 struct file_operations ll_special_fifo_inode_fops = {
354 open: ll_special_open,
357 struct file_operations ll_special_sock_inode_fops = {
359 open: ll_special_open
362 struct file_operations ll_special_chr_file_fops = {
364 llseek: ll_special_file_seek,
365 read: ll_special_file_read,
366 write: ll_special_file_write,
367 poll: ll_special_file_poll,
368 ioctl: ll_special_file_ioctl,
369 open: ll_special_file_open,
370 release: ll_special_file_release,
371 fasync: ll_special_file_fasync,
374 struct file_operations ll_special_fifo_file_fops = {
376 llseek: ll_special_file_seek,
377 read: ll_special_file_read,
378 write: ll_special_file_write,
379 poll: ll_special_file_poll,
380 ioctl: ll_special_file_ioctl,
381 open: ll_special_file_open,
382 release: ll_special_file_release,