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