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