Whamcloud - gitweb
b=7354
[fs/lustre-release.git] / lustre / smfs / file.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/smfs/inode.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
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.
14  *
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.
19  *
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.
23  */
24
25 #define DEBUG_SUBSYSTEM S_SM
26
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>
35 #include <linux/fs.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>
43
44 #include "smfs_internal.h"
45
46 static ssize_t smfs_write(struct file *filp, const char *buf, size_t count,
47                           loff_t *ppos)
48 {
49         struct inode *cache_inode = I2CI(filp->f_dentry->d_inode);
50         struct smfs_file_info *sfi;
51         loff_t *cache_ppos = NULL;
52         int rc = 0;
53         struct hook_write_msg msg = {
54                 .dentry = filp->f_dentry,
55                 .count = count,
56                 .pos = *ppos
57         };
58          
59         ENTRY;
60
61         LASSERT(cache_inode);
62         LASSERT(cache_inode->i_fop->write);
63
64         sfi = F2SMFI(filp);
65         LASSERT(sfi->magic == SMFS_FILE_MAGIC);
66
67         if (filp->f_flags & O_APPEND)
68                 msg.pos = filp->f_dentry->d_inode->i_size;
69         
70         pre_smfs_file(filp, &msg.pos, &cache_ppos);
71         SMFS_PRE_HOOK(filp->f_dentry->d_inode, HOOK_WRITE, &msg);
72
73         rc = cache_inode->i_fop->write(sfi->c_file, buf, count,
74                                        cache_ppos);
75         
76         SMFS_POST_HOOK(filp->f_dentry->d_inode, HOOK_WRITE, &msg, rc);
77         post_smfs_file(filp);
78         *ppos = *cache_ppos;
79         
80         RETURN(rc);
81 }
82
83 int smfs_ioctl(struct inode * inode, struct file * filp,
84                unsigned int cmd, unsigned long arg)
85 {
86         struct  inode *cache_inode = I2CI(filp->f_dentry->d_inode);
87         struct  smfs_file_info *sfi;
88         ssize_t rc = 0;
89
90         ENTRY;
91
92         LASSERT(cache_inode);
93         LASSERT(cache_inode->i_fop->ioctl);
94
95         sfi = F2SMFI(filp);
96         if (sfi->magic != SMFS_FILE_MAGIC) 
97                 LBUG();
98
99         pre_smfs_inode(inode, cache_inode);
100         
101         rc = cache_inode->i_fop->ioctl(cache_inode, sfi->c_file, cmd, arg);
102         
103         post_smfs_inode(inode, cache_inode);
104         duplicate_file(filp, sfi->c_file);
105
106         RETURN(rc);
107 }
108
109 static ssize_t smfs_read(struct file *filp, char *buf,
110                          size_t count, loff_t *ppos)
111 {
112         struct  inode *cache_inode = I2CI(filp->f_dentry->d_inode);
113         struct  smfs_file_info *sfi;
114         loff_t  tmp_ppos;
115         loff_t  *cache_ppos = NULL;
116         ssize_t rc = 0;
117
118         ENTRY;
119
120         LASSERT(cache_inode);
121         LASSERT(cache_inode->i_fop->read);
122
123         sfi = F2SMFI(filp);
124         if (sfi->magic != SMFS_FILE_MAGIC) 
125                 LBUG();
126
127         pre_smfs_file(filp, ppos, &cache_ppos);
128         rc = cache_inode->i_fop->read(sfi->c_file, buf, count, cache_ppos);
129         post_smfs_file(filp);
130         
131         RETURN(rc);
132 }
133
134 static loff_t smfs_llseek(struct file *file, loff_t offset, int origin)
135 {
136         struct  inode *cache_inode = I2CI(file->f_dentry->d_inode);
137         struct  smfs_file_info *sfi;
138         ssize_t rc = 0;
139
140         ENTRY;
141
142         LASSERT(cache_inode);
143         LASSERT(cache_inode->i_fop->llseek);
144
145         sfi = F2SMFI(file);
146         if (sfi->magic != SMFS_FILE_MAGIC) 
147                 LBUG();
148
149         pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
150
151         rc = cache_inode->i_fop->llseek(sfi->c_file, offset, origin);
152         
153         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
154         duplicate_file(file, sfi->c_file);
155
156         RETURN(rc);
157 }
158
159 static int smfs_mmap(struct file *file, struct vm_area_struct *vma)
160 {
161         struct inode *inode = file->f_dentry->d_inode;
162         struct smfs_file_info *sfi;
163         struct inode *cache_inode = I2CI(inode);
164         int rc = 0;
165         ENTRY;
166
167         LASSERT(cache_inode);
168         
169         sfi = F2SMFI(file);
170         if (sfi->magic != SMFS_FILE_MAGIC)
171                 LBUG();
172
173         if (cache_inode->i_mapping == &cache_inode->i_data)
174                 inode->i_mapping = cache_inode->i_mapping;
175
176         pre_smfs_inode(inode, cache_inode);
177         
178         rc = cache_inode->i_fop->mmap(sfi->c_file, vma);
179
180         post_smfs_inode(inode, cache_inode);
181         duplicate_file(file, sfi->c_file);
182
183         RETURN(rc);
184 }
185
186 static int smfs_init_cache_file(struct inode *inode, struct file *filp)
187 {
188         struct smfs_file_info *sfi = NULL;
189         struct file *cache_filp = NULL;
190         struct dentry *cache_dentry = NULL;
191         int rc = 0;
192         ENTRY;
193
194         OBD_ALLOC(sfi, sizeof(struct smfs_file_info));
195         if (!sfi)
196                 RETURN(-ENOMEM);
197
198         cache_filp = get_empty_filp();
199         if (!cache_filp) {
200                 rc = -ENOMEM;
201                 goto err_exit;
202         }
203
204         sfi->magic = SMFS_FILE_MAGIC;
205
206         cache_dentry = pre_smfs_dentry(NULL, I2CI(inode), filp->f_dentry);
207         if (!cache_dentry) {
208                 rc = -ENOMEM;
209                 goto err_exit;
210         }
211
212         cache_filp->f_vfsmnt = filp->f_vfsmnt;
213         cache_filp->f_dentry = cache_dentry;
214         duplicate_file(cache_filp, filp);
215         sfi->c_file = cache_filp;
216
217         if (filp->private_data != NULL)
218                 LBUG();
219
220         filp->private_data = sfi;
221
222         RETURN(0);
223 err_exit:
224         if (sfi)
225                 OBD_FREE(sfi, sizeof(struct smfs_file_info));
226         if (cache_filp)
227                 put_filp(cache_filp);
228         RETURN(rc);
229 }
230
231 static int smfs_cleanup_cache_file(struct file *filp)
232 {
233         struct smfs_file_info *sfi = NULL;
234                 
235         ENTRY;
236
237         if (!filp)
238                 RETURN(0);
239         
240         sfi = F2SMFI(filp);
241
242         post_smfs_dentry(sfi->c_file->f_dentry);
243
244         put_filp(sfi->c_file);
245
246         OBD_FREE(sfi, sizeof(struct smfs_file_info));
247
248         filp->private_data = NULL;
249
250         RETURN(0);
251 }
252
253 int smfs_open(struct inode *inode, struct file *filp)
254 {
255         struct inode *cache_inode = I2CI(inode);
256         int rc = 0;
257         
258         ENTRY;
259         
260         LASSERT(cache_inode);
261         
262         if ((rc = smfs_init_cache_file(inode, filp)))
263                 RETURN(rc);
264         //it possible that backstore fs has no open(), 
265         //but we need it to init cache filp
266         if (cache_inode->i_fop->open) {
267                 rc = cache_inode->i_fop->open(cache_inode, F2CF(filp));
268                 duplicate_file(filp, F2CF(filp));
269         }
270
271         RETURN(rc);
272 }
273
274 int smfs_release(struct inode *inode, struct file *filp)
275 {
276         struct inode *cache_inode = I2CI(inode);
277         struct file *cache_file = NULL;
278         struct smfs_file_info *sfi = NULL;
279         int rc = 0;
280         
281         ENTRY;
282
283         LASSERT(cache_inode);
284         
285         if (filp) {
286                 sfi = F2SMFI(filp);
287                 if (sfi->magic != SMFS_FILE_MAGIC)
288                         LBUG();
289                 cache_file = sfi->c_file;
290         }
291         
292         if (cache_inode->i_fop->release)
293                 rc = cache_inode->i_fop->release(cache_inode, cache_file);
294
295         post_smfs_inode(inode, cache_inode);
296
297         smfs_cleanup_cache_file(filp);
298         
299         RETURN(rc);
300 }
301
302 int smfs_fsync(struct file *file, struct dentry *dentry, int datasync)
303 {
304         struct smfs_file_info *sfi = NULL;
305         struct dentry *cache_dentry = NULL;
306         struct file *cache_file = NULL;
307         struct inode *cache_inode = I2CI(dentry->d_inode);
308         int rc = 0;
309
310         ENTRY;
311         
312         LASSERT(cache_inode);
313         LASSERT(cache_inode->i_fop->fsync);
314         
315         cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
316         if (!cache_dentry)
317                 RETURN(-ENOMEM);
318
319         if (file) {
320                 sfi = F2SMFI(file);
321                 if (sfi->magic != SMFS_FILE_MAGIC)
322                         LBUG();
323                 cache_file = sfi->c_file;
324         } 
325
326         pre_smfs_inode(dentry->d_inode, cache_inode);
327
328         rc = cache_inode->i_fop->fsync(cache_file,
329                                        cache_dentry, datasync);
330         
331         post_smfs_inode(dentry->d_inode, cache_inode);
332         duplicate_file(file, cache_file);
333         post_smfs_dentry(cache_dentry);
334
335         RETURN(rc);
336 }
337
338 struct file_operations smfs_file_fops = {
339         llseek:         smfs_llseek,
340         read:           smfs_read,
341         write:          smfs_write,
342         ioctl:          smfs_ioctl,
343         mmap:           smfs_mmap,
344         open:           smfs_open,
345         release:        smfs_release,
346         fsync:          smfs_fsync,
347 };
348
349 static void smfs_truncate(struct inode *inode)
350 {
351         struct inode *cache_inode = I2CI(inode);
352
353         if (!cache_inode || !cache_inode->i_op->truncate)
354                 return;
355
356         pre_smfs_inode(inode, cache_inode);
357         
358         cache_inode->i_op->truncate(cache_inode);
359
360         post_smfs_inode(inode, cache_inode);
361
362         return;
363 }
364
365 int smfs_setattr(struct dentry *dentry, struct iattr *attr)
366 {
367         struct inode *cache_inode = I2CI(dentry->d_inode);
368         struct dentry *cache_dentry;
369         void  *handle = NULL;
370         int rc = 0;
371         struct hook_attr_msg msg = {
372                 .dentry = dentry,
373                 .attr = attr,
374         };
375
376         ENTRY;
377         
378         LASSERT(cache_inode);
379         LASSERT(cache_inode->i_op->setattr);
380
381         cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
382         if (!cache_dentry)
383                 RETURN(-ENOMEM);
384
385         handle = smfs_trans_start(dentry->d_inode, FSFILT_OP_SETATTR, NULL);
386         if (IS_ERR(handle) ) {
387                 rc = -ENOSPC;
388                 goto exit;
389         }
390
391         pre_smfs_inode(dentry->d_inode, cache_inode);
392         
393         SMFS_PRE_HOOK(dentry->d_inode, HOOK_SETATTR, &msg); 
394                   
395         rc = cache_inode->i_op->setattr(cache_dentry, attr);
396         
397         SMFS_POST_HOOK(dentry->d_inode, HOOK_SETATTR, &msg, rc);
398         
399         post_smfs_inode(dentry->d_inode, cache_inode);
400         smfs_trans_commit(dentry->d_inode, handle, 0);
401 exit:
402         post_smfs_dentry(cache_dentry);
403         RETURN(rc);
404 }
405
406 int smfs_setxattr(struct dentry *dentry, const char *name,
407                   const void *value, size_t size, int flags)
408 {
409         struct inode *cache_inode = I2CI(dentry->d_inode);
410         struct dentry *cache_dentry = NULL;
411         struct hook_xattr_msg msg = {
412                 .name = (char*)name,
413         };
414         int rc = 0;
415
416         ENTRY;
417         
418         LASSERT(cache_inode);
419         LASSERT(cache_inode->i_op->setxattr);
420         
421         cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
422         if (!cache_dentry)
423                 RETURN(-ENOMEM);
424
425         pre_smfs_inode(dentry->d_inode, cache_inode);
426         SMFS_PRE_HOOK(dentry->d_inode, HOOK_SETXATTR, &msg); 
427
428         rc = cache_inode->i_op->setxattr(cache_dentry, name, value,
429                                          size, flags);
430         
431         SMFS_POST_HOOK(dentry->d_inode, HOOK_SETXATTR, &msg, rc);
432
433         post_smfs_inode(dentry->d_inode, cache_inode);
434         post_smfs_dentry(cache_dentry);
435
436         RETURN(rc);
437 }
438
439 int smfs_getxattr(struct dentry *dentry, const char *name,
440                   void *buffer, size_t size)
441 {
442         struct inode *cache_inode = I2CI(dentry->d_inode);
443         struct dentry *cache_dentry;
444         struct hook_xattr_msg msg = {
445                 .name = (char*)name,
446         };
447         int rc = 0;
448
449         ENTRY;
450         
451         LASSERT(cache_inode);
452         LASSERT(cache_inode->i_op->getxattr);
453         
454         cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
455         if (!cache_dentry)
456                 RETURN(-ENOMEM);
457
458         pre_smfs_inode(dentry->d_inode, cache_inode);
459         SMFS_PRE_HOOK(dentry->d_inode, HOOK_GETXATTR, &msg);
460         rc = cache_inode->i_op->getxattr(cache_dentry, name, buffer,
461                                          size);
462         SMFS_POST_HOOK(dentry->d_inode, HOOK_GETXATTR, &msg, rc);
463         post_smfs_inode(dentry->d_inode, cache_inode);
464         post_smfs_dentry(cache_dentry);
465
466         RETURN(rc);
467 }
468
469 ssize_t smfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
470 {
471         struct inode *cache_inode = I2CI(dentry->d_inode);
472         struct dentry *cache_dentry;
473         int rc = 0;
474         struct hook_xattr_msg msg = {
475                 .name = NULL,
476         };
477         
478         ENTRY;
479         
480         LASSERT(cache_inode);
481         LASSERT(cache_inode->i_op->listxattr);
482
483         cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
484         if (!cache_dentry)
485                 RETURN(-ENOMEM);
486
487         pre_smfs_inode(dentry->d_inode, cache_inode);
488         SMFS_PRE_HOOK(dentry->d_inode, HOOK_LISTXATTR, &msg);
489
490         rc = cache_inode->i_op->listxattr(cache_dentry, buffer, size);
491
492         SMFS_POST_HOOK(dentry->d_inode, HOOK_LISTXATTR, &msg, rc);
493         post_smfs_inode(dentry->d_inode, cache_inode);
494         post_smfs_dentry(cache_dentry);
495
496         RETURN(rc);
497 }
498
499 int smfs_removexattr(struct dentry *dentry, const char *name)
500 {
501         struct inode *cache_inode = I2CI(dentry->d_inode);
502         struct dentry *cache_dentry;
503         int rc = 0;
504         struct hook_xattr_msg msg = {
505                 .name = (char*)name,
506         };
507         ENTRY;
508         
509         LASSERT(cache_inode);
510         LASSERT(cache_inode->i_op->removexattr);
511
512         cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
513         if (!cache_dentry)
514                 RETURN(-ENOMEM);
515
516         pre_smfs_inode(dentry->d_inode, cache_inode);
517         SMFS_PRE_HOOK(dentry->d_inode, HOOK_REMOVEXATTR, &msg);
518
519         rc = cache_inode->i_op->removexattr(cache_dentry, name);
520
521         SMFS_POST_HOOK(dentry->d_inode, HOOK_REMOVEXATTR, &msg, rc);
522         post_smfs_inode(dentry->d_inode, cache_inode);
523         post_smfs_dentry(cache_dentry);
524
525         RETURN(rc);
526 }
527
528 int smfs_permission(struct inode *inode, int mask, struct nameidata *nd)
529 {
530         struct inode *cache_inode = I2CI(inode);
531         int rc = 0;
532
533         ENTRY;
534         
535         LASSERT(cache_inode);
536         LASSERT(cache_inode->i_op->permission);
537
538         pre_smfs_inode(inode, cache_inode);
539
540         rc = cache_inode->i_op->permission(cache_inode, mask, nd);
541
542         post_smfs_inode(inode, cache_inode);
543
544         RETURN(rc);
545 }
546
547 struct inode_operations smfs_file_iops = {
548         .truncate       = smfs_truncate,          /* BKL held */
549         .setattr        = smfs_setattr,           /* BKL held */
550         .setxattr       = smfs_setxattr,
551         .getxattr       = smfs_getxattr,
552         .listxattr      = smfs_listxattr,
553         .removexattr    = smfs_removexattr,
554         .permission     = smfs_permission,
555 };
556
557 struct inode_operations smfs_special_iops = {
558         .setattr        = smfs_setattr,           /* BKL held */
559         .setxattr       = smfs_setxattr,
560         .getxattr       = smfs_getxattr,
561         .listxattr      = smfs_listxattr,
562         .removexattr    = smfs_removexattr,
563         .permission     = smfs_permission,
564 };
565