Whamcloud - gitweb
b=7237
[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 #include <linux/jbd.h>
45 #include <linux/ext3_fs.h>
46 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
47 # include <linux/mount.h>
48 # include <linux/buffer_head.h>
49 # include <linux/bio.h>
50 #endif
51
52 #include <linux/obd_class.h>
53 #include <linux/obd_lov.h>
54 #include <linux/obd_ost.h>
55 #include <linux/lustre_dlm.h>
56 #include <linux/lustre_fsfilt.h>
57 #include <linux/lprocfs_status.h>
58 #include <linux/lustre_log.h>
59 #include <linux/lustre_commit_confd.h>
60 #include <libcfs/list.h>
61
62 #include <linux/lustre_smfs.h>
63 #include <linux/lustre_sec.h>
64 #include "filter_internal.h"
65
66 /* Group 0 is no longer a legal group, to catch uninitialized IDs */
67 #define FILTER_MIN_GROUPS 3
68
69 static struct lvfs_callback_ops filter_lvfs_ops;
70
71 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
72                           struct lov_stripe_md *ea, struct obd_trans_info *);
73 struct obd_llogs *filter_grab_llog_for_group(struct obd_device *,
74                                              int, struct obd_export *);
75
76 static void filter_commit_cb(struct obd_device *obd, __u64 transno,
77                              void *cb_data, int error)
78 {
79         obd_transno_commit_cb(obd, transno, error);
80 }
81
82 /* Assumes caller has already pushed us into the kernel context. */
83 int filter_finish_transno(struct obd_export *exp, struct obd_trans_info *oti,
84                           int rc)
85 {
86         struct filter_obd *filter = &exp->exp_obd->u.filter;
87         struct filter_export_data *fed = &exp->exp_filter_data;
88         struct filter_client_data *fcd = fed->fed_fcd;
89         __u64 last_rcvd;
90         loff_t off;
91         int err, log_pri = D_HA;
92
93         /* Propagate error code. */
94         if (rc)
95                 RETURN(rc);
96
97         if (!exp->exp_obd->obd_replayable || oti == NULL)
98                 RETURN(rc);
99
100         /* we don't allocate new transnos for replayed requests */
101         if (oti->oti_transno == 0) {
102                 spin_lock(&filter->fo_translock);
103                 last_rcvd = le64_to_cpu(filter->fo_fsd->fsd_last_transno) + 1;
104                 filter->fo_fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
105                 spin_unlock(&filter->fo_translock);
106                 oti->oti_transno = last_rcvd;
107         } else {
108                 spin_lock(&filter->fo_translock);
109                 last_rcvd = oti->oti_transno;
110                 if (last_rcvd > le64_to_cpu(filter->fo_fsd->fsd_last_transno))
111                         filter->fo_fsd->fsd_last_transno =
112                                 cpu_to_le64(last_rcvd);
113                 spin_unlock(&filter->fo_translock);
114         }
115         
116         fcd->fcd_last_rcvd = cpu_to_le64(last_rcvd);
117
118         /* could get xid from oti, if it's ever needed */
119         fcd->fcd_last_xid = 0;
120
121         off = fed->fed_lr_off;
122
123         fsfilt_add_journal_cb(exp->exp_obd, filter->fo_sb, last_rcvd,
124                               oti->oti_handle, filter_commit_cb, NULL);
125
126         err = fsfilt_write_record(exp->exp_obd, filter->fo_rcvd_filp, fcd,
127                                   sizeof(*fcd), &off, 0);
128         if (err) {
129                 log_pri = D_ERROR;
130                 if (rc == 0)
131                         rc = err;
132         }
133
134         CDEBUG(log_pri, "wrote trans "LPU64" for client %s at #%d: err = %d\n",
135                last_rcvd, fcd->fcd_uuid, fed->fed_lr_idx, err);
136
137         RETURN(rc);
138 }
139
140 void f_dput(struct dentry *dentry)
141 {
142         /* Can't go inside filter_ddelete because it can block */
143         CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
144                dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
145         LASSERT(atomic_read(&dentry->d_count) > 0);
146
147         dput(dentry);
148 }
149
150 /* Add client data to the FILTER.  We use a bitmap to locate a free space
151  * in the last_rcvd file if cl_idx is -1 (i.e. a new client).
152  * Otherwise, we have just read the data from the last_rcvd file and
153  * we know its offset. */
154 static int filter_client_add(struct obd_device *obd, struct filter_obd *filter,
155                              struct filter_export_data *fed, int cl_idx)
156 {
157         unsigned long *bitmap = filter->fo_last_rcvd_slots;
158         int new_client = (cl_idx == -1);
159         ENTRY;
160
161         LASSERT(bitmap != NULL);
162
163         /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
164         if (!strcmp((char *)fed->fed_fcd->fcd_uuid, (char *)obd->obd_uuid.uuid))
165                 RETURN(0);
166
167         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
168          * there's no need for extra complication here
169          */
170         if (new_client) {
171                 cl_idx = find_first_zero_bit(bitmap, FILTER_LR_MAX_CLIENTS);
172         repeat:
173                 if (cl_idx >= FILTER_LR_MAX_CLIENTS) {
174                         CERROR("no client slots - fix FILTER_LR_MAX_CLIENTS\n");
175                         RETURN(-ENOMEM);
176                 }
177                 if (test_and_set_bit(cl_idx, bitmap)) {
178                         CERROR("FILTER client %d: found bit is set in bitmap\n",
179                                cl_idx);
180                         cl_idx = find_next_zero_bit(bitmap,
181                                                     FILTER_LR_MAX_CLIENTS,
182                                                     cl_idx);
183                         goto repeat;
184                 }
185         } else {
186                 if (test_and_set_bit(cl_idx, bitmap)) {
187                         CERROR("FILTER client %d: bit already set in bitmap!\n",
188                                cl_idx);
189                         LBUG();
190                 }
191         }
192
193         fed->fed_lr_idx = cl_idx;
194         fed->fed_lr_off = le32_to_cpu(filter->fo_fsd->fsd_client_start) +
195                 cl_idx * le16_to_cpu(filter->fo_fsd->fsd_client_size);
196
197         CDEBUG(D_INFO, "client at index %d (%llu) with UUID '%s' added\n",
198                fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
199
200         if (new_client) {
201                 struct lvfs_run_ctxt saved;
202                 loff_t off = fed->fed_lr_off;
203                 int err;
204                 void *handle;
205
206                 CDEBUG(D_INFO, "writing client fcd at idx %u (%llu) (len %u)\n",
207                        fed->fed_lr_idx,off,(unsigned int)sizeof(*fed->fed_fcd));
208
209                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
210                 /* Transaction needed to fix bug 1403 */
211                 handle = fsfilt_start(obd,
212                                       filter->fo_rcvd_filp->f_dentry->d_inode,
213                                       FSFILT_OP_SETATTR, NULL);
214                 if (IS_ERR(handle)) {
215                         err = PTR_ERR(handle);
216                         CERROR("unable to start transaction: rc %d\n", err);
217                 } else {
218                         err = fsfilt_write_record(obd, filter->fo_rcvd_filp,
219                                                   fed->fed_fcd,
220                                                   sizeof(*fed->fed_fcd),
221                                                   &off, 1);
222                         fsfilt_commit(obd, filter->fo_sb,
223                                       filter->fo_rcvd_filp->f_dentry->d_inode,
224                                       handle, 1);
225                 }
226                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
227
228                 if (err) {
229                         CERROR("error writing %s client idx %u: rc %d\n",
230                                LAST_RCVD, fed->fed_lr_idx, err);
231                         RETURN(err);
232                 }
233         }
234         RETURN(0);
235 }
236
237 static int filter_client_free(struct obd_export *exp, int flags)
238 {
239         struct filter_export_data *fed = &exp->exp_filter_data;
240         struct filter_obd *filter = &exp->exp_obd->u.filter;
241         struct obd_device *obd = exp->exp_obd;
242         struct filter_client_data zero_fcd;
243         struct lvfs_run_ctxt saved;
244         int rc;
245         loff_t off;
246         ENTRY;
247
248         if (fed->fed_fcd == NULL)
249                 RETURN(0);
250
251         if (flags & OBD_OPT_FAILOVER)
252                 GOTO(free, 0);
253
254         /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
255         if (!strcmp((char *)fed->fed_fcd->fcd_uuid, (char *)obd->obd_uuid.uuid))
256                 GOTO(free, 0);
257
258         LASSERT(filter->fo_last_rcvd_slots != NULL);
259
260         off = fed->fed_lr_off;
261
262         CDEBUG(D_INFO, "freeing client at idx %u (%lld) with UUID '%s'\n",
263                fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
264
265         /* Clear the bit _after_ zeroing out the client so we don't
266            race with filter_client_add and zero out new clients.*/
267         if (!test_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
268                 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
269                        fed->fed_lr_idx);
270                 LBUG();
271         }
272
273         memset(&zero_fcd, 0, sizeof zero_fcd);
274         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
275         rc = fsfilt_write_record(obd, filter->fo_rcvd_filp, &zero_fcd,
276                                  sizeof(zero_fcd), &off, 1);
277         if (rc == 0)
278                 /* update server's transno */
279                 filter_update_server_data(obd, filter->fo_rcvd_filp,
280                                           filter->fo_fsd, 1);
281         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
282
283         CDEBUG(rc == 0 ? D_INFO : D_ERROR,
284                "zeroing disconnecting client %s at idx %u (%llu) in %s rc %d\n",
285                fed->fed_fcd->fcd_uuid, fed->fed_lr_idx, fed->fed_lr_off,
286                LAST_RCVD, rc);
287
288         if (!test_and_clear_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
289                 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
290                        fed->fed_lr_idx);
291                 LBUG();
292         }
293
294 free:
295         OBD_FREE(fed->fed_fcd, sizeof(*fed->fed_fcd));
296
297         RETURN(0);
298 }
299
300 static int filter_free_server_data(struct filter_obd *filter)
301 {
302         OBD_FREE(filter->fo_fsd, sizeof(*filter->fo_fsd));
303         filter->fo_fsd = NULL;
304         OBD_FREE(filter->fo_last_rcvd_slots, FILTER_LR_MAX_CLIENTS/8);
305         filter->fo_last_rcvd_slots = NULL;
306         return 0;
307 }
308
309 /* assumes caller is already in kernel ctxt */
310 int filter_update_server_data(struct obd_device *obd, struct file *filp,
311                               struct filter_server_data *fsd, int force_sync)
312 {
313         loff_t off = 0;
314         int rc;
315         ENTRY;
316
317         CDEBUG(D_INODE, "server uuid      : %s\n", fsd->fsd_uuid);
318         CDEBUG(D_INODE, "server last_rcvd : "LPU64"\n",
319                le64_to_cpu(fsd->fsd_last_transno));
320         CDEBUG(D_INODE, "server last_mount: "LPU64"\n",
321                le64_to_cpu(fsd->fsd_mount_count));
322
323         rc = fsfilt_write_record(obd, filp, fsd, sizeof(*fsd), &off,force_sync);
324         if (rc)
325                 CERROR("error writing filter_server_data: rc = %d\n", rc);
326
327         RETURN(rc);
328 }
329
330 int filter_update_last_objid(struct obd_device *obd, obd_gr group,
331                              int force_sync)
332 {
333         struct filter_obd *filter = &obd->u.filter;
334         __u64 tmp;
335         loff_t off = 0;
336         int rc;
337         ENTRY;
338
339         if (filter->fo_last_objid_files[group] == NULL) {
340                 CERROR("Object group "LPU64" not fully setup; not updating "
341                        "last_objid\n", group);
342                 RETURN(0);
343         }
344
345         CDEBUG(D_INODE, "server last_objid for group "LPU64": "LPU64"\n",
346                group, filter->fo_last_objids[group]);
347
348         tmp = cpu_to_le64(filter->fo_last_objids[group]);
349         rc = fsfilt_write_record(obd, filter->fo_last_objid_files[group],
350                                  &tmp, sizeof(tmp), &off, force_sync);
351         if (rc)
352                 CERROR("error writing group "LPU64" last objid: rc = %d\n",
353                        group, rc);
354         RETURN(rc);
355 }
356
357 /* assumes caller has already in kernel ctxt */
358 static int filter_init_server_data(struct obd_device *obd, struct file * filp)
359 {
360         struct filter_obd *filter = &obd->u.filter;
361         struct filter_server_data *fsd;
362         struct filter_client_data *fcd = NULL;
363         struct inode *inode = filp->f_dentry->d_inode;
364         unsigned long last_rcvd_size = inode->i_size;
365         __u64 mount_count;
366         int cl_idx;
367         loff_t off = 0;
368         int rc;
369
370         /* ensure padding in the struct is the correct size */
371         LASSERT (offsetof(struct filter_server_data, fsd_padding) +
372                  sizeof(fsd->fsd_padding) == FILTER_LR_SERVER_SIZE);
373         LASSERT (offsetof(struct filter_client_data, fcd_padding) +
374                  sizeof(fcd->fcd_padding) == FILTER_LR_CLIENT_SIZE);
375
376         OBD_ALLOC(fsd, sizeof(*fsd));
377         if (!fsd)
378                 RETURN(-ENOMEM);
379         filter->fo_fsd = fsd;
380
381         OBD_ALLOC(filter->fo_last_rcvd_slots, FILTER_LR_MAX_CLIENTS/8);
382         if (filter->fo_last_rcvd_slots == NULL) {
383                 OBD_FREE(fsd, sizeof(*fsd));
384                 RETURN(-ENOMEM);
385         }
386
387         if (last_rcvd_size == 0) {
388                 CWARN("%s: initializing new %s\n", obd->obd_name, LAST_RCVD);
389
390                 memcpy(fsd->fsd_uuid, obd->obd_uuid.uuid,sizeof(fsd->fsd_uuid));
391                 fsd->fsd_last_transno = 0;
392                 mount_count = fsd->fsd_mount_count = 0;
393                 fsd->fsd_server_size = cpu_to_le32(FILTER_LR_SERVER_SIZE);
394                 fsd->fsd_client_start = cpu_to_le32(FILTER_LR_CLIENT_START);
395                 fsd->fsd_client_size = cpu_to_le16(FILTER_LR_CLIENT_SIZE);
396                 fsd->fsd_subdir_count = cpu_to_le16(FILTER_SUBDIR_COUNT);
397                 filter->fo_subdir_count = FILTER_SUBDIR_COUNT;
398         } else {
399                 rc = fsfilt_read_record(obd, filp, fsd, sizeof(*fsd), &off);
400                 if (rc) {
401                         CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
402                                LAST_RCVD, rc);
403                         GOTO(err_fsd, rc);
404                 }
405                 if (strcmp((char *)fsd->fsd_uuid, (char *)obd->obd_uuid.uuid)) {
406                         CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
407                                obd->obd_uuid.uuid, fsd->fsd_uuid);
408                         GOTO(err_fsd, rc = -EINVAL);
409                 }
410                 mount_count = le64_to_cpu(fsd->fsd_mount_count);
411                 filter->fo_subdir_count = le16_to_cpu(fsd->fsd_subdir_count);
412         }
413
414         if (fsd->fsd_feature_incompat & ~cpu_to_le32(FILTER_INCOMPAT_SUPP)) {
415                 CERROR("unsupported feature %x\n",
416                        le32_to_cpu(fsd->fsd_feature_incompat) &
417                        ~FILTER_INCOMPAT_SUPP);
418                 GOTO(err_fsd, rc = -EINVAL);
419         }
420         if (fsd->fsd_feature_rocompat & ~cpu_to_le32(FILTER_ROCOMPAT_SUPP)) {
421                 CERROR("read-only feature %x\n",
422                        le32_to_cpu(fsd->fsd_feature_rocompat) &
423                        ~FILTER_ROCOMPAT_SUPP);
424                 /* Do something like remount filesystem read-only */
425                 GOTO(err_fsd, rc = -EINVAL);
426         }
427
428         CDEBUG(D_INODE, "%s: server last_rcvd : "LPU64"\n",
429                obd->obd_name, le64_to_cpu(fsd->fsd_last_transno));
430         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
431                obd->obd_name, mount_count + 1);
432         CDEBUG(D_INODE, "%s: server data size: %u\n",
433                obd->obd_name, le32_to_cpu(fsd->fsd_server_size));
434         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
435                obd->obd_name, le32_to_cpu(fsd->fsd_client_start));
436         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
437                obd->obd_name, le32_to_cpu(fsd->fsd_client_size));
438         CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
439                obd->obd_name, le16_to_cpu(fsd->fsd_subdir_count));
440         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
441                last_rcvd_size <= le32_to_cpu(fsd->fsd_client_start) ? 0 :
442                (last_rcvd_size - le32_to_cpu(fsd->fsd_client_start)) /
443                 le16_to_cpu(fsd->fsd_client_size));
444
445         if (!obd->obd_replayable) {
446                 CWARN("%s: recovery support OFF\n", obd->obd_name);
447                 GOTO(out, rc = 0);
448         }
449
450         for (cl_idx = 0, off = le32_to_cpu(fsd->fsd_client_start);
451              off < last_rcvd_size; cl_idx++) {
452                 __u64 last_rcvd;
453                 struct obd_export *exp;
454                 struct filter_export_data *fed;
455
456                 if (!fcd) {
457                         OBD_ALLOC(fcd, sizeof(*fcd));
458                         if (!fcd)
459                                 GOTO(err_client, rc = -ENOMEM);
460                 }
461
462                 /* Don't assume off is incremented properly by
463                  * fsfilt_read_record(), in case sizeof(*fcd)
464                  * isn't the same as fsd->fsd_client_size.  */
465                 off = le32_to_cpu(fsd->fsd_client_start) +
466                         cl_idx * le16_to_cpu(fsd->fsd_client_size);
467                 rc = fsfilt_read_record(obd, filp, fcd, sizeof(*fcd), &off);
468                 if (rc) {
469                         CERROR("error reading FILT %s idx %d off %llu: rc %d\n",
470                                LAST_RCVD, cl_idx, off, rc);
471                         break; /* read error shouldn't cause startup to fail */
472                 }
473
474                 if (fcd->fcd_uuid[0] == '\0') {
475                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
476                                cl_idx);
477                         continue;
478                 }
479
480                 last_rcvd = le64_to_cpu(fcd->fcd_last_rcvd);
481
482                 /* These exports are cleaned up by filter_disconnect(), so they
483                  * need to be set up like real exports as filter_connect() does.
484                  */
485                 exp = class_new_export(obd);
486                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64" "
487                        "srv lr: "LPU64" fcd_group %d \n", fcd->fcd_uuid, cl_idx,
488                        last_rcvd, le64_to_cpu(fsd->fsd_last_transno), 
489                        le32_to_cpu(fcd->fcd_group));
490                 if (exp == NULL)
491                         GOTO(err_client, rc = -ENOMEM);
492
493                 memcpy(&exp->exp_client_uuid.uuid, fcd->fcd_uuid,
494                        sizeof exp->exp_client_uuid.uuid);
495                 fed = &exp->exp_filter_data;
496                 fed->fed_fcd = fcd;
497                 fed->fed_group = le32_to_cpu(fcd->fcd_group);
498                 filter_client_add(obd, filter, fed, cl_idx);
499                 /* create helper if export init gets more complex */
500                 spin_lock_init(&fed->fed_lock);
501
502                 fcd = NULL;
503                 exp->exp_connected = 0;
504                 exp->exp_req_replay_needed = 1;
505                 exp->exp_lock_replay_needed = 1;
506                 atomic_inc(&obd->obd_req_replay_clients);
507                 atomic_inc(&obd->obd_lock_replay_clients);
508                 obd->obd_recoverable_clients++;
509                 obd->obd_max_recoverable_clients++;
510                 class_export_put(exp);
511
512                 CDEBUG(D_OTHER, "client at idx %d has last_rcvd = "LPU64"\n",
513                        cl_idx, last_rcvd);
514
515                 if (last_rcvd > le64_to_cpu(fsd->fsd_last_transno))
516                         fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
517
518         }
519
520         obd->obd_last_committed = le64_to_cpu(fsd->fsd_last_transno);
521
522         if (obd->obd_recoverable_clients) {
523                 CWARN("RECOVERY: service %s, %d recoverable clients, "
524                       "last_transno "LPU64"\n", obd->obd_name,
525                       obd->obd_recoverable_clients,
526                       le64_to_cpu(fsd->fsd_last_transno));
527                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
528                 target_start_recovery_thread(obd, ost_handle);
529         }
530
531         if (fcd)
532                 OBD_FREE(fcd, sizeof(*fcd));
533
534 out:
535         filter->fo_mount_count = mount_count + 1;
536         fsd->fsd_mount_count = cpu_to_le64(filter->fo_mount_count);
537
538         /* save it, so mount count and last_transno is current */
539         rc = filter_update_server_data(obd, filp, filter->fo_fsd, 1);
540         if (rc)
541                 GOTO(err_client, rc);
542         RETURN(0);
543
544 err_client:
545         class_disconnect_exports(obd, 0);
546 err_fsd:
547         filter_free_server_data(filter);
548         RETURN(rc);
549 }
550
551 static int filter_cleanup_groups(struct obd_device *obd)
552 {
553         struct filter_obd *filter = &obd->u.filter;
554         struct dentry *dentry;
555         int i, k;
556         ENTRY;
557
558         for (i = 0; i < filter->fo_group_count; i++) {
559                 if (filter->fo_subdirs != NULL) {
560                         for (k = 0; k < filter->fo_subdir_count; k++) {
561                                 dentry = filter->fo_subdirs[i].dentry[k];
562                                 if (dentry == NULL)
563                                         continue;
564                                 f_dput(dentry);
565                                 filter->fo_subdirs[i].dentry[k] = NULL;
566                         }
567                 }
568                 if (filter->fo_last_objid_files[i] != NULL) {
569                         filp_close(filter->fo_last_objid_files[i], 0);
570                         filter->fo_last_objid_files[i] = NULL;
571                 }
572                 if (filter->fo_groups[i] != NULL) {
573                         dput(filter->fo_groups[i]);
574                         filter->fo_groups[i] = NULL;
575                 }
576         }
577         if (filter->fo_subdirs != NULL)
578                 OBD_FREE(filter->fo_subdirs,
579                          filter->fo_group_count * sizeof(*filter->fo_subdirs));
580         if (filter->fo_groups != NULL)
581                 OBD_FREE(filter->fo_groups,
582                          filter->fo_group_count * sizeof(*filter->fo_groups));
583         if (filter->fo_last_objids != NULL)
584                 OBD_FREE(filter->fo_last_objids,
585                          filter->fo_group_count * sizeof(__u64));
586         if (filter->fo_last_objid_files != NULL)
587                 OBD_FREE(filter->fo_last_objid_files,
588                          filter->fo_group_count * sizeof(struct file *));
589         f_dput(filter->fo_dentry_O);
590         RETURN(0);
591 }
592
593 static int filter_update_last_group(struct obd_device *obd, int group)
594 {
595         struct filter_obd *filter = &obd->u.filter;
596         struct file *filp = NULL;
597         int last_group = 0, rc;
598         loff_t off = 0;
599         ENTRY;
600
601         if (group <= filter->fo_committed_group)
602                 RETURN(0);
603
604         filp = filp_open("LAST_GROUP", O_RDWR, 0700);
605         if (IS_ERR(filp)) {
606                 rc = PTR_ERR(filp);
607                 filp = NULL;
608                 CERROR("cannot open LAST_GROUP: rc = %d\n", rc);
609                 GOTO(cleanup, rc);
610         }
611
612         rc = fsfilt_read_record(obd, filp, &last_group, sizeof(__u32), &off);
613         if (rc) {
614                 CDEBUG(D_INODE, "error reading LAST_GROUP: rc %d\n",rc);
615                 GOTO(cleanup, rc);
616         }
617         LASSERT(off == 0 || last_group >= FILTER_MIN_GROUPS);
618         CDEBUG(D_INODE, "%s: previous %d, new %d\n",
619                obd->obd_name, last_group, group);
620
621         off = 0;
622         last_group = group;
623         /* must be sync: bXXXX */
624         rc = fsfilt_write_record(obd, filp, &last_group, sizeof(__u32), &off, 1);
625         if (rc) {
626                 CDEBUG(D_INODE, "error updating LAST_GROUP: rc %d\n", rc);
627                 GOTO(cleanup, rc);
628         }
629
630         filter->fo_committed_group = group;
631 cleanup:
632         if (filp)
633                 filp_close(filp, 0);
634         RETURN(rc);
635 }
636
637 static int filter_read_group_internal(struct obd_device *obd, int group,
638                                       int create)
639 {
640         struct filter_obd *filter = &obd->u.filter;
641         __u64 *new_objids = NULL;
642         struct filter_subdirs *new_subdirs = NULL, *tmp_subdirs = NULL;
643         struct dentry **new_groups = NULL;
644         struct file **new_files = NULL;
645         struct dentry *dentry;
646         struct file *filp;
647         int old_count = filter->fo_group_count, rc, stage = 0, i;
648         char name[25];
649         __u64 last_objid;
650         loff_t off = 0;
651
652         snprintf(name, 24, "%d", group);
653         name[24] = '\0';
654
655         if (!create) {
656                 dentry = ll_lookup_one_len(name, filter->fo_dentry_O,
657                                            strlen(name));
658                 if (IS_ERR(dentry)) {
659                         CERROR("Cannot lookup expected object group %d: %ld\n",
660                                group, PTR_ERR(dentry));
661                         RETURN(PTR_ERR(dentry));
662                 }
663         } else {
664                 dentry = simple_mkdir(filter->fo_dentry_O, name, 0700, 1);
665                 if (IS_ERR(dentry)) {
666                         CERROR("cannot lookup/create O/%s: rc = %ld\n", name,
667                                PTR_ERR(dentry));
668                         RETURN(PTR_ERR(dentry));
669                 }
670         }
671         stage = 1;
672
673         snprintf(name, 24, "O/%d/LAST_ID", group);
674         name[24] = '\0';
675         filp = filp_open(name, O_CREAT | O_RDWR, 0700);
676         if (IS_ERR(filp)) {
677                 CERROR("cannot create %s: rc = %ld\n", name, PTR_ERR(filp));
678                 GOTO(cleanup, rc = PTR_ERR(filp));
679         }
680         stage = 2;
681
682         rc = fsfilt_read_record(obd, filp, &last_objid, sizeof(__u64), &off);
683         if (rc) {
684                 CDEBUG(D_INODE, "error reading %s: rc %d\n", name, rc);
685                 GOTO(cleanup, rc);
686         }
687
688         if (filter->fo_subdir_count) {
689                 OBD_ALLOC(tmp_subdirs, sizeof(*tmp_subdirs));
690                 if (tmp_subdirs == NULL)
691                         GOTO(cleanup, rc = -ENOMEM);
692                 stage = 3;
693
694                 for (i = 0; i < filter->fo_subdir_count; i++) {
695                         char dir[20];
696                         snprintf(dir, sizeof(dir), "d%u", i);
697
698                         tmp_subdirs->dentry[i] =
699                                 simple_mkdir(dentry, dir, 0700, 1);
700                         if (IS_ERR(tmp_subdirs->dentry[i])) {
701                                 rc = PTR_ERR(tmp_subdirs->dentry[i]);
702                                 CERROR("can't lookup/create O/%d/%s: rc = %d\n",
703                                        group, dir, rc);
704                                 GOTO(cleanup, rc);
705                         }
706                         CDEBUG(D_INODE, "got/created O/%d/%s: %p\n", group, dir,
707                                tmp_subdirs->dentry[i]);
708                 }
709         }
710
711         /* 'group' is an index; we need an array of length 'group + 1' */
712         if (group + 1 > old_count) {
713                 int len = group + 1;
714                 OBD_ALLOC(new_objids, len * sizeof(*new_objids));
715                 OBD_ALLOC(new_subdirs, len * sizeof(*new_subdirs));
716                 OBD_ALLOC(new_groups, len * sizeof(*new_groups));
717                 OBD_ALLOC(new_files, len * sizeof(*new_files));
718                 stage = 4;
719                 if (new_objids == NULL || new_subdirs == NULL ||
720                     new_groups == NULL || new_files == NULL)
721                         GOTO(cleanup, rc = -ENOMEM);
722
723                 memcpy(new_objids, filter->fo_last_objids,
724                        old_count * sizeof(*new_objids));
725                 memcpy(new_subdirs, filter->fo_subdirs,
726                        old_count * sizeof(*new_subdirs));
727                 memcpy(new_groups, filter->fo_groups,
728                        old_count * sizeof(*new_groups));
729                 memcpy(new_files, filter->fo_last_objid_files,
730                        old_count * sizeof(*new_files));
731
732                 if (old_count) {
733                         OBD_FREE(filter->fo_last_objids,
734                                  old_count * sizeof(*new_objids));
735                         OBD_FREE(filter->fo_subdirs,
736                                  old_count * sizeof(*new_subdirs));
737                         OBD_FREE(filter->fo_groups,
738                                  old_count * sizeof(*new_groups));
739                         OBD_FREE(filter->fo_last_objid_files,
740                                  old_count * sizeof(*new_files));
741                 }
742                 filter->fo_last_objids = new_objids;
743                 filter->fo_subdirs = new_subdirs;
744                 filter->fo_groups = new_groups;
745                 filter->fo_last_objid_files = new_files;
746                 filter->fo_group_count = len;
747         }
748
749         filter->fo_groups[group] = dentry;
750         filter->fo_last_objid_files[group] = filp;
751         if (filter->fo_subdir_count) {
752                 filter->fo_subdirs[group] = *tmp_subdirs;
753                 OBD_FREE(tmp_subdirs, sizeof(*tmp_subdirs));
754         }
755
756         filter_update_last_group(obd, group);
757         
758         if (filp->f_dentry->d_inode->i_size == 0) {
759                 filter->fo_last_objids[group] = FILTER_INIT_OBJID;
760                 RETURN(0);
761         }
762
763         filter->fo_last_objids[group] = le64_to_cpu(last_objid);
764         CDEBUG(D_INODE, "%s: server last_objid group %d: "LPU64"\n",
765                obd->obd_name, group, last_objid);
766         RETURN(0);
767  cleanup:
768         switch (stage) {
769         case 4:
770                 if (new_objids != NULL)
771                         OBD_FREE(new_objids, group * sizeof(*new_objids));
772                 if (new_subdirs != NULL)
773                         OBD_FREE(new_subdirs, group * sizeof(*new_subdirs));
774                 if (new_groups != NULL)
775                         OBD_FREE(new_groups, group * sizeof(*new_groups));
776                 if (new_files != NULL)
777                         OBD_FREE(new_files, group * sizeof(*new_files));
778         case 3:
779                 if (filter->fo_subdir_count) {
780                         for (i = 0; i < filter->fo_subdir_count; i++) {
781                                 if (tmp_subdirs->dentry[i] != NULL)
782                                         dput(tmp_subdirs->dentry[i]);
783                         }
784                         OBD_FREE(tmp_subdirs, sizeof(*tmp_subdirs));
785                 }
786         case 2:
787                 filp_close(filp, 0);
788         case 1:
789                 dput(dentry);
790         }
791         RETURN(rc);
792 }
793
794 static int filter_read_groups(struct obd_device *obd, int last_group,
795                               int create)
796 {
797         struct filter_obd *filter = &obd->u.filter;
798         int old_count, group, rc = 0;
799
800         down(&filter->fo_init_lock);
801         old_count = filter->fo_group_count;
802         for (group = old_count; group <= last_group; group++) {
803                 if (group == 0)
804                         continue; /* no group zero */
805
806                 rc = filter_read_group_internal(obd, group, create);
807                 if (rc != 0)
808                         break;
809         }
810         up(&filter->fo_init_lock);
811         return rc;
812 }
813
814 static int filter_prep_groups(struct obd_device *obd)
815 {
816         struct filter_obd *filter = &obd->u.filter;
817         struct dentry *dentry, *O_dentry;
818         int rc = 0, cleanup_phase = 0;
819         struct file *filp = NULL;
820         int last_group;
821         loff_t off = 0;
822         ENTRY;
823
824         O_dentry = simple_mkdir(current->fs->pwd, "O", 0700, 1);
825         CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
826         if (IS_ERR(O_dentry)) {
827                 rc = PTR_ERR(O_dentry);
828                 CERROR("cannot open/create O: rc = %d\n", rc);
829                 GOTO(cleanup, rc);
830         }
831         filter->fo_dentry_O = O_dentry;
832         cleanup_phase = 1; /* O_dentry */
833
834         /* Lookup "R" to tell if we're on an old OST FS and need to convert
835          * from O/R/<dir>/<objid> to O/0/<dir>/<objid>.  This can be removed
836          * some time post 1.0 when all old-style OSTs have converted along
837          * with the init_objid hack. */
838         dentry = ll_lookup_one_len("R", O_dentry, 1);
839         if (IS_ERR(dentry))
840                 GOTO(cleanup, rc = PTR_ERR(dentry));
841         if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
842                 struct dentry *O0_dentry = lookup_one_len("0", O_dentry, 1);
843                 ENTRY;
844
845                 CWARN("converting OST to new object layout\n");
846                 if (IS_ERR(O0_dentry)) {
847                         rc = PTR_ERR(O0_dentry);
848                         CERROR("error looking up O/0: rc %d\n", rc);
849                         GOTO(cleanup_R, rc);
850                 }
851
852                 if (O0_dentry->d_inode) {
853                         CERROR("Both O/R and O/0 exist. Fix manually.\n");
854                         GOTO(cleanup_O0, rc = -EEXIST);
855                 }
856
857                 down(&O_dentry->d_inode->i_sem);
858                 rc = vfs_rename(O_dentry->d_inode, dentry,
859                                 O_dentry->d_inode, O0_dentry);
860                 up(&O_dentry->d_inode->i_sem);
861
862                 if (rc) {
863                         CERROR("error renaming O/R to O/0: rc %d\n", rc);
864                         GOTO(cleanup_O0, rc);
865                 }
866                 filter->fo_fsd->fsd_feature_incompat |=
867                         cpu_to_le32(FILTER_INCOMPAT_GROUPS);
868                 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
869                                                filter->fo_fsd, 1);
870                 GOTO(cleanup_O0, rc);
871
872         cleanup_O0:
873                 f_dput(O0_dentry);
874         cleanup_R:
875                 f_dput(dentry);
876                 if (rc)
877                         GOTO(cleanup, rc);
878         } else {
879                 f_dput(dentry);
880         }
881
882         cleanup_phase = 2; /* groups */
883
884         /* we have to initialize all groups before first connections from
885          * clients because they may send create/destroy for any group -bzzz */
886         filp = filp_open("LAST_GROUP", O_CREAT | O_RDWR, 0700);
887         if (IS_ERR(filp)) {
888                 CERROR("cannot create LAST_GROUP: rc = %ld\n", PTR_ERR(filp));
889                 GOTO(cleanup, rc = PTR_ERR(filp));
890         }
891         cleanup_phase = 3; /* filp */
892
893         rc = fsfilt_read_record(obd, filp, &last_group, sizeof(__u32), &off);
894         if (rc) {
895                 CDEBUG(D_INODE, "error reading LAST_GROUP: rc %d\n", rc);
896                 GOTO(cleanup, rc);
897         }
898         if (off == 0) {
899                 last_group = FILTER_MIN_GROUPS;
900         } else {
901                 LASSERT(last_group >= FILTER_MIN_GROUPS);
902         }
903
904         CWARN("%s: initialize groups [%d,%d]\n", obd->obd_name,
905               FILTER_MIN_GROUPS, last_group);
906         filter->fo_committed_group = last_group;
907         rc = filter_read_groups(obd, last_group, 1);
908         if (rc)
909                 GOTO(cleanup, rc);
910         
911         filp_close(filp, 0);
912         RETURN(0);
913
914  cleanup:
915         switch (cleanup_phase) {
916         case 3:
917                 filp_close(filp, 0);
918         case 2:
919                 filter_cleanup_groups(obd);
920         case 1:
921                 f_dput(filter->fo_dentry_O);
922                 filter->fo_dentry_O = NULL;
923         default:
924                 break;
925         }
926         return rc;
927 }
928
929 /* setup the object store with correct subdirectories */
930 static int filter_prep(struct obd_device *obd)
931 {
932         struct lvfs_run_ctxt saved;
933         struct filter_obd *filter = &obd->u.filter;
934         struct file *file;
935         struct inode *inode;
936         int rc = 0;
937         ENTRY;
938
939         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
940         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
941         if (!file || IS_ERR(file)) {
942                 rc = PTR_ERR(file);
943                 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
944                        LAST_RCVD, rc);
945                 GOTO(out, rc);
946         }
947
948         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
949                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
950                        file->f_dentry->d_inode->i_mode);
951                 GOTO(err_filp, rc = -ENOENT);
952         }
953
954         /* steal operations */
955         inode = file->f_dentry->d_inode;
956         filter->fo_fop = file->f_op;
957         filter->fo_iop = inode->i_op;
958         filter->fo_aops = inode->i_mapping->a_ops;
959
960         rc = filter_init_server_data(obd, file);
961         if (rc) {
962                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
963                 GOTO(err_filp, rc);
964         }
965         filter->fo_rcvd_filp = file;
966
967         rc = filter_prep_groups(obd);
968         if (rc)
969                 GOTO(err_server_data, rc);
970
971  out:
972         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
973
974         return(rc);
975
976  err_server_data:
977         //class_disconnect_exports(obd, 0);
978         filter_free_server_data(filter);
979  err_filp:
980         if (filp_close(file, 0))
981                 CERROR("can't close %s after error\n", LAST_RCVD);
982         filter->fo_rcvd_filp = NULL;
983         goto out;
984 }
985
986 /* cleanup the filter: write last used object id to status file */
987 static void filter_post(struct obd_device *obd)
988 {
989         struct lvfs_run_ctxt saved;
990         struct filter_obd *filter = &obd->u.filter;
991         int rc, i;
992
993         /* XXX: filter_update_lastobjid used to call fsync_dev.  It might be
994          * best to start a transaction with h_sync, because we removed this
995          * from lastobjid */
996
997         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
998         rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
999                                        filter->fo_fsd, 0);
1000         if (rc)
1001                 CERROR("error writing server data: rc = %d\n", rc);
1002
1003         for (i = 1; i < filter->fo_group_count; i++) {
1004                 rc = filter_update_last_objid(obd, i,
1005                                              (i == filter->fo_group_count - 1));
1006                 if (rc)
1007                         CERROR("error writing group %d lastobjid: rc = %d\n",
1008                                i, rc);
1009         }
1010
1011         rc = filp_close(filter->fo_rcvd_filp, 0);
1012         filter->fo_rcvd_filp = NULL;
1013         if (rc)
1014                 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
1015
1016         filter_cleanup_groups(obd);
1017         filter_free_server_data(filter);
1018         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1019 }
1020
1021 static void filter_set_last_id(struct filter_obd *filter, int group, obd_id id)
1022 {
1023         LASSERT(filter->fo_fsd != NULL);
1024         LASSERT(group > 0);
1025         LASSERT(group < filter->fo_group_count);
1026
1027         spin_lock(&filter->fo_objidlock);
1028         filter->fo_last_objids[group] = id;
1029         spin_unlock(&filter->fo_objidlock);
1030 }
1031
1032 __u64 filter_last_id(struct filter_obd *filter, int group)
1033 {
1034         obd_id id;
1035         LASSERT(filter->fo_fsd != NULL);
1036         LASSERT(group > 0);
1037         LASSERT(group < filter->fo_group_count);
1038
1039         spin_lock(&filter->fo_objidlock);
1040         id = filter->fo_last_objids[group];
1041         spin_unlock(&filter->fo_objidlock);
1042
1043         return id;
1044 }
1045
1046 static void filter_save_last_id(struct filter_obd *filter, int group, obd_id id)
1047 {
1048         LASSERT(group > 0);
1049         LASSERT(group < filter->fo_group_count);
1050
1051         spin_lock(&filter->fo_lastidlock);
1052         if (id > filter_last_id(filter, group))
1053                 filter_set_last_id(filter, group, id);
1054         spin_unlock(&filter->fo_lastidlock);
1055 }
1056         
1057 /* direct cut-n-paste of mds_blocking_ast() */
1058 static int filter_blocking_ast(struct ldlm_lock *lock,
1059                                struct ldlm_lock_desc *desc,
1060                                void *data, int flag)
1061 {
1062         int do_ast;
1063         ENTRY;
1064
1065         if (flag == LDLM_CB_CANCELING) {
1066                 /* Don't need to do anything here. */
1067                 RETURN(0);
1068         }
1069
1070         /* XXX layering violation!  -phil */
1071         lock_res_and_lock(lock);
1072         /* Get this: if filter_blocking_ast is racing with ldlm_intent_policy,
1073          * such that filter_blocking_ast is called just before l_i_p takes the
1074          * ns_lock, then by the time we get the lock, we might not be the
1075          * correct blocking function anymore.  So check, and return early, if
1076          * so. */
1077         if (lock->l_blocking_ast != filter_blocking_ast) {
1078                 unlock_res_and_lock(lock);
1079                 RETURN(0);
1080         }
1081
1082         lock->l_flags |= LDLM_FL_CBPENDING;
1083         do_ast = (!lock->l_readers && !lock->l_writers);
1084         unlock_res_and_lock(lock);
1085
1086         if (do_ast) {
1087                 struct lustre_handle lockh;
1088                 int rc;
1089
1090                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
1091                 ldlm_lock2handle(lock, &lockh);
1092                 rc = ldlm_cli_cancel(&lockh);
1093                 if (rc < 0)
1094                         CERROR("ldlm_cli_cancel: %d\n", rc);
1095         } else {
1096                 LDLM_DEBUG(lock, "Lock still has references, will be "
1097                            "cancelled later");
1098         }
1099         RETURN(0);
1100 }
1101
1102 extern void *lock_dir(struct inode *dir, struct qstr *name);
1103 extern void unlock_dir(struct inode *dir, void *lock);
1104
1105 static void *filter_lock_dentry(struct obd_device *obd,
1106                                 struct dentry *dparent,
1107                                 obd_id id)
1108 {
1109 #ifdef S_PDIROPS
1110         struct qstr qstr;
1111         char name[32];
1112         int len;
1113
1114         len = sprintf(name, LPU64, id);
1115         qstr_assign(&qstr, (char *)name, len);
1116         return lock_dir(dparent->d_inode, &qstr);
1117 #else
1118         down(&dparent->d_inode->i_sem);
1119 #endif
1120         return 0;
1121 }
1122
1123 /* We never dget the object parent, so DON'T dput it either */
1124 static void filter_parent_unlock(struct dentry *dparent, void *lock)
1125 {
1126 #ifdef S_PDIROPS
1127         LASSERT(lock != NULL);
1128         unlock_dir(dparent->d_inode, lock);
1129 #else
1130         up(&dparent->d_inode->i_sem);
1131 #endif
1132 }
1133
1134 /* We never dget the object parent, so DON'T dput it either */
1135 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
1136 {
1137         struct filter_obd *filter = &obd->u.filter;
1138         LASSERT(group < filter->fo_group_count);
1139         LASSERT(group > 0);
1140
1141         if (filter->fo_subdir_count == 0)
1142                 return filter->fo_groups[group];
1143
1144         return filter->fo_subdirs[group].dentry[objid & (filter->fo_subdir_count - 1)];
1145 }
1146
1147 /* We never dget the object parent, so DON'T dput it either */
1148 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
1149                                   obd_id objid, void **lock)
1150 {
1151         unsigned long now = jiffies;
1152         struct dentry *dparent = filter_parent(obd, group, objid);
1153
1154         if (IS_ERR(dparent))
1155                 return dparent;
1156
1157         LASSERT(dparent);
1158         LASSERT(dparent->d_inode);
1159
1160         *lock = filter_lock_dentry(obd, dparent, objid);
1161         fsfilt_check_slow(now, obd_timeout, "parent lock");
1162         return dparent;
1163 }
1164
1165 /* How to get files, dentries, inodes from object id's.
1166  *
1167  * If dir_dentry is passed, the caller has already locked the parent
1168  * appropriately for this operation (normally a write lock).  If
1169  * dir_dentry is NULL, we do a read lock while we do the lookup to
1170  * avoid races with create/destroy and such changing the directory
1171  * internal to the filesystem code. */
1172 struct dentry *filter_id2dentry(struct obd_device *obd,
1173                                 struct dentry *dir_dentry,
1174                                 obd_gr group, obd_id id)
1175 {
1176         struct dentry *dparent = dir_dentry;
1177         struct dentry *dchild;
1178         void *lock = NULL;
1179         char name[32];
1180         int len;
1181         ENTRY;
1182
1183         if (id == 0) {
1184                 CERROR("fatal: invalid object id 0\n");
1185                 RETURN(ERR_PTR(-ESTALE));
1186         }
1187
1188         len = sprintf(name, LPU64, id);
1189         if (dir_dentry == NULL) {
1190                 dparent = filter_parent_lock(obd, group, id, &lock);
1191                 if (IS_ERR(dparent)) {
1192                         CERROR("%s: error getting object "LPU64":"LPU64
1193                                " parent: rc %ld\n", obd->obd_name,
1194                                id, group, PTR_ERR(dparent));
1195                         RETURN(dparent);
1196                 }
1197         }
1198         CDEBUG(D_INODE, "looking up object O/%.*s/%s\n",
1199                dparent->d_name.len, dparent->d_name.name, name);
1200         dchild = /*ll_*/lookup_one_len(name, dparent, len);
1201         if (dir_dentry == NULL)
1202                 filter_parent_unlock(dparent, lock);
1203         if (IS_ERR(dchild)) {
1204                 CERROR("%s: child lookup error %ld\n", obd->obd_name,
1205                        PTR_ERR(dchild));
1206                 RETURN(dchild);
1207         }
1208
1209         if (dchild->d_inode != NULL && is_bad_inode(dchild->d_inode)) {
1210                 CERROR("%s: got bad inode "LPU64"\n", obd->obd_name, id);
1211                 f_dput(dchild);
1212                 RETURN(ERR_PTR(-ENOENT));
1213         }
1214
1215         CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
1216                name, dchild, atomic_read(&dchild->d_count));
1217
1218         LASSERT(atomic_read(&dchild->d_count) > 0);
1219
1220         RETURN(dchild);
1221 }
1222
1223 static int filter_prepare_destroy(struct obd_device *obd, obd_id objid,
1224                                   obd_id group)
1225 {
1226         struct lustre_handle lockh;
1227         int flags = LDLM_AST_DISCARD_DATA, rc;
1228         struct ldlm_res_id res_id = { .name = { objid, 0, group, 0 } };
1229         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
1230
1231         ENTRY;
1232         /* Tell the clients that the object is gone now and that they should
1233          * throw away any cached pages. */
1234         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
1235                               LDLM_EXTENT, &policy, LCK_PW,
1236                               &flags, filter_blocking_ast, ldlm_completion_ast,
1237                               NULL, NULL, NULL, 0, NULL, &lockh);
1238
1239         /* We only care about the side-effects, just drop the lock. */
1240         if (rc == ELDLM_OK)
1241                 ldlm_lock_decref(&lockh, LCK_PW);
1242
1243         RETURN(rc);
1244 }
1245
1246 /* Caller must hold LCK_PW on parent and push us into kernel context.
1247  * Caller is also required to ensure that dchild->d_inode exists. */
1248 static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
1249                                    struct dentry *dparent,
1250                                    struct dentry *dchild)
1251 {
1252         struct inode *inode = dchild->d_inode;
1253         int rc;
1254         ENTRY;
1255
1256         if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
1257                 CERROR("destroying objid %.*s nlink = %lu, count = %d\n",
1258                        dchild->d_name.len, dchild->d_name.name,
1259                        (unsigned long)inode->i_nlink,
1260                        atomic_read(&inode->i_count));
1261         }
1262
1263         rc = vfs_unlink(dparent->d_inode, dchild);
1264
1265         if (rc)
1266                 CERROR("error unlinking objid %.*s: rc %d\n",
1267                        dchild->d_name.len, dchild->d_name.name, rc);
1268
1269         RETURN(rc);
1270 }
1271
1272 static int filter_intent_policy(struct ldlm_namespace *ns,
1273                                 struct ldlm_lock **lockp, void *req_cookie,
1274                                 ldlm_mode_t mode, int flags, void *data)
1275 {
1276         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1277         struct ptlrpc_request *req = req_cookie;
1278         struct ldlm_lock *lock = *lockp, *l = NULL;
1279         struct ldlm_resource *res = lock->l_resource;
1280         ldlm_processing_policy policy;
1281         struct ost_lvb *res_lvb, *reply_lvb;
1282         struct ldlm_reply *rep;
1283         struct list_head *tmp;
1284         ldlm_error_t err;
1285         int tmpflags = 0, rc, repsize[2] = {sizeof(*rep), sizeof(*reply_lvb)};
1286         ENTRY;
1287
1288         policy = ldlm_get_processing_policy(res);
1289         LASSERT(policy != NULL);
1290         LASSERT(req != NULL);
1291
1292         rc = lustre_pack_reply(req, 2, repsize, NULL);
1293         if (rc)
1294                 RETURN(req->rq_status = rc);
1295
1296         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
1297         LASSERT(rep != NULL);
1298
1299         reply_lvb = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*reply_lvb));
1300         LASSERT(reply_lvb != NULL);
1301
1302         //fixup_handle_for_resent_req(req, lock, &lockh);
1303
1304         /* If we grant any lock at all, it will be a whole-file read lock.
1305          * Call the extent policy function to see if our request can be
1306          * granted, or is blocked. */
1307         lock->l_policy_data.l_extent.start = 0;
1308         lock->l_policy_data.l_extent.end = OBD_OBJECT_EOF;
1309         lock->l_req_mode = LCK_PR;
1310
1311         lock_res_and_lock(lock);
1312         res = lock->l_resource;
1313         rc = policy(lock, &tmpflags, 0, &err, &rpc_list);
1314
1315         /* FIXME: we should change the policy function slightly, to not make
1316          * this list at all, since we just turn around and free it */
1317         while (!list_empty(&rpc_list)) {
1318                 struct ldlm_lock *wlock =
1319                         list_entry(rpc_list.next, struct ldlm_lock, l_cp_ast);
1320                 LASSERT((lock->l_flags & LDLM_FL_AST_SENT) == 0);
1321                 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1322                 lock->l_flags &= ~LDLM_FL_CP_REQD;
1323                 list_del_init(&wlock->l_cp_ast);
1324                 LDLM_LOCK_PUT(wlock);
1325         }
1326
1327         if (rc == LDLM_ITER_CONTINUE) {
1328                 /* The lock met with no resistance; we're finished. */
1329                 unlock_res_and_lock(lock);
1330                 RETURN(ELDLM_LOCK_REPLACED);
1331         }
1332
1333         /* Do not grant any lock, but instead send GL callbacks.  The extent
1334          * policy nicely created a list of all PW locks for us.  We will choose
1335          * the highest of those which are larger than the size in the LVB, if
1336          * any, and perform a glimpse callback. */
1337         res_lvb = res->lr_lvb_data;
1338         LASSERT(res_lvb != NULL);
1339         *reply_lvb = *res_lvb;
1340
1341         list_for_each(tmp, &res->lr_granted) {
1342                 struct ldlm_lock *tmplock =
1343                         list_entry(tmp, struct ldlm_lock, l_res_link);
1344
1345                 if (tmplock->l_granted_mode == LCK_PR)
1346                         continue;
1347
1348                 if (tmplock->l_policy_data.l_extent.end <= reply_lvb->lvb_size)
1349                         continue;
1350
1351                 if (l == NULL) {
1352                         l = LDLM_LOCK_GET(tmplock);
1353                         continue;
1354                 }
1355
1356                 if (l->l_policy_data.l_extent.start >
1357                     tmplock->l_policy_data.l_extent.start)
1358                         continue;
1359
1360                 LDLM_LOCK_PUT(l);
1361                 l = LDLM_LOCK_GET(tmplock);
1362         }
1363         unlock_res(res);
1364
1365         /* There were no PW locks beyond the size in the LVB; finished. */
1366         if (l == NULL)
1367                 RETURN(ELDLM_LOCK_ABORTED);
1368
1369         if (l->l_glimpse_ast == NULL) {
1370                 /* We are racing with unlink(); just return -ENOENT */
1371                 rep->lock_policy_res1 = -ENOENT;
1372                 goto out;
1373         }
1374
1375         LASSERTF(l->l_glimpse_ast != NULL, "l == %p", l);
1376
1377         rc = l->l_glimpse_ast(l, NULL); /* this will update the LVB */
1378         if (rc != 0 && res->lr_namespace->ns_lvbo &&
1379             res->lr_namespace->ns_lvbo->lvbo_update) {
1380                 res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
1381         }
1382
1383         lock_res(res);
1384         *reply_lvb = *res_lvb;
1385         unlock_res(res);
1386 out:
1387         LDLM_LOCK_PUT(l);
1388
1389         RETURN(ELDLM_LOCK_ABORTED);
1390 }
1391
1392 static int filter_post_fs_cleanup(struct obd_device *obd)
1393 {
1394         int rc = 0;
1395
1396         rc = fsfilt_post_cleanup(obd);
1397
1398         RETURN(rc);
1399 }
1400
1401 #if 0
1402 static int filter_group_set_fs_flags(struct obd_device *obd, int group)
1403 {
1404         struct filter_obd *filter = &obd->u.filter;
1405         int rc = 0, i = 0;
1406         ENTRY;        
1407         
1408         /* zero group is not longer valid. */
1409         if (group== 0)
1410                 RETURN(rc); 
1411         for (i = 0; i < filter->fo_subdir_count; i++) {
1412                 struct dentry *dentry;
1413                 dentry = (filter->fo_subdirs + group)->dentry[i];
1414                 rc = fsfilt_set_fs_flags(obd, dentry->d_inode, 
1415                                          SM_DO_REC | SM_DO_COW);
1416                 if (rc)
1417                         RETURN(rc);
1418         }
1419         RETURN(rc);
1420 }
1421 #endif
1422
1423 static int filter_post_fs_setup(struct obd_device *obd)
1424 {
1425         struct filter_obd *filter = &obd->u.filter;
1426         int rc = 0;
1427         
1428         rc = fsfilt_post_setup(obd, filter->fo_dentry_O);
1429
1430         return rc;
1431 }
1432
1433 /* mount the file system (secretly) */
1434 int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
1435                         char *option)
1436 {
1437         struct lustre_cfg *lcfg = buf;
1438         struct filter_obd *filter = &obd->u.filter;
1439         struct lvfs_obd_ctxt *lvfs_ctxt = NULL;
1440         struct vfsmount *mnt;
1441         char *str;
1442         char ns_name[48];
1443         int rc = 0, i;
1444         ENTRY;
1445
1446         if ((LUSTRE_CFG_BUFLEN(lcfg, 1)) < 1 || 
1447             (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1)) 
1448                 RETURN(-EINVAL);
1449
1450         obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
1451         if (IS_ERR(obd->obd_fsops))
1452                 RETURN(PTR_ERR(obd->obd_fsops));
1453
1454         rc = lvfs_mount_fs(lustre_cfg_string(lcfg, 1), 
1455                            lustre_cfg_string(lcfg, 2), 
1456                            option, MS_NOATIME | MS_NODIRATIME, &lvfs_ctxt);
1457         if (rc) {
1458                 CERROR("lvfs_mount_fs failed: rc = %d\n", rc);
1459                 GOTO(err_ops, rc);
1460         }
1461         LASSERT(lvfs_ctxt);
1462
1463         mnt = lvfs_ctxt->loc_mnt;
1464         filter->fo_lvfs_ctxt = lvfs_ctxt;
1465
1466         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && lustre_cfg_buf(lcfg, 3)) {
1467                 str = lustre_cfg_string(lcfg, 3);
1468                 if (*str == 'f') {
1469                         obd->obd_replayable = 1;
1470                         obd_sync_filter = 1;
1471                         CWARN("%s: recovery enabled\n", obd->obd_name);
1472                 } else {
1473                         if (*str != 'n') {
1474                                 CERROR("unrecognised flag '%c'\n",
1475                                        *str);
1476                         }
1477                         // XXX Robert? Why do we get errors here
1478                         // GOTO(err_mntput, rc = -EINVAL);
1479                 }
1480         }
1481
1482         filter->fo_vfsmnt = mnt;
1483         filter->fo_sb = mnt->mnt_sb;
1484         filter->fo_fstype = mnt->mnt_sb->s_type->name;
1485         CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
1486
1487         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1488         obd->obd_lvfs_ctxt.pwdmnt = mnt;
1489         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1490         obd->obd_lvfs_ctxt.fs = get_ds();
1491         obd->obd_lvfs_ctxt.cb_ops = filter_lvfs_ops;
1492
1493         ll_clear_rdonly(ll_sbdev(filter->fo_sb));
1494         
1495         rc = fsfilt_setup(obd, mnt->mnt_sb);
1496         if (rc)
1497                 GOTO(err_mntput, rc);
1498
1499         sema_init(&filter->fo_init_lock, 1);
1500         filter->fo_committed_group = 0;
1501         rc = filter_prep(obd);
1502         if (rc)
1503                 GOTO(err_mntput, rc);
1504
1505         filter->fo_destroys_in_progress = 0;
1506         for (i = 0; i < 32; i++)
1507                 sema_init(&filter->fo_create_locks[i], 1);
1508
1509         spin_lock_init(&filter->fo_translock);
1510         spin_lock_init(&filter->fo_objidlock);
1511         spin_lock_init(&filter->fo_lastidlock);
1512         INIT_LIST_HEAD(&filter->fo_export_list);
1513         sema_init(&filter->fo_alloc_lock, 1);
1514         spin_lock_init(&filter->fo_r_pages.oh_lock);
1515         spin_lock_init(&filter->fo_w_pages.oh_lock);
1516         spin_lock_init(&filter->fo_r_discont_pages.oh_lock);
1517         spin_lock_init(&filter->fo_w_discont_pages.oh_lock);
1518         spin_lock_init(&filter->fo_r_discont_blocks.oh_lock);
1519         spin_lock_init(&filter->fo_w_discont_blocks.oh_lock);
1520         filter->fo_readcache_max_filesize = FILTER_MAX_CACHE_SIZE;
1521
1522         INIT_LIST_HEAD(&filter->fo_llog_list);
1523         spin_lock_init(&filter->fo_llog_list_lock);
1524
1525         spin_lock_init(&filter->fo_denylist_lock);
1526         INIT_LIST_HEAD(&filter->fo_denylist);
1527
1528         sprintf(ns_name, "filter-%s", obd->obd_uuid.uuid);
1529         obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
1530
1531         if (obd->obd_namespace == NULL)
1532                 GOTO(err_post, rc = -ENOMEM);
1533         obd->obd_namespace->ns_lvbp = obd;
1534         obd->obd_namespace->ns_lvbo = &filter_lvbo;
1535         ldlm_register_intent(obd->obd_namespace, filter_intent_policy);
1536
1537         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1538                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1539
1540         rc = obd_llog_cat_initialize(obd, &obd->obd_llogs, 1, CATLIST);
1541         if (rc) {
1542                 CERROR("failed to setup llogging subsystems\n");
1543                 GOTO(err_post, rc);
1544         }
1545         RETURN(0);
1546
1547 err_post:
1548         filter_post(obd);
1549 err_mntput:
1550         unlock_kernel();
1551         lvfs_umount_fs(filter->fo_lvfs_ctxt);
1552         filter->fo_sb = 0;
1553         lock_kernel();
1554 err_ops:
1555         fsfilt_put_ops(obd->obd_fsops);
1556         return rc;
1557 }
1558
1559 static int filter_attach(struct obd_device *obd, obd_count len, void *data)
1560 {
1561         struct lprocfs_static_vars lvars;
1562         int rc;
1563
1564         lprocfs_init_vars(filter, &lvars);
1565         rc = lprocfs_obd_attach(obd, lvars.obd_vars);
1566         if (rc != 0)
1567                 return rc;
1568
1569         rc = lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST);
1570         if (rc != 0)
1571                 return rc;
1572
1573         /* Init obdfilter private stats here */
1574         lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
1575                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
1576         lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
1577                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
1578
1579         return lproc_filter_attach_seqstat(obd);
1580 }
1581
1582 static int filter_detach(struct obd_device *dev)
1583 {
1584         lprocfs_free_obd_stats(dev);
1585         return lprocfs_obd_detach(dev);
1586 }
1587
1588 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
1589 {
1590         struct lustre_cfg *lcfg = buf;
1591         unsigned long page;
1592         int rc;
1593         ENTRY;
1594
1595         /* 2.6.9 selinux wants a full option page for do_kern_mount (bug6471) */
1596         page = get_zeroed_page(GFP_KERNEL);
1597         if (!page)
1598                 RETURN(-ENOMEM);
1599
1600         memcpy((void *)page, lustre_cfg_buf(lcfg, 4),
1601                LUSTRE_CFG_BUFLEN(lcfg, 4));
1602         
1603         /* all mount options including errors=remount-ro and asyncdel are passed
1604          * using 4th lcfg param. And it is good, finally we have got rid of
1605          * hardcoded fs types in the code. */
1606         rc = filter_common_setup(obd, len, buf, (void *)page);
1607         free_page(page);
1608         
1609         if (rc)
1610                 RETURN(rc);
1611         rc = filter_post_fs_setup(obd);
1612         RETURN(rc);
1613 }
1614
1615 static int filter_cleanup(struct obd_device *obd, int flags)
1616 {
1617         struct filter_obd *filter = &obd->u.filter;
1618         ll_sbdev_type save_dev;
1619         ENTRY;
1620
1621         if (flags & OBD_OPT_FAILOVER)
1622                 CERROR("%s: shutting down for failover; client state will"
1623                        " be preserved.\n", obd->obd_name);
1624
1625         if (!list_empty(&obd->obd_exports)) {
1626                 CERROR("%s: still has clients!\n", obd->obd_name);
1627                 class_disconnect_exports(obd, flags);
1628                 if (!list_empty(&obd->obd_exports)) {
1629                         CERROR("still has exports after forced cleanup?\n");
1630                         RETURN(-EBUSY);
1631                 }
1632         }
1633
1634         target_cleanup_recovery(obd);
1635         
1636         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
1637
1638         if (filter->fo_sb == NULL)
1639                 RETURN(0);
1640
1641         save_dev = ll_sbdev(filter->fo_sb);
1642         filter_post_fs_cleanup(obd);
1643         filter_post(obd);
1644
1645         shrink_dcache_parent(filter->fo_sb->s_root);
1646         filter->fo_sb = 0;
1647
1648         spin_lock(&filter->fo_denylist_lock);
1649         while (!list_empty(&filter->fo_denylist)) {
1650                 deny_sec_t *p_deny_sec = list_entry(filter->fo_denylist.next,
1651                                                     deny_sec_t, list);
1652                 list_del(&p_deny_sec->list);
1653                 OBD_FREE(p_deny_sec, sizeof(*p_deny_sec));
1654         }
1655         spin_unlock(&filter->fo_denylist_lock);
1656
1657         unlock_kernel();
1658         lvfs_umount_fs(filter->fo_lvfs_ctxt);
1659         //destroy_buffers(filter->fo_sb->s_dev);
1660         filter->fo_sb = NULL;
1661         fsfilt_put_ops(obd->obd_fsops);
1662         lock_kernel();
1663
1664         ll_clear_rdonly(save_dev);
1665
1666         RETURN(0);
1667 }
1668
1669 static int filter_process_config(struct obd_device *obd, obd_count len, void *buf)
1670 {
1671         struct lustre_cfg *lcfg = buf;
1672         struct filter_obd *filter = &obd->u.filter;
1673         int rc = 0;
1674         ENTRY;
1675
1676         switch(lcfg->lcfg_command) {
1677         case LCFG_SET_SECURITY: {
1678                 if ((LUSTRE_CFG_BUFLEN(lcfg, 1) == 0) ||
1679                     (LUSTRE_CFG_BUFLEN(lcfg, 2) == 0))
1680                         GOTO(out, rc = -EINVAL);
1681
1682                 if (!strcmp(lustre_cfg_string(lcfg, 1), "deny_sec")){
1683                         spin_lock(&filter->fo_denylist_lock);
1684                         rc = add_deny_security(lustre_cfg_string(lcfg, 2),
1685                                                &filter->fo_denylist);
1686                         spin_unlock(&filter->fo_denylist_lock);
1687                 }else {
1688                         CERROR("Unrecognized key\n");
1689                         rc = -EINVAL;
1690                 }
1691                 break;
1692         }
1693         default: {
1694                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1695                 GOTO(out, rc = -EINVAL);
1696
1697         }
1698         }
1699 out:
1700         RETURN(rc);
1701 }
1702
1703 static int filter_connect_post(struct obd_export *exp, unsigned initial,
1704                                unsigned long connect_flags)
1705 {
1706         struct obd_device *obd = exp->exp_obd;
1707         struct filter_export_data *fed;
1708         char str[PTL_NALFMT_SIZE];
1709         struct obd_llogs *llog;
1710         struct llog_ctxt *ctxt;
1711         int rc = 0;
1712         ENTRY;
1713
1714         fed = &exp->exp_filter_data;
1715         if (fed->fed_group < FILTER_MIN_GROUPS)
1716                 RETURN(0);
1717
1718         /* initialize llogs for connections from MDS */
1719         llog = filter_grab_llog_for_group(obd, fed->fed_group, exp);
1720         LASSERT(llog != NULL);
1721
1722         ctxt = llog_get_context(llog, LLOG_UNLINK_REPL_CTXT);
1723         LASSERT(ctxt != NULL);
1724
1725         rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
1726         
1727         portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
1728                         exp->exp_connection->c_peer.peer_id.nid, str);
1729         
1730         CDEBUG(D_OTHER, "%s: init llog ctxt for export "LPX64"/%s, group %d\n",
1731                obd->obd_name, exp->exp_connection->c_peer.peer_id.nid,
1732                str, fed->fed_group);
1733
1734         RETURN(rc);
1735 }
1736
1737 /* nearly identical to mds_connect */
1738 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
1739                           struct obd_uuid *cluuid,
1740                           struct obd_connect_data *data,
1741                           unsigned long connect_flags)
1742 {
1743         struct obd_export *exp;
1744         struct filter_export_data *fed;
1745         struct filter_client_data *fcd = NULL;
1746         struct filter_obd *filter = &obd->u.filter;
1747         struct lvfs_run_ctxt saved;
1748         int rc, group;
1749         ENTRY;
1750
1751         if (conn == NULL || obd == NULL || cluuid == NULL)
1752                 RETURN(-EINVAL);
1753
1754         rc = class_connect(conn, obd, cluuid);
1755         if (rc)
1756                 RETURN(rc);
1757         exp = class_conn2export(conn);
1758         LASSERT(exp != NULL);
1759
1760         fed = &exp->exp_filter_data;
1761
1762         spin_lock_init(&fed->fed_lock);
1763
1764        /* connection from MDS */
1765         group = connect_flags;
1766         if (obd->obd_replayable) {
1767                 OBD_ALLOC(fcd, sizeof(*fcd));
1768                 if (!fcd) {
1769                         CERROR("filter: out of memory for client data\n");
1770                         GOTO(cleanup, rc = -ENOMEM);
1771                 }
1772
1773                 memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
1774                 fed->fed_fcd = fcd;
1775                 fed->fed_fcd->fcd_group = group;
1776                 rc = filter_client_add(obd, filter, fed, -1);
1777                 if (rc)
1778                         GOTO(cleanup, rc);
1779         }
1780         CWARN("%s: Received MDS connection ("LPX64"); group %d\n",
1781               obd->obd_name, exp->exp_handle.h_cookie, group);
1782  
1783         if (group == 0)
1784                 GOTO(cleanup, rc);
1785         
1786         if (fed->fed_group != 0 && fed->fed_group != group) {
1787                 char str[PTL_NALFMT_SIZE];
1788                 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
1789                                 exp->exp_connection->c_peer.peer_id.nid, str);
1790                 CERROR("!!! This export (nid "LPX64"/%s) used object group %d "
1791                        "earlier; now it's trying to use group %d!  This could "
1792                        "be a bug in the MDS.  Tell CFS.\n",
1793                        exp->exp_connection->c_peer.peer_id.nid, str,
1794                        fed->fed_group, group);
1795                 GOTO(cleanup, rc = -EPROTO);
1796         }
1797         fed->fed_group = group;
1798
1799         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1800         rc = filter_read_groups(obd, group, 1);
1801         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1802         if (rc != 0) {
1803                 CERROR("can't read group %u\n", group);
1804                 GOTO(cleanup, rc);
1805         }
1806 #if 0
1807         rc = filter_group_set_fs_flags(obd, group);
1808         if (rc != 0) {
1809                 CERROR("can't set kml flags %u\n", group);
1810                 GOTO(cleanup, rc);
1811         }
1812 #endif
1813 cleanup:
1814         if (rc) {
1815                 if (fcd)
1816                         OBD_FREE(fcd, sizeof(*fcd));
1817                 class_disconnect(exp, 0);
1818         } else {
1819                 class_export_put(exp);
1820         }
1821         return rc;
1822 }
1823
1824 static int filter_precleanup(struct obd_device *obd, int flags)
1825 {
1826         struct filter_group_llog *log;
1827         struct filter_obd *filter;
1828         int rc = 0;
1829         ENTRY;
1830
1831         filter = &obd->u.filter;
1832
1833         spin_lock(&filter->fo_llog_list_lock);
1834         while (!list_empty(&filter->fo_llog_list)) {
1835                 log = list_entry(filter->fo_llog_list.next,
1836                                  struct filter_group_llog, list);
1837                 list_del(&log->list);
1838                 spin_unlock(&filter->fo_llog_list_lock);
1839
1840                 rc = obd_llog_finish(obd, log->llogs, 0);
1841                 if (rc)
1842                         CERROR("failed to cleanup llogging subsystem for %u\n",
1843                                 log->group);
1844                 OBD_FREE(log->llogs, sizeof(*(log->llogs)));
1845                 OBD_FREE(log, sizeof(*log));
1846                 spin_lock(&filter->fo_llog_list_lock);
1847         }
1848         spin_unlock(&filter->fo_llog_list_lock);
1849
1850         rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1851         if (rc)
1852                 CERROR("failed to cleanup llogging subsystem\n");
1853
1854         RETURN(rc);
1855 }
1856
1857 /* Do extra sanity checks for grant accounting.  We do this at connect,
1858  * disconnect, and statfs RPC time, so it shouldn't be too bad.  We can
1859  * always get rid of it or turn it off when we know accounting is good. */
1860 static void filter_grant_sanity_check(struct obd_device *obd, const char *func)
1861 {
1862         struct filter_export_data *fed;
1863         struct obd_export *exp;
1864         obd_size maxsize = obd->obd_osfs.os_blocks * obd->obd_osfs.os_bsize;
1865         obd_size tot_dirty = 0, tot_pending = 0, tot_granted = 0;
1866         obd_size fo_tot_dirty, fo_tot_pending, fo_tot_granted;
1867         int level = D_CACHE;
1868
1869         if (list_empty(&obd->obd_exports))
1870                 return;
1871
1872         spin_lock(&obd->obd_osfs_lock);
1873         spin_lock(&obd->obd_dev_lock);
1874         list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1875                 fed = &exp->exp_filter_data;
1876                 if (fed->fed_grant < 0 || fed->fed_pending < 0 ||
1877                     fed->fed_dirty < 0)
1878                         level = D_ERROR;
1879                 if (maxsize > 0) { /* we may not have done a statfs yet */
1880                         LASSERTF(fed->fed_grant + fed->fed_pending <= maxsize,
1881                                  "cli %s/%p %ld+%ld > "LPU64"\n",
1882                                  exp->exp_client_uuid.uuid, exp,
1883                                  fed->fed_grant, fed->fed_pending, maxsize);
1884                         LASSERTF(fed->fed_dirty <= maxsize,
1885                                  "cli %s/%p %ld > "LPU64"\n",
1886                                  exp->exp_client_uuid.uuid, exp,
1887                                  fed->fed_dirty, maxsize);
1888                 }
1889                 CDEBUG(level, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
1890                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
1891                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1892                 tot_granted += fed->fed_grant + fed->fed_pending;
1893                 tot_pending += fed->fed_pending;
1894                 tot_dirty += fed->fed_dirty;
1895         }
1896         fo_tot_granted = obd->u.filter.fo_tot_granted;
1897         fo_tot_pending = obd->u.filter.fo_tot_pending;
1898         fo_tot_dirty = obd->u.filter.fo_tot_dirty;
1899         spin_unlock(&obd->obd_dev_lock);
1900         spin_unlock(&obd->obd_osfs_lock);
1901
1902         /* Do these assertions outside the spinlocks so we don't kill system */
1903         if (tot_granted != fo_tot_granted)
1904                 CERROR("%s: tot_granted "LPU64" != fo_tot_granted "LPU64"\n",
1905                        func, tot_granted, fo_tot_granted);
1906         if (tot_pending != fo_tot_pending)
1907                 CERROR("%s: tot_pending "LPU64" != fo_tot_pending "LPU64"\n",
1908                        func, tot_pending, fo_tot_pending);
1909         if (tot_dirty != fo_tot_dirty)
1910                 CERROR("%s: tot_dirty "LPU64" != fo_tot_dirty "LPU64"\n",
1911                        func, tot_dirty, fo_tot_dirty);
1912         if (tot_pending > tot_granted)
1913                 CERROR("%s: tot_pending "LPU64" > tot_granted "LPU64"\n",
1914                        func, tot_pending, tot_granted);
1915         if (tot_granted > maxsize)
1916                 CERROR("%s: tot_granted "LPU64" > maxsize "LPU64"\n",
1917                        func, tot_granted, maxsize);
1918         if (tot_dirty > maxsize)
1919                 CERROR("%s: tot_dirty "LPU64" > maxsize "LPU64"\n",
1920                        func, tot_dirty, maxsize);
1921 }
1922
1923 /* Remove this client from the grant accounting totals.  We also remove
1924  * the export from the obd device under the osfs and dev locks to ensure
1925  * that the filter_grant_sanity_check() calculations are always valid.
1926  * The client should do something similar when it invalidates its import. */
1927 static void filter_grant_discard(struct obd_export *exp)
1928 {
1929         struct obd_device *obd = exp->exp_obd;
1930         struct filter_obd *filter = &obd->u.filter;
1931         struct filter_export_data *fed = &exp->exp_filter_data;
1932         int level = D_CACHE;
1933
1934         spin_lock(&obd->obd_osfs_lock);
1935         spin_lock(&exp->exp_obd->obd_dev_lock);
1936         list_del_init(&exp->exp_obd_chain);
1937         spin_unlock(&exp->exp_obd->obd_dev_lock);
1938
1939         if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0)
1940                 level = D_ERROR;
1941         CDEBUG(level, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
1942                obd->obd_name, exp->exp_client_uuid.uuid, exp,
1943                fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1944
1945         LASSERTF(filter->fo_tot_granted >= fed->fed_grant,
1946                  "%s: tot_granted "LPU64" cli %s/%p fed_grant %ld\n",
1947                  obd->obd_name, filter->fo_tot_granted,
1948                  exp->exp_client_uuid.uuid, exp, fed->fed_grant);
1949         filter->fo_tot_granted -= fed->fed_grant;
1950         LASSERTF(filter->fo_tot_pending >= fed->fed_pending,
1951                  "%s: tot_pending "LPU64" cli %s/%p fed_pending %ld\n",
1952                  obd->obd_name, filter->fo_tot_pending,
1953                  exp->exp_client_uuid.uuid, exp, fed->fed_pending);
1954         LASSERTF(filter->fo_tot_dirty >= fed->fed_dirty,
1955                  "%s: tot_dirty "LPU64" cli %s/%p fed_dirty %ld\n",
1956                  obd->obd_name, filter->fo_tot_dirty,
1957                  exp->exp_client_uuid.uuid, exp, fed->fed_dirty);
1958         filter->fo_tot_dirty -= fed->fed_dirty;
1959         fed->fed_dirty = 0;
1960         fed->fed_grant = 0;
1961
1962         spin_unlock(&obd->obd_osfs_lock);
1963 }
1964
1965 static int filter_destroy_export(struct obd_export *exp)
1966 {
1967         ENTRY;
1968
1969         if (exp->exp_filter_data.fed_pending)
1970                 CERROR("%s: cli %s/%p has %lu pending on destroyed export\n",
1971                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
1972                        exp, exp->exp_filter_data.fed_pending);
1973
1974         target_destroy_export(exp);
1975
1976         if (exp->exp_obd->obd_replayable)
1977                 filter_client_free(exp, exp->exp_flags);
1978
1979         filter_grant_discard(exp);
1980         if (!(exp->exp_flags & OBD_OPT_FORCE))
1981                 filter_grant_sanity_check(exp->exp_obd, __FUNCTION__);
1982
1983         RETURN(0);
1984 }
1985
1986 static void filter_sync_llogs(struct obd_device *obd, struct obd_export *dexp)
1987 {
1988         struct filter_group_llog *fglog, *nlog;
1989         struct filter_obd *filter;
1990         int worked = 0, group;
1991         struct llog_ctxt *ctxt;
1992         ENTRY;
1993
1994         filter = &obd->u.filter;
1995
1996         /* we can't sync log holding spinlock. also, we do not want to get
1997          * into livelock. so we do following: loop over MDS's exports in
1998          * group order and skip already synced llogs -bzzz */
1999         do {
2000                 /* look for group with min. number, but > worked */
2001                 fglog = NULL;
2002                 group = 1 << 30;
2003                 spin_lock(&filter->fo_llog_list_lock);
2004                 list_for_each_entry(nlog, &filter->fo_llog_list, list) {
2005                        
2006                         if (nlog->group <= worked) {
2007                                 /* this group is already synced */
2008                                 continue;
2009                         }
2010         
2011                         if (group < nlog->group) {
2012                                 /* we have group with smaller number to sync */
2013                                 continue;
2014                         }
2015
2016                         /* store current minimal group */
2017                         fglog = nlog;
2018                         group = nlog->group;
2019                 }
2020                 spin_unlock(&filter->fo_llog_list_lock);
2021
2022                 if (fglog == NULL)
2023                         break;
2024
2025                 worked = fglog->group;
2026                 if (fglog->exp && (dexp == fglog->exp || dexp == NULL)) {
2027                         ctxt = llog_get_context(fglog->llogs,
2028                                         LLOG_UNLINK_REPL_CTXT);
2029                         LASSERT(ctxt != NULL);
2030                         llog_sync(ctxt, fglog->exp);
2031                 }
2032         } while (fglog != NULL);
2033 }
2034
2035 /* also incredibly similar to mds_disconnect */
2036 static int filter_disconnect(struct obd_export *exp, unsigned long flags)
2037 {
2038         struct obd_device *obd = exp->exp_obd;
2039         unsigned long irqflags;
2040         int rc;
2041         ENTRY;
2042
2043         LASSERT(exp);
2044         class_export_get(exp);
2045
2046         spin_lock_irqsave(&exp->exp_lock, irqflags);
2047         exp->exp_flags = flags;
2048         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
2049
2050         if (!(flags & OBD_OPT_FORCE))
2051                 filter_grant_sanity_check(obd, __FUNCTION__);
2052         filter_grant_discard(exp);
2053
2054         /* Disconnect early so that clients can't keep using export */
2055         rc = class_disconnect(exp, flags);
2056
2057         ldlm_cancel_locks_for_export(exp);
2058
2059         fsfilt_sync(obd, obd->u.filter.fo_sb);
2060
2061         /* flush any remaining cancel messages out to the target */
2062         filter_sync_llogs(obd, exp);
2063         class_export_put(exp);
2064         RETURN(rc);
2065 }
2066
2067 struct dentry *__filter_oa2dentry(struct obd_device *obd,
2068                                   struct obdo *oa, const char *what)
2069 {
2070         struct dentry *dchild = NULL;
2071         obd_gr group = 0;
2072         ENTRY;
2073
2074         if (oa->o_valid & OBD_MD_FLGROUP)
2075                 group = oa->o_gr;
2076
2077         dchild = filter_id2dentry(obd, NULL, group, oa->o_id);
2078
2079         if (IS_ERR(dchild)) {
2080                 CERROR("%s error looking up object: "LPU64"\n",
2081                        what, oa->o_id);
2082                 RETURN(dchild);
2083         }
2084
2085         if (dchild->d_inode == NULL) {
2086                 CDEBUG(D_INFO, "%s: %s on non-existent object: "
2087                        LPU64"\n", obd->obd_name, what, oa->o_id);
2088                 f_dput(dchild);
2089                 RETURN(ERR_PTR(-ENOENT));
2090         }
2091
2092         RETURN(dchild);
2093 }
2094
2095 static int filter_getattr(struct obd_export *exp, struct obdo *oa,
2096                           struct lov_stripe_md *md)
2097 {
2098         struct dentry *dentry = NULL;
2099         struct obd_device *obd;
2100         int rc = 0;
2101         ENTRY;
2102
2103         obd = class_exp2obd(exp);
2104         if (obd == NULL) {
2105                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2106                        exp->exp_handle.h_cookie);
2107                 RETURN(-EINVAL);
2108         }
2109
2110         dentry = filter_oa2dentry(obd, oa);
2111         if (IS_ERR(dentry))
2112                 RETURN(PTR_ERR(dentry));
2113
2114         /* Limit the valid bits in the return data to what we actually use */
2115         oa->o_valid = OBD_MD_FLID;
2116         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2117
2118         f_dput(dentry);
2119         RETURN(rc);
2120 }
2121
2122 int filter_setattr_internal(struct obd_export *exp, struct dentry *dentry,
2123                             struct obdo *oa, struct obd_trans_info *oti)
2124 {
2125         struct filter_obd *filter;
2126         struct iattr iattr;
2127         void *handle;
2128         int rc, err;
2129         ENTRY;
2130
2131         LASSERT(dentry != NULL);
2132         LASSERT(!IS_ERR(dentry));
2133         LASSERT(dentry->d_inode != NULL);
2134         
2135         filter = &exp->exp_obd->u.filter;
2136         iattr_from_obdo(&iattr, oa, oa->o_valid);
2137
2138         if (iattr.ia_valid & ATTR_SIZE)
2139                 down(&dentry->d_inode->i_sem);
2140         handle = fsfilt_start(exp->exp_obd, dentry->d_inode,
2141                               FSFILT_OP_SETATTR, oti);
2142         if (IS_ERR(handle))
2143                 GOTO(out_unlock, rc = PTR_ERR(handle));
2144
2145         /* XXX this could be a rwsem instead, if filter_preprw played along */
2146         if (iattr.ia_valid & ATTR_ATTR_FLAG)
2147                 rc = fsfilt_iocontrol(exp->exp_obd, dentry->d_inode,
2148                                       NULL, EXT3_IOC_SETFLAGS,
2149                                       (long)&iattr.ia_attr_flags);
2150         else
2151                 rc = fsfilt_setattr(exp->exp_obd, dentry, handle,
2152                                     &iattr, 1);
2153         
2154         rc = filter_finish_transno(exp, oti, rc);
2155         
2156         err = fsfilt_commit(exp->exp_obd, filter->fo_sb,
2157                             dentry->d_inode, handle,
2158                             exp->exp_sync);
2159         if (err) {
2160                 CERROR("error on commit, err = %d\n", err);
2161                 if (!rc)
2162                         rc = err;
2163         }
2164         EXIT;
2165 out_unlock:
2166         if (iattr.ia_valid & ATTR_SIZE)
2167                 up(&dentry->d_inode->i_sem);
2168         return rc;
2169 }
2170
2171 /* this is called from filter_truncate() until we have filter_punch() */
2172 int filter_setattr(struct obd_export *exp, struct obdo *oa,
2173                    struct lov_stripe_md *md, struct obd_trans_info *oti)
2174 {
2175         struct ldlm_res_id res_id = { .name = { oa->o_id, 0, oa->o_gr, 0 } };
2176         struct ldlm_valblock_ops *ns_lvbo;
2177         struct lvfs_run_ctxt saved;
2178         struct filter_obd *filter;
2179         struct ldlm_resource *res;
2180         struct dentry *dentry;
2181         obd_uid uid;
2182         obd_gid gid;
2183         int rc;
2184         ENTRY;
2185
2186         LASSERT(oti != NULL);
2187
2188         filter = &exp->exp_obd->u.filter;
2189         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2190
2191         uid = oa->o_valid & OBD_MD_FLUID ? oa->o_uid : 0;
2192         gid = oa->o_valid & OBD_MD_FLGID ? oa->o_gid : 0;
2193         
2194         /* make sure that object is allocated. */
2195         dentry = filter_crow_object(exp->exp_obd, oa->o_gr,
2196                                     oa->o_id, uid, gid);
2197         if (IS_ERR(dentry))
2198                 GOTO(out_pop, rc = PTR_ERR(dentry));
2199
2200         lock_kernel();
2201
2202         /* setting objects attributes (including owner/group) */
2203         rc = filter_setattr_internal(exp, dentry, oa, oti);
2204         if (rc)
2205                 GOTO(out_unlock, rc);
2206
2207         res = ldlm_resource_get(exp->exp_obd->obd_namespace, NULL,
2208                                 res_id, LDLM_EXTENT, 0);
2209         
2210         if (res != NULL) {
2211                 ns_lvbo = res->lr_namespace->ns_lvbo;
2212                 if (ns_lvbo && ns_lvbo->lvbo_update)
2213                         rc = ns_lvbo->lvbo_update(res, NULL, 0, 0);
2214                 ldlm_resource_putref(res);
2215         }
2216         
2217         oa->o_valid = OBD_MD_FLID;
2218         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2219
2220         EXIT;
2221 out_unlock:
2222         unlock_kernel();
2223         f_dput(dentry);
2224 out_pop:
2225         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2226         return rc;
2227 }
2228
2229 /* XXX identical to osc_unpackmd */
2230 static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2231                            struct lov_mds_md *lmm, int lmm_bytes)
2232 {
2233         int lsm_size;
2234         ENTRY;
2235
2236         if (lmm != NULL) {
2237                 if (lmm_bytes < sizeof (*lmm)) {
2238                         CERROR("lov_mds_md too small: %d, need %d\n",
2239                                lmm_bytes, (int)sizeof(*lmm));
2240                         RETURN(-EINVAL);
2241                 }
2242                 /* XXX LOV_MAGIC etc check? */
2243
2244                 if (lmm->lmm_object_id == cpu_to_le64(0)) {
2245                         CERROR("lov_mds_md: zero lmm_object_id\n");
2246                         RETURN(-EINVAL);
2247                 }
2248         }
2249
2250         lsm_size = lov_stripe_md_size(1);
2251         if (lsmp == NULL)
2252                 RETURN(lsm_size);
2253
2254         if (*lsmp != NULL && lmm == NULL) {
2255                 OBD_FREE(*lsmp, lsm_size);
2256                 *lsmp = NULL;
2257                 RETURN(0);
2258         }
2259
2260         if (*lsmp == NULL) {
2261                 OBD_ALLOC(*lsmp, lsm_size);
2262                 if (*lsmp == NULL)
2263                         RETURN(-ENOMEM);
2264
2265                 loi_init((*lsmp)->lsm_oinfo);
2266         }
2267
2268         if (lmm != NULL) {
2269                 /* XXX zero *lsmp? */
2270                 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
2271                 LASSERT((*lsmp)->lsm_object_id);
2272         }
2273
2274         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
2275
2276         RETURN(lsm_size);
2277 }
2278
2279 static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2280                          unsigned long max_age)
2281 {
2282         struct filter_obd *filter = &obd->u.filter;
2283         int blockbits = filter->fo_sb->s_blocksize_bits;
2284         int rc;
2285         ENTRY;
2286
2287         /* at least try to account for cached pages.  its still racey and
2288          * might be under-reporting if clients haven't announced their
2289          * caches with brw recently */
2290         spin_lock(&obd->obd_osfs_lock);
2291         rc = fsfilt_statfs(obd, filter->fo_sb, max_age);
2292         memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
2293         spin_unlock(&obd->obd_osfs_lock);
2294
2295         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
2296                " pending "LPU64" free "LPU64" avail "LPU64"\n",
2297                filter->fo_tot_dirty, filter->fo_tot_granted,
2298                filter->fo_tot_pending,
2299                osfs->os_bfree << blockbits, osfs->os_bavail << blockbits);
2300
2301         filter_grant_sanity_check(obd, __FUNCTION__);
2302
2303         osfs->os_bavail -= min(osfs->os_bavail,
2304                                (filter->fo_tot_dirty + filter->fo_tot_pending +
2305                                 osfs->os_bsize -1) >> blockbits);
2306
2307         RETURN(rc);
2308 }
2309
2310 int filter_create_object(struct obd_device *obd, obd_gr group,
2311                          obd_id id, obd_uid uid, obd_gid gid)
2312 {
2313         struct dentry *dparent = NULL;
2314         struct dentry *dchild = NULL;
2315         struct lvfs_ucred uc = {0,};
2316         struct lvfs_run_ctxt saved;
2317         struct filter_obd *filter;
2318         int cleanup_phase = 0;
2319         int err = 0, rc = 0;
2320         void *handle = NULL;
2321         void *lock = NULL;
2322         ENTRY;
2323
2324         filter = &obd->u.filter;
2325
2326         down(&filter->fo_create_locks[group]);
2327
2328         if (test_bit(group, &filter->fo_destroys_in_progress)) {
2329                 CWARN("%s: create aborted by destroy\n",
2330                       obd->obd_name);
2331                 GOTO(out, rc = -EALREADY);
2332         }
2333
2334         CDEBUG(D_INFO, "create objid "LPU64"\n", id);
2335
2336         dparent = filter_parent_lock(obd, group, id, &lock);
2337         if (IS_ERR(dparent))
2338                 GOTO(cleanup, rc = PTR_ERR(dparent));
2339         cleanup_phase = 1;
2340
2341         dchild = filter_id2dentry(obd, dparent, group, id);
2342         if (IS_ERR(dchild))
2343                 GOTO(cleanup, rc = PTR_ERR(dchild));
2344         cleanup_phase = 2;
2345
2346         if (dchild->d_inode != NULL)
2347                 GOTO(cleanup, rc = 0);
2348
2349         handle = fsfilt_start_log(obd, dparent->d_inode,
2350                                   FSFILT_OP_CREATE, NULL, 1);
2351         if (IS_ERR(handle))
2352                 GOTO(cleanup, rc = PTR_ERR(handle));
2353         cleanup_phase = 3;
2354
2355         uc.luc_uid = uid;
2356         uc.luc_gid = gid;
2357         uc.luc_fsuid = uid;
2358         uc.luc_fsgid = gid;
2359
2360         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2361         rc = ll_vfs_create(dparent->d_inode, dchild, S_IFREG, NULL);
2362         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2363
2364         if (rc) {
2365                 CERROR("create failed rc = %d\n", rc);
2366                 GOTO(cleanup, rc);
2367         }
2368
2369         fsfilt_set_fs_flags(obd, dparent->d_inode, SM_DO_REC);
2370
2371         /* save last created object id */
2372         filter_save_last_id(filter, group, id);
2373
2374         rc = filter_update_last_objid(obd, group, 0);
2375         if (rc) {
2376                 CERROR("unable to write lastobjid, but "
2377                        "orphans were deleted, err = %d\n",
2378                        rc);
2379                 rc = 0;
2380         }
2381 cleanup:
2382         switch(cleanup_phase) {
2383         case 3:
2384                 err = fsfilt_commit(obd, filter->fo_sb,
2385                                     dparent->d_inode, handle, 0);
2386                 if (err) {
2387                         CERROR("error on commit, err = %d\n", err);
2388                         if (!rc)
2389                                 rc = err;
2390                 }
2391         case 2:
2392                 f_dput(dchild);
2393         case 1:
2394                 filter_parent_unlock(dparent, lock);
2395         case 0:
2396                 break;
2397         }
2398
2399         if (rc)
2400                 GOTO(out, rc);
2401
2402 out:
2403         up(&filter->fo_create_locks[group]);
2404         RETURN(rc);
2405 }
2406
2407 struct dentry *
2408 filter_crow_object(struct obd_device *obd, __u64 ogr,
2409                    __u64 oid, obd_uid uid, obd_gid gid)
2410 {
2411         struct dentry *dentry;
2412         int rc = 0;
2413         ENTRY;
2414
2415         /* check if object is already allocated */
2416         dentry = filter_id2dentry(obd, NULL, ogr, oid);
2417         if (IS_ERR(dentry))
2418                 RETURN(dentry);
2419
2420         if (dentry->d_inode)
2421                 RETURN(dentry);
2422
2423         f_dput(dentry);
2424         
2425         CDEBUG(D_INODE, "OSS object "LPU64"/"LPU64
2426                " does not exists - allocate it now\n",
2427                oid, ogr);
2428
2429         rc = filter_create_object(obd, ogr, oid, uid, gid);
2430         if (rc) {
2431                 CERROR("cannot create OSS object "LPU64"/"LPU64
2432                        ", err = %d\n", oid, ogr, rc);
2433                 RETURN(ERR_PTR(rc));
2434         }
2435
2436         /* lookup for just created object and return it to caller */
2437         dentry = filter_id2dentry(obd, NULL, ogr, oid);
2438         if (IS_ERR(dentry))
2439                 RETURN(dentry);
2440                 
2441         if (dentry->d_inode == NULL) {
2442                 f_dput(dentry);
2443                 dentry = ERR_PTR(-ENOENT);
2444                 CERROR("cannot find just created OSS object "
2445                        LPU64"/"LPU64" err = %d\n", oid,
2446                        ogr, (int)PTR_ERR(dentry));
2447                 RETURN(dentry);
2448         } else {
2449                 /* XXX: here should be storing fid from client into OSS object
2450                  * EA to use it for quota later.  --umka */
2451         }
2452
2453         RETURN(dentry);
2454 }
2455
2456 static int
2457 filter_clear_orphans(struct obd_export *exp, struct obdo *oa)
2458 {
2459         struct obd_device *obd = NULL;
2460         struct filter_obd *filter;
2461         struct obdo *doa = NULL;
2462         int rc = 0, orphans;
2463         __u64 last, id;
2464         ENTRY;
2465         
2466         LASSERT(oa);
2467         LASSERT(oa->o_gr != 0);
2468         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2469
2470         obd = exp->exp_obd;
2471         filter = &obd->u.filter;
2472
2473         last = filter_last_id(filter, oa->o_gr);
2474         orphans = last - oa->o_id;
2475         
2476         if (orphans <= 0)
2477                 RETURN(0);
2478                 
2479         doa = obdo_alloc();
2480         if (doa == NULL)
2481                 RETURN(-ENOMEM);
2482
2483         doa->o_gr = oa->o_gr;
2484         doa->o_mode = S_IFREG;
2485         doa->o_valid = oa->o_valid & (OBD_MD_FLGROUP | OBD_MD_FLID);
2486
2487         set_bit(doa->o_gr, &filter->fo_destroys_in_progress);
2488         down(&filter->fo_create_locks[doa->o_gr]);
2489         if (!test_bit(doa->o_gr, &filter->fo_destroys_in_progress)) {
2490                 CERROR("%s:["LPU64"] destroy_in_progress already cleared\n",
2491                        exp->exp_obd->obd_name, doa->o_gr);
2492                 up(&filter->fo_create_locks[doa->o_gr]);
2493                 GOTO(out_free_doa, 0);
2494         }
2495
2496         CDEBUG(D_ERROR, "%s:["LPU64"] deleting orphan objects from "LPU64" to "
2497               LPU64"\n", exp->exp_obd->obd_name, doa->o_gr,
2498               oa->o_id + 1, last);
2499         
2500         for (id = oa->o_id + 1; id <= last; id++) {
2501                 doa->o_id = id;
2502                 filter_destroy(exp, doa, NULL, NULL);
2503         }
2504
2505         CDEBUG(D_ERROR, "%s:["LPU64"] after destroy: set last_objids = "
2506                LPU64"\n", exp->exp_obd->obd_name, doa->o_gr, last + 1);
2507
2508         /* return next free id to be used as a new start of sequence -bzzz */
2509         oa->o_id = last + 1;
2510
2511         filter_set_last_id(filter, oa->o_gr, oa->o_id);
2512         clear_bit(doa->o_gr, &filter->fo_destroys_in_progress);
2513         up(&filter->fo_create_locks[oa->o_gr]);
2514
2515         EXIT;
2516 out_free_doa:
2517         obdo_free(doa);
2518         return rc;
2519 }
2520
2521 /*
2522  * by now this function is only needed as entry point for deleting orphanes on
2523  * OSS as objects are created on first write attempt. --umka
2524  */
2525 static int
2526 filter_create(struct obd_export *exp, struct obdo *oa, void *acl,
2527               int acl_size, struct lov_stripe_md **ea,
2528               struct obd_trans_info *oti)
2529 {
2530         struct filter_export_data *fed;
2531         struct obd_device *obd = NULL;
2532         int group = oa->o_gr, rc = 0;
2533         struct lvfs_run_ctxt saved;
2534         struct filter_obd *filter;
2535         char str[PTL_NALFMT_SIZE];
2536         ENTRY;
2537
2538         LASSERT(acl == NULL && acl_size == 0);
2539
2540         if (!(oa->o_valid & OBD_MD_FLGROUP) || group == 0) {
2541                 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
2542                                 exp->exp_connection->c_peer.peer_id.nid, str);
2543                 CERROR("!!! nid "LPX64"/%s sent invalid object group %d\n",
2544                        exp->exp_connection->c_peer.peer_id.nid, str, group);
2545                 RETURN(-EINVAL);
2546         }
2547
2548         obd = exp->exp_obd;
2549         fed = &exp->exp_filter_data;
2550         filter = &obd->u.filter;
2551
2552         if (fed->fed_group != group) {
2553                 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
2554                                 exp->exp_connection->c_peer.peer_id.nid, str);
2555                 CERROR("!!! this export (nid "LPX64"/%s) used object group %d "
2556                        "earlier; now it's trying to use group %d!  This could "
2557                        "be a bug in the MDS.  Tell CFS.\n",
2558                        exp->exp_connection->c_peer.peer_id.nid, str,
2559                        fed->fed_group, group);
2560                 RETURN(-ENOTUNIQ);
2561         }
2562
2563         CDEBUG(D_INFO, "filter_create(od->o_gr=%d,od->o_id="LPU64")\n",
2564                group, oa->o_id);
2565
2566         obd = exp->exp_obd;
2567         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2568
2569         LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
2570                 (oa->o_flags == OBD_FL_DELORPHAN));
2571                 
2572         rc = filter_clear_orphans(exp, oa);
2573         if (rc) {
2574                 CERROR("cannot clear orphanes starting from "
2575                        LPU64", err = %d\n", oa->o_id, rc);
2576         } else {
2577                 rc = filter_update_last_objid(obd, group, 0);
2578                 if (rc) {
2579                         CERROR("unable to write lastobjid, but "
2580                                "orphans were deleted, err = %d\n",
2581                                rc);
2582                 }
2583         }
2584         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2585         
2586         RETURN(0);
2587 }
2588
2589 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
2590                           struct lov_stripe_md *ea, struct obd_trans_info *oti)
2591 {
2592         int rc, rc2, cleanup_phase = 0, have_prepared = 0;
2593         struct dentry *dchild = NULL, *dparent = NULL;
2594         struct llog_cookie *fcc = NULL;
2595         struct lvfs_run_ctxt saved;
2596         struct filter_obd *filter;
2597         struct obd_device *obd;
2598         void *handle = NULL;
2599         void *lock = NULL;
2600         struct iattr iattr;
2601         ENTRY;
2602
2603         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2604
2605         obd = exp->exp_obd;
2606         filter = &obd->u.filter;
2607
2608         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2609
2610  acquire_locks:
2611         dparent = filter_parent_lock(obd, oa->o_gr, oa->o_id, &lock);
2612         if (IS_ERR(dparent))
2613                 GOTO(cleanup, rc = PTR_ERR(dparent));
2614         cleanup_phase = 1;
2615
2616         dchild = filter_id2dentry(obd, dparent, oa->o_gr, oa->o_id);
2617         if (IS_ERR(dchild))
2618                 GOTO(cleanup, rc = PTR_ERR(dchild));
2619         cleanup_phase = 2;
2620
2621         if (dchild->d_inode == NULL) {
2622                 CDEBUG(D_INODE, "destroying non-existent object "LPU64"\n",
2623                        oa->o_id);
2624                 if (have_prepared)
2625                         GOTO(cleanup, rc = -ENOENT);
2626         }
2627
2628         if (!have_prepared) {
2629                 /* If we're really going to destroy the object, get ready
2630                  * by getting the clients to discard their cached data.
2631                  *
2632                  * We have to drop the parent lock, because
2633                  * filter_prepare_destroy will acquire a PW on the object, and
2634                  * we don't want to deadlock with an incoming write to the
2635                  * object, which has the extent PW and then wants to get the
2636                  * parent dentry to do the lookup.
2637                  *
2638                  * We dput the child because it's not worth the extra
2639                  * complication of condition the above code to skip it on the
2640                  * second time through. */
2641                 f_dput(dchild);
2642                 filter_parent_unlock(dparent, lock);
2643
2644                 filter_prepare_destroy(obd, oa->o_id, oa->o_gr);
2645                 have_prepared = 1;
2646                 goto acquire_locks;
2647         }
2648
2649         /* Our MDC connection is established by the MDS to us */
2650         if (oa->o_valid & OBD_MD_FLCOOKIE) {
2651                 OBD_ALLOC(fcc, sizeof(*fcc));
2652                 if (fcc != NULL)
2653                         memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
2654         }
2655
2656         /* we're gonna truncate it first in order to avoid possible
2657          * deadlock:
2658          *      P1                      P2
2659          * open trasaction      open transaction
2660          * down(i_zombie)       down(i_zombie)
2661          *                      restart transaction
2662          * (see BUG 4180) -bzzz
2663          */
2664         down(&dchild->d_inode->i_sem);
2665         handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_SETATTR,NULL,1);
2666         if (IS_ERR(handle)) {
2667                 up(&dchild->d_inode->i_sem);
2668                 GOTO(cleanup, rc = PTR_ERR(handle));
2669         }
2670
2671         iattr.ia_valid = ATTR_SIZE;
2672         iattr.ia_size = 0;
2673         rc = fsfilt_setattr(obd, dchild, handle, &iattr, 1);
2674
2675         rc2 = fsfilt_commit(obd, filter->fo_sb, dparent->d_inode, handle, 0);
2676         up(&dchild->d_inode->i_sem);
2677         if (rc)
2678                 GOTO(cleanup, rc);
2679         if (rc2)
2680                 GOTO(cleanup, rc = rc2);
2681
2682         handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_UNLINK,oti,1);
2683         if (IS_ERR(handle))
2684                 GOTO(cleanup, rc = PTR_ERR(handle));
2685
2686         cleanup_phase = 3;
2687
2688         rc = filter_destroy_internal(obd, oa->o_id, dparent, dchild);
2689
2690 cleanup:
2691         switch(cleanup_phase) {
2692         case 3:
2693                 if (fcc != NULL) {
2694                         fsfilt_add_journal_cb(obd, filter->fo_sb, 0,
2695                                               oti ? oti->oti_handle : handle,
2696                                               filter_cancel_cookies_cb, fcc);
2697                 }
2698                 rc = filter_finish_transno(exp, oti, rc);
2699                 rc2 = fsfilt_commit(obd, filter->fo_sb, dparent->d_inode, 
2700                                     handle, exp->exp_sync);
2701                 if (rc2) {
2702                         CERROR("error on commit, err = %d\n", rc2);
2703                         if (!rc)
2704                                 rc = rc2;
2705                 }
2706         case 2:
2707                 f_dput(dchild);
2708         case 1:
2709                 filter_parent_unlock(dparent, lock);
2710         case 0:
2711                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2712                 break;
2713         default:
2714                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2715                 LBUG();
2716         }
2717
2718         RETURN(rc);
2719 }
2720
2721 /* NB start and end are used for punch, but not truncate */
2722 static int filter_truncate(struct obd_export *exp, struct obdo *oa,
2723                            struct lov_stripe_md *lsm,
2724                            obd_off start, obd_off end,
2725                            struct obd_trans_info *oti)
2726 {
2727         int error;
2728         ENTRY;
2729
2730         if (end != OBD_OBJECT_EOF)
2731                 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
2732                        end);
2733
2734         CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = "LPU64", "
2735                "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
2736         oa->o_size = start;
2737         error = filter_setattr(exp, oa, NULL, oti);
2738         RETURN(error);
2739 }
2740
2741 static int filter_sync(struct obd_export *exp, struct obdo *oa,
2742                        struct lov_stripe_md *lsm, obd_off start, obd_off end)
2743 {
2744         struct obd_device *obd = exp->exp_obd;
2745         struct lvfs_run_ctxt saved;
2746         struct filter_obd *filter;
2747         struct dentry *dentry;
2748         int rc, rc2;
2749         ENTRY;
2750
2751         filter = &obd->u.filter;
2752
2753         /* an objid of zero is taken to mean "sync whole filesystem" */
2754         if (!oa || !(oa->o_valid & OBD_MD_FLID)) {
2755                 rc = fsfilt_sync(obd, filter->fo_sb);
2756                 /* flush any remaining cancel messages out to the target */
2757                 filter_sync_llogs(obd, NULL);
2758                 RETURN(rc);
2759         }
2760
2761         dentry = filter_oa2dentry(obd, oa);
2762         if (IS_ERR(dentry))
2763                 RETURN(PTR_ERR(dentry));
2764
2765         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2766
2767         down(&dentry->d_inode->i_sem);
2768         rc = filemap_fdatawrite(dentry->d_inode->i_mapping);
2769         if (rc == 0) {
2770                 /* just any file to grab fsync method - "file" arg unused */
2771                 struct file *file = filter->fo_rcvd_filp;
2772
2773                 if (file->f_op && file->f_op->fsync)
2774                         rc = file->f_op->fsync(NULL, dentry, 1);
2775
2776                 rc2 = filemap_fdatawait(dentry->d_inode->i_mapping);
2777                 if (!rc)
2778                         rc = rc2;
2779         }
2780         up(&dentry->d_inode->i_sem);
2781
2782         oa->o_valid = OBD_MD_FLID;
2783         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2784
2785         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2786
2787         f_dput(dentry);
2788         RETURN(rc);
2789 }
2790
2791 static int filter_get_info(struct obd_export *exp, __u32 keylen,
2792                            void *key, __u32 *vallen, void *val)
2793 {
2794         struct filter_export_data *fed = &exp->exp_filter_data;
2795         struct obd_device *obd;
2796         ENTRY;
2797
2798         obd = class_exp2obd(exp);
2799         if (obd == NULL) {
2800                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2801                        exp->exp_handle.h_cookie);
2802                 RETURN(-EINVAL);
2803         }
2804
2805         if (keylen == strlen("blocksize") &&
2806             memcmp(key, "blocksize", keylen) == 0) {
2807                 __u32 *blocksize = val;
2808                 *vallen = sizeof(*blocksize);
2809                 *blocksize = obd->u.filter.fo_sb->s_blocksize;
2810                 RETURN(0);
2811         }
2812
2813         if (keylen == strlen("blocksize_bits") &&
2814             memcmp(key, "blocksize_bits", keylen) == 0) {
2815                 __u32 *blocksize_bits = val;
2816                 *vallen = sizeof(*blocksize_bits);
2817                 *blocksize_bits = obd->u.filter.fo_sb->s_blocksize_bits;
2818                 RETURN(0);
2819         }
2820
2821         if (keylen >= strlen("last_id") && memcmp(key, "last_id", 7) == 0) {
2822                 obd_id *last_id = val;
2823                 *last_id = filter_last_id(&obd->u.filter, fed->fed_group);
2824                 RETURN(0);
2825         }
2826         if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
2827                 /*Get log_context handle*/
2828                 unsigned long *llh_handle = val;
2829                 *vallen = sizeof(unsigned long);
2830                 *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
2831                 RETURN(0);
2832         }
2833         if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
2834                 /*Get log_context handle*/
2835                 unsigned long *sb = val;
2836                 *vallen = sizeof(unsigned long);
2837                 *sb = (unsigned long)obd->u.filter.fo_sb;
2838                 RETURN(0);
2839         }
2840
2841         CDEBUG(D_IOCTL, "invalid key\n");
2842         RETURN(-EINVAL);
2843 }
2844
2845 struct obd_llogs *filter_grab_llog_for_group(struct obd_device *obd, int group,
2846                                              struct obd_export *export)
2847 {
2848         struct filter_group_llog *fglog, *nlog;
2849         char name[32] = "CATLIST";
2850         struct filter_obd *filter;
2851         struct llog_ctxt *ctxt;
2852         struct list_head *cur;
2853         int rc;
2854
2855         filter = &obd->u.filter;
2856
2857         spin_lock(&filter->fo_llog_list_lock);
2858         list_for_each(cur, &filter->fo_llog_list) {
2859                 fglog = list_entry(cur, struct filter_group_llog, list);
2860                 if (fglog->group == group) {
2861                         if (!(fglog->exp == NULL || fglog->exp == export || export == NULL))
2862                                 CWARN("%s: export for group %d changes: 0x%p -> 0x%p\n",
2863                                       obd->obd_name, group, fglog->exp, export);
2864                         spin_unlock(&filter->fo_llog_list_lock);
2865                         goto init;
2866                 }
2867         }
2868         spin_unlock(&filter->fo_llog_list_lock);
2869
2870         if (export == NULL)
2871                 RETURN(NULL);
2872
2873         OBD_ALLOC(fglog, sizeof(*fglog));
2874         if (fglog == NULL)
2875                 RETURN(NULL);
2876         fglog->group = group;
2877
2878         OBD_ALLOC(fglog->llogs, sizeof(struct obd_llogs));
2879         if (fglog->llogs == NULL) {
2880                 OBD_FREE(fglog, sizeof(*fglog));
2881                 RETURN(NULL);
2882         }
2883
2884         spin_lock(&filter->fo_llog_list_lock);
2885         list_for_each(cur, &filter->fo_llog_list) {
2886                 nlog = list_entry(cur, struct filter_group_llog, list);
2887                 LASSERT(nlog->group != group);
2888         }
2889         list_add(&fglog->list, &filter->fo_llog_list);
2890         spin_unlock(&filter->fo_llog_list_lock);
2891
2892         rc = obd_llog_cat_initialize(obd, fglog->llogs, 1, name);
2893         if (rc) {
2894                 OBD_FREE(fglog->llogs, sizeof(*(fglog->llogs)));
2895                 OBD_FREE(fglog, sizeof(*fglog));
2896                 RETURN(NULL);
2897         }
2898
2899 init:
2900         if (export) {
2901                 fglog->exp = export;
2902                 ctxt = llog_get_context(fglog->llogs, LLOG_UNLINK_REPL_CTXT);
2903                 LASSERT(ctxt != NULL);
2904
2905                 llog_receptor_accept(ctxt, export->exp_imp_reverse);
2906         }
2907         CDEBUG(D_OTHER, "%s: new llog 0x%p for group %u\n",
2908                obd->obd_name, fglog->llogs, group);
2909
2910         RETURN(fglog->llogs);
2911 }
2912
2913 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
2914                      int len, void *karg, void *uarg)
2915 {
2916         struct obd_device *obd = exp->exp_obd;
2917         struct obd_ioctl_data *data = karg;
2918         int rc = 0;
2919
2920         switch (cmd) {
2921         case OBD_IOC_ABORT_RECOVERY:
2922                 target_stop_recovery_thread(obd);
2923                 RETURN(0);
2924
2925         case OBD_IOC_SET_READONLY: {
2926                 void *handle;
2927                 struct super_block *sb = obd->u.filter.fo_sb;
2928                 struct inode *inode = sb->s_root->d_inode;
2929                 BDEVNAME_DECLARE_STORAGE(tmp);
2930                 CERROR("setting device %s read-only\n",
2931                        ll_bdevname(sb, tmp));
2932
2933                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
2934                 LASSERT(handle);
2935                 (void)fsfilt_commit(obd, sb, inode, handle, 1);
2936
2937                 ll_set_rdonly(ll_sbdev(obd->u.filter.fo_sb));
2938                 RETURN(0);
2939         }
2940
2941         case OBD_IOC_CATLOGLIST: {
2942                 rc = llog_catalog_list(obd, 1, data);
2943                 RETURN(rc);
2944         }
2945         case OBD_IOC_LLOG_CANCEL:
2946         case OBD_IOC_LLOG_REMOVE:
2947         case OBD_IOC_LLOG_INFO:
2948         case OBD_IOC_LLOG_PRINT: {
2949                 /* FIXME to be finished */
2950                 RETURN(-EOPNOTSUPP);
2951 /*
2952                 struct llog_ctxt *ctxt = NULL;
2953
2954                 push_ctxt(&saved, &ctxt->loc_ctxt, NULL);
2955                 rc = llog_ioctl(ctxt, cmd, data);
2956                 pop_ctxt(&saved, &ctxt->loc_ctxt, NULL);
2957
2958                 RETURN(rc);
2959 */
2960         }
2961
2962
2963         default:
2964                 RETURN(-EINVAL);
2965         }
2966         RETURN(0);
2967 }
2968
2969 static struct llog_operations filter_unlink_repl_logops;
2970 static struct llog_operations filter_size_orig_logops = {
2971         lop_setup: llog_obd_origin_setup,
2972         lop_cleanup: llog_catalog_cleanup,
2973         lop_add: llog_catalog_add,
2974 };
2975
2976 static int filter_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
2977                             struct obd_device *tgt, int count,
2978                             struct llog_catid *catid)
2979 {
2980         struct llog_ctxt *ctxt;
2981         int rc;
2982         ENTRY;
2983
2984         filter_unlink_repl_logops = llog_client_ops;
2985         filter_unlink_repl_logops.lop_cancel = llog_obd_repl_cancel;
2986         filter_unlink_repl_logops.lop_connect = llog_repl_connect;
2987         filter_unlink_repl_logops.lop_sync = llog_obd_repl_sync;
2988
2989         rc = obd_llog_setup(obd, llogs, LLOG_UNLINK_REPL_CTXT, tgt, 0, NULL,
2990                         &filter_unlink_repl_logops);
2991         if (rc)
2992                 RETURN(rc);
2993         /* FIXME - assign unlink_cb for filter's recovery */
2994         ctxt = llog_get_context(llogs, LLOG_UNLINK_REPL_CTXT);
2995         ctxt->llog_proc_cb = filter_recov_log_unlink_cb;
2996
2997         filter_size_orig_logops = llog_lvfs_ops;
2998 #if 0
2999         filter_size_orig_logops.lop_setup = llog_obd_origin_setup;
3000         filter_size_orig_logops.lop_cleanup = llog_catalog_cleanup;
3001         filter_size_orig_logops.lop_add = llog_catalog_add;
3002 #endif
3003         rc = obd_llog_setup(obd, llogs, LLOG_SIZE_ORIG_CTXT, tgt, 1, 
3004                             &catid->lci_logid, &filter_size_orig_logops);
3005         RETURN(rc);
3006 }
3007
3008 static int filter_llog_finish(struct obd_device *obd,
3009                               struct obd_llogs *llogs, int count)
3010 {
3011         int rc;
3012         ENTRY;
3013
3014         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_UNLINK_REPL_CTXT));
3015         if (rc)
3016                 RETURN(rc);
3017
3018         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_SIZE_ORIG_CTXT));
3019         RETURN(rc);
3020 }
3021
3022 static int filter_llog_connect(struct obd_export *exp,
3023                                struct llogd_conn_body *body) 
3024 {
3025         struct obd_device *obd = exp->exp_obd;
3026         struct llog_ctxt *ctxt;
3027         struct obd_llogs *llog;
3028         int rc;
3029         ENTRY;
3030
3031         CDEBUG(D_OTHER, "handle connect for %s: %u/%u/%u\n", obd->obd_name,
3032                (unsigned) body->lgdc_logid.lgl_ogr,
3033                (unsigned) body->lgdc_logid.lgl_oid,
3034                (unsigned) body->lgdc_logid.lgl_ogen);
3035         llog = filter_grab_llog_for_group(obd, body->lgdc_logid.lgl_ogr, exp);
3036         LASSERT(llog != NULL);
3037         ctxt = llog_get_context(llog, body->lgdc_ctxt_idx);
3038         rc = llog_connect(ctxt, 1, &body->lgdc_logid,
3039                           &body->lgdc_gen, NULL);
3040         if (rc != 0)
3041                 CERROR("failed to connect\n");
3042
3043         RETURN(rc);
3044 }
3045
3046 static struct dentry *filter_lvfs_id2dentry(__u64 id, __u32 gen, 
3047                                             __u64 gr, void *data)
3048 {
3049         return filter_id2dentry(data, NULL, gr, id);
3050 }
3051
3052 static struct lvfs_callback_ops filter_lvfs_ops = {
3053         l_id2dentry:     filter_lvfs_id2dentry,
3054 };
3055
3056 static struct obd_ops filter_obd_ops = {
3057         .o_owner          = THIS_MODULE,
3058         .o_attach         = filter_attach,
3059         .o_detach         = filter_detach,
3060         .o_get_info       = filter_get_info,
3061         .o_setup          = filter_setup,
3062         .o_precleanup     = filter_precleanup,
3063         .o_cleanup        = filter_cleanup,
3064         .o_process_config = filter_process_config,
3065         .o_connect        = filter_connect,
3066         .o_connect_post   = filter_connect_post,
3067         .o_disconnect     = filter_disconnect,
3068         .o_statfs         = filter_statfs,
3069         .o_getattr        = filter_getattr,
3070         .o_unpackmd       = filter_unpackmd,
3071         .o_create         = filter_create,
3072         .o_setattr        = filter_setattr,
3073         .o_destroy        = filter_destroy,
3074         .o_brw            = filter_brw,
3075         .o_punch          = filter_truncate,
3076         .o_sync           = filter_sync,
3077         .o_preprw         = filter_preprw,
3078         .o_commitrw       = filter_commitrw,
3079         .o_do_cow         = filter_do_cow,
3080         .o_write_extents  = filter_write_extents,
3081         .o_destroy_export = filter_destroy_export,
3082         .o_llog_init      = filter_llog_init,
3083         .o_llog_finish    = filter_llog_finish,
3084         .o_llog_connect   = filter_llog_connect,
3085         .o_iocontrol      = filter_iocontrol,
3086 };
3087
3088 static struct obd_ops filter_sanobd_ops = {
3089         .o_owner          = THIS_MODULE,
3090         .o_attach         = filter_attach,
3091         .o_detach         = filter_detach,
3092         .o_get_info       = filter_get_info,
3093         .o_setup          = filter_san_setup,
3094         .o_precleanup     = filter_precleanup,
3095         .o_cleanup        = filter_cleanup,
3096         .o_connect        = filter_connect,
3097         .o_connect_post   = filter_connect_post,
3098         .o_disconnect     = filter_disconnect,
3099         .o_statfs         = filter_statfs,
3100         .o_getattr        = filter_getattr,
3101         .o_unpackmd       = filter_unpackmd,
3102         .o_create         = filter_create,
3103         .o_setattr        = filter_setattr,
3104         .o_destroy        = filter_destroy,
3105         .o_brw            = filter_brw,
3106         .o_punch          = filter_truncate,
3107         .o_sync           = filter_sync,
3108         .o_preprw         = filter_preprw,
3109         .o_commitrw       = filter_commitrw,
3110         .o_do_cow         = filter_do_cow,
3111         .o_write_extents  = filter_write_extents,
3112         .o_san_preprw     = filter_san_preprw,
3113         .o_destroy_export = filter_destroy_export,
3114         .o_llog_init      = filter_llog_init,
3115         .o_llog_finish    = filter_llog_finish,
3116         .o_llog_connect   = filter_llog_connect,
3117         .o_iocontrol      = filter_iocontrol,
3118 };
3119
3120 static int __init obdfilter_init(void)
3121 {
3122         struct lprocfs_static_vars lvars;
3123         int size, rc;
3124
3125         printk(KERN_INFO "Lustre: Filtering OBD driver; info@clusterfs.com\n");
3126
3127         lprocfs_init_vars(filter, &lvars);
3128
3129         size = OBDFILTER_CREATED_SCRATCHPAD_ENTRIES * 
3130                 sizeof(*obdfilter_created_scratchpad);
3131         
3132         OBD_ALLOC(obdfilter_created_scratchpad, size);
3133         if (obdfilter_created_scratchpad == NULL) {
3134                 CERROR ("Can't allocate scratchpad\n");
3135                 return -ENOMEM;
3136         }
3137
3138         rc = class_register_type(&filter_obd_ops, NULL, lvars.module_vars,
3139                                  OBD_FILTER_DEVICENAME);
3140         if (rc) {
3141                 OBD_FREE(obdfilter_created_scratchpad, size);
3142                 return rc;
3143         }
3144
3145         rc = class_register_type(&filter_sanobd_ops, NULL, lvars.module_vars,
3146                                  OBD_FILTER_SAN_DEVICENAME);
3147         if (rc) {
3148                 class_unregister_type(OBD_FILTER_DEVICENAME);
3149                 OBD_FREE(obdfilter_created_scratchpad, size);
3150         }
3151         return rc;
3152 }
3153
3154 static void __exit obdfilter_exit(void)
3155 {
3156         class_unregister_type(OBD_FILTER_SAN_DEVICENAME);
3157         class_unregister_type(OBD_FILTER_DEVICENAME);
3158         OBD_FREE(obdfilter_created_scratchpad,
3159                  OBDFILTER_CREATED_SCRATCHPAD_ENTRIES * 
3160                  sizeof(*obdfilter_created_scratchpad));
3161 }
3162
3163 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3164 MODULE_DESCRIPTION("Lustre Filtering OBD driver");
3165 MODULE_LICENSE("GPL");
3166
3167 module_init(obdfilter_init);
3168 module_exit(obdfilter_exit);