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