Whamcloud - gitweb
- Fixed some 64bit warnings
[fs/lustre-release.git] / lustre / obdfilter / filter.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/filter/filter.c
5  *
6  * Copyright (C) 2001  Cluster File Systems, Inc.
7  *
8  * This code is issued under the GNU General Public License.
9  * See the file COPYING in this distribution
10  *
11  * by Peter Braam <braam@clusterfs.com>
12  */
13
14 #define EXPORT_SYMTAB
15 #define DEBUG_SUBSYSTEM S_FILTER
16
17 #include <linux/module.h>
18 #include <linux/obd_filter.h>
19
20 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
21 long filter_memory;
22
23 #define FILTER_ROOTINO 2
24
25 #define S_SHIFT 12
26 static char *obd_type_by_mode[S_IFMT >> S_SHIFT] = {
27         [0]                     NULL,
28         [S_IFREG >> S_SHIFT]    "R",
29         [S_IFDIR >> S_SHIFT]    "D",
30         [S_IFCHR >> S_SHIFT]    "C",
31         [S_IFBLK >> S_SHIFT]    "B",
32         [S_IFIFO >> S_SHIFT]    "F",
33         [S_IFSOCK >> S_SHIFT]   "S",
34         [S_IFLNK >> S_SHIFT]    "L"
35 };
36
37 static inline const char *obd_mode_to_type(int mode)
38 {
39         return obd_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
40 }
41
42 /* write the pathname into the string */
43 static int filter_id(char *buf, obd_id id, obd_mode mode)
44 {
45         return sprintf(buf, "O/%s/%Ld", obd_mode_to_type(mode),
46                        (unsigned long long)id);
47 }
48
49 /* setup the object store with correct subdirectories */
50 static int filter_prep(struct obd_device *obddev)
51 {
52         struct obd_run_ctxt saved;
53         struct filter_obd *filter = &obddev->u.filter;
54         struct dentry *dentry;
55         struct file *file;
56         struct inode *inode;
57         loff_t off;
58         int rc = 0;
59         char rootid[128];
60         __u64 lastino = 2;
61         int mode = 0;
62
63         push_ctxt(&saved, &filter->fo_ctxt);
64         dentry = simple_mkdir(current->fs->pwd, "O", 0700);
65         CDEBUG(D_INODE, "got/created O: %p\n", dentry);
66         if (IS_ERR(dentry)) {
67                 rc = PTR_ERR(dentry);
68                 CERROR("cannot open/create O: rc = %d\n", rc);
69                 GOTO(out, rc);
70         }
71         filter->fo_dentry_O = dentry;
72         dentry = simple_mkdir(current->fs->pwd, "P", 0700);
73         CDEBUG(D_INODE, "got/created P: %p\n", dentry);
74         if (IS_ERR(dentry)) {
75                 rc = PTR_ERR(dentry);
76                 CERROR("cannot open/create P: rc = %d\n", rc);
77                 GOTO(out_O, rc);
78         }
79         CDEBUG(D_INODE, "putting P: %p, count = %d\n", dentry,dentry->d_count.counter-1);
80         l_dput(dentry);
81         dentry = simple_mkdir(current->fs->pwd, "D", 0700);
82         CDEBUG(D_INODE, "got/created D: %p\n", dentry);
83         if (IS_ERR(dentry)) {
84                 rc = PTR_ERR(dentry);
85                 CERROR("cannot open/create D: rc = %d\n", rc);
86                 GOTO(out_O, rc);
87         }
88         CDEBUG(D_INODE, "putting D: %p, count = %d\n", dentry,dentry->d_count.counter-1);
89         l_dput(dentry);
90
91         /*
92          * Create directories and/or get dentries for each object type.
93          * This saves us from having to do multiple lookups for each one.
94          */
95         for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
96                 char *type = obd_type_by_mode[mode];
97
98                 if (!type) {
99                         filter->fo_dentry_O_mode[mode] = NULL;
100                         continue;
101                 }
102                 dentry = simple_mkdir(filter->fo_dentry_O, type, 0700);
103                 CDEBUG(D_INODE, "got/created O/%s: %p\n", type, dentry);
104                 if (IS_ERR(dentry)) {
105                         rc = PTR_ERR(dentry);
106                         CERROR("cannot create O/%s: rc = %d\n", type, rc);
107                         GOTO(out_O_mode, rc);
108                 }
109                 filter->fo_dentry_O_mode[mode] = dentry;
110         }
111
112         filter_id(rootid, FILTER_ROOTINO, S_IFDIR);
113         file = filp_open(rootid, O_RDWR | O_CREAT, 0755);
114         if (IS_ERR(file)) {
115                 rc = PTR_ERR(file);
116                 CERROR("OBD filter: cannot open/create root %s: rc = %d\n",
117                        rootid, rc);
118                 GOTO(out_O_mode, rc);
119         }
120         filp_close(file, 0);
121
122         file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
123         if ( !file || IS_ERR(file) ) {
124                 rc = PTR_ERR(file);
125                 CERROR("OBD filter: cannot open/create status %s: rc = %d\n",
126                        "D/status", rc);
127                 GOTO(out_O_mode, rc);
128         }
129
130         /* steal operations */
131         inode = file->f_dentry->d_inode;
132         filter->fo_fop = file->f_op;
133         filter->fo_iop = inode->i_op;
134         filter->fo_aops = inode->i_mapping->a_ops;
135
136         off = 0;
137         if (inode->i_size == 0) {
138                 ssize_t retval = file->f_op->write(file, (char *)&lastino,
139                                                    sizeof(lastino), &off);
140                 if (retval != sizeof(lastino)) {
141                         CDEBUG(D_INODE, "OBD filter: error writing lastino\n");
142                         filp_close(file, 0);
143                         GOTO(out_O_mode, rc = -EIO);
144                 }
145         } else {
146                 ssize_t retval = file->f_op->read(file, (char *)&lastino,
147                                                   sizeof(lastino), &off);
148                 if (retval != sizeof(lastino)) {
149                         CDEBUG(D_INODE, "OBD filter: error reading lastino\n");
150                         filp_close(file, 0);
151                         GOTO(out_O_mode, rc = -EIO);
152                 }
153         }
154         filter->fo_lastino = lastino;
155         filp_close(file, 0);
156
157         rc = 0;
158  out:
159         pop_ctxt(&saved);
160
161         return(rc);
162
163 out_O_mode:
164         while (--mode >= 0) {
165                 if (filter->fo_dentry_O_mode[mode]) {
166                         CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
167                                obd_type_by_mode[mode],
168                                filter->fo_dentry_O_mode[mode],
169                                filter->fo_dentry_O_mode[mode]->d_count.counter-1);
170                         l_dput(filter->fo_dentry_O_mode[mode]);
171                         filter->fo_dentry_O_mode[mode] = NULL;
172                 }
173         }
174 out_O:
175         CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
176                filter->fo_dentry_O->d_count.counter);
177         l_dput(filter->fo_dentry_O);
178         filter->fo_dentry_O = NULL;
179         goto out;
180 }
181
182 /* cleanup the filter: write last used object id to status file */
183 static void filter_post(struct obd_device *obddev)
184 {
185         struct obd_run_ctxt saved;
186         struct filter_obd *filter = &obddev->u.filter;
187         long rc;
188         struct file *file;
189         loff_t off = 0;
190         int mode;
191
192         push_ctxt(&saved, &filter->fo_ctxt);
193         file = filp_open("D/status", O_RDWR | O_CREAT, 0700);
194         if (IS_ERR(file)) {
195                 CERROR("OBD filter: cannot create status file\n");
196                 goto out;
197         }
198         rc = file->f_op->write(file, (char *)&filter->fo_lastino,
199                        sizeof(filter->fo_lastino), &off);
200         if (rc != sizeof(filter->fo_lastino))
201                 CERROR("OBD filter: error writing lastino: rc = %ld\n", rc);
202
203         rc = filp_close(file, NULL);
204         if (rc)
205                 CERROR("OBD filter: cannot close status file: rc = %ld\n", rc);
206
207         for (mode = 0; mode < (S_IFMT >> S_SHIFT); mode++) {
208                 if (filter->fo_dentry_O_mode[mode]) {
209                         CDEBUG(D_INODE, "putting O/%s: %p, count = %d\n",
210                                obd_type_by_mode[mode],
211                                filter->fo_dentry_O_mode[mode],
212                                filter->fo_dentry_O_mode[mode]->d_count.counter-1);
213                         l_dput(filter->fo_dentry_O_mode[mode]);
214                         filter->fo_dentry_O_mode[mode] = NULL;
215                 }
216         }
217         CDEBUG(D_INODE, "putting O: %p, count = %d\n", filter->fo_dentry_O,
218                filter->fo_dentry_O->d_count.counter);
219         l_dput(filter->fo_dentry_O);
220 out:
221         pop_ctxt(&saved);
222 }
223
224
225 static __u64 filter_next_id(struct obd_device *obddev)
226 {
227         __u64 id;
228         spin_lock(&obddev->u.filter.fo_lock);
229         obddev->u.filter.fo_lastino++;
230         id =    obddev->u.filter.fo_lastino;
231         spin_unlock(&obddev->u.filter.fo_lock);
232         return id;
233 }
234
235 /* how to get files, dentries, inodes from object id's */
236 /* parent i_sem is already held if needed for exclusivity */
237 static struct dentry *filter_fid2dentry(struct obd_device *obddev,
238                                         struct dentry *dparent,
239                                         __u64 id, __u32 type)
240 {
241         struct super_block *sb = obddev->u.filter.fo_sb;
242         struct dentry *dchild;
243         char name[32];
244         int len;
245         ENTRY;
246
247         if (!sb || !sb->s_dev) {
248                 CERROR("fatal: device not initialized.\n");
249                 RETURN(ERR_PTR(-ENXIO));
250         }
251
252         if (id == 0) {
253                 CERROR("fatal: invalid object #0\n");
254                 RETURN(ERR_PTR(-ESTALE));
255         }
256
257         if (!(type & S_IFMT)) {
258                 CERROR("OBD %s, object %Lu has bad type: %o\n", __FUNCTION__,
259                        (unsigned long long)id, type);
260                 RETURN(ERR_PTR(-EINVAL));
261         }
262
263         len = sprintf(name, "%Ld", id);
264         CDEBUG(D_INODE, "opening object O/%s/%s\n", obd_mode_to_type(type),
265                name);
266         dchild = lookup_one_len(name, dparent, len);
267         CDEBUG(D_INODE, "got child obj O/%s/%s: %p, count = %d\n",
268                obd_mode_to_type(type), name, dchild, dchild->d_count.counter);
269
270         if (IS_ERR(dchild)) {
271                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
272                 RETURN(dchild);
273         }
274
275         RETURN(dchild);
276 }
277
278 static struct file *filter_obj_open(struct obd_device *obddev,
279                                     __u64 id, __u32 type)
280 {
281         struct super_block *sb = obddev->u.filter.fo_sb;
282         struct obd_run_ctxt saved;
283         char name[24];
284         struct file *file;
285         ENTRY;
286
287         if (!sb || !sb->s_dev) {
288                 CERROR("fatal: device not initialized.\n");
289                 RETURN(ERR_PTR(-ENXIO));
290         }
291
292         if (!id) {
293                 CERROR("fatal: invalid obdo %Lu\n", (unsigned long long)id);
294                 RETURN(ERR_PTR(-ESTALE));
295         }
296
297         if (!(type & S_IFMT)) {
298                 CERROR("OBD %s, no type (%Ld), mode %o!\n", __FUNCTION__,
299                        (unsigned long long)id, type);
300                 RETURN(ERR_PTR(-EINVAL));
301         }
302
303         filter_id(name, id, type);
304         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
305         file = filp_open(name, O_RDONLY | O_LARGEFILE, 0 /* type? */);
306         pop_ctxt(&saved);
307
308         CDEBUG(D_INODE, "opening obdo %s: rc = %p\n", name, file);
309
310         if (IS_ERR(file))
311                 file = NULL;
312         RETURN(file);
313 }
314
315 static struct dentry *filter_parent(struct obd_device *obddev, obd_mode mode)
316 {
317         struct filter_obd *filter = &obddev->u.filter;
318
319         return filter->fo_dentry_O_mode[(mode & S_IFMT) >> S_SHIFT];
320 }
321
322
323 static struct inode *filter_inode_from_obj(struct obd_device *obddev,
324                                            __u64 id, __u32 type)
325 {
326         struct dentry *dentry;
327         struct inode *inode;
328
329         dentry = filter_fid2dentry(obddev, filter_parent(obddev, type),
330                                    id, type);
331         if (IS_ERR(dentry)) {
332                 CERROR("%s: lookup failed: rc = %ld\n", __FUNCTION__,
333                        PTR_ERR(dentry));
334                 RETURN(NULL);
335         }
336
337         lock_kernel();
338         inode = iget(dentry->d_inode->i_sb, dentry->d_inode->i_ino);
339         unlock_kernel();
340         CDEBUG(D_INODE, "put child %p, count = %d\n", dentry, dentry->d_count.counter-1);
341         l_dput(dentry);
342         CDEBUG(D_INODE, "got inode %p (%ld), count = %d\n", inode, inode->i_ino,
343                inode->i_count.counter);
344         return inode;
345 }
346
347 /* obd methods */
348 static int filter_connect(struct obd_conn *conn)
349 {
350         int rc;
351
352         MOD_INC_USE_COUNT;
353         rc = gen_connect(conn);
354
355         if (rc)
356                 MOD_DEC_USE_COUNT;
357
358         return rc;
359 }
360
361 static int filter_disconnect(struct obd_conn *conn)
362 {
363         int rc;
364
365         rc = gen_disconnect(conn);
366         if (!rc)
367                 MOD_DEC_USE_COUNT;
368
369         /* XXX cleanup preallocated inodes */
370         return rc;
371 }
372
373 /* mount the file system (secretly) */
374 static int filter_setup(struct obd_device *obddev, obd_count len, void *buf)
375 {
376         struct obd_ioctl_data* data = buf;
377         struct filter_obd *filter;
378         struct vfsmount *mnt;
379         int err = 0;
380         ENTRY;
381
382         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
383                 RETURN(-EINVAL);
384
385         MOD_INC_USE_COUNT;
386         mnt = do_kern_mount(data->ioc_inlbuf2, 0, data->ioc_inlbuf1, NULL);
387         err = PTR_ERR(mnt);
388         if (IS_ERR(mnt))
389                 GOTO(err_dec, err);
390
391         filter = &obddev->u.filter;;
392         filter->fo_sb = mnt->mnt_root->d_inode->i_sb;
393         /* XXX is this even possible if do_kern_mount succeeded? */
394         if (!filter->fo_sb)
395                 GOTO(err_put, err = -ENODEV);
396
397         filter->fo_vfsmnt = mnt;
398         filter->fo_fstype = strdup(data->ioc_inlbuf2);
399
400         filter->fo_ctxt.pwdmnt = mnt;
401         filter->fo_ctxt.pwd = mnt->mnt_root;
402         filter->fo_ctxt.fs = KERNEL_DS;
403
404         err = filter_prep(obddev);
405         if (err)
406                 GOTO(err_kfree, err);
407         spin_lock_init(&filter->fo_lock);
408
409         RETURN(0);
410
411 err_kfree:
412         kfree(filter->fo_fstype);
413 err_put:
414         unlock_kernel();
415         mntput(filter->fo_vfsmnt);
416         filter->fo_sb = 0;
417         lock_kernel();
418
419 err_dec:
420         MOD_DEC_USE_COUNT;
421         return err;
422 }
423
424
425 static int filter_cleanup(struct obd_device * obddev)
426 {
427         struct super_block *sb;
428         ENTRY;
429
430         if (!(obddev->obd_flags & OBD_SET_UP))
431                 RETURN(0);
432
433         if (!list_empty(&obddev->obd_gen_clients)) {
434                 CERROR("still has clients!\n");
435                 RETURN(-EBUSY);
436         }
437
438         sb = obddev->u.filter.fo_sb;
439         if (!obddev->u.filter.fo_sb)
440                 RETURN(0);
441
442         filter_post(obddev);
443
444         shrink_dcache_parent(sb->s_root);
445         unlock_kernel();
446         CDEBUG(D_INODE, "putting sb->s_root: count = %d\n",
447                sb->s_root->d_count.counter-1);
448         mntput(obddev->u.filter.fo_vfsmnt);
449         obddev->u.filter.fo_sb = 0;
450         kfree(obddev->u.filter.fo_fstype);
451
452         lock_kernel();
453
454         MOD_DEC_USE_COUNT;
455         RETURN(0);
456 }
457
458
459 static inline void filter_from_inode(struct obdo *oa, struct inode *inode)
460 {
461         int type = oa->o_mode & S_IFMT;
462         ENTRY;
463
464         CDEBUG(D_INFO, "src inode %ld, dst obdo %ld valid 0x%08x\n",
465                inode->i_ino, (long)oa->o_id, oa->o_valid);
466         obdo_from_inode(oa, inode);
467         oa->o_mode &= ~S_IFMT;
468         oa->o_mode |= type;
469
470         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
471                 obd_rdev rdev = kdev_t_to_nr(inode->i_rdev);
472                 CDEBUG(D_INODE, "copying device %x from inode to obdo\n",
473                        rdev);
474                 *((obd_rdev *)oa->o_inline) = rdev;
475                 oa->o_obdflags |= OBD_FL_INLINEDATA;
476                 oa->o_valid |= OBD_MD_FLINLINE;
477         }
478
479 #if 0
480         else if (filter_has_inline(inode)) {
481                 CDEBUG(D_INFO, "copying inline from inode to obdo\n");
482                 memcpy(oa->o_inline, inode->u.ext2_i.i_data,
483                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
484                 oa->o_obdflags |= OBD_FL_INLINEDATA;
485                 oa->o_valid |= OBD_MD_FLINLINE;
486         }
487
488         if (filter_has_obdmd(inode)) {
489                 /* XXX this will change when we don't store the obdmd in data */
490                 CDEBUG(D_INFO, "copying obdmd from inode to obdo\n");
491                 memcpy(oa->o_obdmd, inode->u.ext2_i.i_data,
492                        MIN(sizeof(inode->u.ext2_i.i_data),OBD_INLINESZ));
493                 oa->o_obdflags |= OBD_FL_OBDMDEXISTS;
494                 oa->o_valid |= OBD_MD_FLOBDMD;
495         }
496 #endif
497         EXIT;
498 }
499
500 static int filter_getattr(struct obd_conn *conn, struct obdo *oa)
501 {
502         struct inode *inode;
503         ENTRY;
504
505         if (!gen_client(conn)) {
506                 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
507                 RETURN(-EINVAL);
508         }
509
510         inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
511         if (!inode)
512                 RETURN(-ENOENT);
513
514         oa->o_valid &= ~OBD_MD_FLID;
515         filter_from_inode(oa, inode);
516
517         CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n", inode,
518                inode->i_ino, inode->i_count.counter - 1, inode->i_nlink);
519         iput(inode);
520         RETURN(0);
521 }
522
523 static int filter_setattr(struct obd_conn *conn, struct obdo *oa)
524 {
525         struct obd_run_ctxt saved;
526         struct inode *inode;
527         struct iattr iattr;
528         int rc;
529         struct dentry de;
530         ENTRY;
531
532         if (!gen_client(conn)) {
533                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
534                 RETURN(-EINVAL);
535         }
536
537         inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
538         if (!inode)
539                 RETURN(-ENOENT);
540
541         iattr_from_obdo(&iattr, oa);
542         iattr.ia_mode &= ~S_IFMT;
543         iattr.ia_mode |= S_IFREG;
544         de.d_inode = inode;
545         lock_kernel();
546         if (iattr.ia_mode & ATTR_SIZE)
547                 down(&inode->i_sem);
548         push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
549         if (inode->i_op->setattr)
550                 rc = inode->i_op->setattr(&de, &iattr);
551         else
552                 rc = inode_setattr(inode, &iattr);
553         pop_ctxt(&saved);
554         if (iattr.ia_mode & ATTR_SIZE)
555                 up(&inode->i_sem);
556         unlock_kernel();
557
558         CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n", inode,
559                inode->i_ino, inode->i_count.counter - 1, inode->i_nlink);
560         iput(inode);
561         RETURN(rc);
562 }
563
564 static int filter_open(struct obd_conn *conn, struct obdo *oa)
565 {
566         struct inode *inode;
567         /* ENTRY; */
568
569         if (!gen_client(conn)) {
570                 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
571                 RETURN(-EINVAL);
572         }
573
574         inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
575         if (!inode)
576                 RETURN(-ENOENT);
577
578         return 0;
579 } /* filter_open */
580
581 static int filter_close(struct obd_conn *conn, struct obdo *oa)
582 {
583         struct inode *inode;
584         /* ENTRY; */
585
586         if (!gen_client(conn)) {
587                 CDEBUG(D_IOCTL, "fatal: invalid client %u\n", conn->oc_id);
588                 RETURN(-EINVAL);
589         }
590
591         inode = filter_inode_from_obj(conn->oc_dev, oa->o_id, oa->o_mode);
592         if (!inode)
593                 RETURN(-ENOENT);
594
595         CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n", inode,
596                inode->i_ino, inode->i_count.counter - 1, inode->i_nlink);
597         iput(inode);  /* for the close */
598         CDEBUG(D_INODE, "objid #%ld has %d links, %d count after close\n",
599                inode->i_ino, inode->i_nlink, inode->i_count.counter - 1);
600         CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n", inode,
601                inode->i_ino, inode->i_count.counter - 1, inode->i_nlink);
602         iput(inode);  /* for this call */
603         return 0;
604 } /* filter_close */
605
606 static int filter_create(struct obd_conn* conn, struct obdo *oa)
607 {
608         char name[64];
609         struct obd_run_ctxt saved;
610         struct file *file;
611         int mode;
612         struct obd_device *obddev = conn->oc_dev;
613         struct iattr;
614         ENTRY;
615
616         if (!gen_client(conn)) {
617                 CERROR("invalid client %u\n", conn->oc_id);
618                 return -EINVAL;
619         }
620
621         oa->o_id = filter_next_id(conn->oc_dev);
622         if (!(oa->o_mode && S_IFMT)) {
623                 CERROR("filter obd: no type!\n");
624                 return -ENOENT;
625         }
626
627         filter_id(name, oa->o_id, oa->o_mode);
628         mode = (oa->o_mode & ~S_IFMT) | S_IFREG;
629         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
630         file = filp_open(name, O_RDONLY | O_CREAT, mode);
631         pop_ctxt(&saved);
632         if (IS_ERR(file)) {
633                 CERROR("Error mknod obj %s, err %ld\n", name, PTR_ERR(file));
634                 return -ENOENT;
635         }
636         filp_close(file, 0);
637
638         /* Set flags for fields we have set in the inode struct */
639         oa->o_valid |= OBD_MD_FLID | OBD_MD_FLBLKSZ | OBD_MD_FLBLOCKS |
640                  OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME |
641                  OBD_MD_FLUID | OBD_MD_FLGID;
642
643         /* XXX Hmm, shouldn't we copy the fields into the obdo here? */
644         return 0;
645 }
646
647 static int filter_destroy(struct obd_conn *conn, struct obdo *oa)
648 {
649         struct obd_run_ctxt saved;
650         struct obd_device *obddev;
651         struct obd_client *cli;
652         struct inode *inode;
653         struct dentry *dir_dentry, *object_dentry;
654         int rc;
655         ENTRY;
656
657         if (!(cli = gen_client(conn))) {
658                 CERROR("invalid client %u\n", conn->oc_id);
659                 RETURN(-EINVAL);
660         }
661
662         CDEBUG(D_INODE, "destroying object %Ld\n",oa->o_id);
663         obddev = conn->oc_dev;
664
665         dir_dentry = filter_parent(obddev, oa->o_mode);
666         down(&dir_dentry->d_inode->i_sem);
667
668         object_dentry = filter_fid2dentry(obddev, dir_dentry, oa->o_id,
669                                           oa->o_mode);
670         if (IS_ERR(object_dentry))
671                 GOTO(out, rc = -ENOENT);
672
673         inode = object_dentry->d_inode;
674         if (inode->i_nlink != 1) {
675                 CERROR("destroying inode with nlink = %d\n", inode->i_nlink);
676                 inode->i_nlink = 1;
677         }
678         inode->i_mode = 010000;
679
680         push_ctxt(&saved, &obddev->u.filter.fo_ctxt);
681         rc = vfs_unlink(dir_dentry->d_inode, object_dentry);
682         pop_ctxt(&saved);
683         CDEBUG(D_INODE, "put child %p, count = %d\n", object_dentry,
684                object_dentry->d_count.counter-1);
685         l_dput(object_dentry);
686
687         EXIT;
688 out:
689         up(&dir_dentry->d_inode->i_sem);
690         return rc;
691 }
692
693 /* NB count and offset are used for punch, but not truncate */
694 static int filter_truncate(struct obd_conn *conn, struct obdo *oa,
695                            obd_size count, obd_off offset)
696 {
697         int error;
698         ENTRY;
699
700         CDEBUG(D_INODE, "calling truncate for object #%Ld, valid = %x, "
701                "o_size = %Ld\n", oa->o_id, oa->o_valid, oa->o_size);
702         error = filter_setattr(conn, oa);
703         oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLCTIME | OBD_MD_FLMTIME;
704
705         RETURN(error);
706 }
707
708 /* buffer must lie in user memory here */
709 static int filter_read(struct obd_conn *conn, struct obdo *oa, char *buf,
710                         obd_size *count, obd_off offset)
711 {
712         struct file * file;
713         unsigned long retval;
714         int err;
715         ENTRY;
716
717         if (!gen_client(conn)) {
718                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
719                 RETURN(-EINVAL);
720         }
721
722         file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
723         if (IS_ERR(file))
724                 RETURN(PTR_ERR(file));
725
726         /* count doubles as retval */
727         retval = file->f_op->read(file, buf, *count, (loff_t *)&offset);
728         filp_close(file, 0);
729
730         if ( retval >= 0 ) {
731                 err = 0;
732                 *count = retval;
733         } else {
734                 err = retval;
735                 *count = 0;
736         }
737
738         return err;
739 }
740
741
742 /* buffer must lie in user memory here */
743 static int filter_write(struct obd_conn *conn, struct obdo *oa, char *buf,
744                          obd_size *count, obd_off offset)
745 {
746         struct obd_run_ctxt saved;
747         int err;
748         struct file * file;
749         unsigned long retval;
750         ENTRY;
751
752         if (!gen_client(conn)) {
753                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
754                 RETURN(-EINVAL);
755         }
756
757         file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
758         if (IS_ERR(file))
759                 RETURN(PTR_ERR(file));
760
761         /* count doubles as retval */
762         push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
763         retval = file->f_op->write(file, buf, *count, (loff_t *)&offset);
764         pop_ctxt(&saved);
765         filp_close(file, 0);
766
767         if ( retval >= 0 ) {
768                 err = 0;
769                 *count = retval;
770                 EXIT;
771         } else {
772                 err = retval;
773                 *count = 0;
774                 EXIT;
775         }
776
777         return err;
778 } /* filter_write */
779
780 static int filter_pgcache_brw(int rw, struct obd_conn *conn, obd_count num_oa,
781                                struct obdo **oa, obd_count *oa_bufs,
782                                struct page **pages, obd_size *count,
783                                obd_off *offset, obd_flag *flags)
784 {
785         struct obd_run_ctxt      saved;
786         struct super_block      *sb;
787         int                      onum;          /* index to oas */
788         int                      pnum;          /* index to pages (bufs) */
789         unsigned long            retval;
790         int                      error;
791         struct file             *file;
792         ENTRY;
793
794         if (!gen_client(conn)) {
795                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
796                 RETURN(-EINVAL);
797         }
798
799         sb = conn->oc_dev->u.filter.fo_sb;
800         // if (rw == WRITE)
801         push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
802         pnum = 0; /* pnum indexes buf 0..num_pages */
803         for (onum = 0; onum < num_oa; onum++) {
804                 int pg;
805
806                 file = filter_obj_open(conn->oc_dev, oa[onum]->o_id,
807                                        oa[onum]->o_mode);
808                 if (IS_ERR(file))
809                         GOTO(out, retval = PTR_ERR(file));
810
811                 /* count doubles as retval */
812                 for (pg = 0; pg < oa_bufs[onum]; pg++) {
813                         CDEBUG(D_INODE, "OP %d obdo no/pno: (%d,%d) (%ld,%ld) "
814                                "off count (%Ld,%Ld)\n",
815                                rw, onum, pnum, file->f_dentry->d_inode->i_ino,
816                                (unsigned long)offset[pnum] >> PAGE_CACHE_SHIFT,
817                                (unsigned long long)offset[pnum],
818                                (unsigned long long)count[pnum]);
819                         if (rw == WRITE) {
820                                 loff_t off;
821                                 char *buffer;
822                                 off = offset[pnum];
823                                 buffer = kmap(pages[pnum]);
824                                 retval = file->f_op->write(file, buffer, count[pnum], &off);
825                                 kunmap(pages[pnum]);
826                                 CDEBUG(D_INODE, "retval %ld\n", retval);
827                         } else {
828                                 loff_t off = offset[pnum];
829                                 char *buffer = kmap(pages[pnum]);
830
831                                 if (off >= file->f_dentry->d_inode->i_size) {
832                                         memset(buffer, 0, count[pnum]);
833                                         retval = count[pnum];
834                                 } else {
835                                         retval = file->f_op->read(file, buffer, count[pnum], &off);
836                                 }
837                                 kunmap(pages[pnum]);
838
839                                 if (retval != count[pnum]) {
840                                         filp_close(file, 0);
841                                         GOTO(out, retval = -EIO);
842                                 }
843                                 CDEBUG(D_INODE, "retval %ld\n", retval);
844                         }
845                         pnum++;
846                 }
847                 /* sizes and blocks are set by generic_file_write */
848                 /* ctimes/mtimes will follow with a setattr call */
849                 filp_close(file, 0);
850         }
851
852         EXIT;
853 out:
854         // if (rw == WRITE)
855         pop_ctxt(&saved);
856         error = (retval >= 0) ? 0 : retval;
857         return error;
858 }
859
860
861 struct inode *ioobj_to_inode(struct obd_conn *conn, struct obd_ioobj *o)
862 {
863         struct super_block *sb = conn->oc_dev->u.filter.fo_sb;
864         struct inode *inode = NULL;
865         ENTRY;
866
867         if (!sb || !sb->s_dev) {
868                 CDEBUG(D_SUPER, "fatal: device not initialized.\n");
869                 RETURN(NULL);
870         }
871
872         if ( !o->ioo_id ) {
873                 CDEBUG(D_INODE, "fatal: invalid obdo %lu\n", (long)o->ioo_id);
874                 RETURN(NULL);
875         }
876
877         inode = filter_inode_from_obj(conn->oc_dev, o->ioo_id, S_IFREG);
878         if (!inode || inode->i_nlink == 0 || is_bad_inode(inode)) {
879                 CERROR("from obdo - fatal: invalid inode %ld (%s).\n",
880                        (long)o->ioo_id, inode ? inode->i_nlink ? "bad inode" :
881                        "no links" : "NULL");
882                 iput(inode);
883                 RETURN(NULL);
884         }
885
886         RETURN(inode);
887 }
888
889 static int filter_preprw(int cmd, struct obd_conn *conn,
890                          int objcount, struct obd_ioobj *obj,
891                          int niocount, struct niobuf_remote *nb,
892                          struct niobuf_local *res)
893 {
894         struct obd_run_ctxt saved;
895         struct obd_ioobj *o = obj;
896         struct niobuf_remote *b = nb;
897         struct niobuf_local *r = res;
898         int i;
899         ENTRY;
900
901         memset(res, 0, sizeof(*res) * niocount);
902
903         // if (cmd == OBD_BRW_WRITE)
904         push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
905         for (i = 0; i < objcount; i++, o++) {
906                 int j;
907                 for (j = 0; j < o->ioo_bufcnt; j++, b++, r++) {
908                         unsigned long index = b->offset >> PAGE_SHIFT;
909                         struct inode *inode = ioobj_to_inode(conn, o);
910                         struct page *page;
911
912                         /* FIXME: we need to iput all inodes on error */
913                         if (!inode)
914                                 RETURN(-EINVAL);
915
916                         if (cmd == OBD_BRW_WRITE)
917                                 page = lustre_get_page_write(inode, index);
918                         else
919                                 page = lustre_get_page_read(inode, index);
920                         if (IS_ERR(page))
921                                 RETURN(PTR_ERR(page));
922
923                         r->addr = (__u64)(unsigned long)page_address(page);
924                         r->offset = b->offset;
925                         r->page = page;
926                         r->len = PAGE_SIZE;
927                 }
928         }
929         // if (cmd == OBD_BRW_WRITE)
930         pop_ctxt(&saved);
931         return(0);
932 }
933
934 static int filter_commitrw(int cmd, struct obd_conn *conn,
935                            int objcount, struct obd_ioobj *obj,
936                            int niocount, struct niobuf_local *res)
937 {
938         struct obd_run_ctxt saved;
939         struct obd_ioobj *o = obj;
940         struct niobuf_local *r = res;
941         int i;
942         ENTRY;
943
944         // if (cmd == OBD_BRW_WRITE)
945         push_ctxt(&saved, &conn->oc_dev->u.filter.fo_ctxt);
946         for (i = 0; i < objcount; i++, obj++) {
947                 int j;
948                 for (j = 0 ; j < o->ioo_bufcnt ; j++, r++) {
949                         struct page *page = r->page;
950
951                         if (!r->page)
952                                 LBUG();
953
954                         if (cmd == OBD_BRW_WRITE) {
955                                 int rc = lustre_commit_page(page, 0, PAGE_SIZE);
956
957                                 /* FIXME: still need to iput the other inodes */
958                                 if (rc)
959                                         RETURN(rc);
960                         } else
961                                 lustre_put_page(page);
962
963                         CDEBUG(D_INODE, "put inode %p (%ld), count = %d, nlink = %d\n",
964                                page->mapping->host,
965                                page->mapping->host->i_ino,
966                                page->mapping->host->i_count.counter - 1,
967                                page->mapping->host->i_nlink);
968                         iput(page->mapping->host);
969                 }
970         }
971         // if (cmd == OBD_BRW_WRITE)
972         pop_ctxt(&saved);
973         RETURN(0);
974 }
975
976 static int filter_statfs(struct obd_conn *conn, struct statfs * statfs)
977 {
978         struct super_block *sb;
979         int err;
980         ENTRY;
981
982         if (!gen_client(conn)) {
983                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
984                 RETURN(-EINVAL);
985         }
986
987         sb = conn->oc_dev->u.filter.fo_sb;
988
989         err = sb->s_op->statfs(sb, statfs);
990         RETURN(err);
991 } /* filter_statfs */
992
993
994 static int filter_get_info(struct obd_conn *conn, obd_count keylen,
995                            void *key, obd_count *vallen, void **val)
996 {
997         struct obd_device *obddev;
998         struct obd_client * cli;
999         ENTRY;
1000
1001         if (!(cli = gen_client(conn))) {
1002                 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
1003                 RETURN(-EINVAL);
1004         }
1005
1006         obddev = conn->oc_dev;
1007
1008         if ( keylen == strlen("blocksize") &&
1009              memcmp(key, "blocksize", keylen) == 0 ) {
1010                 *vallen = sizeof(long);
1011                 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize;
1012                 RETURN(0);
1013         }
1014
1015         if ( keylen == strlen("blocksize_bits") &&
1016              memcmp(key, "blocksize_bits", keylen) == 0 ){
1017                 *vallen = sizeof(long);
1018                 *val = (void *)(long)obddev->u.filter.fo_sb->s_blocksize_bits;
1019                 RETURN(0);
1020         }
1021
1022         if ( keylen == strlen("root_ino") &&
1023              memcmp(key, "root_ino", keylen) == 0 ){
1024                 *vallen = sizeof(long);
1025                 *val = (void *)(long)FILTER_ROOTINO;
1026                 RETURN(0);
1027         }
1028
1029         CDEBUG(D_IOCTL, "invalid key\n");
1030         RETURN(-EINVAL);
1031 }
1032
1033
1034 struct obd_ops filter_obd_ops = {
1035         o_iocontrol:   NULL,
1036         o_get_info:    filter_get_info,
1037         o_setup:       filter_setup,
1038         o_cleanup:     filter_cleanup,
1039         o_connect:     filter_connect,
1040         o_disconnect:  filter_disconnect,
1041         o_statfs:      filter_statfs,
1042         o_getattr:     filter_getattr,
1043         o_create:      filter_create,
1044         o_setattr:     filter_setattr,
1045         o_destroy:     filter_destroy,
1046         o_open:        filter_open,
1047         o_close:       filter_close,
1048         o_read:        filter_read,
1049         o_write:       filter_write,
1050         o_brw:         filter_pgcache_brw,
1051         o_punch:       filter_truncate,
1052         o_preprw:      filter_preprw,
1053         o_commitrw:    filter_commitrw
1054 #if 0
1055         o_preallocate: filter_preallocate_inodes,
1056         o_migrate:     filter_migrate,
1057         o_copy:        gen_copy_data,
1058         o_iterate:     filter_iterate
1059 #endif
1060 };
1061
1062
1063 static int __init obdfilter_init(void)
1064 {
1065         printk(KERN_INFO "Filtering OBD driver  v0.001, braam@clusterfs.com\n");
1066         return obd_register_type(&filter_obd_ops, OBD_FILTER_DEVICENAME);
1067 }
1068
1069 static void __exit obdfilter_exit(void)
1070 {
1071         obd_unregister_type(OBD_FILTER_DEVICENAME);
1072 }
1073
1074 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
1075 MODULE_DESCRIPTION("Lustre Filtering OBD driver v1.0");
1076 MODULE_LICENSE("GPL");
1077
1078 module_init(obdfilter_init);
1079 module_exit(obdfilter_exit);