Whamcloud - gitweb
Quiet compiler warning.
[fs/lustre-release.git] / lustre / llite / special.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Special file handling for Lustre.
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *   Author: Wang Di <wangdi@clusterfs.com>
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
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.
15  *
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.
20  *
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.
24  */
25
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>
33 #endif
34 #include <asm/poll.h>
35 #include "llite_internal.h"
36
37 #define INODE_OPS 1
38 #define FILE_OPS 2
39
40 static struct file_operations **get_save_fops(struct file* filp, int mode)
41 {
42         struct inode *inode = filp->f_dentry->d_inode;
43         struct ll_inode_info *lli = ll_i2info(inode);
44
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) {
50                         case 1: /*O_RDONLY*/
51                                 return &(lli->ll_save_ffop);
52                         case 2: /*O_WRONLY*/
53                                 return &(lli->ll_save_wfop);
54                         case 3: /* O_RDWR */
55                                 return &(lli->ll_save_wrfop);
56                         default:
57                                 return NULL;
58                         }
59                 }
60                 return &(lli->ll_save_ffop);
61         } else {
62                 CERROR("invalid special file ops %d\n", mode);
63                 LBUG();
64                 return NULL;
65         }
66 }
67
68 static void save_fops(struct file *filp, struct inode *inode,
69                       struct file_operations *sfops)
70 {
71         if (sfops != filp->f_op) {
72                 struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
73
74                 *pfop = filp->f_op;
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;
79         }
80 }
81
82 static ssize_t ll_special_file_read(struct file *filp, char *buf,
83                                     size_t count, loff_t *ppos)
84 {
85         struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
86         int rc = -EINVAL;
87
88         if (pfop && *pfop && (*pfop)->read)
89                 rc = (*pfop)->read(filp, buf, count, ppos);
90
91         RETURN(rc);
92 }
93
94 static ssize_t ll_special_file_write(struct file *filp, const char *buf,
95                                      size_t count, loff_t *ppos)
96 {
97         struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
98         int rc = -EINVAL;
99
100         if (pfop && *pfop && (*pfop)->write)
101                 rc = (*pfop)->write(filp, buf, count, ppos);
102
103         RETURN(rc);
104 }
105
106 static int ll_special_file_ioctl(struct inode *inode, struct file *filp,
107                                  unsigned int cmd, unsigned long arg)
108 {
109         struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
110         int rc = -ENOTTY;
111
112         if (pfop && *pfop && (*pfop)->ioctl) {
113                 struct file_operations *sfops = filp->f_op;
114
115                 rc = (*pfop)->ioctl(inode, filp, cmd, arg);
116                 save_fops(filp, inode, sfops);
117         }
118         RETURN(rc);
119 }
120
121 static loff_t ll_special_file_seek(struct file *filp, loff_t offset, int origin)
122 {
123         struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
124         int rc = 0;
125
126         if (pfop && *pfop && (*pfop)->llseek)
127                 rc = (*pfop)->llseek(filp, offset, origin);
128         else
129                 rc = default_llseek(filp, offset, origin);
130
131         RETURN(rc);
132 }
133
134
135 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
136
137 static unsigned int ll_special_file_poll(struct file *filp,
138                                          struct poll_table_struct *poll_table)
139 {
140         struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
141         int rc = DEFAULT_POLLMASK;
142
143         if (pfop && *pfop && (*pfop)->poll)
144                 rc = (*pfop)->poll(filp, poll_table);
145
146         RETURN(rc);
147 }
148
149 static int ll_special_file_open(struct inode *inode, struct file *filp)
150 {
151         struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
152         int rc = -EINVAL;
153
154         if (pfop && *pfop && (*pfop)->open)
155                 rc = (*pfop)->open(inode, filp);
156
157         RETURN(rc);
158 }
159
160 static ssize_t ll_special_read(struct file *filp, char *buf, size_t count,
161                                loff_t *ppos)
162 {
163         struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
164         int rc = -EINVAL;
165
166         if (pfop && *pfop && (*pfop)->read)
167                 rc = (*pfop)->read(filp, buf, count, ppos);
168
169         RETURN(rc);
170 }
171
172 static ssize_t ll_special_write(struct file *filp, const char *buf,
173                                 size_t count, loff_t *ppos)
174 {
175         struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
176         int rc = -EINVAL;
177
178         if (pfop && *pfop && (*pfop)->write)
179                 rc = (*pfop)->write(filp, buf, count, ppos);
180
181         RETURN(rc);
182 }
183
184 static int ll_special_ioctl(struct inode *inode, struct file *filp,
185                             unsigned int cmd, unsigned long arg)
186 {
187         struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
188         int rc = -ENOTTY;
189
190         if (pfop && *pfop && (*pfop)->ioctl) {
191                 struct file_operations *sfops = filp->f_op;
192
193                 rc = (*pfop)->ioctl(inode, filp, cmd, arg);
194                 /* sometimes, file_operations will be changed in ioctl */
195                 save_fops(filp, inode, sfops);
196         }
197
198         RETURN(rc);
199 }
200
201 static int ll_special_mmap(struct file * filp, struct vm_area_struct * vma)
202 {
203         struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
204         int rc = -ENODEV;
205
206         if (pfop && *pfop && (*pfop)->mmap)
207                 rc = (*pfop)->mmap(filp, vma);
208
209         RETURN(rc);
210 }
211
212 static loff_t ll_special_seek(struct file *filp, loff_t offset, int origin)
213 {
214         struct file_operations** pfop = get_save_fops (filp, INODE_OPS);
215         int    rc = 0;
216
217         if (pfop && *pfop && (*pfop)->llseek)
218                 rc = (*pfop)->llseek(filp, offset, origin);
219         else
220                 rc = default_llseek(filp, offset, origin);
221
222         RETURN(rc);
223 }
224
225 static int ll_special_fsync(struct file *filp, struct dentry *dentry, int data)
226 {
227         struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
228         int rc = -EINVAL;
229
230         if (pfop && *pfop && (*pfop)->fsync)
231                 rc = (*pfop)->fsync(filp, dentry, data);
232
233         RETURN(rc);
234 }
235
236 static int ll_special_file_fasync(int fd, struct file *filp, int on)
237 {
238         struct file_operations **pfop = get_save_fops(filp, FILE_OPS);
239         int rc = -EINVAL;
240
241         if (pfop && *pfop && (*pfop)->fasync)
242                 rc = (*pfop)->fasync(fd, filp, on);
243
244         RETURN(rc);
245 }
246
247 static int ll_special_release_internal(struct inode *inode, struct file *filp,
248                                        int mode)
249 {
250        struct file_operations **pfop = get_save_fops(filp, mode);
251        struct ll_sb_info *sbi = ll_i2sbi(inode);
252        int rc = 0, err;
253        ENTRY;
254
255         if (pfop && *pfop) {
256                 if ((*pfop)->release)
257                         rc = (*pfop)->release(inode, filp);
258                 /* FIXME fops_put */
259         }
260
261         lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
262
263         err = ll_mdc_close(sbi->ll_mdc_exp, inode, filp);
264         if (err && rc == 0)
265                 rc = err;
266
267         RETURN(rc);
268 }
269
270 static int ll_special_open(struct inode *inode, struct file *filp)
271 {
272         struct file_operations **pfop = get_save_fops(filp, INODE_OPS);
273         struct file_operations *sfops = filp->f_op;
274         struct ptlrpc_request *req;
275         struct lookup_intent *it;
276         int rc = -EINVAL, err;
277         ENTRY;
278
279         if (pfop && *pfop) {
280                 /* FIXME fops_get */
281                 if ((*pfop)->open) {
282                         rc = (*pfop)->open(inode, filp);
283
284                         /* sometimes file_operations will be changed in open */
285                         save_fops(filp, inode, sfops);
286                 }
287         }
288
289         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
290
291         it = filp->f_it;
292
293         err = ll_local_open(filp, it);
294         if (rc != 0) {
295                 CERROR("error opening special file: rc %d", rc);
296                 ll_mdc_close(ll_i2sbi(inode)->ll_mdc_exp, inode, filp);
297         } else if (err) {
298                 if (pfop && *pfop && (*pfop)->release)
299                         (*pfop)->release(inode, filp);
300                 /* FIXME fops_put */
301                 rc = err;
302         }
303
304         req = it->d.lustre.it_data;
305         if (req)
306                 ptlrpc_req_finished(req);
307
308         RETURN(rc);
309 }
310
311 static int ll_special_release(struct inode *inode, struct file *filp)
312 {
313         return ll_special_release_internal(inode, filp, INODE_OPS);
314 }
315
316 static int ll_special_file_release(struct inode *inode, struct file *filp)
317 {
318         return ll_special_release_internal(inode, filp, FILE_OPS);
319 }
320
321 struct inode_operations ll_special_inode_operations = {
322         setattr_raw:    ll_setattr_raw,
323         setattr:        ll_setattr,
324 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
325         getattr_it:     ll_getattr,
326 #else
327         revalidate_it:  ll_inode_revalidate_it,
328 #endif
329 };
330
331 struct file_operations ll_special_chr_inode_fops = {
332         owner:          THIS_MODULE,
333         open:           ll_special_open,
334 };
335
336 struct file_operations ll_special_blk_inode_fops = {
337         owner:          THIS_MODULE,
338         read:           ll_special_read,
339         write:          ll_special_write,
340         ioctl:          ll_special_ioctl,
341         open:           ll_special_open,
342         release:        ll_special_release,
343         mmap:           ll_special_mmap,
344         llseek:         ll_special_seek,
345         fsync:          ll_special_fsync,
346 };
347
348 struct file_operations ll_special_fifo_inode_fops = {
349         owner:          THIS_MODULE,
350         open:           ll_special_open,
351 };
352
353 struct file_operations ll_special_sock_inode_fops = {
354         owner:          THIS_MODULE,
355         open:           ll_special_open
356 };
357
358 struct file_operations ll_special_chr_file_fops = {
359         owner:          THIS_MODULE,
360         llseek:         ll_special_file_seek,
361         read:           ll_special_file_read,
362         write:          ll_special_file_write,
363         poll:           ll_special_file_poll,
364         ioctl:          ll_special_file_ioctl,
365         open:           ll_special_file_open,
366         release:        ll_special_file_release,
367         fasync:         ll_special_file_fasync,
368 };
369
370 struct file_operations ll_special_fifo_file_fops = {
371         owner:          THIS_MODULE,
372         llseek:         ll_special_file_seek,
373         read:           ll_special_file_read,
374         write:          ll_special_file_write,
375         poll:           ll_special_file_poll,
376         ioctl:          ll_special_file_ioctl,
377         open:           ll_special_file_open,
378         release:        ll_special_file_release,
379 };