Whamcloud - gitweb
- move the osc histogram helpers into lprocfs and rename accordingly
[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 = cpu_to_le64(filter->fo_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 & ~cpu_to_le32(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 & ~cpu_to_le32(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 mount_count: "LPU64"\n",
402                obd->obd_name, mount_count + 1);
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(fsd->fsd_last_transno))
487                         fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
488
489         }
490
491         obd->obd_last_committed = le64_to_cpu(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(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         filter->fo_mount_count = mount_count + 1;
506         fsd->fsd_mount_count = cpu_to_le64(filter->fo_mount_count);
507
508         /* save it, so mount count and last_transno is current */
509         rc = filter_update_server_data(obd, filp, filter->fo_fsd, 1);
510
511         RETURN(rc);
512
513 err_client:
514         class_disconnect_exports(obd, 0);
515 err_fsd:
516         filter_free_server_data(filter);
517         RETURN(rc);
518 }
519
520 static int filter_cleanup_groups(struct obd_device *obd)
521 {
522         struct filter_obd *filter = &obd->u.filter;
523         int i;
524         ENTRY;
525
526         if (filter->fo_dentry_O_groups != NULL &&
527             filter->fo_last_objids != NULL &&
528             filter->fo_last_objid_files != NULL) {
529                 for (i = 0; i < FILTER_GROUPS; i++) {
530                         struct dentry *dentry = filter->fo_dentry_O_groups[i];
531                         struct file *filp = filter->fo_last_objid_files[i];
532                         if (dentry != NULL) {
533                                 f_dput(dentry);
534                                 filter->fo_dentry_O_groups[i] = NULL;
535                         }
536                         if (filp != NULL) {
537                                 filp_close(filp, 0);
538                                 filter->fo_last_objid_files[i] = NULL;
539                         }
540                 }
541         }
542         if (filter->fo_dentry_O_sub != NULL && filter->fo_subdir_count) {
543                 for (i = 0; i < filter->fo_subdir_count; i++) {
544                         struct dentry *dentry = filter->fo_dentry_O_sub[i];
545                         if (dentry != NULL) {
546                                 f_dput(dentry);
547                                 filter->fo_dentry_O_sub[i] = NULL;
548                         }
549                 }
550                 OBD_FREE(filter->fo_dentry_O_sub,
551                          filter->fo_subdir_count *
552                          sizeof(*filter->fo_dentry_O_sub));
553         }
554         if (filter->fo_dentry_O_groups != NULL)
555                 OBD_FREE(filter->fo_dentry_O_groups,
556                          FILTER_GROUPS * sizeof(struct dentry *));
557         if (filter->fo_last_objids != NULL)
558                 OBD_FREE(filter->fo_last_objids,
559                          FILTER_GROUPS * sizeof(__u64));
560         if (filter->fo_last_objid_files != NULL)
561                 OBD_FREE(filter->fo_last_objid_files,
562                          FILTER_GROUPS * sizeof(struct file *));
563         RETURN(0);
564 }
565
566 /* FIXME: object groups */
567 static int filter_prep_groups(struct obd_device *obd)
568 {
569         struct filter_obd *filter = &obd->u.filter;
570         struct dentry *dentry, *O_dentry;
571         struct file *filp;
572         int i, rc = 0, cleanup_phase = 0;
573         ENTRY;
574
575         O_dentry = simple_mkdir(current->fs->pwd, "O", 0700);
576         CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
577         if (IS_ERR(O_dentry)) {
578                 rc = PTR_ERR(O_dentry);
579                 CERROR("cannot open/create O: rc = %d\n", rc);
580                 GOTO(cleanup, rc);
581         }
582         filter->fo_dentry_O = O_dentry;
583         cleanup_phase = 1; /* O_dentry */
584
585         /* Lookup "R" to tell if we're on an old OST FS and need to convert
586          * from O/R/<dir>/<objid> to O/0/<dir>/<objid>.  This can be removed
587          * some time post 1.0 when all old-style OSTs have converted along
588          * with the init_objid hack. */
589         dentry = ll_lookup_one_len("R", O_dentry, 1);
590         if (IS_ERR(dentry))
591                 GOTO(cleanup, rc = PTR_ERR(dentry));
592         if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
593                 struct dentry *O0_dentry = lookup_one_len("0", O_dentry, 1);
594                 ENTRY;
595
596                 CWARN("converting OST to new object layout\n");
597                 if (IS_ERR(O0_dentry)) {
598                         rc = PTR_ERR(O0_dentry);
599                         CERROR("error looking up O/0: rc %d\n", rc);
600                         GOTO(cleanup_R, rc);
601                 }
602
603                 if (O0_dentry->d_inode) {
604                         CERROR("Both O/R and O/0 exist. Fix manually.\n");
605                         GOTO(cleanup_O0, rc = -EEXIST);
606                 }
607
608                 down(&O_dentry->d_inode->i_sem);
609                 rc = vfs_rename(O_dentry->d_inode, dentry,
610                                 O_dentry->d_inode, O0_dentry);
611                 up(&O_dentry->d_inode->i_sem);
612
613                 if (rc) {
614                         CERROR("error renaming O/R to O/0: rc %d\n", rc);
615                         GOTO(cleanup_O0, rc);
616                 }
617                 filter->fo_fsd->fsd_feature_incompat |=
618                         cpu_to_le32(FILTER_INCOMPAT_GROUPS);
619                 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
620                                                filter->fo_fsd, 1);
621                 GOTO(cleanup_O0, rc);
622
623         cleanup_O0:
624                 dput(O0_dentry);
625         cleanup_R:
626                 dput(dentry);
627                 if (rc)
628                         GOTO(cleanup, rc);
629         } else {
630                 dput(dentry);
631         }
632
633         OBD_ALLOC(filter->fo_last_objids, FILTER_GROUPS * sizeof(__u64));
634         if (filter->fo_last_objids == NULL)
635                 GOTO(cleanup, rc = -ENOMEM);
636         cleanup_phase = 2; /* groups */
637
638         OBD_ALLOC(filter->fo_dentry_O_groups, FILTER_GROUPS * sizeof(dentry));
639         if (filter->fo_dentry_O_groups == NULL)
640                 GOTO(cleanup, rc = -ENOMEM);
641         OBD_ALLOC(filter->fo_last_objid_files, FILTER_GROUPS * sizeof(filp));
642         if (filter->fo_last_objid_files == NULL)
643                 GOTO(cleanup, rc = -ENOMEM);
644
645         for (i = 0; i < FILTER_GROUPS; i++) {
646                 char name[25];
647                 loff_t off = 0;
648
649                 sprintf(name, "%d", i);
650                 dentry = simple_mkdir(O_dentry, name, 0700);
651                 CDEBUG(D_INODE, "got/created O/%s: %p\n", name, dentry);
652                 if (IS_ERR(dentry)) {
653                         rc = PTR_ERR(dentry);
654                         CERROR("cannot lookup/create O/%s: rc = %d\n",
655                                name, rc);
656                         GOTO(cleanup, rc);
657                 }
658                 filter->fo_dentry_O_groups[i] = dentry;
659
660                 sprintf(name, "O/%d/LAST_ID", i);
661                 filp = filp_open(name, O_CREAT | O_RDWR, 0700);
662                 if (IS_ERR(dentry)) {
663                         rc = PTR_ERR(dentry);
664                         CERROR("cannot create %s: rc = %d\n", name, rc);
665                         GOTO(cleanup, rc);
666                 }
667                 filter->fo_last_objid_files[i] = filp;
668
669                 if (filp->f_dentry->d_inode->i_size == 0) {
670                         if (i == 0 && filter->fo_fsd->fsd_unused != 0) {
671                                 /* OST conversion, remove sometime post 1.0 */
672                                 filter->fo_last_objids[i] =
673                                         le64_to_cpu(filter->fo_fsd->fsd_unused);
674                                 CWARN("saving old objid "LPU64" to LAST_ID\n",
675                                       filter->fo_last_objids[i]);
676                                 rc = filter_update_last_objid(obd, 0, 1);
677                                 if (rc)
678                                         GOTO(cleanup, rc);
679                         } else {
680                                 filter->fo_last_objids[i] = FILTER_INIT_OBJID;
681                         }
682                         continue;
683                 }
684
685                 rc = fsfilt_read_record(obd, filp, &filter->fo_last_objids[i],
686                                         sizeof(__u64), &off);
687                 if (rc) {
688                         CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
689                                name, rc);
690                         GOTO(cleanup, rc);
691                 }
692                 filter->fo_last_objids[i] =
693                         le64_to_cpu(filter->fo_last_objids[i]);
694                 CDEBUG(D_INODE, "%s: server last_objid group %d: "LPU64"\n",
695                        obd->obd_name, i, filter->fo_last_objids[i]);
696         }
697
698         if (filter->fo_subdir_count) {
699                 O_dentry = filter->fo_dentry_O_groups[0];
700                 OBD_ALLOC(filter->fo_dentry_O_sub,
701                           filter->fo_subdir_count * sizeof(dentry));
702                 if (filter->fo_dentry_O_sub == NULL)
703                         GOTO(cleanup, rc = -ENOMEM);
704
705                 for (i = 0; i < filter->fo_subdir_count; i++) {
706                         char dir[20];
707                         snprintf(dir, sizeof(dir), "d%u", i);
708
709                         dentry = simple_mkdir(O_dentry, dir, 0700);
710                         CDEBUG(D_INODE, "got/created O/0/%s: %p\n", dir,dentry);
711                         if (IS_ERR(dentry)) {
712                                 rc = PTR_ERR(dentry);
713                                 CERROR("can't lookup/create O/0/%s: rc = %d\n",
714                                        dir, rc);
715                                 GOTO(cleanup, rc);
716                         }
717                         filter->fo_dentry_O_sub[i] = dentry;
718                 }
719         }
720         RETURN(0);
721
722  cleanup:
723         switch (cleanup_phase) {
724         case 2:
725                 filter_cleanup_groups(obd);
726         case 1:
727                 f_dput(filter->fo_dentry_O);
728                 filter->fo_dentry_O = NULL;
729         default:
730                 break;
731         }
732         return rc;
733 }
734
735 /* setup the object store with correct subdirectories */
736 static int filter_prep(struct obd_device *obd)
737 {
738         struct obd_run_ctxt saved;
739         struct filter_obd *filter = &obd->u.filter;
740         struct file *file;
741         struct inode *inode;
742         int rc = 0;
743         ENTRY;
744
745         push_ctxt(&saved, &obd->obd_ctxt, NULL);
746         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
747         if (!file || IS_ERR(file)) {
748                 rc = PTR_ERR(file);
749                 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
750                        LAST_RCVD, rc);
751                 GOTO(out, rc);
752         }
753
754         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
755                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
756                        file->f_dentry->d_inode->i_mode);
757                 GOTO(err_filp, rc = -ENOENT);
758         }
759
760         /* steal operations */
761         inode = file->f_dentry->d_inode;
762         filter->fo_fop = file->f_op;
763         filter->fo_iop = inode->i_op;
764         filter->fo_aops = inode->i_mapping->a_ops;
765
766         rc = filter_init_server_data(obd, file);
767         if (rc) {
768                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
769                 GOTO(err_filp, rc);
770         }
771         filter->fo_rcvd_filp = file;
772
773         rc = filter_prep_groups(obd);
774         if (rc)
775                 GOTO(err_server_data, rc);
776
777  out:
778         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
779
780         return(rc);
781
782  err_server_data:
783         //class_disconnect_exports(obd, 0);
784         filter_free_server_data(filter);
785  err_filp:
786         if (filp_close(file, 0))
787                 CERROR("can't close %s after error\n", LAST_RCVD);
788         filter->fo_rcvd_filp = NULL;
789         goto out;
790 }
791
792 /* cleanup the filter: write last used object id to status file */
793 static void filter_post(struct obd_device *obd)
794 {
795         struct obd_run_ctxt saved;
796         struct filter_obd *filter = &obd->u.filter;
797         int rc, i;
798
799         /* XXX: filter_update_lastobjid used to call fsync_dev.  It might be
800          * best to start a transaction with h_sync, because we removed this
801          * from lastobjid */
802
803         push_ctxt(&saved, &obd->obd_ctxt, NULL);
804         rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
805                                        filter->fo_fsd, 0);
806         if (rc)
807                 CERROR("error writing server data: rc = %d\n", rc);
808
809         for (i = 0; i < FILTER_GROUPS; i++) {
810                 rc = filter_update_last_objid(obd, i, (i == FILTER_GROUPS - 1));
811                 if (rc)
812                         CERROR("error writing group %d lastobjid: rc = %d\n",
813                                i, rc);
814         }
815
816         filp_close(filter->fo_rcvd_filp, 0);
817         filter->fo_rcvd_filp = NULL;
818         if (rc)
819                 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
820
821         filter_cleanup_groups(obd);
822         f_dput(filter->fo_dentry_O);
823         filter_free_server_data(filter);
824         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
825 }
826
827 static void filter_set_last_id(struct filter_obd *filter, struct obdo *oa,
828                                obd_id id)
829 {
830         obd_gr group = 0;
831         LASSERT(filter->fo_fsd != NULL);
832
833         if (oa != NULL) {
834                 LASSERT(oa->o_gr <= FILTER_GROUPS);
835                 group = oa->o_gr;
836         }
837
838         spin_lock(&filter->fo_objidlock);
839         filter->fo_last_objids[group] = id;
840         spin_unlock(&filter->fo_objidlock);
841 }
842
843 __u64 filter_last_id(struct filter_obd *filter, struct obdo *oa)
844 {
845         obd_id id;
846         obd_gr group = 0;
847         LASSERT(filter->fo_fsd != NULL);
848
849         if (oa != NULL) {
850                 LASSERT(oa->o_gr <= FILTER_GROUPS);
851                 group = oa->o_gr;
852         }
853
854         /* FIXME: object groups */
855         spin_lock(&filter->fo_objidlock);
856         id = filter->fo_last_objids[group];
857         spin_unlock(&filter->fo_objidlock);
858
859         return id;
860 }
861
862 /* direct cut-n-paste of mds_blocking_ast() */
863 static int filter_blocking_ast(struct ldlm_lock *lock,
864                                struct ldlm_lock_desc *desc,
865                                void *data, int flag)
866 {
867         int do_ast;
868         ENTRY;
869
870         if (flag == LDLM_CB_CANCELING) {
871                 /* Don't need to do anything here. */
872                 RETURN(0);
873         }
874
875         /* XXX layering violation!  -phil */
876         l_lock(&lock->l_resource->lr_namespace->ns_lock);
877         /* Get this: if filter_blocking_ast is racing with ldlm_intent_policy,
878          * such that filter_blocking_ast is called just before l_i_p takes the
879          * ns_lock, then by the time we get the lock, we might not be the
880          * correct blocking function anymore.  So check, and return early, if
881          * so. */
882         if (lock->l_blocking_ast != filter_blocking_ast) {
883                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
884                 RETURN(0);
885         }
886
887         lock->l_flags |= LDLM_FL_CBPENDING;
888         do_ast = (!lock->l_readers && !lock->l_writers);
889         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
890
891         if (do_ast) {
892                 struct lustre_handle lockh;
893                 int rc;
894
895                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
896                 ldlm_lock2handle(lock, &lockh);
897                 rc = ldlm_cli_cancel(&lockh);
898                 if (rc < 0)
899                         CERROR("ldlm_cli_cancel: %d\n", rc);
900         } else {
901                 LDLM_DEBUG(lock, "Lock still has references, will be "
902                            "cancelled later");
903         }
904         RETURN(0);
905 }
906
907 static int filter_lock_dentry(struct obd_device *obd, struct dentry *de,
908                               ldlm_mode_t lock_mode,struct lustre_handle *lockh)
909 {
910         struct ldlm_res_id res_id = { .name = {0} };
911         int flags = 0, rc;
912         ENTRY;
913
914         res_id.name[0] = de->d_inode->i_ino;
915         res_id.name[1] = de->d_inode->i_generation;
916         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
917                               res_id, LDLM_PLAIN, NULL, 0, lock_mode,
918                               &flags, ldlm_completion_ast,
919                               filter_blocking_ast, NULL, lockh);
920
921         RETURN(rc == ELDLM_OK ? 0 : -EIO);  /* XXX translate ldlm code */
922 }
923
924 /* We never dget the object parent, so DON'T dput it either */
925 static void filter_parent_unlock(struct dentry *dparent,
926                                  struct lustre_handle *lockh,
927                                  ldlm_mode_t lock_mode)
928 {
929         ldlm_lock_decref(lockh, lock_mode);
930 }
931
932 /* We never dget the object parent, so DON'T dput it either */
933 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
934 {
935         struct filter_obd *filter = &obd->u.filter;
936         LASSERT(group < FILTER_GROUPS); /* FIXME: object groups */
937
938         if (group > 0 || filter->fo_subdir_count == 0)
939                 return filter->fo_dentry_O_groups[group];
940
941         return filter->fo_dentry_O_sub[objid & (filter->fo_subdir_count - 1)];
942 }
943
944 /* We never dget the object parent, so DON'T dput it either */
945 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
946                                   obd_id objid, ldlm_mode_t lock_mode,
947                                   struct lustre_handle *lockh)
948 {
949         unsigned long now = jiffies;
950         struct dentry *de = filter_parent(obd, group, objid);
951         int rc;
952
953         if (IS_ERR(de))
954                 return de;
955
956         rc = filter_lock_dentry(obd, de, lock_mode, lockh);
957         if (time_after(jiffies, now + 15 * HZ))
958                 CERROR("slow parent lock %lus\n", (jiffies - now) / HZ);
959         return rc ? ERR_PTR(rc) : de;
960 }
961
962 /* How to get files, dentries, inodes from object id's.
963  *
964  * If dir_dentry is passed, the caller has already locked the parent
965  * appropriately for this operation (normally a write lock).  If
966  * dir_dentry is NULL, we do a read lock while we do the lookup to
967  * avoid races with create/destroy and such changing the directory
968  * internal to the filesystem code. */
969 struct dentry *filter_fid2dentry(struct obd_device *obd,
970                                  struct dentry *dir_dentry,
971                                  obd_gr group, obd_id id)
972 {
973         struct lustre_handle lockh;
974         struct dentry *dparent = dir_dentry;
975         struct dentry *dchild;
976         char name[32];
977         int len;
978         ENTRY;
979
980         if (id == 0) {
981                 CERROR("fatal: invalid object id 0\n");
982                 RETURN(ERR_PTR(-ESTALE));
983         }
984
985         len = sprintf(name, LPU64, id);
986         if (dir_dentry == NULL) {
987                 dparent = filter_parent_lock(obd, group, id, LCK_PR, &lockh);
988                 if (IS_ERR(dparent))
989                         RETURN(dparent);
990         }
991         CDEBUG(D_INODE, "looking up object O/%*s/%s\n",
992                dparent->d_name.len, dparent->d_name.name, name);
993         dchild = ll_lookup_one_len(name, dparent, len);
994         if (dir_dentry == NULL)
995                 filter_parent_unlock(dparent, &lockh, LCK_PR);
996         if (IS_ERR(dchild)) {
997                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
998                 RETURN(dchild);
999         }
1000
1001         CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
1002                name, dchild, atomic_read(&dchild->d_count));
1003
1004         LASSERT(atomic_read(&dchild->d_count) > 0);
1005
1006         RETURN(dchild);
1007 }
1008
1009 static int filter_prepare_destroy(struct obd_device *obd, obd_id objid)
1010 {
1011         struct lustre_handle lockh;
1012         int flags = LDLM_AST_DISCARD_DATA, rc;
1013         struct ldlm_res_id res_id = { .name = { objid } };
1014         struct ldlm_extent extent = { 0, OBD_OBJECT_EOF };
1015         ENTRY;
1016
1017         /* Tell the clients that the object is gone now and that they should
1018          * throw away any cached pages.  If we're the OST at stripe 0 in the
1019          * file then this enqueue will communicate the DISCARD to all the
1020          * clients.  This assumes that we always destroy all the objects for
1021          * a file at a time, as is currently the case.  If we're not the
1022          * OST at stripe 0 then we'll harmlessly get a very lonely lock in 
1023          * the local DLM and immediately drop it. */
1024         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
1025                               res_id, LDLM_EXTENT, &extent,
1026                               sizeof(extent), LCK_PW, &flags,
1027                               ldlm_completion_ast, filter_blocking_ast,
1028                               NULL, &lockh);
1029
1030         /* We only care about the side-effects, just drop the lock. */
1031         if (rc == ELDLM_OK)
1032                 ldlm_lock_decref(&lockh, LCK_PW);
1033
1034         RETURN(rc);
1035 }
1036
1037 /* Caller must hold LCK_PW on parent and push us into kernel context.
1038  * Caller is also required to ensure that dchild->d_inode exists. */
1039 static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
1040                                    struct dentry *dparent,
1041                                    struct dentry *dchild)
1042 {
1043         struct inode *inode = dchild->d_inode;
1044         int rc;
1045         ENTRY;
1046
1047         if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
1048                 CERROR("destroying objid %*s nlink = %lu, count = %d\n",
1049                        dchild->d_name.len, dchild->d_name.name,
1050                        (unsigned long)inode->i_nlink, 
1051                        atomic_read(&inode->i_count));
1052         }
1053
1054         rc = vfs_unlink(dparent->d_inode, dchild);
1055
1056         if (rc)
1057                 CERROR("error unlinking objid %*s: rc %d\n",
1058                        dchild->d_name.len, dchild->d_name.name, rc);
1059
1060         RETURN(rc);
1061 }
1062
1063 /* mount the file system (secretly) */
1064 int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
1065                         char *option)
1066 {
1067         struct lustre_cfg* lcfg = buf;
1068         struct filter_obd *filter = &obd->u.filter;
1069         struct vfsmount *mnt;
1070         int rc = 0;
1071         ENTRY;
1072
1073         dev_clear_rdonly(2);
1074
1075         if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
1076                 RETURN(-EINVAL);
1077
1078         obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
1079         if (IS_ERR(obd->obd_fsops))
1080                 RETURN(PTR_ERR(obd->obd_fsops));
1081
1082         mnt = do_kern_mount(lcfg->lcfg_inlbuf2, MS_NOATIME | MS_NODIRATIME,
1083                             lcfg->lcfg_inlbuf1, option);
1084         rc = PTR_ERR(mnt);
1085         if (IS_ERR(mnt))
1086                 GOTO(err_ops, rc);
1087
1088         if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3) {
1089                 if (*lcfg->lcfg_inlbuf3 == 'f') {
1090                         obd->obd_replayable = 1;
1091                         obd_sync_filter = 1;
1092                         CERROR("%s: recovery enabled\n", obd->obd_name);
1093                 } else {
1094                         if (*lcfg->lcfg_inlbuf3 != 'n') {
1095                                 CERROR("unrecognised flag '%c'\n",
1096                                        *lcfg->lcfg_inlbuf3);
1097                         }
1098                         // XXX Robert? Why do we get errors here
1099                         // GOTO(err_mntput, rc = -EINVAL);
1100                 }
1101         }
1102
1103         filter->fo_vfsmnt = mnt;
1104         filter->fo_sb = mnt->mnt_sb;
1105         filter->fo_fstype = mnt->mnt_sb->s_type->name;
1106         CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
1107
1108         OBD_SET_CTXT_MAGIC(&obd->obd_ctxt);
1109         obd->obd_ctxt.pwdmnt = mnt;
1110         obd->obd_ctxt.pwd = mnt->mnt_root;
1111         obd->obd_ctxt.fs = get_ds();
1112         obd->obd_ctxt.cb_ops = filter_lvfs_ops;
1113
1114         rc = filter_prep(obd);
1115         if (rc)
1116                 GOTO(err_mntput, rc);
1117
1118         spin_lock_init(&filter->fo_translock);
1119         spin_lock_init(&filter->fo_objidlock);
1120         INIT_LIST_HEAD(&filter->fo_export_list);
1121         sema_init(&filter->fo_alloc_lock, 1);
1122         spin_lock_init(&filter->fo_r_pages.oh_lock);
1123         spin_lock_init(&filter->fo_w_pages.oh_lock);
1124         spin_lock_init(&filter->fo_r_discont_pages.oh_lock);
1125         spin_lock_init(&filter->fo_w_discont_pages.oh_lock);
1126         spin_lock_init(&filter->fo_r_discont_blocks.oh_lock);
1127         spin_lock_init(&filter->fo_w_discont_blocks.oh_lock);
1128
1129         obd->obd_namespace = ldlm_namespace_new("filter-tgt",
1130                                                 LDLM_NAMESPACE_SERVER);
1131         if (obd->obd_namespace == NULL)
1132                 GOTO(err_post, rc = -ENOMEM);
1133
1134         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1135                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1136
1137         RETURN(0);
1138
1139 err_post:
1140         filter_post(obd);
1141 err_mntput:
1142         unlock_kernel();
1143         mntput(mnt);
1144         filter->fo_sb = 0;
1145         lock_kernel();
1146 err_ops:
1147         fsfilt_put_ops(obd->obd_fsops);
1148         return rc;
1149 }
1150
1151 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
1152 {
1153         struct lustre_cfg* lcfg = buf;
1154         const char *str = NULL;
1155         char *option = NULL;
1156         int n = 0;
1157         int rc;
1158
1159         if (!strcmp(lcfg->lcfg_inlbuf2, "ext3")) {
1160 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1161         /* bug 1577: implement async-delete for 2.5 */
1162                 str = "errors=remount-ro,asyncdel";
1163 #else
1164                 str = "errors=remount-ro";
1165 #endif
1166                 n = strlen(str) + 1;
1167                 OBD_ALLOC(option, n);
1168                 if (option == NULL)
1169                         RETURN(-ENOMEM);
1170                 strcpy(option, str);
1171         }
1172
1173         rc = filter_common_setup(obd, len, buf, option);
1174         if (option)
1175                 OBD_FREE(option, n);
1176         return rc;
1177 }
1178
1179 static int filter_postsetup(struct obd_device *obd)
1180 {
1181         int rc = 0;
1182         ENTRY;
1183
1184         // XXX add a storage location for the logid for size changes
1185 #ifdef ENABLE_ORPHANS
1186         rc = llog_cat_initialize(obd, 1);
1187         if (rc)
1188                 CERROR("failed to setup llogging subsystems\n");
1189 #endif
1190         RETURN(rc);
1191 }
1192
1193 static int filter_cleanup(struct obd_device *obd, int flags)
1194 {
1195         struct filter_obd *filter = &obd->u.filter;
1196         ENTRY;
1197
1198         if (flags & OBD_OPT_FAILOVER)
1199                 CERROR("%s: shutting down for failover; client state will"
1200                        " be preserved.\n", obd->obd_name);
1201
1202         if (!list_empty(&obd->obd_exports)) {
1203                 CERROR("%s: still has clients!\n", obd->obd_name);
1204                 class_disconnect_exports(obd, flags);
1205                 if (!list_empty(&obd->obd_exports)) {
1206                         CERROR("still has exports after forced cleanup?\n");
1207                         RETURN(-EBUSY);
1208                 }
1209         }
1210
1211         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
1212
1213         if (filter->fo_sb == NULL)
1214                 RETURN(0);
1215
1216         filter_post(obd);
1217
1218         shrink_dcache_parent(filter->fo_sb->s_root);
1219         filter->fo_sb = 0;
1220
1221         if (atomic_read(&filter->fo_vfsmnt->mnt_count) > 1)
1222                 CERROR("%s: mount point %p busy, mnt_count: %d\n",
1223                        obd->obd_name, filter->fo_vfsmnt,
1224                        atomic_read(&filter->fo_vfsmnt->mnt_count));
1225
1226         unlock_kernel();
1227         mntput(filter->fo_vfsmnt);
1228         //destroy_buffers(filter->fo_sb->s_dev);
1229         filter->fo_sb = NULL;
1230         fsfilt_put_ops(obd->obd_fsops);
1231         lock_kernel();
1232
1233         dev_clear_rdonly(2);
1234
1235         RETURN(0);
1236 }
1237
1238 static int filter_attach(struct obd_device *obd, obd_count len, void *data)
1239 {
1240         struct lprocfs_static_vars lvars;
1241         int rc;
1242
1243         lprocfs_init_vars(filter, &lvars);
1244         rc = lprocfs_obd_attach(obd, lvars.obd_vars);
1245         if (rc != 0)
1246                 return rc;
1247
1248         rc = lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST);
1249         if (rc != 0)
1250                 return rc;
1251
1252         /* Init obdfilter private stats here */
1253         lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
1254                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
1255         lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
1256                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
1257
1258         return lproc_filter_attach_seqstat(obd);
1259 }
1260
1261 static int filter_detach(struct obd_device *dev)
1262 {
1263         lprocfs_free_obd_stats(dev);
1264         return lprocfs_obd_detach(dev);
1265 }
1266
1267 /* nearly identical to mds_connect */
1268 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
1269                           struct obd_uuid *cluuid)
1270 {
1271         struct obd_export *exp;
1272         struct filter_export_data *fed;
1273         struct filter_client_data *fcd = NULL;
1274         struct filter_obd *filter = &obd->u.filter;
1275         int rc;
1276         ENTRY;
1277
1278         if (conn == NULL || obd == NULL || cluuid == NULL)
1279                 RETURN(-EINVAL);
1280
1281         rc = class_connect(conn, obd, cluuid);
1282         if (rc)
1283                 RETURN(rc);
1284         exp = class_conn2export(conn);
1285         LASSERT(exp != NULL);
1286
1287         fed = &exp->exp_filter_data;
1288
1289         spin_lock_init(&fed->fed_lock);
1290
1291         if (!obd->obd_replayable)
1292                 GOTO(cleanup, rc = 0);
1293
1294         OBD_ALLOC(fcd, sizeof(*fcd));
1295         if (!fcd) {
1296                 CERROR("filter: out of memory for client data\n");
1297                 GOTO(cleanup, rc = -ENOMEM);
1298         }
1299
1300         memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
1301         fed->fed_fcd = fcd;
1302         fcd->fcd_mount_count = cpu_to_le64(filter->fo_mount_count);
1303
1304         rc = filter_client_add(obd, filter, fed, -1);
1305
1306 cleanup:
1307         if (rc) {
1308                 if (fcd)
1309                         OBD_FREE(fcd, sizeof(*fcd));
1310                 class_disconnect(exp, 0);
1311         } else {
1312                 class_export_put(exp);
1313         }
1314         return rc;
1315 }
1316
1317 static int filter_precleanup(struct obd_device *obd, int flags)
1318 {
1319         int rc = 0;
1320         ENTRY;
1321
1322 #ifdef ENABLE_ORPHANS
1323         rc = obd_llog_finish(obd, 0);
1324         if (rc)
1325                 CERROR("failed to cleanup llogging subsystem\n");
1326 #endif
1327
1328         RETURN(rc);
1329 }
1330
1331 static int filter_destroy_export(struct obd_export *exp)
1332 {
1333         ENTRY;
1334
1335         target_destroy_export(exp);
1336
1337         if (exp->exp_obd->obd_replayable)
1338                 filter_client_free(exp, exp->exp_flags);
1339         RETURN(0);
1340 }
1341
1342 /* also incredibly similar to mds_disconnect */
1343 static int filter_disconnect(struct obd_export *exp, int flags)
1344 {
1345         struct filter_obd *filter = &exp->exp_obd->u.filter;
1346         struct filter_export_data *fed = &exp->exp_filter_data;
1347         unsigned long irqflags;
1348         struct llog_ctxt *ctxt;
1349         int rc;
1350         ENTRY;
1351
1352         LASSERT(exp);
1353
1354         /* XXX should this go into filter_destroy_export() instead? */
1355         /* forget what this client had cached, I bet this needs to be
1356          * matched with appropriate client behaviour in the face of
1357          * disconnects */
1358         spin_lock(&exp->exp_obd->obd_osfs_lock);
1359         filter->fo_tot_cached -= fed->fed_cached;
1360         filter->fo_tot_granted -= fed->fed_grant;
1361         fed->fed_cached = 0;
1362         fed->fed_grant = 0;
1363         fed->fed_grant_sent = 0;
1364         fed->fed_grant_waiting = 0;
1365         spin_unlock(&exp->exp_obd->obd_osfs_lock);
1366
1367         ldlm_cancel_locks_for_export(exp);
1368
1369         spin_lock_irqsave(&exp->exp_lock, irqflags);
1370         exp->exp_flags = flags;
1371         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
1372
1373         fsfilt_sync(exp->exp_obd, filter->fo_sb);
1374         /* XXX cleanup preallocated inodes */
1375
1376         /* flush any remaining cancel messages out to the target */
1377         ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_REPL_CTXT);
1378         llog_sync(ctxt, exp);
1379
1380         rc = class_disconnect(exp, flags);
1381         RETURN(rc);
1382 }
1383
1384 struct dentry *__filter_oa2dentry(struct obd_device *obd,
1385                                   struct obdo *oa, const char *what)
1386 {
1387         struct dentry *dchild = NULL;
1388         obd_gr group = 0;
1389
1390         if (oa->o_valid & OBD_MD_FLGROUP)
1391                 group = oa->o_gr;
1392
1393         dchild = filter_fid2dentry(obd, NULL, group, oa->o_id);
1394
1395         if (IS_ERR(dchild)) {
1396                 CERROR("%s error looking up object: "LPU64"\n", what, oa->o_id);
1397                 RETURN(dchild);
1398         }
1399
1400         if (dchild->d_inode == NULL) {
1401                 CERROR("%s on non-existent object: "LPU64"\n", what, oa->o_id);
1402                 f_dput(dchild);
1403                 RETURN(ERR_PTR(-ENOENT));
1404         }
1405
1406         return dchild;
1407 }
1408
1409 static int filter_getattr(struct obd_export *exp, struct obdo *oa,
1410                           struct lov_stripe_md *md)
1411 {
1412         struct dentry *dentry = NULL;
1413         struct obd_device *obd;
1414         int rc = 0;
1415         ENTRY;
1416
1417         obd = class_exp2obd(exp);
1418         if (obd == NULL) {
1419                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1420                        exp->exp_handle.h_cookie);
1421                 RETURN(-EINVAL);
1422         }
1423
1424         dentry = filter_oa2dentry(obd, oa);
1425         if (IS_ERR(dentry))
1426                 RETURN(PTR_ERR(dentry));
1427
1428         /* Limit the valid bits in the return data to what we actually use */
1429         oa->o_valid = OBD_MD_FLID;
1430         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1431
1432         f_dput(dentry);
1433         RETURN(rc);
1434 }
1435
1436 /* this is called from filter_truncate() until we have filter_punch() */
1437 static int filter_setattr(struct obd_export *exp, struct obdo *oa,
1438                           struct lov_stripe_md *md, struct obd_trans_info *oti)
1439 {
1440         struct obd_run_ctxt saved;
1441         struct filter_obd *filter;
1442         struct dentry *dentry;
1443         struct iattr iattr;
1444         void *handle;
1445         int rc, rc2;
1446         ENTRY;
1447
1448         LASSERT(oti != NULL);
1449
1450         dentry = filter_oa2dentry(exp->exp_obd, oa);
1451         if (IS_ERR(dentry))
1452                 RETURN(PTR_ERR(dentry));
1453
1454         filter = &exp->exp_obd->u.filter;
1455
1456         iattr_from_obdo(&iattr, oa, oa->o_valid);
1457
1458         push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1459         lock_kernel();
1460
1461         if (iattr.ia_valid & ATTR_SIZE)
1462                 down(&dentry->d_inode->i_sem);
1463         handle = fsfilt_start(exp->exp_obd, dentry->d_inode, FSFILT_OP_SETATTR,
1464                               oti);
1465         if (IS_ERR(handle))
1466                 GOTO(out_unlock, rc = PTR_ERR(handle));
1467
1468         /* XXX this could be a rwsem instead, if filter_preprw played along */
1469         if (iattr.ia_valid & ATTR_ATTR_FLAG)
1470                 rc = fsfilt_iocontrol(exp->exp_obd, dentry->d_inode, NULL,
1471                                       EXT3_IOC_SETFLAGS,
1472                                       (long)&iattr.ia_attr_flags);
1473         else
1474                 rc = fsfilt_setattr(exp->exp_obd, dentry, handle, &iattr, 1);
1475         rc = filter_finish_transno(exp, oti, rc);
1476         rc2 = fsfilt_commit(exp->exp_obd, dentry->d_inode, handle, 0);
1477         if (rc2) {
1478                 CERROR("error on commit, err = %d\n", rc2);
1479                 if (!rc)
1480                         rc = rc2;
1481         }
1482
1483         oa->o_valid = OBD_MD_FLID;
1484         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1485
1486 out_unlock:
1487         if (iattr.ia_valid & ATTR_SIZE)
1488                 up(&dentry->d_inode->i_sem);
1489         unlock_kernel();
1490         pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1491
1492         f_dput(dentry);
1493         RETURN(rc);
1494 }
1495
1496 /* XXX identical to osc_unpackmd */
1497 static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
1498                            struct lov_mds_md *lmm, int lmm_bytes)
1499 {
1500         int lsm_size;
1501         ENTRY;
1502
1503         if (lmm != NULL) {
1504                 if (lmm_bytes < sizeof (*lmm)) {
1505                         CERROR("lov_mds_md too small: %d, need %d\n",
1506                                lmm_bytes, (int)sizeof(*lmm));
1507                         RETURN(-EINVAL);
1508                 }
1509                 /* XXX LOV_MAGIC etc check? */
1510
1511                 if (lmm->lmm_object_id == cpu_to_le64(0)) {
1512                         CERROR("lov_mds_md: zero lmm_object_id\n");
1513                         RETURN(-EINVAL);
1514                 }
1515         }
1516
1517         lsm_size = lov_stripe_md_size(1);
1518         if (lsmp == NULL)
1519                 RETURN(lsm_size);
1520
1521         if (*lsmp != NULL && lmm == NULL) {
1522                 OBD_FREE(*lsmp, lsm_size);
1523                 *lsmp = NULL;
1524                 RETURN(0);
1525         }
1526
1527         if (*lsmp == NULL) {
1528                 OBD_ALLOC(*lsmp, lsm_size);
1529                 if (*lsmp == NULL)
1530                         RETURN(-ENOMEM);
1531
1532                 loi_init((*lsmp)->lsm_oinfo);
1533         }
1534
1535         if (lmm != NULL) {
1536                 /* XXX zero *lsmp? */
1537                 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
1538                 LASSERT((*lsmp)->lsm_object_id);
1539         }
1540
1541         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
1542
1543         RETURN(lsm_size);
1544 }
1545
1546 static void filter_destroy_precreated(struct obd_export *exp, struct obdo *oa,
1547                                       struct filter_obd *filter)
1548 {
1549         struct obdo doa; /* XXX obdo on stack */
1550         __u64 last, id;
1551         ENTRY;
1552         LASSERT(oa);
1553
1554         memset(&doa, 0, sizeof(doa));
1555         if (oa->o_valid & OBD_MD_FLGROUP)
1556                 doa.o_gr = oa->o_gr;
1557         else
1558                 doa.o_gr = 0;
1559         doa.o_mode = S_IFREG;
1560         last = filter_last_id(filter, &doa); /* FIXME: object groups */
1561         CWARN("deleting orphan objects from "LPU64" to "LPU64"\n",
1562                oa->o_id + 1, last);
1563         for (id = oa->o_id + 1; id <= last; id++) {
1564                 doa.o_id = id;
1565                 filter_destroy(exp, &doa, NULL, NULL);
1566         }
1567         spin_lock(&filter->fo_objidlock);
1568         filter->fo_last_objids[0] = oa->o_id; /* FIXME: object groups */
1569         spin_unlock(&filter->fo_objidlock);
1570         EXIT;
1571 }
1572
1573 /* returns a negative error or a nonnegative number of files to create */
1574 static int filter_should_precreate(struct obd_export *exp, struct obdo *oa,
1575                                    int group)
1576 {
1577         struct obd_device *obd = exp->exp_obd;
1578         struct filter_obd *filter = &obd->u.filter;
1579         int diff, rc;
1580         ENTRY;
1581
1582         diff = oa->o_id - filter_last_id(filter, oa);
1583         CDEBUG(D_INFO, "filter_last_id() = "LPU64" -> diff = %d\n",
1584                filter_last_id(filter, oa), diff);
1585        
1586         /* delete orphans request */
1587         if ((oa->o_valid & OBD_MD_FLFLAGS) && 
1588             (oa->o_flags & OBD_FL_DELORPHAN)) {
1589                 LASSERT(diff <= 0);
1590                 if (diff == 0)
1591                         RETURN(0);
1592                 filter_destroy_precreated(exp, oa, filter);
1593                 rc = filter_update_last_objid(obd, group, 0);
1594                 if (rc)
1595                         CERROR("unable to write lastobjid, but orphans" 
1596                                "were deleted\n");
1597                 RETURN(0);
1598         } else {
1599                 /* only precreate if group == 0 and o_id is specfied */
1600                 if (!(oa->o_valid & OBD_FL_DELORPHAN) && 
1601                     (group != 0 || oa->o_id == 0))
1602                         RETURN(1);
1603
1604                 LASSERT(diff >= 0);
1605                 RETURN(diff);
1606         }
1607
1608 }
1609
1610 /* We rely on the fact that only one thread will be creating files in a given
1611  * group at a time, which is why we don't need an atomic filter_get_new_id.
1612  * Even if we had that atomic function, the following race would exist:
1613  *
1614  * thread 1: gets id x from filter_next_id
1615  * thread 2: gets id (x + 1) from filter_next_id
1616  * thread 2: creates object (x + 1)
1617  * thread 1: tries to create object x, gets -ENOSPC
1618  */
1619 static int filter_precreate(struct obd_device *obd, struct obdo *oa,
1620                             obd_gr group, int *num)
1621 {
1622         struct lustre_handle parent_lockh;
1623         struct dentry *dchild = NULL;
1624         struct filter_obd *filter;
1625         struct dentry *dparent;
1626         int err = 0, rc = 0, i;
1627         __u64 next_id;
1628         void *handle;
1629         ENTRY;
1630
1631         filter = &obd->u.filter;
1632
1633         for (i = 0; i < *num && err == 0; i++) {
1634                 int cleanup_phase = 0;
1635
1636                 next_id = filter_last_id(filter, oa) + 1;
1637                 CDEBUG(D_INFO, "precreate objid "LPU64"\n", next_id);
1638
1639                 dparent = filter_parent_lock(obd, group, next_id, LCK_PW,
1640                                              &parent_lockh);
1641                 if (IS_ERR(dparent))
1642                         GOTO(cleanup, rc = PTR_ERR(dparent));
1643                 cleanup_phase = 1;
1644
1645                 dchild = filter_fid2dentry(obd, dparent, group, next_id);
1646                 if (IS_ERR(dchild))
1647                         GOTO(cleanup, rc = PTR_ERR(dchild));
1648                 cleanup_phase = 2;
1649
1650                 if (dchild->d_inode != NULL) {
1651                         /* This would only happen if lastobjid was bad on disk*/
1652                         CERROR("Serious error: objid %*s already exists; is "
1653                                "this filesystem corrupt?\n",
1654                                dchild->d_name.len, dchild->d_name.name);
1655                         GOTO(cleanup, rc = -EEXIST);
1656                 }
1657
1658                 handle = fsfilt_start(obd, dparent->d_inode,
1659                                       FSFILT_OP_CREATE_LOG, NULL);
1660                 if (IS_ERR(handle))
1661                         GOTO(cleanup, rc = PTR_ERR(handle));
1662                 cleanup_phase = 3;
1663
1664                 rc = ll_vfs_create(dparent->d_inode, dchild, S_IFREG, NULL);
1665                 if (rc) {
1666                         CERROR("create failed rc = %d\n", rc);
1667                         GOTO(cleanup, rc);
1668                 } 
1669
1670                 filter_set_last_id(filter, oa, next_id);
1671                 err = filter_update_last_objid(obd, group, 0);
1672                 if (err)
1673                         CERROR("unable to write lastobjid but file created\n");
1674
1675         cleanup:
1676                 switch(cleanup_phase) {
1677                 case 3:
1678                         err = fsfilt_commit(obd, dparent->d_inode, handle, 0);
1679                         if (err) {
1680                                 CERROR("error on commit, err = %d\n", err);
1681                                 if (!rc)
1682                                         rc = err;
1683                         }
1684                 case 2:
1685                         f_dput(dchild);
1686                 case 1:
1687                         filter_parent_unlock(dparent, &parent_lockh, LCK_PW);
1688                 case 0:
1689                         break;
1690                 }
1691                 
1692                 if (rc)
1693                         break;
1694         }
1695         *num = i;
1696
1697         CDEBUG(D_INFO, "filter_precreate() created %d objects\n", i);
1698         RETURN(rc);
1699 }
1700
1701 static int filter_create(struct obd_export *exp, struct obdo *oa,
1702                          struct lov_stripe_md **ea, struct obd_trans_info *oti)
1703 {
1704         struct obd_device *obd = NULL;
1705         struct obd_run_ctxt saved;
1706         struct lov_stripe_md *lsm = NULL;
1707         obd_gr group = 0;
1708         int rc = 0, diff;
1709         ENTRY;
1710
1711         if (oa->o_valid & OBD_MD_FLGROUP)
1712                 group = oa->o_gr;
1713
1714         CDEBUG(D_INFO, "filter_create(od->o_gr="LPU64",od->o_id="LPU64")\n",
1715                group, oa->o_id);
1716         if (ea != NULL) {
1717                 lsm = *ea;
1718                 if (lsm == NULL) {
1719                         rc = obd_alloc_memmd(exp, &lsm);
1720                         if (rc < 0)
1721                                 RETURN(rc);
1722                 }
1723         }
1724
1725         obd = exp->exp_obd;
1726         push_ctxt(&saved, &obd->obd_ctxt, NULL);
1727
1728         diff = filter_should_precreate(exp, oa, group);
1729         if (diff > 0) {
1730                 oa->o_id = filter_last_id(&obd->u.filter, oa);
1731                 rc = filter_precreate(obd, oa, group, &diff);
1732                 oa->o_id += diff;
1733                 oa->o_valid = OBD_MD_FLID;
1734         }
1735
1736         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1737         if (rc && ea != NULL && *ea != lsm) {
1738                 obd_free_memmd(exp, &lsm);
1739         } else if (rc == 0 && ea != NULL) {
1740                 /* XXX LOV STACKING: the lsm that is passed to us from
1741                  * LOV does not have valid lsm_oinfo data structs, so
1742                  * don't go touching that.  This needs to be fixed in a
1743                  * big way. */
1744                 lsm->lsm_object_id = oa->o_id;
1745                 *ea = lsm;
1746         }
1747
1748         RETURN(rc);
1749 }
1750
1751 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
1752                           struct lov_stripe_md *ea, struct obd_trans_info *oti)
1753 {
1754         struct obd_device *obd;
1755         struct filter_obd *filter;
1756         struct dentry *dchild = NULL, *dparent = NULL;
1757         struct obd_run_ctxt saved;
1758         void *handle = NULL;
1759         struct lustre_handle parent_lockh;
1760         struct llog_cookie *fcc = NULL;
1761         int rc, rc2, cleanup_phase = 0, have_prepared = 0;
1762         obd_gr group = 0;
1763         ENTRY;
1764
1765         if (oa->o_valid & OBD_MD_FLGROUP)
1766                 group = oa->o_gr;
1767
1768         obd = exp->exp_obd;
1769         filter = &obd->u.filter;
1770
1771         push_ctxt(&saved, &obd->obd_ctxt, NULL);
1772
1773  acquire_locks:
1774         dparent = filter_parent_lock(obd, group, oa->o_id, LCK_PW,
1775                                      &parent_lockh);
1776         if (IS_ERR(dparent))
1777                 GOTO(cleanup, rc = PTR_ERR(dparent));
1778         cleanup_phase = 1;
1779
1780         dchild = filter_fid2dentry(obd, dparent, group, oa->o_id);
1781         if (IS_ERR(dchild))
1782                 GOTO(cleanup, rc = -ENOENT);
1783         cleanup_phase = 2;
1784
1785         if (dchild->d_inode == NULL) {
1786                 CERROR("destroying non-existent object "LPU64"\n", oa->o_id);
1787                 GOTO(cleanup, rc = -ENOENT);
1788         }
1789
1790         if (!have_prepared) {
1791                 /* If we're really going to destroy the object, get ready
1792                  * by getting the clients to discard their cached data.
1793                  *
1794                  * We have to drop the parent lock, because
1795                  * filter_prepare_destroy will acquire a PW on the object, and
1796                  * we don't want to deadlock with an incoming write to the
1797                  * object, which has the extent PW and then wants to get the
1798                  * parent dentry to do the lookup.
1799                  *
1800                  * We dput the child because it's not worth the extra
1801                  * complication of condition the above code to skip it on the
1802                  * second time through. */
1803                 f_dput(dchild);
1804                 filter_parent_unlock(dparent, &parent_lockh, LCK_PW);
1805
1806                 filter_prepare_destroy(obd, oa->o_id);
1807                 have_prepared = 1;
1808                 goto acquire_locks;
1809         }
1810
1811         handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK_LOG, oti);
1812         if (IS_ERR(handle))
1813                 GOTO(cleanup, rc = PTR_ERR(handle));
1814         cleanup_phase = 3;
1815
1816         /* Our MDC connection is established by the MDS to us */
1817         if (oa->o_valid & OBD_MD_FLCOOKIE) {
1818                 OBD_ALLOC(fcc, sizeof(*fcc));
1819                 if (fcc != NULL)
1820                         memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
1821         }
1822
1823         rc = filter_destroy_internal(obd, oa->o_id, dparent, dchild);
1824
1825 cleanup:
1826         switch(cleanup_phase) {
1827         case 3:
1828                 if (fcc != NULL)
1829                         fsfilt_add_journal_cb(obd, 0, oti->oti_handle,
1830                                               filter_cancel_cookies_cb, fcc);
1831                 rc = filter_finish_transno(exp, oti, rc);
1832                 rc2 = fsfilt_commit(obd, dparent->d_inode, handle, 0);
1833                 if (rc2) {
1834                         CERROR("error on commit, err = %d\n", rc2);
1835                         if (!rc)
1836                                 rc = rc2;
1837                 }
1838         case 2:
1839                 f_dput(dchild);
1840         case 1:
1841                 if (rc || oti == NULL) {
1842                         filter_parent_unlock(dparent, &parent_lockh, LCK_PW);
1843                 } else {
1844                         memcpy(&oti->oti_ack_locks[0].lock, &parent_lockh,
1845                                sizeof(parent_lockh));
1846                         oti->oti_ack_locks[0].mode = LCK_PW;
1847                 }
1848         case 0:
1849                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1850                 break;
1851         default:
1852                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1853                 LBUG();
1854         }
1855
1856         RETURN(rc);
1857 }
1858
1859 /* NB start and end are used for punch, but not truncate */
1860 static int filter_truncate(struct obd_export *exp, struct obdo *oa,
1861                            struct lov_stripe_md *lsm,
1862                            obd_off start, obd_off end,
1863                            struct obd_trans_info *oti)
1864 {
1865         int error;
1866         ENTRY;
1867
1868         if (end != OBD_OBJECT_EOF)
1869                 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
1870                        end);
1871
1872         CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = %x, "
1873                "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
1874         oa->o_size = start;
1875         error = filter_setattr(exp, oa, NULL, oti);
1876         RETURN(error);
1877 }
1878
1879 static int filter_sync(struct obd_export *exp, struct obdo *oa,
1880                        struct lov_stripe_md *lsm, obd_off start, obd_off end)
1881 {
1882         struct obd_run_ctxt saved;
1883         struct filter_obd *filter;
1884         struct dentry *dentry;
1885         int rc, rc2;
1886         ENTRY;
1887
1888         filter = &exp->exp_obd->u.filter;
1889
1890         /* an objid of zero is taken to mean "sync whole filesystem" */
1891         if (!oa || !(oa->o_valid & OBD_MD_FLID)) {
1892                 rc = fsfilt_sync(exp->exp_obd, filter->fo_sb);
1893                 RETURN(rc);
1894         }
1895
1896         dentry = filter_oa2dentry(exp->exp_obd, oa);
1897         if (IS_ERR(dentry))
1898                 RETURN(PTR_ERR(dentry));
1899
1900         push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1901
1902         down(&dentry->d_inode->i_sem);
1903         rc = filemap_fdatasync(dentry->d_inode->i_mapping);
1904         if (rc == 0) {
1905                 /* just any file to grab fsync method - "file" arg unused */
1906                 struct file *file = filter->fo_rcvd_filp;
1907
1908                 if (file->f_op && file->f_op->fsync)
1909                         rc = file->f_op->fsync(NULL, dentry, 1);
1910
1911                 rc2 = filemap_fdatawait(dentry->d_inode->i_mapping);
1912                 if (!rc)
1913                         rc = rc2;
1914         }
1915         up(&dentry->d_inode->i_sem);
1916
1917         oa->o_valid = OBD_MD_FLID;
1918         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1919
1920         pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1921
1922         f_dput(dentry);
1923         RETURN(rc);
1924 }
1925
1926 /* debugging to make sure that nothing bad happens, can be turned off soon */
1927 static void filter_grant_sanity_check(struct obd_device *obd,
1928                                       struct filter_obd *filter, __u64 maxsize)
1929 {
1930         struct filter_export_data *fed;
1931         struct obd_export *exp_pos;
1932         obd_size tot_cached = 0, tot_granted = 0;
1933
1934         list_for_each_entry(exp_pos, &obd->obd_exports, exp_obd_chain) {
1935                 fed = &exp_pos->exp_filter_data;
1936                 LASSERT(fed->fed_cached <= maxsize);
1937                 LASSERT(fed->fed_grant <= maxsize);
1938                 tot_cached += fed->fed_cached;
1939                 tot_granted += fed->fed_grant;
1940         }
1941         LASSERT(tot_cached == filter->fo_tot_cached);
1942         LASSERT(tot_granted == filter->fo_tot_granted);
1943         LASSERT(tot_cached <= maxsize);
1944         LASSERT(tot_granted <= maxsize);
1945 }
1946
1947 static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1948                          unsigned long max_age)
1949 {
1950         struct filter_obd *filter = &obd->u.filter;
1951         __u64 cached;
1952         int blockbits = filter->fo_sb->s_blocksize_bits;
1953         int rc;
1954         ENTRY;
1955
1956         /* at least try to account for cached pages.  its still racey and
1957          * might be under-reporting if clients haven't announced their
1958          * caches with brw recently */
1959         spin_lock(&obd->obd_osfs_lock);
1960         rc = fsfilt_statfs(obd, filter->fo_sb, max_age);
1961         memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1962         filter_grant_sanity_check(obd, filter, osfs->os_blocks << blockbits);
1963         cached = filter->fo_tot_cached + osfs->os_bsize - 1;
1964         spin_unlock(&obd->obd_osfs_lock);
1965
1966         cached >>= blockbits;
1967         if (cached > osfs->os_bavail) {
1968                 CERROR("cached "LPU64" > bavail "LPU64", clamping\n", cached,
1969                        osfs->os_bavail);
1970                 cached = osfs->os_bavail;
1971         }
1972         osfs->os_bavail -= cached;
1973         osfs->os_bfree -= cached;
1974
1975         RETURN(rc);
1976 }
1977
1978 static int filter_get_info(struct obd_export *exp, __u32 keylen,
1979                            void *key, __u32 *vallen, void *val)
1980 {
1981         struct obd_device *obd;
1982         ENTRY;
1983
1984         obd = class_exp2obd(exp);
1985         if (obd == NULL) {
1986                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1987                        exp->exp_handle.h_cookie);
1988                 RETURN(-EINVAL);
1989         }
1990
1991         if (keylen == strlen("blocksize") &&
1992             memcmp(key, "blocksize", keylen) == 0) {
1993                 __u32 *blocksize = val;
1994                 *vallen = sizeof(*blocksize);
1995                 *blocksize = obd->u.filter.fo_sb->s_blocksize;
1996                 RETURN(0);
1997         }
1998
1999         if (keylen == strlen("blocksize_bits") &&
2000             memcmp(key, "blocksize_bits", keylen) == 0) {
2001                 __u32 *blocksize_bits = val;
2002                 *vallen = sizeof(*blocksize_bits);
2003                 *blocksize_bits = obd->u.filter.fo_sb->s_blocksize_bits;
2004                 RETURN(0);
2005         }
2006
2007         if (keylen >= strlen("last_id") && memcmp(key, "last_id", 7) == 0) {
2008                 obd_id *last_id = val;
2009                 /* FIXME: object groups */
2010                 *last_id = filter_last_id(&obd->u.filter, 0);
2011                 RETURN(0);
2012         }
2013         CDEBUG(D_IOCTL, "invalid key\n");
2014         RETURN(-EINVAL);
2015 }
2016
2017 static int filter_set_info(struct obd_export *exp, __u32 keylen,
2018                            void *key, __u32 vallen, void *val)
2019 {
2020         struct obd_device *obd;
2021         struct lustre_handle conn;
2022 #ifdef ENABLE_ORPHANS
2023         struct llog_ctxt *ctxt;
2024 #endif
2025         int rc = 0;
2026         ENTRY;
2027
2028         conn.cookie = exp->exp_handle.h_cookie;
2029
2030         obd = exp->exp_obd;
2031         if (obd == NULL) {
2032                 CDEBUG(D_IOCTL, "invalid exp %p cookie "LPX64"\n",
2033                        exp, conn.cookie);
2034                 RETURN(-EINVAL);
2035         }
2036
2037         if (keylen < strlen("mds_conn") ||
2038             memcmp(key, "mds_conn", keylen) != 0)
2039                 RETURN(-EINVAL);
2040
2041         CWARN("Received MDS connection ("LPX64")\n", conn.cookie);
2042         memcpy(&obd->u.filter.fo_mdc_conn, &conn, sizeof(conn));
2043 #ifdef ENABLE_ORPHANS
2044         ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
2045         rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
2046 #endif
2047         RETURN(rc);
2048 }
2049
2050 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
2051                      int len, void *karg, void *uarg)
2052 {
2053         struct obd_device *obd = exp->exp_obd;
2054         struct obd_ioctl_data *data = karg;
2055         int rc = 0;
2056
2057         switch (cmd) {
2058         case OBD_IOC_ABORT_RECOVERY:
2059                 CERROR("aborting recovery for device %s\n", obd->obd_name);
2060                 target_abort_recovery(obd);
2061                 RETURN(0);
2062
2063         case OBD_IOC_SET_READONLY: {
2064                 void *handle;
2065                 struct super_block *sb = obd->u.filter.fo_sb;
2066                 struct inode *inode = sb->s_root->d_inode;
2067                 BDEVNAME_DECLARE_STORAGE(tmp);
2068                 CERROR("setting device %s read-only\n",
2069                        ll_bdevname(sb, tmp));
2070                 
2071                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
2072                 LASSERT(handle);
2073                 (void)fsfilt_commit(obd, inode, handle, 1);
2074
2075                 dev_set_rdonly(ll_sbdev(obd->u.filter.fo_sb), 2);
2076                 RETURN(0);
2077         }
2078
2079         case OBD_IOC_CATLOGLIST: {
2080                 rc = llog_catlog_list(obd, 1, data);
2081                 RETURN(rc);
2082         }
2083
2084         case OBD_IOC_LLOG_CANCEL:
2085         case OBD_IOC_LLOG_REMOVE: 
2086         case OBD_IOC_LLOG_INFO:
2087         case OBD_IOC_LLOG_PRINT: {
2088                 /* FIXME to be finished */
2089                 RETURN(-EOPNOTSUPP);
2090 /*
2091                 struct llog_ctxt *ctxt = NULL;
2092                 
2093                 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
2094                 rc = llog_ioctl(ctxt, cmd, data);
2095                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
2096                 
2097                 RETURN(rc);
2098 */
2099         }
2100
2101
2102         default:
2103                 RETURN(-EINVAL);
2104         }
2105         RETURN(0);
2106 }
2107
2108 static struct llog_operations filter_unlink_repl_logops;
2109 static struct llog_operations filter_size_orig_logops = {
2110         lop_setup: llog_obd_origin_setup,
2111         lop_cleanup: llog_obd_origin_cleanup,
2112         lop_add: llog_obd_origin_add
2113 };
2114
2115 static int filter_llog_init(struct obd_device *obd, struct obd_device *tgt,
2116                             int count, struct llog_logid *logid) 
2117 {
2118         struct llog_ctxt *ctxt;
2119         int rc;
2120         ENTRY;
2121         
2122         filter_unlink_repl_logops = llog_client_ops;
2123         filter_unlink_repl_logops.lop_cancel = llog_obd_repl_cancel;
2124         filter_unlink_repl_logops.lop_connect = llog_repl_connect;
2125         filter_unlink_repl_logops.lop_sync = llog_obd_repl_sync;
2126
2127         rc = llog_setup(obd, LLOG_UNLINK_REPL_CTXT, tgt, 0, NULL,
2128                         &filter_unlink_repl_logops);
2129         if (rc)
2130                 RETURN(rc);
2131         /* FIXME - assign unlink_cb for filter's recovery */
2132         ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
2133         ctxt->llog_proc_cb = filter_recov_log_unlink_cb;
2134
2135         rc = llog_setup(obd, LLOG_SIZE_ORIG_CTXT, tgt, 0, NULL,
2136                         &filter_size_orig_logops);
2137         RETURN(rc);
2138 }
2139
2140 static int filter_llog_finish(struct obd_device *obd, int count)
2141 {
2142         int rc;
2143         ENTRY;
2144         
2145         rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_REPL_CTXT));
2146         if (rc)
2147                 RETURN(rc);
2148
2149         rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_ORIG_CTXT));
2150         RETURN(rc);
2151 }
2152
2153 static struct dentry *filter_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2154                                              void *data)
2155 {
2156         return filter_fid2dentry(data, NULL, gr, id);
2157 }
2158
2159 static struct lvfs_callback_ops filter_lvfs_ops = {
2160         l_fid2dentry:     filter_lvfs_fid2dentry,
2161 };
2162
2163 static struct obd_ops filter_obd_ops = {
2164         o_owner:          THIS_MODULE,
2165         o_attach:         filter_attach,
2166         o_detach:         filter_detach,
2167         o_get_info:       filter_get_info,
2168         o_set_info:       filter_set_info,
2169         o_setup:          filter_setup,
2170         o_postsetup:      filter_postsetup,
2171         o_precleanup:     filter_precleanup,
2172         o_cleanup:        filter_cleanup,
2173         o_connect:        filter_connect,
2174         o_disconnect:     filter_disconnect,
2175         o_statfs:         filter_statfs,
2176         o_getattr:        filter_getattr,
2177         o_unpackmd:       filter_unpackmd,
2178         o_create:         filter_create,
2179         o_setattr:        filter_setattr,
2180         o_destroy:        filter_destroy,
2181         o_brw:            filter_brw,
2182         o_punch:          filter_truncate,
2183         o_sync:           filter_sync,
2184         o_preprw:         filter_preprw,
2185         o_commitrw:       filter_commitrw,
2186         o_destroy_export: filter_destroy_export,
2187         o_llog_init:      filter_llog_init,
2188         o_llog_finish:    filter_llog_finish,
2189         o_iocontrol:      filter_iocontrol,
2190 };
2191
2192 static struct obd_ops filter_sanobd_ops = {
2193         o_owner:          THIS_MODULE,
2194         o_attach:         filter_attach,
2195         o_detach:         filter_detach,
2196         o_get_info:       filter_get_info,
2197         o_set_info:       filter_set_info,
2198         o_setup:          filter_san_setup,
2199         o_precleanup:     filter_precleanup,
2200         o_cleanup:        filter_cleanup,
2201         o_connect:        filter_connect,
2202         o_disconnect:     filter_disconnect,
2203         o_statfs:         filter_statfs,
2204         o_getattr:        filter_getattr,
2205         o_unpackmd:       filter_unpackmd,
2206         o_create:         filter_create,
2207         o_setattr:        filter_setattr,
2208         o_destroy:        filter_destroy,
2209         o_brw:            filter_brw,
2210         o_punch:          filter_truncate,
2211         o_sync:           filter_sync,
2212         o_preprw:         filter_preprw,
2213         o_commitrw:       filter_commitrw,
2214         o_san_preprw:     filter_san_preprw,
2215         o_destroy_export: filter_destroy_export,
2216         o_llog_init:      filter_llog_init,
2217         o_llog_finish:    filter_llog_finish,
2218         o_iocontrol:      filter_iocontrol,
2219 };
2220
2221 static int __init obdfilter_init(void)
2222 {
2223         struct lprocfs_static_vars lvars;
2224         int rc;
2225
2226         printk(KERN_INFO "Lustre: Filtering OBD driver; info@clusterfs.com\n");
2227
2228         lprocfs_init_vars(filter, &lvars);
2229
2230         rc = class_register_type(&filter_obd_ops, lvars.module_vars,
2231                                  OBD_FILTER_DEVICENAME);
2232         if (rc)
2233                 return rc;
2234
2235         rc = class_register_type(&filter_sanobd_ops, lvars.module_vars,
2236                                  OBD_FILTER_SAN_DEVICENAME);
2237         if (rc)
2238                 class_unregister_type(OBD_FILTER_DEVICENAME);
2239         return rc;
2240 }
2241
2242 static void __exit obdfilter_exit(void)
2243 {
2244         class_unregister_type(OBD_FILTER_SAN_DEVICENAME);
2245         class_unregister_type(OBD_FILTER_DEVICENAME);
2246 }
2247
2248 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2249 MODULE_DESCRIPTION("Lustre Filtering OBD driver");
2250 MODULE_LICENSE("GPL");
2251
2252 module_init(obdfilter_init);
2253 module_exit(obdfilter_exit);