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