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