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