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