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