Whamcloud - gitweb
Handle SF IOR better by working harder to avoid granting conflicting locks.
[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/obdfilter/filter.c
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *   Author: Peter Braam <braam@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 /*
27  * Invariant: Get O/R i_sem for lookup, if needed, before any journal ops
28  *            (which need to get journal_lock, may block if journal full).
29  *
30  * Invariant: Call filter_start_transno() before any journal ops to avoid the
31  *            same deadlock problem.  We can (and want) to get rid of the
32  *            transno sem in favour of the dir/inode i_sem to avoid single
33  *            threaded operation on the OST.
34  */
35
36 #define DEBUG_SUBSYSTEM S_FILTER
37
38 #include <linux/config.h>
39 #include <linux/module.h>
40 #include <linux/fs.h>
41 #include <linux/dcache.h>
42 #include <linux/init.h>
43 #include <linux/version.h>
44 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
45 # include <linux/mount.h>
46 # include <linux/buffer_head.h>
47 #endif
48
49 #include <linux/obd_class.h>
50 #include <linux/obd_lov.h>
51 #include <linux/lustre_dlm.h>
52 #include <linux/lustre_fsfilt.h>
53 #include <linux/lprocfs_status.h>
54 #include <linux/lustre_log.h>
55 #include <linux/lustre_commit_confd.h>
56 #include <portals/list.h>
57
58 #include "filter_internal.h"
59
60 static struct lvfs_callback_ops filter_lvfs_ops;
61
62 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
63                           struct lov_stripe_md *ea, struct obd_trans_info *);
64
65 static void filter_commit_cb(struct obd_device *obd, __u64 transno,
66                              void *cb_data, int error)
67 {
68         obd_transno_commit_cb(obd, transno, error);
69 }
70
71
72 /* Assumes caller has already pushed us into the kernel context. */
73 int filter_finish_transno(struct obd_export *exp, struct obd_trans_info *oti,
74                           int rc)
75 {
76         struct filter_obd *filter = &exp->exp_obd->u.filter;
77         struct filter_export_data *fed = &exp->exp_filter_data;
78         struct filter_client_data *fcd = fed->fed_fcd;
79         __u64 last_rcvd;
80         loff_t off;
81         int err, log_pri = D_HA;
82
83         /* Propagate error code. */
84         if (rc)
85                 RETURN(rc);
86
87         if (!exp->exp_obd->obd_replayable || oti == NULL)
88                 RETURN(rc);
89
90         /* we don't allocate new transnos for replayed requests */
91         if (oti->oti_transno == 0) {
92                 spin_lock(&filter->fo_translock);
93                 last_rcvd = le64_to_cpu(filter->fo_fsd->fsd_last_transno) + 1;
94                 filter->fo_fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
95                 spin_unlock(&filter->fo_translock);
96                 oti->oti_transno = last_rcvd;
97         } else {
98                 spin_lock(&filter->fo_translock);
99                 last_rcvd = oti->oti_transno;
100                 if (last_rcvd > le64_to_cpu(filter->fo_fsd->fsd_last_transno))
101                         filter->fo_fsd->fsd_last_transno =
102                                 cpu_to_le64(last_rcvd);
103                 spin_unlock(&filter->fo_translock);
104         }
105         fcd->fcd_last_rcvd = cpu_to_le64(last_rcvd);
106
107         /* could get xid from oti, if it's ever needed */
108         fcd->fcd_last_xid = 0;
109
110         off = fed->fed_lr_off;
111         fsfilt_add_journal_cb(exp->exp_obd, last_rcvd, oti->oti_handle,
112                               filter_commit_cb, NULL);
113         err = fsfilt_write_record(exp->exp_obd, filter->fo_rcvd_filp, fcd,
114                                   sizeof(*fcd), &off, 0);
115         if (err) {
116                 log_pri = D_ERROR;
117                 if (rc == 0)
118                         rc = err;
119         }
120
121         CDEBUG(log_pri, "wrote trans "LPU64" for client %s at #%d: err = %d\n",
122                last_rcvd, fcd->fcd_uuid, fed->fed_lr_idx, err);
123
124         RETURN(rc);
125 }
126
127 void f_dput(struct dentry *dentry)
128 {
129         /* Can't go inside filter_ddelete because it can block */
130         CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
131                dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
132         LASSERT(atomic_read(&dentry->d_count) > 0);
133
134         dput(dentry);
135 }
136
137 /* Add client data to the FILTER.  We use a bitmap to locate a free space
138  * in the last_rcvd file if cl_idx is -1 (i.e. a new client).
139  * Otherwise, we have just read the data from the last_rcvd file and
140  * we know its offset. */
141 static int filter_client_add(struct obd_device *obd, struct filter_obd *filter,
142                              struct filter_export_data *fed, int cl_idx)
143 {
144         unsigned long *bitmap = filter->fo_last_rcvd_slots;
145         int new_client = (cl_idx == -1);
146         ENTRY;
147
148         LASSERT(bitmap != NULL);
149
150         /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
151         if (!strcmp(fed->fed_fcd->fcd_uuid, obd->obd_uuid.uuid))
152                 RETURN(0);
153
154         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
155          * there's no need for extra complication here
156          */
157         if (new_client) {
158                 cl_idx = find_first_zero_bit(bitmap, FILTER_LR_MAX_CLIENTS);
159         repeat:
160                 if (cl_idx >= FILTER_LR_MAX_CLIENTS) {
161                         CERROR("no client slots - fix FILTER_LR_MAX_CLIENTS\n");
162                         RETURN(-ENOMEM);
163                 }
164                 if (test_and_set_bit(cl_idx, bitmap)) {
165                         CERROR("FILTER client %d: found bit is set in bitmap\n",
166                                cl_idx);
167                         cl_idx = find_next_zero_bit(bitmap,
168                                                     FILTER_LR_MAX_CLIENTS,
169                                                     cl_idx);
170                         goto repeat;
171                 }
172         } else {
173                 if (test_and_set_bit(cl_idx, bitmap)) {
174                         CERROR("FILTER client %d: bit already set in bitmap!\n",
175                                cl_idx);
176                         LBUG();
177                 }
178         }
179
180         fed->fed_lr_idx = cl_idx;
181         fed->fed_lr_off = le32_to_cpu(filter->fo_fsd->fsd_client_start) +
182                 cl_idx * le16_to_cpu(filter->fo_fsd->fsd_client_size);
183
184         CDEBUG(D_INFO, "client at index %d (%llu) with UUID '%s' added\n",
185                fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
186
187         if (new_client) {
188                 struct obd_run_ctxt saved;
189                 loff_t off = fed->fed_lr_off;
190                 int err;
191                 void *handle;
192
193                 CDEBUG(D_INFO, "writing client fcd at idx %u (%llu) (len %u)\n",
194                        fed->fed_lr_idx,off,(unsigned int)sizeof(*fed->fed_fcd));
195
196                 push_ctxt(&saved, &obd->obd_ctxt, NULL);
197                 /* Transaction needed to fix bug 1403 */
198                 handle = fsfilt_start(obd,
199                                       filter->fo_rcvd_filp->f_dentry->d_inode,
200                                       FSFILT_OP_SETATTR, NULL);
201                 if (IS_ERR(handle)) {
202                         err = PTR_ERR(handle);
203                         CERROR("unable to start transaction: rc %d\n", err);
204                 } else {
205                         err = fsfilt_write_record(obd, filter->fo_rcvd_filp,
206                                                   fed->fed_fcd,
207                                                   sizeof(*fed->fed_fcd),
208                                                   &off, 1);
209                         fsfilt_commit(obd,
210                                       filter->fo_rcvd_filp->f_dentry->d_inode,
211                                       handle, 1);
212                 }
213                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
214
215                 if (err) {
216                         CERROR("error writing %s client idx %u: rc %d\n",
217                                LAST_RCVD, fed->fed_lr_idx, err);
218                         RETURN(err);
219                 }
220         }
221         RETURN(0);
222 }
223
224 static int filter_client_free(struct obd_export *exp, int flags)
225 {
226         struct filter_export_data *fed = &exp->exp_filter_data;
227         struct filter_obd *filter = &exp->exp_obd->u.filter;
228         struct obd_device *obd = exp->exp_obd;
229         struct filter_client_data zero_fcd;
230         struct obd_run_ctxt saved;
231         int rc;
232         loff_t off;
233         ENTRY;
234
235         if (fed->fed_fcd == NULL)
236                 RETURN(0);
237
238         if (flags & OBD_OPT_FAILOVER)
239                 GOTO(free, 0);
240
241         /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
242         if (strcmp(fed->fed_fcd->fcd_uuid, obd->obd_uuid.uuid ) == 0)
243                 GOTO(free, 0);
244
245         LASSERT(filter->fo_last_rcvd_slots != NULL);
246
247         off = fed->fed_lr_off;
248
249         CDEBUG(D_INFO, "freeing client at idx %u (%lld) with UUID '%s'\n",
250                fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
251
252         /* Clear the bit _after_ zeroing out the client so we don't
253            race with filter_client_add and zero out new clients.*/
254         if (!test_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
255                 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
256                        fed->fed_lr_idx);
257                 LBUG();
258         }
259
260         memset(&zero_fcd, 0, sizeof zero_fcd);
261         push_ctxt(&saved, &obd->obd_ctxt, NULL);
262         rc = fsfilt_write_record(obd, filter->fo_rcvd_filp, &zero_fcd,
263                                  sizeof(zero_fcd), &off, 1);
264         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
265
266         CDEBUG(rc == 0 ? D_INFO : D_ERROR,
267                "zeroing disconnecting client %s at idx %u (%llu) in %s rc %d\n",
268                fed->fed_fcd->fcd_uuid, fed->fed_lr_idx, fed->fed_lr_off,
269                LAST_RCVD, rc);
270
271         if (!test_and_clear_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
272                 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
273                        fed->fed_lr_idx);
274                 LBUG();
275         }
276
277 free:
278         OBD_FREE(fed->fed_fcd, sizeof(*fed->fed_fcd));
279
280         RETURN(0);
281 }
282
283 static int filter_free_server_data(struct filter_obd *filter)
284 {
285         OBD_FREE(filter->fo_fsd, sizeof(*filter->fo_fsd));
286         filter->fo_fsd = NULL;
287         OBD_FREE(filter->fo_last_rcvd_slots,
288                  FILTER_LR_MAX_CLIENT_WORDS * sizeof(unsigned long));
289         filter->fo_last_rcvd_slots = NULL;
290         return 0;
291 }
292
293 /* assumes caller is already in kernel ctxt */
294 int filter_update_server_data(struct obd_device *obd, struct file *filp,
295                               struct filter_server_data *fsd, int force_sync)
296 {
297         loff_t off = 0;
298         int rc;
299         ENTRY;
300
301         CDEBUG(D_INODE, "server uuid      : %s\n", fsd->fsd_uuid);
302         CDEBUG(D_INODE, "server last_rcvd : "LPU64"\n",
303                le64_to_cpu(fsd->fsd_last_transno));
304         CDEBUG(D_INODE, "server last_mount: "LPU64"\n",
305                le64_to_cpu(fsd->fsd_mount_count));
306
307         rc = fsfilt_write_record(obd, filp, fsd, sizeof(*fsd), &off,force_sync);
308         if (rc)
309                 CERROR("error writing filter_server_data: rc = %d\n", rc);
310
311         RETURN(rc);
312 }
313
314 int filter_update_last_objid(struct obd_device *obd, obd_gr group,
315                              int force_sync)
316 {
317         struct filter_obd *filter = &obd->u.filter;
318         __u64 tmp;
319         loff_t off = 0;
320         int rc;
321         ENTRY;
322
323         CDEBUG(D_INODE, "server last_objid for group "LPU64": "LPU64"\n",
324                group, filter->fo_last_objids[group]);
325
326         tmp = cpu_to_le64(filter->fo_last_objids[group]);
327         rc = fsfilt_write_record(obd, filter->fo_last_objid_files[group],
328                                  &tmp, sizeof(tmp), &off, force_sync);
329         if (rc)
330                 CERROR("error writing group "LPU64" last objid: rc = %d\n",
331                        group, rc);
332         RETURN(rc);
333 }
334
335 /* assumes caller has already in kernel ctxt */
336 static int filter_init_server_data(struct obd_device *obd, struct file * filp)
337 {
338         struct filter_obd *filter = &obd->u.filter;
339         struct filter_server_data *fsd;
340         struct filter_client_data *fcd = NULL;
341         struct inode *inode = filp->f_dentry->d_inode;
342         unsigned long last_rcvd_size = inode->i_size;
343         __u64 mount_count;
344         int cl_idx;
345         loff_t off = 0;
346         int rc;
347
348         /* ensure padding in the struct is the correct size */
349         LASSERT (offsetof(struct filter_server_data, fsd_padding) +
350                  sizeof(fsd->fsd_padding) == FILTER_LR_SERVER_SIZE);
351         LASSERT (offsetof(struct filter_client_data, fcd_padding) +
352                  sizeof(fcd->fcd_padding) == FILTER_LR_CLIENT_SIZE);
353
354         OBD_ALLOC(fsd, sizeof(*fsd));
355         if (!fsd)
356                 RETURN(-ENOMEM);
357         filter->fo_fsd = fsd;
358
359         OBD_ALLOC(filter->fo_last_rcvd_slots,
360                   FILTER_LR_MAX_CLIENT_WORDS * sizeof(unsigned long));
361         if (filter->fo_last_rcvd_slots == NULL) {
362                 OBD_FREE(fsd, sizeof(*fsd));
363                 RETURN(-ENOMEM);
364         }
365
366         if (last_rcvd_size == 0) {
367                 CWARN("%s: initializing new %s\n", obd->obd_name, LAST_RCVD);
368
369                 memcpy(fsd->fsd_uuid, obd->obd_uuid.uuid,sizeof(fsd->fsd_uuid));
370                 fsd->fsd_last_transno = 0;
371                 mount_count = fsd->fsd_mount_count = 0;
372                 fsd->fsd_server_size = cpu_to_le32(FILTER_LR_SERVER_SIZE);
373                 fsd->fsd_client_start = cpu_to_le32(FILTER_LR_CLIENT_START);
374                 fsd->fsd_client_size = cpu_to_le16(FILTER_LR_CLIENT_SIZE);
375                 fsd->fsd_subdir_count = cpu_to_le16(FILTER_SUBDIR_COUNT);
376                 filter->fo_subdir_count = FILTER_SUBDIR_COUNT;
377         } else {
378                 rc = fsfilt_read_record(obd, filp, fsd, sizeof(*fsd), &off);
379                 if (rc) {
380                         CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
381                                LAST_RCVD, rc);
382                         GOTO(err_fsd, rc);
383                 }
384                 if (strcmp(fsd->fsd_uuid, obd->obd_uuid.uuid) != 0) {
385                         CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
386                                obd->obd_uuid.uuid, fsd->fsd_uuid);
387                         GOTO(err_fsd, rc = -EINVAL);
388                 }
389                 mount_count = le64_to_cpu(fsd->fsd_mount_count);
390                 filter->fo_subdir_count = le16_to_cpu(fsd->fsd_subdir_count);
391         }
392
393         if (fsd->fsd_feature_incompat & ~cpu_to_le32(FILTER_INCOMPAT_SUPP)) {
394                 CERROR("unsupported feature %x\n",
395                        le32_to_cpu(fsd->fsd_feature_incompat) &
396                        ~FILTER_INCOMPAT_SUPP);
397                 GOTO(err_fsd, rc = -EINVAL);
398         }
399         if (fsd->fsd_feature_rocompat & ~cpu_to_le32(FILTER_ROCOMPAT_SUPP)) {
400                 CERROR("read-only feature %x\n",
401                        le32_to_cpu(fsd->fsd_feature_rocompat) &
402                        ~FILTER_ROCOMPAT_SUPP);
403                 /* Do something like remount filesystem read-only */
404                 GOTO(err_fsd, rc = -EINVAL);
405         }
406
407         CDEBUG(D_INODE, "%s: server last_rcvd : "LPU64"\n",
408                obd->obd_name, le64_to_cpu(fsd->fsd_last_transno));
409         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
410                obd->obd_name, mount_count + 1);
411         CDEBUG(D_INODE, "%s: server data size: %u\n",
412                obd->obd_name, le32_to_cpu(fsd->fsd_server_size));
413         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
414                obd->obd_name, le32_to_cpu(fsd->fsd_client_start));
415         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
416                obd->obd_name, le32_to_cpu(fsd->fsd_client_size));
417         CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
418                obd->obd_name, le16_to_cpu(fsd->fsd_subdir_count));
419         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
420                last_rcvd_size <= FILTER_LR_CLIENT_START ? 0 :
421                (last_rcvd_size-FILTER_LR_CLIENT_START) /FILTER_LR_CLIENT_SIZE);
422
423         if (!obd->obd_replayable) {
424                 CWARN("%s: recovery support OFF\n", obd->obd_name);
425                 GOTO(out, rc = 0);
426         }
427
428         for (cl_idx = 0, off = le32_to_cpu(fsd->fsd_client_start);
429              off < last_rcvd_size; cl_idx++) {
430                 __u64 last_rcvd;
431                 struct obd_export *exp;
432                 struct filter_export_data *fed;
433
434                 if (!fcd) {
435                         OBD_ALLOC(fcd, sizeof(*fcd));
436                         if (!fcd)
437                                 GOTO(err_client, rc = -ENOMEM);
438                 }
439
440                 /* Don't assume off is incremented properly by
441                  * fsfilt_read_record(), in case sizeof(*fcd)
442                  * isn't the same as fsd->fsd_client_size.  */
443                 off = le32_to_cpu(fsd->fsd_client_start) +
444                         cl_idx * le16_to_cpu(fsd->fsd_client_size);
445                 rc = fsfilt_read_record(obd, filp, fcd, sizeof(*fcd), &off);
446                 if (rc) {
447                         CERROR("error reading FILT %s idx %d off %llu: rc %d\n",
448                                LAST_RCVD, cl_idx, off, rc);
449                         break; /* read error shouldn't cause startup to fail */
450                 }
451
452                 if (fcd->fcd_uuid[0] == '\0') {
453                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
454                                cl_idx);
455                         continue;
456                 }
457
458                 last_rcvd = le64_to_cpu(fcd->fcd_last_rcvd);
459
460                 /* These exports are cleaned up by filter_disconnect(), so they
461                  * need to be set up like real exports as filter_connect() does.
462                  */
463                 exp = class_new_export(obd);
464                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
465                        " srv lr: "LPU64"\n", fcd->fcd_uuid, cl_idx,
466                        last_rcvd, le64_to_cpu(fsd->fsd_last_transno));
467                 if (exp == NULL)
468                         GOTO(err_client, rc = -ENOMEM);
469
470                 memcpy(&exp->exp_client_uuid.uuid, fcd->fcd_uuid,
471                        sizeof exp->exp_client_uuid.uuid);
472                 fed = &exp->exp_filter_data;
473                 fed->fed_fcd = fcd;
474                 filter_client_add(obd, filter, fed, cl_idx);
475                 /* create helper if export init gets more complex */
476                 spin_lock_init(&fed->fed_lock);
477
478                 fcd = NULL;
479                 obd->obd_recoverable_clients++;
480                 class_export_put(exp);
481
482                 CDEBUG(D_OTHER, "client at idx %d has last_rcvd = "LPU64"\n",
483                        cl_idx, last_rcvd);
484
485                 if (last_rcvd > le64_to_cpu(fsd->fsd_last_transno))
486                         fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
487
488         }
489
490         obd->obd_last_committed = le64_to_cpu(fsd->fsd_last_transno);
491
492         if (obd->obd_recoverable_clients) {
493                 CWARN("RECOVERY: %d recoverable clients, last_rcvd "
494                        LPU64"\n", obd->obd_recoverable_clients,
495                        le64_to_cpu(fsd->fsd_last_transno));
496                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
497                 obd->obd_recovering = 1;
498         }
499
500         if (fcd)
501                 OBD_FREE(fcd, sizeof(*fcd));
502
503 out:
504         filter->fo_mount_count = mount_count + 1;
505         fsd->fsd_mount_count = cpu_to_le64(filter->fo_mount_count);
506
507         /* save it, so mount count and last_transno is current */
508         rc = filter_update_server_data(obd, filp, filter->fo_fsd, 1);
509
510         RETURN(rc);
511
512 err_client:
513         class_disconnect_exports(obd, 0);
514 err_fsd:
515         filter_free_server_data(filter);
516         RETURN(rc);
517 }
518
519 static int filter_cleanup_groups(struct obd_device *obd)
520 {
521         struct filter_obd *filter = &obd->u.filter;
522         int i;
523         ENTRY;
524
525         if (filter->fo_dentry_O_groups != NULL &&
526             filter->fo_last_objids != NULL &&
527             filter->fo_last_objid_files != NULL) {
528                 for (i = 0; i < FILTER_GROUPS; i++) {
529                         struct dentry *dentry = filter->fo_dentry_O_groups[i];
530                         struct file *filp = filter->fo_last_objid_files[i];
531                         if (dentry != NULL) {
532                                 f_dput(dentry);
533                                 filter->fo_dentry_O_groups[i] = NULL;
534                         }
535                         if (filp != NULL) {
536                                 filp_close(filp, 0);
537                                 filter->fo_last_objid_files[i] = NULL;
538                         }
539                 }
540         }
541         if (filter->fo_dentry_O_sub != NULL && filter->fo_subdir_count) {
542                 for (i = 0; i < filter->fo_subdir_count; i++) {
543                         struct dentry *dentry = filter->fo_dentry_O_sub[i];
544                         if (dentry != NULL) {
545                                 f_dput(dentry);
546                                 filter->fo_dentry_O_sub[i] = NULL;
547                         }
548                 }
549                 OBD_FREE(filter->fo_dentry_O_sub,
550                          filter->fo_subdir_count *
551                          sizeof(*filter->fo_dentry_O_sub));
552         }
553         if (filter->fo_dentry_O_groups != NULL)
554                 OBD_FREE(filter->fo_dentry_O_groups,
555                          FILTER_GROUPS * sizeof(struct dentry *));
556         if (filter->fo_last_objids != NULL)
557                 OBD_FREE(filter->fo_last_objids,
558                          FILTER_GROUPS * sizeof(__u64));
559         if (filter->fo_last_objid_files != NULL)
560                 OBD_FREE(filter->fo_last_objid_files,
561                          FILTER_GROUPS * sizeof(struct file *));
562         RETURN(0);
563 }
564
565 /* FIXME: object groups */
566 static int filter_prep_groups(struct obd_device *obd)
567 {
568         struct filter_obd *filter = &obd->u.filter;
569         struct dentry *dentry, *O_dentry;
570         struct file *filp;
571         int i, rc = 0, cleanup_phase = 0;
572         ENTRY;
573
574         O_dentry = simple_mkdir(current->fs->pwd, "O", 0700, 1);
575         CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
576         if (IS_ERR(O_dentry)) {
577                 rc = PTR_ERR(O_dentry);
578                 CERROR("cannot open/create O: rc = %d\n", rc);
579                 GOTO(cleanup, rc);
580         }
581         filter->fo_dentry_O = O_dentry;
582         cleanup_phase = 1; /* O_dentry */
583
584         /* Lookup "R" to tell if we're on an old OST FS and need to convert
585          * from O/R/<dir>/<objid> to O/0/<dir>/<objid>.  This can be removed
586          * some time post 1.0 when all old-style OSTs have converted along
587          * with the init_objid hack. */
588         dentry = ll_lookup_one_len("R", O_dentry, 1);
589         if (IS_ERR(dentry))
590                 GOTO(cleanup, rc = PTR_ERR(dentry));
591         if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
592                 struct dentry *O0_dentry = lookup_one_len("0", O_dentry, 1);
593                 ENTRY;
594
595                 CWARN("converting OST to new object layout\n");
596                 if (IS_ERR(O0_dentry)) {
597                         rc = PTR_ERR(O0_dentry);
598                         CERROR("error looking up O/0: rc %d\n", rc);
599                         GOTO(cleanup_R, rc);
600                 }
601
602                 if (O0_dentry->d_inode) {
603                         CERROR("Both O/R and O/0 exist. Fix manually.\n");
604                         GOTO(cleanup_O0, rc = -EEXIST);
605                 }
606
607                 down(&O_dentry->d_inode->i_sem);
608                 rc = vfs_rename(O_dentry->d_inode, dentry,
609                                 O_dentry->d_inode, O0_dentry);
610                 up(&O_dentry->d_inode->i_sem);
611
612                 if (rc) {
613                         CERROR("error renaming O/R to O/0: rc %d\n", rc);
614                         GOTO(cleanup_O0, rc);
615                 }
616                 filter->fo_fsd->fsd_feature_incompat |=
617                         cpu_to_le32(FILTER_INCOMPAT_GROUPS);
618                 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
619                                                filter->fo_fsd, 1);
620                 GOTO(cleanup_O0, rc);
621
622         cleanup_O0:
623                 dput(O0_dentry);
624         cleanup_R:
625                 dput(dentry);
626                 if (rc)
627                         GOTO(cleanup, rc);
628         } else {
629                 dput(dentry);
630         }
631
632         OBD_ALLOC(filter->fo_last_objids, FILTER_GROUPS * sizeof(__u64));
633         if (filter->fo_last_objids == NULL)
634                 GOTO(cleanup, rc = -ENOMEM);
635         cleanup_phase = 2; /* groups */
636
637         OBD_ALLOC(filter->fo_dentry_O_groups, FILTER_GROUPS * sizeof(dentry));
638         if (filter->fo_dentry_O_groups == NULL)
639                 GOTO(cleanup, rc = -ENOMEM);
640         OBD_ALLOC(filter->fo_last_objid_files, FILTER_GROUPS * sizeof(filp));
641         if (filter->fo_last_objid_files == NULL)
642                 GOTO(cleanup, rc = -ENOMEM);
643
644         for (i = 0; i < FILTER_GROUPS; i++) {
645                 char name[25];
646                 loff_t off = 0;
647
648                 sprintf(name, "%d", i);
649                 dentry = simple_mkdir(O_dentry, name, 0700, 1);
650                 CDEBUG(D_INODE, "got/created O/%s: %p\n", name, dentry);
651                 if (IS_ERR(dentry)) {
652                         rc = PTR_ERR(dentry);
653                         CERROR("cannot lookup/create O/%s: rc = %d\n",
654                                name, rc);
655                         GOTO(cleanup, rc);
656                 }
657                 filter->fo_dentry_O_groups[i] = dentry;
658
659                 sprintf(name, "O/%d/LAST_ID", i);
660                 filp = filp_open(name, O_CREAT | O_RDWR, 0700);
661                 if (IS_ERR(dentry)) {
662                         rc = PTR_ERR(dentry);
663                         CERROR("cannot create %s: rc = %d\n", name, rc);
664                         GOTO(cleanup, rc);
665                 }
666                 filter->fo_last_objid_files[i] = filp;
667
668                 if (filp->f_dentry->d_inode->i_size == 0) {
669                         if (i == 0 && filter->fo_fsd->fsd_unused != 0) {
670                                 /* OST conversion, remove sometime post 1.0 */
671                                 filter->fo_last_objids[i] =
672                                         le64_to_cpu(filter->fo_fsd->fsd_unused);
673                                 CWARN("saving old objid "LPU64" to LAST_ID\n",
674                                       filter->fo_last_objids[i]);
675                                 rc = filter_update_last_objid(obd, 0, 1);
676                                 if (rc)
677                                         GOTO(cleanup, rc);
678                         } else {
679                                 filter->fo_last_objids[i] = FILTER_INIT_OBJID;
680                         }
681                         continue;
682                 }
683
684                 rc = fsfilt_read_record(obd, filp, &filter->fo_last_objids[i],
685                                         sizeof(__u64), &off);
686                 if (rc) {
687                         CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
688                                name, rc);
689                         GOTO(cleanup, rc);
690                 }
691                 filter->fo_last_objids[i] =
692                         le64_to_cpu(filter->fo_last_objids[i]);
693                 CDEBUG(D_INODE, "%s: server last_objid group %d: "LPU64"\n",
694                        obd->obd_name, i, filter->fo_last_objids[i]);
695         }
696
697         if (filter->fo_subdir_count) {
698                 O_dentry = filter->fo_dentry_O_groups[0];
699                 OBD_ALLOC(filter->fo_dentry_O_sub,
700                           filter->fo_subdir_count * sizeof(dentry));
701                 if (filter->fo_dentry_O_sub == NULL)
702                         GOTO(cleanup, rc = -ENOMEM);
703
704                 for (i = 0; i < filter->fo_subdir_count; i++) {
705                         char dir[20];
706                         snprintf(dir, sizeof(dir), "d%u", i);
707
708                         dentry = simple_mkdir(O_dentry, dir, 0700, 1);
709                         CDEBUG(D_INODE, "got/created O/0/%s: %p\n", dir,dentry);
710                         if (IS_ERR(dentry)) {
711                                 rc = PTR_ERR(dentry);
712                                 CERROR("can't lookup/create O/0/%s: rc = %d\n",
713                                        dir, rc);
714                                 GOTO(cleanup, rc);
715                         }
716                         filter->fo_dentry_O_sub[i] = dentry;
717                 }
718         }
719         RETURN(0);
720
721  cleanup:
722         switch (cleanup_phase) {
723         case 2:
724                 filter_cleanup_groups(obd);
725         case 1:
726                 f_dput(filter->fo_dentry_O);
727                 filter->fo_dentry_O = NULL;
728         default:
729                 break;
730         }
731         return rc;
732 }
733
734 /* setup the object store with correct subdirectories */
735 static int filter_prep(struct obd_device *obd)
736 {
737         struct obd_run_ctxt saved;
738         struct filter_obd *filter = &obd->u.filter;
739         struct file *file;
740         struct inode *inode;
741         int rc = 0;
742         ENTRY;
743
744         push_ctxt(&saved, &obd->obd_ctxt, NULL);
745         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
746         if (!file || IS_ERR(file)) {
747                 rc = PTR_ERR(file);
748                 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
749                        LAST_RCVD, rc);
750                 GOTO(out, rc);
751         }
752
753         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
754                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
755                        file->f_dentry->d_inode->i_mode);
756                 GOTO(err_filp, rc = -ENOENT);
757         }
758
759         /* steal operations */
760         inode = file->f_dentry->d_inode;
761         filter->fo_fop = file->f_op;
762         filter->fo_iop = inode->i_op;
763         filter->fo_aops = inode->i_mapping->a_ops;
764
765         rc = filter_init_server_data(obd, file);
766         if (rc) {
767                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
768                 GOTO(err_filp, rc);
769         }
770         filter->fo_rcvd_filp = file;
771
772         rc = filter_prep_groups(obd);
773         if (rc)
774                 GOTO(err_server_data, rc);
775
776  out:
777         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
778
779         return(rc);
780
781  err_server_data:
782         //class_disconnect_exports(obd, 0);
783         filter_free_server_data(filter);
784  err_filp:
785         if (filp_close(file, 0))
786                 CERROR("can't close %s after error\n", LAST_RCVD);
787         filter->fo_rcvd_filp = NULL;
788         goto out;
789 }
790
791 /* cleanup the filter: write last used object id to status file */
792 static void filter_post(struct obd_device *obd)
793 {
794         struct obd_run_ctxt saved;
795         struct filter_obd *filter = &obd->u.filter;
796         int rc, i;
797
798         /* XXX: filter_update_lastobjid used to call fsync_dev.  It might be
799          * best to start a transaction with h_sync, because we removed this
800          * from lastobjid */
801
802         push_ctxt(&saved, &obd->obd_ctxt, NULL);
803         rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
804                                        filter->fo_fsd, 0);
805         if (rc)
806                 CERROR("error writing server data: rc = %d\n", rc);
807
808         for (i = 0; i < FILTER_GROUPS; i++) {
809                 rc = filter_update_last_objid(obd, i, (i == FILTER_GROUPS - 1));
810                 if (rc)
811                         CERROR("error writing group %d lastobjid: rc = %d\n",
812                                i, rc);
813         }
814
815         filp_close(filter->fo_rcvd_filp, 0);
816         filter->fo_rcvd_filp = NULL;
817         if (rc)
818                 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
819
820         filter_cleanup_groups(obd);
821         f_dput(filter->fo_dentry_O);
822         filter_free_server_data(filter);
823         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
824 }
825
826 static void filter_set_last_id(struct filter_obd *filter, struct obdo *oa,
827                                obd_id id)
828 {
829         obd_gr group = 0;
830         LASSERT(filter->fo_fsd != NULL);
831
832         if (oa != NULL) {
833                 LASSERT(oa->o_gr <= FILTER_GROUPS);
834                 group = oa->o_gr;
835         }
836
837         spin_lock(&filter->fo_objidlock);
838         filter->fo_last_objids[group] = id;
839         spin_unlock(&filter->fo_objidlock);
840 }
841
842 __u64 filter_last_id(struct filter_obd *filter, struct obdo *oa)
843 {
844         obd_id id;
845         obd_gr group = 0;
846         LASSERT(filter->fo_fsd != NULL);
847
848         if (oa != NULL) {
849                 LASSERT(oa->o_gr <= FILTER_GROUPS);
850                 group = oa->o_gr;
851         }
852
853         /* FIXME: object groups */
854         spin_lock(&filter->fo_objidlock);
855         id = filter->fo_last_objids[group];
856         spin_unlock(&filter->fo_objidlock);
857
858         return id;
859 }
860
861 /* direct cut-n-paste of mds_blocking_ast() */
862 static int filter_blocking_ast(struct ldlm_lock *lock,
863                                struct ldlm_lock_desc *desc,
864                                void *data, int flag)
865 {
866         int do_ast;
867         ENTRY;
868
869         if (flag == LDLM_CB_CANCELING) {
870                 /* Don't need to do anything here. */
871                 RETURN(0);
872         }
873
874         /* XXX layering violation!  -phil */
875         l_lock(&lock->l_resource->lr_namespace->ns_lock);
876         /* Get this: if filter_blocking_ast is racing with ldlm_intent_policy,
877          * such that filter_blocking_ast is called just before l_i_p takes the
878          * ns_lock, then by the time we get the lock, we might not be the
879          * correct blocking function anymore.  So check, and return early, if
880          * so. */
881         if (lock->l_blocking_ast != filter_blocking_ast) {
882                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
883                 RETURN(0);
884         }
885
886         lock->l_flags |= LDLM_FL_CBPENDING;
887         do_ast = (!lock->l_readers && !lock->l_writers);
888         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
889
890         if (do_ast) {
891                 struct lustre_handle lockh;
892                 int rc;
893
894                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
895                 ldlm_lock2handle(lock, &lockh);
896                 rc = ldlm_cli_cancel(&lockh);
897                 if (rc < 0)
898                         CERROR("ldlm_cli_cancel: %d\n", rc);
899         } else {
900                 LDLM_DEBUG(lock, "Lock still has references, will be "
901                            "cancelled later");
902         }
903         RETURN(0);
904 }
905
906 static int filter_lock_dentry(struct obd_device *obd, struct dentry *dparent)
907 {
908         down(&dparent->d_inode->i_sem);
909         return 0;
910 }
911
912 /* We never dget the object parent, so DON'T dput it either */
913 static void filter_parent_unlock(struct dentry *dparent)
914 {
915         up(&dparent->d_inode->i_sem);
916 }
917
918 /* We never dget the object parent, so DON'T dput it either */
919 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
920 {
921         struct filter_obd *filter = &obd->u.filter;
922         LASSERT(group < FILTER_GROUPS); /* FIXME: object groups */
923
924         if (group > 0 || filter->fo_subdir_count == 0)
925                 return filter->fo_dentry_O_groups[group];
926
927         return filter->fo_dentry_O_sub[objid & (filter->fo_subdir_count - 1)];
928 }
929
930 /* We never dget the object parent, so DON'T dput it either */
931 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
932                                   obd_id objid)
933 {
934         unsigned long now = jiffies;
935         struct dentry *dparent = filter_parent(obd, group, objid);
936         int rc;
937
938         if (IS_ERR(dparent))
939                 return dparent;
940
941         rc = filter_lock_dentry(obd, dparent);
942         if (time_after(jiffies, now + 15 * HZ))
943                 CERROR("slow parent lock %lus\n", (jiffies - now) / HZ);
944         return rc ? ERR_PTR(rc) : dparent;
945 }
946
947 /* How to get files, dentries, inodes from object id's.
948  *
949  * If dir_dentry is passed, the caller has already locked the parent
950  * appropriately for this operation (normally a write lock).  If
951  * dir_dentry is NULL, we do a read lock while we do the lookup to
952  * avoid races with create/destroy and such changing the directory
953  * internal to the filesystem code. */
954 struct dentry *filter_fid2dentry(struct obd_device *obd,
955                                  struct dentry *dir_dentry,
956                                  obd_gr group, obd_id id)
957 {
958         struct dentry *dparent = dir_dentry;
959         struct dentry *dchild;
960         char name[32];
961         int len;
962         ENTRY;
963
964         if (id == 0) {
965                 CERROR("fatal: invalid object id 0\n");
966                 RETURN(ERR_PTR(-ESTALE));
967         }
968
969         len = sprintf(name, LPU64, id);
970         if (dir_dentry == NULL) {
971                 dparent = filter_parent_lock(obd, group, id);
972                 if (IS_ERR(dparent))
973                         RETURN(dparent);
974         }
975         CDEBUG(D_INODE, "looking up object O/%*s/%s\n",
976                dparent->d_name.len, dparent->d_name.name, name);
977         dchild = /*ll_*/lookup_one_len(name, dparent, len);
978         if (dir_dentry == NULL)
979                 filter_parent_unlock(dparent);
980         if (IS_ERR(dchild)) {
981                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
982                 RETURN(dchild);
983         }
984
985         CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
986                name, dchild, atomic_read(&dchild->d_count));
987
988         LASSERT(atomic_read(&dchild->d_count) > 0);
989
990         RETURN(dchild);
991 }
992
993 static int filter_prepare_destroy(struct obd_device *obd, obd_id objid)
994 {
995         struct lustre_handle lockh;
996         int flags = LDLM_AST_DISCARD_DATA, rc;
997         struct ldlm_res_id res_id = { .name = { objid } };
998         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
999
1000         ENTRY;
1001         /* Tell the clients that the object is gone now and that they should
1002          * throw away any cached pages. */
1003         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
1004                               LDLM_EXTENT, &policy, LCK_PW,
1005                               &flags, filter_blocking_ast, ldlm_completion_ast,
1006                               NULL, NULL, NULL, 0, NULL, &lockh);
1007
1008         /* We only care about the side-effects, just drop the lock. */
1009         if (rc == ELDLM_OK)
1010                 ldlm_lock_decref(&lockh, LCK_PW);
1011
1012         RETURN(rc);
1013 }
1014
1015 /* Caller must hold LCK_PW on parent and push us into kernel context.
1016  * Caller is also required to ensure that dchild->d_inode exists. */
1017 static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
1018                                    struct dentry *dparent,
1019                                    struct dentry *dchild)
1020 {
1021         struct inode *inode = dchild->d_inode;
1022         int rc;
1023         ENTRY;
1024
1025         if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
1026                 CERROR("destroying objid %*s nlink = %lu, count = %d\n",
1027                        dchild->d_name.len, dchild->d_name.name,
1028                        (unsigned long)inode->i_nlink,
1029                        atomic_read(&inode->i_count));
1030         }
1031
1032         rc = vfs_unlink(dparent->d_inode, dchild);
1033
1034         if (rc)
1035                 CERROR("error unlinking objid %*s: rc %d\n",
1036                        dchild->d_name.len, dchild->d_name.name, rc);
1037
1038         RETURN(rc);
1039 }
1040
1041 static int filter_intent_policy(struct ldlm_namespace *ns,
1042                                 struct ldlm_lock **lockp, void *req_cookie,
1043                                 ldlm_mode_t mode, int flags, void *data)
1044 {
1045         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1046         struct ptlrpc_request *req = req_cookie;
1047         struct ldlm_lock *lock = *lockp, *l = NULL;
1048         struct ldlm_resource *res = lock->l_resource;
1049         ldlm_processing_policy policy;
1050         struct ost_lvb *res_lvb, *reply_lvb;
1051         struct list_head *tmp;
1052         ldlm_error_t err;
1053         int tmpflags = 0, rc, repsize[2] = {sizeof(struct ldlm_reply),
1054                                             sizeof(struct ost_lvb) };
1055         ENTRY;
1056
1057         policy = ldlm_get_processing_policy(res);
1058         LASSERT(policy != NULL);
1059         LASSERT(req != NULL);
1060
1061         rc = lustre_pack_reply(req, 2, repsize, NULL);
1062         if (rc)
1063                 RETURN(req->rq_status = rc);
1064
1065         reply_lvb = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*reply_lvb));
1066         LASSERT(reply_lvb != NULL);
1067
1068         //fixup_handle_for_resent_req(req, lock, &lockh);
1069
1070         /* If we grant any lock at all, it will be a whole-file read lock.
1071          * Call the extent policy function to see if our request can be
1072          * granted, or is blocked. */
1073         lock->l_policy_data.l_extent.start = 0;
1074         lock->l_policy_data.l_extent.end = OBD_OBJECT_EOF;
1075         lock->l_req_mode = LCK_PR;
1076
1077         l_lock(&res->lr_namespace->ns_lock);
1078
1079         res->lr_tmp = &rpc_list;
1080         rc = policy(lock, &tmpflags, 0, &err);
1081         res->lr_tmp = NULL;
1082
1083         /* FIXME: we should change the policy function slightly, to not make
1084          * this list at all, since we just turn around and free it */
1085         while (!list_empty(&rpc_list)) {
1086                 struct ldlm_ast_work *w =
1087                         list_entry(rpc_list.next, struct ldlm_ast_work, w_list);
1088                 list_del(&w->w_list);
1089                 LDLM_LOCK_PUT(w->w_lock);
1090                 OBD_FREE(w, sizeof(*w));
1091         }
1092
1093         if (rc == LDLM_ITER_CONTINUE) {
1094                 /* The lock met with no resistance; we're finished. */
1095                 l_unlock(&res->lr_namespace->ns_lock);
1096                 RETURN(ELDLM_LOCK_REPLACED);
1097         }
1098
1099         /* Do not grant any lock, but instead send GL callbacks.  The extent
1100          * policy nicely created a list of all PW locks for us.  We will choose
1101          * the highest of those which are larger than the size in the LVB, if
1102          * any, and perform a glimpse callback. */
1103         down(&res->lr_lvb_sem);
1104         res_lvb = res->lr_lvb_data;
1105         LASSERT(res_lvb != NULL);
1106         reply_lvb->lvb_size = res_lvb->lvb_size;
1107         up(&res->lr_lvb_sem);
1108
1109         list_for_each(tmp, &res->lr_granted) {
1110                 struct ldlm_lock *tmplock =
1111                         list_entry(tmp, struct ldlm_lock, l_res_link);
1112
1113                 if (tmplock->l_granted_mode == LCK_PR)
1114                         continue;
1115
1116                 if (tmplock->l_policy_data.l_extent.end <= reply_lvb->lvb_size)
1117                         continue;
1118
1119                 if (l == NULL) {
1120                         l = LDLM_LOCK_GET(tmplock);
1121                         continue;
1122                 }
1123
1124                 if (l->l_policy_data.l_extent.start >
1125                     tmplock->l_policy_data.l_extent.start)
1126                         continue;
1127
1128                 LDLM_LOCK_PUT(l);
1129                 l = LDLM_LOCK_GET(tmplock);
1130         }
1131         l_unlock(&res->lr_namespace->ns_lock);
1132
1133         /* There were no PW locks beyond the size in the LVB; finished. */
1134         if (l == NULL)
1135                 RETURN(ELDLM_LOCK_ABORTED);
1136
1137         LASSERT(l->l_glimpse_ast != NULL);
1138         rc = l->l_glimpse_ast(l, NULL); /* this will update the LVB */
1139         if (rc != 0 && res->lr_namespace->ns_lvbo &&
1140             res->lr_namespace->ns_lvbo->lvbo_update) {
1141                 res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
1142         }
1143
1144         down(&res->lr_lvb_sem);
1145         reply_lvb->lvb_size = res_lvb->lvb_size;
1146         up(&res->lr_lvb_sem);
1147
1148         LDLM_LOCK_PUT(l);
1149
1150         RETURN(ELDLM_LOCK_ABORTED);
1151 }
1152
1153 /* mount the file system (secretly) */
1154 int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
1155                         char *option)
1156 {
1157         struct lustre_cfg* lcfg = buf;
1158         struct filter_obd *filter = &obd->u.filter;
1159         struct vfsmount *mnt;
1160         int rc = 0;
1161         ENTRY;
1162
1163         dev_clear_rdonly(2);
1164
1165         if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
1166                 RETURN(-EINVAL);
1167
1168         obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
1169         if (IS_ERR(obd->obd_fsops))
1170                 RETURN(PTR_ERR(obd->obd_fsops));
1171
1172         mnt = do_kern_mount(lcfg->lcfg_inlbuf2, MS_NOATIME | MS_NODIRATIME,
1173                             lcfg->lcfg_inlbuf1, option);
1174         rc = PTR_ERR(mnt);
1175         if (IS_ERR(mnt))
1176                 GOTO(err_ops, rc);
1177
1178         if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3) {
1179                 if (*lcfg->lcfg_inlbuf3 == 'f') {
1180                         obd->obd_replayable = 1;
1181                         obd_sync_filter = 1;
1182                         CWARN("%s: recovery enabled\n", obd->obd_name);
1183                 } else {
1184                         if (*lcfg->lcfg_inlbuf3 != 'n') {
1185                                 CERROR("unrecognised flag '%c'\n",
1186                                        *lcfg->lcfg_inlbuf3);
1187                         }
1188                         // XXX Robert? Why do we get errors here
1189                         // GOTO(err_mntput, rc = -EINVAL);
1190                 }
1191         }
1192
1193         filter->fo_vfsmnt = mnt;
1194         filter->fo_sb = mnt->mnt_sb;
1195         filter->fo_fstype = mnt->mnt_sb->s_type->name;
1196         CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
1197
1198         OBD_SET_CTXT_MAGIC(&obd->obd_ctxt);
1199         obd->obd_ctxt.pwdmnt = mnt;
1200         obd->obd_ctxt.pwd = mnt->mnt_root;
1201         obd->obd_ctxt.fs = get_ds();
1202         obd->obd_ctxt.cb_ops = filter_lvfs_ops;
1203
1204         rc = filter_prep(obd);
1205         if (rc)
1206                 GOTO(err_mntput, rc);
1207
1208         spin_lock_init(&filter->fo_translock);
1209         spin_lock_init(&filter->fo_objidlock);
1210         INIT_LIST_HEAD(&filter->fo_export_list);
1211         sema_init(&filter->fo_alloc_lock, 1);
1212         spin_lock_init(&filter->fo_r_pages.oh_lock);
1213         spin_lock_init(&filter->fo_w_pages.oh_lock);
1214         spin_lock_init(&filter->fo_r_discont_pages.oh_lock);
1215         spin_lock_init(&filter->fo_w_discont_pages.oh_lock);
1216         spin_lock_init(&filter->fo_r_discont_blocks.oh_lock);
1217         spin_lock_init(&filter->fo_w_discont_blocks.oh_lock);
1218         filter->fo_readcache_max_filesize = FILTER_MAX_CACHE_SIZE;
1219
1220         obd->obd_namespace = ldlm_namespace_new("filter-tgt",
1221                                                 LDLM_NAMESPACE_SERVER);
1222         if (obd->obd_namespace == NULL)
1223                 GOTO(err_post, rc = -ENOMEM);
1224         obd->obd_namespace->ns_lvbp = obd;
1225         obd->obd_namespace->ns_lvbo = &filter_lvbo;
1226         ldlm_register_intent(obd->obd_namespace, filter_intent_policy);
1227
1228         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1229                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1230
1231         rc = llog_cat_initialize(obd, 1);
1232         if (rc) {
1233                 CERROR("failed to setup llogging subsystems\n");
1234                 GOTO(err_post, rc);
1235         }
1236
1237         RETURN(0);
1238
1239 err_post:
1240         filter_post(obd);
1241 err_mntput:
1242         unlock_kernel();
1243         mntput(mnt);
1244         filter->fo_sb = 0;
1245         lock_kernel();
1246 err_ops:
1247         fsfilt_put_ops(obd->obd_fsops);
1248         return rc;
1249 }
1250
1251 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
1252 {
1253         struct lustre_cfg* lcfg = buf;
1254         const char *str = NULL;
1255         char *option = NULL;
1256         int n = 0;
1257         int rc;
1258
1259         if (!strcmp(lcfg->lcfg_inlbuf2, "ext3")) {
1260 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1261         /* bug 1577: implement async-delete for 2.5 */
1262                 str = "errors=remount-ro,asyncdel";
1263 #else
1264                 str = "errors=remount-ro";
1265 #endif
1266                 n = strlen(str) + 1;
1267                 OBD_ALLOC(option, n);
1268                 if (option == NULL)
1269                         RETURN(-ENOMEM);
1270                 strcpy(option, str);
1271         }
1272
1273         rc = filter_common_setup(obd, len, buf, option);
1274         if (option)
1275                 OBD_FREE(option, n);
1276         return rc;
1277 }
1278
1279 static int filter_cleanup(struct obd_device *obd, int flags)
1280 {
1281         struct filter_obd *filter = &obd->u.filter;
1282         ENTRY;
1283
1284         if (flags & OBD_OPT_FAILOVER)
1285                 CERROR("%s: shutting down for failover; client state will"
1286                        " be preserved.\n", obd->obd_name);
1287
1288         if (!list_empty(&obd->obd_exports)) {
1289                 CERROR("%s: still has clients!\n", obd->obd_name);
1290                 class_disconnect_exports(obd, flags);
1291                 if (!list_empty(&obd->obd_exports)) {
1292                         CERROR("still has exports after forced cleanup?\n");
1293                         RETURN(-EBUSY);
1294                 }
1295         }
1296
1297         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
1298
1299         if (filter->fo_sb == NULL)
1300                 RETURN(0);
1301
1302         filter_post(obd);
1303
1304         shrink_dcache_parent(filter->fo_sb->s_root);
1305         filter->fo_sb = 0;
1306
1307         if (atomic_read(&filter->fo_vfsmnt->mnt_count) > 1)
1308                 CERROR("%s: mount point %p busy, mnt_count: %d\n",
1309                        obd->obd_name, filter->fo_vfsmnt,
1310                        atomic_read(&filter->fo_vfsmnt->mnt_count));
1311
1312         unlock_kernel();
1313         mntput(filter->fo_vfsmnt);
1314         //destroy_buffers(filter->fo_sb->s_dev);
1315         filter->fo_sb = NULL;
1316         fsfilt_put_ops(obd->obd_fsops);
1317         lock_kernel();
1318
1319         dev_clear_rdonly(2);
1320
1321         RETURN(0);
1322 }
1323
1324 static int filter_attach(struct obd_device *obd, obd_count len, void *data)
1325 {
1326         struct lprocfs_static_vars lvars;
1327         int rc;
1328
1329         lprocfs_init_vars(filter, &lvars);
1330         rc = lprocfs_obd_attach(obd, lvars.obd_vars);
1331         if (rc != 0)
1332                 return rc;
1333
1334         rc = lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST);
1335         if (rc != 0)
1336                 return rc;
1337
1338         /* Init obdfilter private stats here */
1339         lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
1340                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
1341         lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
1342                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
1343
1344         return lproc_filter_attach_seqstat(obd);
1345 }
1346
1347 static int filter_detach(struct obd_device *dev)
1348 {
1349         lprocfs_free_obd_stats(dev);
1350         return lprocfs_obd_detach(dev);
1351 }
1352
1353 /* nearly identical to mds_connect */
1354 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
1355                           struct obd_uuid *cluuid)
1356 {
1357         struct obd_export *exp;
1358         struct filter_export_data *fed;
1359         struct filter_client_data *fcd = NULL;
1360         struct filter_obd *filter = &obd->u.filter;
1361         int rc;
1362         ENTRY;
1363
1364         if (conn == NULL || obd == NULL || cluuid == NULL)
1365                 RETURN(-EINVAL);
1366
1367         rc = class_connect(conn, obd, cluuid);
1368         if (rc)
1369                 RETURN(rc);
1370         exp = class_conn2export(conn);
1371         LASSERT(exp != NULL);
1372
1373         fed = &exp->exp_filter_data;
1374
1375         spin_lock_init(&fed->fed_lock);
1376
1377         if (!obd->obd_replayable)
1378                 GOTO(cleanup, rc = 0);
1379
1380         OBD_ALLOC(fcd, sizeof(*fcd));
1381         if (!fcd) {
1382                 CERROR("filter: out of memory for client data\n");
1383                 GOTO(cleanup, rc = -ENOMEM);
1384         }
1385
1386         memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
1387         fed->fed_fcd = fcd;
1388
1389         rc = filter_client_add(obd, filter, fed, -1);
1390
1391 cleanup:
1392         if (rc) {
1393                 if (fcd)
1394                         OBD_FREE(fcd, sizeof(*fcd));
1395                 class_disconnect(exp, 0);
1396         } else {
1397                 class_export_put(exp);
1398         }
1399         return rc;
1400 }
1401
1402 static int filter_precleanup(struct obd_device *obd, int flags)
1403 {
1404         int rc = 0;
1405         ENTRY;
1406
1407         rc = obd_llog_finish(obd, 0);
1408         if (rc)
1409                 CERROR("failed to cleanup llogging subsystem\n");
1410
1411         RETURN(rc);
1412 }
1413
1414 /* Do extra sanity checks for grant accounting.  We do this at connect,
1415  * disconnect, and statfs RPC time, so it shouldn't be too bad.  We can
1416  * always get rid of it or turn it off when we know accounting is good. */
1417 static void filter_grant_sanity_check(struct obd_device *obd, char *func)
1418 {
1419         struct filter_export_data *fed;
1420         struct obd_export *exp;
1421         obd_size maxsize = obd->obd_osfs.os_blocks * obd->obd_osfs.os_bsize;
1422         obd_size tot_dirty = 0, tot_pending = 0, tot_granted = 0;
1423         obd_size fo_tot_dirty, fo_tot_pending, fo_tot_granted;
1424
1425         if (list_empty(&obd->obd_exports))
1426                 return;
1427
1428         spin_lock(&obd->obd_osfs_lock);
1429         spin_lock(&obd->obd_dev_lock);
1430         list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1431                 fed = &exp->exp_filter_data;
1432                 LASSERTF(fed->fed_grant + fed->fed_pending <= maxsize,
1433                          "cli %s/%p %lu+%lu > "LPU64"\n",
1434                          exp->exp_client_uuid.uuid, exp,
1435                          fed->fed_grant, fed->fed_pending, maxsize);
1436                 LASSERTF(fed->fed_dirty <= maxsize, "cli %s/%p %lu > "LPU64"\n",
1437                          exp->exp_client_uuid.uuid, exp,fed->fed_dirty,maxsize);
1438                 CDEBUG(D_CACHE,"%s: cli %s/%p dirty %lu pend %lu grant %lu\n",
1439                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
1440                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1441                 tot_granted += fed->fed_grant + fed->fed_pending;
1442                 tot_pending += fed->fed_pending;
1443                 tot_dirty += fed->fed_dirty;
1444         }
1445         fo_tot_granted = obd->u.filter.fo_tot_granted;
1446         fo_tot_pending = obd->u.filter.fo_tot_pending;
1447         fo_tot_dirty = obd->u.filter.fo_tot_dirty;
1448         spin_unlock(&obd->obd_dev_lock);
1449         spin_unlock(&obd->obd_osfs_lock);
1450
1451         /* Do these assertions outside the spinlocks so we don't kill system */
1452         if (tot_granted != fo_tot_granted)
1453                 CERROR("%s: tot_granted "LPU64" != fo_tot_granted "LPU64"\n",
1454                        func, tot_granted, fo_tot_granted);
1455         if (tot_pending != fo_tot_pending)
1456                 CERROR("%s: tot_pending "LPU64" != fo_tot_pending "LPU64"\n",
1457                        func, tot_pending, fo_tot_pending);
1458         if (tot_dirty != fo_tot_dirty)
1459                 CERROR("%s: tot_dirty "LPU64" != fo_tot_dirty "LPU64"\n",
1460                        func, tot_dirty, fo_tot_dirty);
1461         if (tot_pending > tot_granted)
1462                 CERROR("%s: tot_pending "LPU64" > tot_granted "LPU64"\n",
1463                        func, tot_pending, tot_granted);
1464         if (tot_granted > maxsize)
1465                 CERROR("%s: tot_granted "LPU64" > maxsize "LPU64"\n",
1466                        func, tot_granted, maxsize);
1467         if (tot_dirty > maxsize)
1468                 CERROR("%s: tot_dirty "LPU64" > maxsize "LPU64"\n",
1469                        func, tot_dirty, maxsize);
1470 }
1471
1472 /* Remove this client from the grant accounting totals.  We also remove
1473  * the export from the obd device under the osfs and dev locks to ensure
1474  * that the filter_grant_sanity_check() calculations are always valid.
1475  * The client should do something similar when it invalidates its import. */
1476 static void filter_grant_discard(struct obd_export *exp)
1477 {
1478         struct obd_device *obd = exp->exp_obd;
1479         struct filter_obd *filter = &obd->u.filter;
1480         struct filter_export_data *fed = &exp->exp_filter_data;
1481
1482         spin_lock(&obd->obd_osfs_lock);
1483         spin_lock(&exp->exp_obd->obd_dev_lock);
1484         list_del_init(&exp->exp_obd_chain);
1485         spin_unlock(&exp->exp_obd->obd_dev_lock);
1486
1487         CDEBUG(D_CACHE, "%s: cli %s/%p dirty %lu pend %lu grant %lu\n",
1488                obd->obd_name, exp->exp_client_uuid.uuid, exp,
1489                fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1490
1491         LASSERTF(filter->fo_tot_granted >= fed->fed_grant,
1492                  "%s: tot_granted "LPU64" cli %s/%p fed_grant %lu\n",
1493                  obd->obd_name, filter->fo_tot_granted,
1494                  exp->exp_client_uuid.uuid, exp, fed->fed_grant);
1495         filter->fo_tot_granted -= fed->fed_grant;
1496         LASSERTF(exp->exp_obd->u.filter.fo_tot_pending >= fed->fed_pending,
1497                  "%s: tot_pending "LPU64" cli %s/%p fed_pending %lu\n",
1498                  obd->obd_name, filter->fo_tot_pending,
1499                  exp->exp_client_uuid.uuid, exp, fed->fed_pending);
1500         LASSERTF(filter->fo_tot_dirty >= fed->fed_dirty,
1501                  "%s: tot_dirty "LPU64" cli %s/%p fed_dirty %lu\n",
1502                  obd->obd_name, filter->fo_tot_dirty,
1503                  exp->exp_client_uuid.uuid, exp, fed->fed_dirty);
1504         filter->fo_tot_dirty -= fed->fed_dirty;
1505         fed->fed_dirty = 0;
1506         fed->fed_grant = 0;
1507
1508         spin_unlock(&obd->obd_osfs_lock);
1509 }
1510
1511 static int filter_destroy_export(struct obd_export *exp)
1512 {
1513         ENTRY;
1514
1515         if (exp->exp_filter_data.fed_pending)
1516                 CERROR("%s: cli %s/%p has %lu pending on destroyed export\n",
1517                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
1518                        exp, exp->exp_filter_data.fed_pending);
1519
1520         target_destroy_export(exp);
1521
1522         if (exp->exp_obd->obd_replayable)
1523                 filter_client_free(exp, exp->exp_flags);
1524
1525         filter_grant_discard(exp);
1526         if (!(exp->exp_flags & OBD_OPT_FORCE))
1527                 filter_grant_sanity_check(exp->exp_obd, __FUNCTION__);
1528
1529         RETURN(0);
1530 }
1531
1532 /* also incredibly similar to mds_disconnect */
1533 static int filter_disconnect(struct obd_export *exp, int flags)
1534 {
1535         struct obd_device *obd = exp->exp_obd;
1536         unsigned long irqflags;
1537         struct llog_ctxt *ctxt;
1538         int rc;
1539         ENTRY;
1540
1541         LASSERT(exp);
1542         class_export_get(exp);
1543
1544         spin_lock_irqsave(&exp->exp_lock, irqflags);
1545         exp->exp_flags = flags;
1546         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
1547
1548         if (!(flags & OBD_OPT_FORCE))
1549                 filter_grant_sanity_check(obd, __FUNCTION__);
1550         filter_grant_discard(exp);
1551
1552         /* Disconnect early so that clients can't keep using export */
1553         rc = class_disconnect(exp, flags);
1554
1555         ldlm_cancel_locks_for_export(exp);
1556
1557         fsfilt_sync(obd, obd->u.filter.fo_sb);
1558
1559         /* flush any remaining cancel messages out to the target */
1560         ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
1561         llog_sync(ctxt, exp);
1562
1563         class_export_put(exp);
1564         RETURN(rc);
1565 }
1566
1567 struct dentry *__filter_oa2dentry(struct obd_device *obd,
1568                                   struct obdo *oa, const char *what)
1569 {
1570         struct dentry *dchild = NULL;
1571         obd_gr group = 0;
1572
1573         if (oa->o_valid & OBD_MD_FLGROUP)
1574                 group = oa->o_gr;
1575
1576         dchild = filter_fid2dentry(obd, NULL, group, oa->o_id);
1577
1578         if (IS_ERR(dchild)) {
1579                 CERROR("%s error looking up object: "LPU64"\n", what, oa->o_id);
1580                 RETURN(dchild);
1581         }
1582
1583         if (dchild->d_inode == NULL) {
1584                 CERROR("%s on non-existent object: "LPU64"\n", what, oa->o_id);
1585                 f_dput(dchild);
1586                 RETURN(ERR_PTR(-ENOENT));
1587         }
1588
1589         return dchild;
1590 }
1591
1592 static int filter_getattr(struct obd_export *exp, struct obdo *oa,
1593                           struct lov_stripe_md *md)
1594 {
1595         struct dentry *dentry = NULL;
1596         struct obd_device *obd;
1597         int rc = 0;
1598         ENTRY;
1599
1600         obd = class_exp2obd(exp);
1601         if (obd == NULL) {
1602                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1603                        exp->exp_handle.h_cookie);
1604                 RETURN(-EINVAL);
1605         }
1606
1607         dentry = filter_oa2dentry(obd, oa);
1608         if (IS_ERR(dentry))
1609                 RETURN(PTR_ERR(dentry));
1610
1611         /* Limit the valid bits in the return data to what we actually use */
1612         oa->o_valid = OBD_MD_FLID;
1613         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1614
1615         f_dput(dentry);
1616         RETURN(rc);
1617 }
1618
1619 /* this is called from filter_truncate() until we have filter_punch() */
1620 static int filter_setattr(struct obd_export *exp, struct obdo *oa,
1621                           struct lov_stripe_md *md, struct obd_trans_info *oti)
1622 {
1623         struct obd_run_ctxt saved;
1624         struct filter_obd *filter;
1625         struct dentry *dentry;
1626         struct iattr iattr;
1627         struct ldlm_res_id res_id = { .name = { oa->o_id } };
1628         struct ldlm_resource *res;
1629         void *handle;
1630         int rc, rc2;
1631         ENTRY;
1632
1633         LASSERT(oti != NULL);
1634
1635         dentry = filter_oa2dentry(exp->exp_obd, oa);
1636         if (IS_ERR(dentry))
1637                 RETURN(PTR_ERR(dentry));
1638
1639         filter = &exp->exp_obd->u.filter;
1640
1641         iattr_from_obdo(&iattr, oa, oa->o_valid);
1642
1643         push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1644         lock_kernel();
1645
1646         if (iattr.ia_valid & ATTR_SIZE)
1647                 down(&dentry->d_inode->i_sem);
1648         handle = fsfilt_start(exp->exp_obd, dentry->d_inode, FSFILT_OP_SETATTR,
1649                               oti);
1650         if (IS_ERR(handle))
1651                 GOTO(out_unlock, rc = PTR_ERR(handle));
1652
1653         /* XXX this could be a rwsem instead, if filter_preprw played along */
1654         if (iattr.ia_valid & ATTR_ATTR_FLAG)
1655                 rc = fsfilt_iocontrol(exp->exp_obd, dentry->d_inode, NULL,
1656                                       EXT3_IOC_SETFLAGS,
1657                                       (long)&iattr.ia_attr_flags);
1658         else
1659                 rc = fsfilt_setattr(exp->exp_obd, dentry, handle, &iattr, 1);
1660         rc = filter_finish_transno(exp, oti, rc);
1661         rc2 = fsfilt_commit(exp->exp_obd, dentry->d_inode, handle, 0);
1662         if (rc2) {
1663                 CERROR("error on commit, err = %d\n", rc2);
1664                 if (!rc)
1665                         rc = rc2;
1666         }
1667
1668         if (iattr.ia_valid & ATTR_SIZE) {
1669                 res = ldlm_resource_get(exp->exp_obd->obd_namespace, NULL,
1670                                         res_id, LDLM_EXTENT, 0);
1671                 if (res == NULL) {
1672                         CERROR("!!! resource_get failed for object "LPU64" -- "
1673                                "filter_setattr with no lock?\n", oa->o_id);
1674                 } else {
1675                         if (res->lr_namespace->ns_lvbo &&
1676                             res->lr_namespace->ns_lvbo->lvbo_update) {
1677                                 rc = res->lr_namespace->ns_lvbo->lvbo_update
1678                                         (res, NULL, 0, 0);
1679                         }
1680                         ldlm_resource_putref(res);
1681                 }
1682         }
1683
1684         oa->o_valid = OBD_MD_FLID;
1685         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1686
1687 out_unlock:
1688         if (iattr.ia_valid & ATTR_SIZE)
1689                 up(&dentry->d_inode->i_sem);
1690         unlock_kernel();
1691         pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1692
1693         f_dput(dentry);
1694         RETURN(rc);
1695 }
1696
1697 /* XXX identical to osc_unpackmd */
1698 static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
1699                            struct lov_mds_md *lmm, int lmm_bytes)
1700 {
1701         int lsm_size;
1702         ENTRY;
1703
1704         if (lmm != NULL) {
1705                 if (lmm_bytes < sizeof (*lmm)) {
1706                         CERROR("lov_mds_md too small: %d, need %d\n",
1707                                lmm_bytes, (int)sizeof(*lmm));
1708                         RETURN(-EINVAL);
1709                 }
1710                 /* XXX LOV_MAGIC etc check? */
1711
1712                 if (lmm->lmm_object_id == cpu_to_le64(0)) {
1713                         CERROR("lov_mds_md: zero lmm_object_id\n");
1714                         RETURN(-EINVAL);
1715                 }
1716         }
1717
1718         lsm_size = lov_stripe_md_size(1);
1719         if (lsmp == NULL)
1720                 RETURN(lsm_size);
1721
1722         if (*lsmp != NULL && lmm == NULL) {
1723                 OBD_FREE(*lsmp, lsm_size);
1724                 *lsmp = NULL;
1725                 RETURN(0);
1726         }
1727
1728         if (*lsmp == NULL) {
1729                 OBD_ALLOC(*lsmp, lsm_size);
1730                 if (*lsmp == NULL)
1731                         RETURN(-ENOMEM);
1732
1733                 loi_init((*lsmp)->lsm_oinfo);
1734         }
1735
1736         if (lmm != NULL) {
1737                 /* XXX zero *lsmp? */
1738                 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
1739                 LASSERT((*lsmp)->lsm_object_id);
1740         }
1741
1742         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
1743
1744         RETURN(lsm_size);
1745 }
1746
1747 static void filter_destroy_precreated(struct obd_export *exp, struct obdo *oa,
1748                                       struct filter_obd *filter)
1749 {
1750         struct obdo doa; /* XXX obdo on stack */
1751         __u64 last, id;
1752         ENTRY;
1753         LASSERT(oa);
1754
1755         memset(&doa, 0, sizeof(doa));
1756         if (oa->o_valid & OBD_MD_FLGROUP)
1757                 doa.o_gr = oa->o_gr;
1758         else
1759                 doa.o_gr = 0;
1760         doa.o_mode = S_IFREG;
1761         last = filter_last_id(filter, &doa); /* FIXME: object groups */
1762         CWARN("deleting orphan objects from "LPU64" to "LPU64"\n",
1763                oa->o_id + 1, last);
1764         for (id = oa->o_id + 1; id <= last; id++) {
1765                 doa.o_id = id;
1766                 filter_destroy(exp, &doa, NULL, NULL);
1767         }
1768         spin_lock(&filter->fo_objidlock);
1769         filter->fo_last_objids[0] = oa->o_id; /* FIXME: object groups */
1770         spin_unlock(&filter->fo_objidlock);
1771         EXIT;
1772 }
1773
1774 /* returns a negative error or a nonnegative number of files to create */
1775 static int filter_should_precreate(struct obd_export *exp, struct obdo *oa,
1776                                    int group)
1777 {
1778         struct obd_device *obd = exp->exp_obd;
1779         struct filter_obd *filter = &obd->u.filter;
1780         int diff, rc;
1781         ENTRY;
1782
1783         diff = oa->o_id - filter_last_id(filter, oa);
1784         CDEBUG(D_INFO, "filter_last_id() = "LPU64" -> diff = %d\n",
1785                filter_last_id(filter, oa), diff);
1786
1787         /* delete orphans request */
1788         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1789             (oa->o_flags & OBD_FL_DELORPHAN)) {
1790                 if (diff >= 0)
1791                         RETURN(diff);
1792                 if (-diff > 10000) { /* XXX make this smarter */
1793                         CERROR("ignoring bogus orphan destroy request: obdid "
1794                                LPU64" last_id "LPU64"\n",
1795                                oa->o_id, filter_last_id(filter, oa));
1796                         RETURN(-EINVAL);
1797                 }
1798                 filter_destroy_precreated(exp, oa, filter);
1799                 rc = filter_update_last_objid(obd, group, 0);
1800                 if (rc)
1801                         CERROR("unable to write lastobjid, but orphans"
1802                                "were deleted\n");
1803                 RETURN(0);
1804         } else {
1805                 /* only precreate if group == 0 and o_id is specfied */
1806                 if (!(oa->o_valid & OBD_FL_DELORPHAN) &&
1807                     (group != 0 || oa->o_id == 0))
1808                         RETURN(1);
1809
1810                 LASSERT(diff >= 0);
1811                 RETURN(diff);
1812         }
1813 }
1814
1815 /* We rely on the fact that only one thread will be creating files in a given
1816  * group at a time, which is why we don't need an atomic filter_get_new_id.
1817  * Even if we had that atomic function, the following race would exist:
1818  *
1819  * thread 1: gets id x from filter_next_id
1820  * thread 2: gets id (x + 1) from filter_next_id
1821  * thread 2: creates object (x + 1)
1822  * thread 1: tries to create object x, gets -ENOSPC
1823  */
1824 static int filter_precreate(struct obd_device *obd, struct obdo *oa,
1825                             obd_gr group, int *num)
1826 {
1827         struct dentry *dchild = NULL;
1828         struct filter_obd *filter;
1829         struct dentry *dparent;
1830         int err = 0, rc = 0, i;
1831         __u64 next_id;
1832         int recreate_obj = 0;
1833         void *handle = NULL;
1834         ENTRY;
1835
1836         filter = &obd->u.filter;
1837
1838         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1839             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1840                 recreate_obj = 1;
1841         }
1842
1843         for (i = 0; i < *num && err == 0; i++) {
1844                 int cleanup_phase = 0;
1845
1846                 if (recreate_obj) {
1847                         __u64 last_id;
1848                         next_id = oa->o_id;
1849                         last_id = filter_last_id(filter, NULL);
1850                         if (next_id > last_id) {
1851                                 CERROR("Error: Trying to recreate obj greater"
1852                                        "than last id "LPD64" > "LPD64"\n",
1853                                        next_id, last_id);
1854                                 RETURN(-EINVAL);
1855                         }
1856                 } else
1857                         next_id = filter_last_id(filter, NULL) + 1;
1858
1859                 CDEBUG(D_INFO, "precreate objid "LPU64"\n", next_id);
1860
1861                 dparent = filter_parent_lock(obd, group, next_id);
1862                 if (IS_ERR(dparent))
1863                         GOTO(cleanup, rc = PTR_ERR(dparent));
1864                 cleanup_phase = 1;
1865
1866                 dchild = filter_fid2dentry(obd, dparent, group, next_id);
1867                 if (IS_ERR(dchild))
1868                         GOTO(cleanup, rc = PTR_ERR(dchild));
1869                 cleanup_phase = 2;
1870
1871                 if (dchild->d_inode != NULL) {
1872                         /* This would only happen if lastobjid was bad on disk*/
1873                         /* Could also happen if recreating missing obj but
1874                          * already exists
1875                          */
1876                         if (recreate_obj) {
1877                                 CERROR("Serious error: recreating obj %*s but "
1878                                        "obj already exists \n",
1879                                        dchild->d_name.len, dchild->d_name.name);
1880                         } else {
1881                                 CERROR("Serious error: objid %*s already "
1882                                        "exists; is this filesystem corrupt?\n",
1883                                        dchild->d_name.len, dchild->d_name.name);
1884                         }
1885                         GOTO(cleanup, rc = -EEXIST);
1886                 }
1887
1888                 handle = fsfilt_start_log(obd, dparent->d_inode,
1889                                           FSFILT_OP_CREATE, NULL, 1);
1890                 if (IS_ERR(handle))
1891                         GOTO(cleanup, rc = PTR_ERR(handle));
1892                 cleanup_phase = 3;
1893
1894                 rc = ll_vfs_create(dparent->d_inode, dchild, S_IFREG, NULL);
1895                 if (rc) {
1896                         CERROR("create failed rc = %d\n", rc);
1897                         GOTO(cleanup, rc);
1898                 }
1899
1900                 if (!recreate_obj) {
1901                         filter_set_last_id(filter, NULL, next_id);
1902                         err = filter_update_last_objid(obd, group, 0);
1903                         if (err)
1904                                 CERROR("unable to write lastobjid "
1905                                        "but file created\n");
1906                 }
1907
1908         cleanup:
1909                 switch(cleanup_phase) {
1910                 case 3:
1911                         err = fsfilt_commit(obd, dparent->d_inode, handle, 0);
1912                         if (err) {
1913                                 CERROR("error on commit, err = %d\n", err);
1914                                 if (!rc)
1915                                         rc = err;
1916                         }
1917                 case 2:
1918                         f_dput(dchild);
1919                 case 1:
1920                         filter_parent_unlock(dparent);
1921                 case 0:
1922                         break;
1923                 }
1924
1925                 if (rc)
1926                         break;
1927         }
1928         *num = i;
1929
1930         CDEBUG(D_INFO, "filter_precreate() created %d objects\n", i);
1931         RETURN(rc);
1932 }
1933
1934 static int filter_create(struct obd_export *exp, struct obdo *oa,
1935                          struct lov_stripe_md **ea, struct obd_trans_info *oti)
1936 {
1937         struct obd_device *obd = NULL;
1938         struct obd_run_ctxt saved;
1939         struct lov_stripe_md *lsm = NULL;
1940         obd_gr group = 0;
1941         int rc = 0, diff;
1942         ENTRY;
1943
1944         if (oa->o_valid & OBD_MD_FLGROUP)
1945                 group = oa->o_gr;
1946
1947         CDEBUG(D_INFO, "filter_create(od->o_gr="LPU64",od->o_id="LPU64")\n",
1948                group, oa->o_id);
1949         if (ea != NULL) {
1950                 lsm = *ea;
1951                 if (lsm == NULL) {
1952                         rc = obd_alloc_memmd(exp, &lsm);
1953                         if (rc < 0)
1954                                 RETURN(rc);
1955                 }
1956         }
1957
1958         obd = exp->exp_obd;
1959         push_ctxt(&saved, &obd->obd_ctxt, NULL);
1960
1961         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1962             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1963                 if (oa->o_id > filter_last_id(&obd->u.filter, oa)) {
1964                         CERROR("recreate objid "LPU64" > last id "LPU64"\n",
1965                                oa->o_id, filter_last_id(&obd->u.filter, oa));
1966                         rc = -EINVAL;
1967                 } else {
1968                         diff = 1;
1969                         rc = filter_precreate(obd, oa, group, &diff);
1970                 }
1971         } else {
1972                 diff = filter_should_precreate(exp, oa, group);
1973                 if (diff > 0) {
1974                         oa->o_id = filter_last_id(&obd->u.filter, oa);
1975                         rc = filter_precreate(obd, oa, group, &diff);
1976                         oa->o_id += diff;
1977                         oa->o_valid = OBD_MD_FLID;
1978                 }
1979         }
1980
1981         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1982         if (rc && ea != NULL && *ea != lsm) {
1983                 obd_free_memmd(exp, &lsm);
1984         } else if (rc == 0 && ea != NULL) {
1985                 /* XXX LOV STACKING: the lsm that is passed to us from
1986                  * LOV does not have valid lsm_oinfo data structs, so
1987                  * don't go touching that.  This needs to be fixed in a
1988                  * big way. */
1989                 lsm->lsm_object_id = oa->o_id;
1990                 *ea = lsm;
1991         }
1992
1993         RETURN(rc);
1994 }
1995
1996 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
1997                           struct lov_stripe_md *ea, struct obd_trans_info *oti)
1998 {
1999         struct obd_device *obd;
2000         struct filter_obd *filter;
2001         struct dentry *dchild = NULL, *dparent = NULL;
2002         struct obd_run_ctxt saved;
2003         void *handle = NULL;
2004         struct llog_cookie *fcc = NULL;
2005         int rc, rc2, cleanup_phase = 0, have_prepared = 0;
2006         obd_gr group = 0;
2007         ENTRY;
2008
2009         if (oa->o_valid & OBD_MD_FLGROUP)
2010                 group = oa->o_gr;
2011
2012         obd = exp->exp_obd;
2013         filter = &obd->u.filter;
2014
2015         push_ctxt(&saved, &obd->obd_ctxt, NULL);
2016
2017  acquire_locks:
2018         dparent = filter_parent_lock(obd, group, oa->o_id);
2019         if (IS_ERR(dparent))
2020                 GOTO(cleanup, rc = PTR_ERR(dparent));
2021         cleanup_phase = 1;
2022
2023         dchild = filter_fid2dentry(obd, dparent, group, oa->o_id);
2024         if (IS_ERR(dchild))
2025                 GOTO(cleanup, rc = -ENOENT);
2026         cleanup_phase = 2;
2027
2028         if (dchild->d_inode == NULL) {
2029                 CDEBUG(D_INODE, "destroying non-existent object "LPU64"\n",
2030                        oa->o_id);
2031                 GOTO(cleanup, rc = -ENOENT);
2032         }
2033
2034         if (!have_prepared) {
2035                 /* If we're really going to destroy the object, get ready
2036                  * by getting the clients to discard their cached data.
2037                  *
2038                  * We have to drop the parent lock, because
2039                  * filter_prepare_destroy will acquire a PW on the object, and
2040                  * we don't want to deadlock with an incoming write to the
2041                  * object, which has the extent PW and then wants to get the
2042                  * parent dentry to do the lookup.
2043                  *
2044                  * We dput the child because it's not worth the extra
2045                  * complication of condition the above code to skip it on the
2046                  * second time through. */
2047                 f_dput(dchild);
2048                 filter_parent_unlock(dparent);
2049
2050                 filter_prepare_destroy(obd, oa->o_id);
2051                 have_prepared = 1;
2052                 goto acquire_locks;
2053         }
2054
2055         handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_UNLINK,oti,1);
2056         if (IS_ERR(handle))
2057                 GOTO(cleanup, rc = PTR_ERR(handle));
2058         cleanup_phase = 3;
2059
2060         /* Our MDC connection is established by the MDS to us */
2061         if (oa->o_valid & OBD_MD_FLCOOKIE) {
2062                 OBD_ALLOC(fcc, sizeof(*fcc));
2063                 if (fcc != NULL)
2064                         memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
2065         }
2066
2067         rc = filter_destroy_internal(obd, oa->o_id, dparent, dchild);
2068
2069 cleanup:
2070         switch(cleanup_phase) {
2071         case 3:
2072                 if (fcc != NULL) {
2073                         if (oti != NULL)
2074                                 fsfilt_add_journal_cb(obd, 0, oti->oti_handle,
2075                                                       filter_cancel_cookies_cb,
2076                                                       fcc);
2077                         else
2078                                 fsfilt_add_journal_cb(obd, 0, handle,
2079                                                       filter_cancel_cookies_cb,
2080                                                       fcc);
2081                 }
2082                 rc = filter_finish_transno(exp, oti, rc);
2083                 rc2 = fsfilt_commit(obd, dparent->d_inode, handle, 0);
2084                 if (rc2) {
2085                         CERROR("error on commit, err = %d\n", rc2);
2086                         if (!rc)
2087                                 rc = rc2;
2088                 }
2089         case 2:
2090                 f_dput(dchild);
2091         case 1:
2092                 filter_parent_unlock(dparent);
2093         case 0:
2094                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
2095                 break;
2096         default:
2097                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2098                 LBUG();
2099         }
2100
2101         RETURN(rc);
2102 }
2103
2104 /* NB start and end are used for punch, but not truncate */
2105 static int filter_truncate(struct obd_export *exp, struct obdo *oa,
2106                            struct lov_stripe_md *lsm,
2107                            obd_off start, obd_off end,
2108                            struct obd_trans_info *oti)
2109 {
2110         int error;
2111         ENTRY;
2112
2113         if (end != OBD_OBJECT_EOF)
2114                 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
2115                        end);
2116
2117         CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = %x, "
2118                "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
2119         oa->o_size = start;
2120         error = filter_setattr(exp, oa, NULL, oti);
2121         RETURN(error);
2122 }
2123
2124 static int filter_sync(struct obd_export *exp, struct obdo *oa,
2125                        struct lov_stripe_md *lsm, obd_off start, obd_off end)
2126 {
2127         struct obd_run_ctxt saved;
2128         struct filter_obd *filter;
2129         struct dentry *dentry;
2130         struct llog_ctxt *ctxt;
2131         int rc, rc2;
2132         ENTRY;
2133
2134         filter = &exp->exp_obd->u.filter;
2135
2136         /* an objid of zero is taken to mean "sync whole filesystem" */
2137         if (!oa || !(oa->o_valid & OBD_MD_FLID)) {
2138                 rc = fsfilt_sync(exp->exp_obd, filter->fo_sb);
2139                 /* flush any remaining cancel messages out to the target */
2140                 ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_REPL_CTXT);
2141                 llog_sync(ctxt, exp);
2142                 RETURN(rc);
2143         }
2144
2145         dentry = filter_oa2dentry(exp->exp_obd, oa);
2146         if (IS_ERR(dentry))
2147                 RETURN(PTR_ERR(dentry));
2148
2149         push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
2150
2151         down(&dentry->d_inode->i_sem);
2152         rc = filemap_fdatasync(dentry->d_inode->i_mapping);
2153         if (rc == 0) {
2154                 /* just any file to grab fsync method - "file" arg unused */
2155                 struct file *file = filter->fo_rcvd_filp;
2156
2157                 if (file->f_op && file->f_op->fsync)
2158                         rc = file->f_op->fsync(NULL, dentry, 1);
2159
2160                 rc2 = filemap_fdatawait(dentry->d_inode->i_mapping);
2161                 if (!rc)
2162                         rc = rc2;
2163         }
2164         up(&dentry->d_inode->i_sem);
2165
2166         oa->o_valid = OBD_MD_FLID;
2167         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2168
2169         pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
2170
2171         f_dput(dentry);
2172         RETURN(rc);
2173 }
2174
2175 static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2176                          unsigned long max_age)
2177 {
2178         struct filter_obd *filter = &obd->u.filter;
2179         int blockbits = filter->fo_sb->s_blocksize_bits;
2180         int rc;
2181         ENTRY;
2182
2183         /* at least try to account for cached pages.  its still racey and
2184          * might be under-reporting if clients haven't announced their
2185          * caches with brw recently */
2186         spin_lock(&obd->obd_osfs_lock);
2187         rc = fsfilt_statfs(obd, filter->fo_sb, max_age);
2188         memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
2189         spin_unlock(&obd->obd_osfs_lock);
2190
2191         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
2192                " pending "LPU64" free "LPU64" avail "LPU64"\n",
2193                filter->fo_tot_dirty, filter->fo_tot_granted,
2194                filter->fo_tot_pending,
2195                osfs->os_bfree << blockbits, osfs->os_bavail << blockbits);
2196
2197         filter_grant_sanity_check(obd, __FUNCTION__);
2198
2199         osfs->os_bavail -= min(osfs->os_bavail,
2200                                (filter->fo_tot_dirty + filter->fo_tot_pending +
2201                                 osfs->os_bsize -1) >> blockbits);
2202
2203         RETURN(rc);
2204 }
2205
2206 static int filter_get_info(struct obd_export *exp, __u32 keylen,
2207                            void *key, __u32 *vallen, void *val)
2208 {
2209         struct obd_device *obd;
2210         ENTRY;
2211
2212         obd = class_exp2obd(exp);
2213         if (obd == NULL) {
2214                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2215                        exp->exp_handle.h_cookie);
2216                 RETURN(-EINVAL);
2217         }
2218
2219         if (keylen == strlen("blocksize") &&
2220             memcmp(key, "blocksize", keylen) == 0) {
2221                 __u32 *blocksize = val;
2222                 *vallen = sizeof(*blocksize);
2223                 *blocksize = obd->u.filter.fo_sb->s_blocksize;
2224                 RETURN(0);
2225         }
2226
2227         if (keylen == strlen("blocksize_bits") &&
2228             memcmp(key, "blocksize_bits", keylen) == 0) {
2229                 __u32 *blocksize_bits = val;
2230                 *vallen = sizeof(*blocksize_bits);
2231                 *blocksize_bits = obd->u.filter.fo_sb->s_blocksize_bits;
2232                 RETURN(0);
2233         }
2234
2235         if (keylen >= strlen("last_id") && memcmp(key, "last_id", 7) == 0) {
2236                 obd_id *last_id = val;
2237                 /* FIXME: object groups */
2238                 *last_id = filter_last_id(&obd->u.filter, 0);
2239                 RETURN(0);
2240         }
2241         CDEBUG(D_IOCTL, "invalid key\n");
2242         RETURN(-EINVAL);
2243 }
2244
2245 static int filter_set_info(struct obd_export *exp, __u32 keylen,
2246                            void *key, __u32 vallen, void *val)
2247 {
2248         struct obd_device *obd;
2249         struct lustre_handle conn;
2250         struct llog_ctxt *ctxt;
2251         int rc = 0;
2252         ENTRY;
2253
2254         conn.cookie = exp->exp_handle.h_cookie;
2255
2256         obd = exp->exp_obd;
2257         if (obd == NULL) {
2258                 CDEBUG(D_IOCTL, "invalid exp %p cookie "LPX64"\n",
2259                        exp, conn.cookie);
2260                 RETURN(-EINVAL);
2261         }
2262
2263         if (keylen < strlen("mds_conn") ||
2264             memcmp(key, "mds_conn", keylen) != 0)
2265                 RETURN(-EINVAL);
2266
2267         CWARN("Received MDS connection ("LPX64")\n", conn.cookie);
2268         memcpy(&obd->u.filter.fo_mdc_conn, &conn, sizeof(conn));
2269         ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
2270         rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
2271         RETURN(rc);
2272 }
2273
2274 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
2275                      int len, void *karg, void *uarg)
2276 {
2277         struct obd_device *obd = exp->exp_obd;
2278         struct obd_ioctl_data *data = karg;
2279         int rc = 0;
2280
2281         switch (cmd) {
2282         case OBD_IOC_ABORT_RECOVERY:
2283                 CERROR("aborting recovery for device %s\n", obd->obd_name);
2284                 target_abort_recovery(obd);
2285                 RETURN(0);
2286
2287         case OBD_IOC_SET_READONLY: {
2288                 void *handle;
2289                 struct super_block *sb = obd->u.filter.fo_sb;
2290                 struct inode *inode = sb->s_root->d_inode;
2291                 BDEVNAME_DECLARE_STORAGE(tmp);
2292                 CERROR("setting device %s read-only\n",
2293                        ll_bdevname(sb, tmp));
2294
2295                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
2296                 LASSERT(handle);
2297                 (void)fsfilt_commit(obd, inode, handle, 1);
2298
2299                 dev_set_rdonly(ll_sbdev(obd->u.filter.fo_sb), 2);
2300                 RETURN(0);
2301         }
2302
2303         case OBD_IOC_CATLOGLIST: {
2304                 rc = llog_catlog_list(obd, 1, data);
2305                 RETURN(rc);
2306         }
2307
2308         case OBD_IOC_LLOG_CANCEL:
2309         case OBD_IOC_LLOG_REMOVE:
2310         case OBD_IOC_LLOG_INFO:
2311         case OBD_IOC_LLOG_PRINT: {
2312                 /* FIXME to be finished */
2313                 RETURN(-EOPNOTSUPP);
2314 /*
2315                 struct llog_ctxt *ctxt = NULL;
2316
2317                 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
2318                 rc = llog_ioctl(ctxt, cmd, data);
2319                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
2320
2321                 RETURN(rc);
2322 */
2323         }
2324
2325
2326         default:
2327                 RETURN(-EINVAL);
2328         }
2329         RETURN(0);
2330 }
2331
2332 static struct llog_operations filter_unlink_repl_logops;
2333 static struct llog_operations filter_size_orig_logops = {
2334         lop_setup: llog_obd_origin_setup,
2335         lop_cleanup: llog_obd_origin_cleanup,
2336         lop_add: llog_obd_origin_add
2337 };
2338
2339 static int filter_llog_init(struct obd_device *obd, struct obd_device *tgt,
2340                             int count, struct llog_catid *logid)
2341 {
2342         struct llog_ctxt *ctxt;
2343         int rc;
2344         ENTRY;
2345
2346         filter_unlink_repl_logops = llog_client_ops;
2347         filter_unlink_repl_logops.lop_cancel = llog_obd_repl_cancel;
2348         filter_unlink_repl_logops.lop_connect = llog_repl_connect;
2349         filter_unlink_repl_logops.lop_sync = llog_obd_repl_sync;
2350
2351         rc = llog_setup(obd, LLOG_UNLINK_REPL_CTXT, tgt, 0, NULL,
2352                         &filter_unlink_repl_logops);
2353         if (rc)
2354                 RETURN(rc);
2355         /* FIXME - assign unlink_cb for filter's recovery */
2356         ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
2357         ctxt->llog_proc_cb = filter_recov_log_unlink_cb;
2358
2359         rc = llog_setup(obd, LLOG_SIZE_ORIG_CTXT, tgt, 0, NULL,
2360                         &filter_size_orig_logops);
2361         RETURN(rc);
2362 }
2363
2364 static int filter_llog_finish(struct obd_device *obd, int count)
2365 {
2366         int rc;
2367         ENTRY;
2368
2369         rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_REPL_CTXT));
2370         if (rc)
2371                 RETURN(rc);
2372
2373         rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_ORIG_CTXT));
2374         RETURN(rc);
2375 }
2376
2377 static struct dentry *filter_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2378                                              void *data)
2379 {
2380         return filter_fid2dentry(data, NULL, gr, id);
2381 }
2382
2383 static struct lvfs_callback_ops filter_lvfs_ops = {
2384         l_fid2dentry:     filter_lvfs_fid2dentry,
2385 };
2386
2387 static struct obd_ops filter_obd_ops = {
2388         o_owner:          THIS_MODULE,
2389         o_attach:         filter_attach,
2390         o_detach:         filter_detach,
2391         o_get_info:       filter_get_info,
2392         o_set_info:       filter_set_info,
2393         o_setup:          filter_setup,
2394         o_precleanup:     filter_precleanup,
2395         o_cleanup:        filter_cleanup,
2396         o_connect:        filter_connect,
2397         o_disconnect:     filter_disconnect,
2398         o_statfs:         filter_statfs,
2399         o_getattr:        filter_getattr,
2400         o_unpackmd:       filter_unpackmd,
2401         o_create:         filter_create,
2402         o_setattr:        filter_setattr,
2403         o_destroy:        filter_destroy,
2404         o_brw:            filter_brw,
2405         o_punch:          filter_truncate,
2406         o_sync:           filter_sync,
2407         o_preprw:         filter_preprw,
2408         o_commitrw:       filter_commitrw,
2409         o_destroy_export: filter_destroy_export,
2410         o_llog_init:      filter_llog_init,
2411         o_llog_finish:    filter_llog_finish,
2412         o_iocontrol:      filter_iocontrol,
2413 };
2414
2415 static struct obd_ops filter_sanobd_ops = {
2416         o_owner:          THIS_MODULE,
2417         o_attach:         filter_attach,
2418         o_detach:         filter_detach,
2419         o_get_info:       filter_get_info,
2420         o_set_info:       filter_set_info,
2421         o_setup:          filter_san_setup,
2422         o_precleanup:     filter_precleanup,
2423         o_cleanup:        filter_cleanup,
2424         o_connect:        filter_connect,
2425         o_disconnect:     filter_disconnect,
2426         o_statfs:         filter_statfs,
2427         o_getattr:        filter_getattr,
2428         o_unpackmd:       filter_unpackmd,
2429         o_create:         filter_create,
2430         o_setattr:        filter_setattr,
2431         o_destroy:        filter_destroy,
2432         o_brw:            filter_brw,
2433         o_punch:          filter_truncate,
2434         o_sync:           filter_sync,
2435         o_preprw:         filter_preprw,
2436         o_commitrw:       filter_commitrw,
2437         o_san_preprw:     filter_san_preprw,
2438         o_destroy_export: filter_destroy_export,
2439         o_llog_init:      filter_llog_init,
2440         o_llog_finish:    filter_llog_finish,
2441         o_iocontrol:      filter_iocontrol,
2442 };
2443
2444 static int __init obdfilter_init(void)
2445 {
2446         struct lprocfs_static_vars lvars;
2447         int rc;
2448
2449         printk(KERN_INFO "Lustre: Filtering OBD driver; info@clusterfs.com\n");
2450
2451         lprocfs_init_vars(filter, &lvars);
2452
2453         rc = class_register_type(&filter_obd_ops, lvars.module_vars,
2454                                  OBD_FILTER_DEVICENAME);
2455         if (rc)
2456                 return rc;
2457
2458         rc = class_register_type(&filter_sanobd_ops, lvars.module_vars,
2459                                  OBD_FILTER_SAN_DEVICENAME);
2460         if (rc)
2461                 class_unregister_type(OBD_FILTER_DEVICENAME);
2462         return rc;
2463 }
2464
2465 static void __exit obdfilter_exit(void)
2466 {
2467         class_unregister_type(OBD_FILTER_SAN_DEVICENAME);
2468         class_unregister_type(OBD_FILTER_DEVICENAME);
2469 }
2470
2471 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2472 MODULE_DESCRIPTION("Lustre Filtering OBD driver");
2473 MODULE_LICENSE("GPL");
2474
2475 module_init(obdfilter_init);
2476 module_exit(obdfilter_exit);