Whamcloud - gitweb
- removed not needed filter_group_set_fs_flags()
[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] = simple_mkdir(dentry, dir, 0700, 1);
699                         if (IS_ERR(tmp_subdirs->dentry[i])) {
700                                 rc = PTR_ERR(tmp_subdirs->dentry[i]);
701                                 CERROR("can't lookup/create O/%d/%s: rc = %d\n",
702                                        group, dir, rc);
703                                 GOTO(cleanup, rc);
704                         }
705
706                         /* turn on smfs plugins if any */
707                         fsfilt_set_fs_flags(obd, tmp_subdirs->dentry[i]->d_inode,
708                                             SM_ALL_PLG);
709
710                         CDEBUG(D_INODE, "got/created O/%d/%s: %p\n", group, dir,
711                                tmp_subdirs->dentry[i]);
712                 }
713         }
714
715         /* 'group' is an index; we need an array of length 'group + 1' */
716         if (group + 1 > old_count) {
717                 int len = group + 1;
718                 OBD_ALLOC(new_objids, len * sizeof(*new_objids));
719                 OBD_ALLOC(new_subdirs, len * sizeof(*new_subdirs));
720                 OBD_ALLOC(new_groups, len * sizeof(*new_groups));
721                 OBD_ALLOC(new_files, len * sizeof(*new_files));
722                 stage = 4;
723                 if (new_objids == NULL || new_subdirs == NULL ||
724                     new_groups == NULL || new_files == NULL)
725                         GOTO(cleanup, rc = -ENOMEM);
726
727                 memcpy(new_objids, filter->fo_last_objids,
728                        old_count * sizeof(*new_objids));
729                 memcpy(new_subdirs, filter->fo_subdirs,
730                        old_count * sizeof(*new_subdirs));
731                 memcpy(new_groups, filter->fo_groups,
732                        old_count * sizeof(*new_groups));
733                 memcpy(new_files, filter->fo_last_objid_files,
734                        old_count * sizeof(*new_files));
735
736                 if (old_count) {
737                         OBD_FREE(filter->fo_last_objids,
738                                  old_count * sizeof(*new_objids));
739                         OBD_FREE(filter->fo_subdirs,
740                                  old_count * sizeof(*new_subdirs));
741                         OBD_FREE(filter->fo_groups,
742                                  old_count * sizeof(*new_groups));
743                         OBD_FREE(filter->fo_last_objid_files,
744                                  old_count * sizeof(*new_files));
745                 }
746                 filter->fo_last_objids = new_objids;
747                 filter->fo_subdirs = new_subdirs;
748                 filter->fo_groups = new_groups;
749                 filter->fo_last_objid_files = new_files;
750                 filter->fo_group_count = len;
751         }
752
753         filter->fo_groups[group] = dentry;
754         filter->fo_last_objid_files[group] = filp;
755         if (filter->fo_subdir_count) {
756                 filter->fo_subdirs[group] = *tmp_subdirs;
757                 OBD_FREE(tmp_subdirs, sizeof(*tmp_subdirs));
758         }
759
760         filter_update_last_group(obd, group);
761         
762         if (filp->f_dentry->d_inode->i_size == 0) {
763                 filter->fo_last_objids[group] = FILTER_INIT_OBJID;
764                 RETURN(0);
765         }
766
767         filter->fo_last_objids[group] = le64_to_cpu(last_objid);
768         CDEBUG(D_INODE, "%s: server last_objid group %d: "LPU64"\n",
769                obd->obd_name, group, last_objid);
770         RETURN(0);
771  cleanup:
772         switch (stage) {
773         case 4:
774                 if (new_objids != NULL)
775                         OBD_FREE(new_objids, group * sizeof(*new_objids));
776                 if (new_subdirs != NULL)
777                         OBD_FREE(new_subdirs, group * sizeof(*new_subdirs));
778                 if (new_groups != NULL)
779                         OBD_FREE(new_groups, group * sizeof(*new_groups));
780                 if (new_files != NULL)
781                         OBD_FREE(new_files, group * sizeof(*new_files));
782         case 3:
783                 if (filter->fo_subdir_count) {
784                         for (i = 0; i < filter->fo_subdir_count; i++) {
785                                 if (tmp_subdirs->dentry[i] != NULL)
786                                         dput(tmp_subdirs->dentry[i]);
787                         }
788                         OBD_FREE(tmp_subdirs, sizeof(*tmp_subdirs));
789                 }
790         case 2:
791                 filp_close(filp, 0);
792         case 1:
793                 dput(dentry);
794         }
795         RETURN(rc);
796 }
797
798 static int filter_read_groups(struct obd_device *obd, int last_group,
799                               int create)
800 {
801         struct filter_obd *filter = &obd->u.filter;
802         int old_count, group, rc = 0;
803
804         down(&filter->fo_init_lock);
805         old_count = filter->fo_group_count;
806         for (group = old_count; group <= last_group; group++) {
807                 if (group == 0)
808                         continue; /* no group zero */
809
810                 rc = filter_read_group_internal(obd, group, create);
811                 if (rc != 0)
812                         break;
813         }
814         up(&filter->fo_init_lock);
815         return rc;
816 }
817
818 static int filter_prep_groups(struct obd_device *obd)
819 {
820         struct filter_obd *filter = &obd->u.filter;
821         struct dentry *dentry, *O_dentry;
822         int rc = 0, cleanup_phase = 0;
823         struct file *filp = NULL;
824         int last_group;
825         loff_t off = 0;
826         ENTRY;
827
828         O_dentry = simple_mkdir(current->fs->pwd, "O", 0700, 1);
829         CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
830         if (IS_ERR(O_dentry)) {
831                 rc = PTR_ERR(O_dentry);
832                 CERROR("cannot open/create O: rc = %d\n", rc);
833                 GOTO(cleanup, rc);
834         }
835         filter->fo_dentry_O = O_dentry;
836         cleanup_phase = 1; /* O_dentry */
837
838         /* Lookup "R" to tell if we're on an old OST FS and need to convert
839          * from O/R/<dir>/<objid> to O/0/<dir>/<objid>.  This can be removed
840          * some time post 1.0 when all old-style OSTs have converted along
841          * with the init_objid hack. */
842         dentry = ll_lookup_one_len("R", O_dentry, 1);
843         if (IS_ERR(dentry))
844                 GOTO(cleanup, rc = PTR_ERR(dentry));
845         if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
846                 struct dentry *O0_dentry = lookup_one_len("0", O_dentry, 1);
847                 ENTRY;
848
849                 CWARN("converting OST to new object layout\n");
850                 if (IS_ERR(O0_dentry)) {
851                         rc = PTR_ERR(O0_dentry);
852                         CERROR("error looking up O/0: rc %d\n", rc);
853                         GOTO(cleanup_R, rc);
854                 }
855
856                 if (O0_dentry->d_inode) {
857                         CERROR("Both O/R and O/0 exist. Fix manually.\n");
858                         GOTO(cleanup_O0, rc = -EEXIST);
859                 }
860
861                 down(&O_dentry->d_inode->i_sem);
862                 rc = vfs_rename(O_dentry->d_inode, dentry,
863                                 O_dentry->d_inode, O0_dentry);
864                 up(&O_dentry->d_inode->i_sem);
865
866                 if (rc) {
867                         CERROR("error renaming O/R to O/0: rc %d\n", rc);
868                         GOTO(cleanup_O0, rc);
869                 }
870                 filter->fo_fsd->fsd_feature_incompat |=
871                         cpu_to_le32(FILTER_INCOMPAT_GROUPS);
872                 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
873                                                filter->fo_fsd, 1);
874                 GOTO(cleanup_O0, rc);
875
876         cleanup_O0:
877                 f_dput(O0_dentry);
878         cleanup_R:
879                 f_dput(dentry);
880                 if (rc)
881                         GOTO(cleanup, rc);
882         } else {
883                 f_dput(dentry);
884         }
885
886         cleanup_phase = 2; /* groups */
887
888         /* we have to initialize all groups before first connections from
889          * clients because they may send create/destroy for any group -bzzz */
890         filp = filp_open("LAST_GROUP", O_CREAT | O_RDWR, 0700);
891         if (IS_ERR(filp)) {
892                 CERROR("cannot create LAST_GROUP: rc = %ld\n", PTR_ERR(filp));
893                 GOTO(cleanup, rc = PTR_ERR(filp));
894         }
895         cleanup_phase = 3; /* filp */
896
897         rc = fsfilt_read_record(obd, filp, &last_group, sizeof(__u32), &off);
898         if (rc) {
899                 CDEBUG(D_INODE, "error reading LAST_GROUP: rc %d\n", rc);
900                 GOTO(cleanup, rc);
901         }
902         if (off == 0) {
903                 last_group = FILTER_MIN_GROUPS;
904         } else {
905                 LASSERT(last_group >= FILTER_MIN_GROUPS);
906         }
907
908         CWARN("%s: initialize groups [%d,%d]\n", obd->obd_name,
909               FILTER_MIN_GROUPS, last_group);
910         filter->fo_committed_group = last_group;
911         rc = filter_read_groups(obd, last_group, 1);
912         if (rc)
913                 GOTO(cleanup, rc);
914         
915         filp_close(filp, 0);
916         RETURN(0);
917
918  cleanup:
919         switch (cleanup_phase) {
920         case 3:
921                 filp_close(filp, 0);
922         case 2:
923                 filter_cleanup_groups(obd);
924         case 1:
925                 f_dput(filter->fo_dentry_O);
926                 filter->fo_dentry_O = NULL;
927         default:
928                 break;
929         }
930         return rc;
931 }
932
933
934 /* setup the object store with correct subdirectories */
935 static int filter_prep(struct obd_device *obd)
936 {
937         struct lvfs_run_ctxt saved;
938         struct filter_obd *filter = &obd->u.filter;
939         struct file *file;
940         struct inode *inode;
941         int rc = 0;
942         ENTRY;
943
944         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
945         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
946         if (!file || IS_ERR(file)) {
947                 rc = PTR_ERR(file);
948                 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
949                        LAST_RCVD, rc);
950                 GOTO(out, rc);
951         }
952
953         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
954                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
955                        file->f_dentry->d_inode->i_mode);
956                 GOTO(err_filp, rc = -ENOENT);
957         }
958
959         /* steal operations */
960         inode = file->f_dentry->d_inode;
961         filter->fo_fop = file->f_op;
962         filter->fo_iop = inode->i_op;
963         filter->fo_aops = inode->i_mapping->a_ops;
964
965         rc = filter_init_server_data(obd, file);
966         if (rc) {
967                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
968                 GOTO(err_filp, rc);
969         }
970         filter->fo_rcvd_filp = file;
971
972         rc = filter_prep_groups(obd);
973         if (rc)
974                 GOTO(err_server_data, rc);
975
976  out:
977         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
978
979         return(rc);
980
981  err_server_data:
982         //class_disconnect_exports(obd, 0);
983         filter_free_server_data(filter);
984  err_filp:
985         if (filp_close(file, 0))
986                 CERROR("can't close %s after error\n", LAST_RCVD);
987         filter->fo_rcvd_filp = NULL;
988         goto out;
989 }
990
991 /* cleanup the filter: write last used object id to status file */
992 static void filter_post(struct obd_device *obd)
993 {
994         struct lvfs_run_ctxt saved;
995         struct filter_obd *filter = &obd->u.filter;
996         int rc, i;
997
998         /* XXX: filter_update_lastobjid used to call fsync_dev.  It might be
999          * best to start a transaction with h_sync, because we removed this
1000          * from lastobjid */
1001
1002         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1003         rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
1004                                        filter->fo_fsd, 0);
1005         if (rc)
1006                 CERROR("error writing server data: rc = %d\n", rc);
1007
1008         for (i = 1; i < filter->fo_group_count; i++) {
1009                 rc = filter_update_last_objid(obd, i,
1010                                              (i == filter->fo_group_count - 1));
1011                 if (rc)
1012                         CERROR("error writing group %d lastobjid: rc = %d\n",
1013                                i, rc);
1014         }
1015
1016         rc = filp_close(filter->fo_rcvd_filp, 0);
1017         filter->fo_rcvd_filp = NULL;
1018         if (rc)
1019                 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
1020
1021         filter_cleanup_groups(obd);
1022         filter_free_server_data(filter);
1023         filter_free_capa_keys(filter);
1024         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1025 }
1026
1027 static void filter_set_last_id(struct filter_obd *filter, int group, obd_id id)
1028 {
1029         LASSERT(filter->fo_fsd != NULL);
1030         LASSERT(group > 0);
1031         LASSERT(group < filter->fo_group_count);
1032
1033         spin_lock(&filter->fo_objidlock);
1034         filter->fo_last_objids[group] = id;
1035         spin_unlock(&filter->fo_objidlock);
1036 }
1037
1038 __u64 filter_last_id(struct filter_obd *filter, int group)
1039 {
1040         obd_id id;
1041         LASSERT(filter->fo_fsd != NULL);
1042         LASSERT(group > 0);
1043         LASSERT(group < filter->fo_group_count);
1044
1045         spin_lock(&filter->fo_objidlock);
1046         id = filter->fo_last_objids[group];
1047         spin_unlock(&filter->fo_objidlock);
1048
1049         return id;
1050 }
1051
1052 static void filter_save_last_id(struct filter_obd *filter, int group, obd_id id)
1053 {
1054         LASSERT(group > 0);
1055         LASSERT(group < filter->fo_group_count);
1056
1057         spin_lock(&filter->fo_lastidlock);
1058         if (id > filter_last_id(filter, group))
1059                 filter_set_last_id(filter, group, id);
1060         spin_unlock(&filter->fo_lastidlock);
1061 }
1062         
1063 /* direct cut-n-paste of mds_blocking_ast() */
1064 static int filter_blocking_ast(struct ldlm_lock *lock,
1065                                struct ldlm_lock_desc *desc,
1066                                void *data, int flag)
1067 {
1068         int do_ast;
1069         ENTRY;
1070
1071         if (flag == LDLM_CB_CANCELING) {
1072                 /* Don't need to do anything here. */
1073                 RETURN(0);
1074         }
1075
1076         /* XXX layering violation!  -phil */
1077         lock_res_and_lock(lock);
1078         /* Get this: if filter_blocking_ast is racing with ldlm_intent_policy,
1079          * such that filter_blocking_ast is called just before l_i_p takes the
1080          * ns_lock, then by the time we get the lock, we might not be the
1081          * correct blocking function anymore.  So check, and return early, if
1082          * so. */
1083         if (lock->l_blocking_ast != filter_blocking_ast) {
1084                 unlock_res_and_lock(lock);
1085                 RETURN(0);
1086         }
1087
1088         lock->l_flags |= LDLM_FL_CBPENDING;
1089         do_ast = (!lock->l_readers && !lock->l_writers);
1090         unlock_res_and_lock(lock);
1091
1092         if (do_ast) {
1093                 struct lustre_handle lockh;
1094                 int rc;
1095
1096                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
1097                 ldlm_lock2handle(lock, &lockh);
1098                 rc = ldlm_cli_cancel(&lockh);
1099                 if (rc < 0)
1100                         CERROR("ldlm_cli_cancel: %d\n", rc);
1101         } else {
1102                 LDLM_DEBUG(lock, "Lock still has references, will be "
1103                            "cancelled later");
1104         }
1105         RETURN(0);
1106 }
1107
1108 extern void *lock_dir(struct inode *dir, struct qstr *name);
1109 extern void unlock_dir(struct inode *dir, void *lock);
1110
1111 static void *filter_lock_dentry(struct obd_device *obd,
1112                                 struct dentry *dparent,
1113                                 obd_id id)
1114 {
1115 #ifdef S_PDIROPS
1116         struct qstr qstr;
1117         char name[32];
1118         int len;
1119
1120         len = sprintf(name, LPU64, id);
1121         qstr_assign(&qstr, (char *)name, len);
1122         return lock_dir(dparent->d_inode, &qstr);
1123 #else
1124         down(&dparent->d_inode->i_sem);
1125 #endif
1126         return 0;
1127 }
1128
1129 /* We never dget the object parent, so DON'T dput it either */
1130 static void filter_parent_unlock(struct dentry *dparent, void *lock)
1131 {
1132 #ifdef S_PDIROPS
1133         LASSERT(lock != NULL);
1134         unlock_dir(dparent->d_inode, lock);
1135 #else
1136         up(&dparent->d_inode->i_sem);
1137 #endif
1138 }
1139
1140 /* We never dget the object parent, so DON'T dput it either */
1141 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
1142 {
1143         struct filter_obd *filter = &obd->u.filter;
1144         LASSERTF(group < filter->fo_group_count,
1145                  "group %d < filter->fo_group_count %d failed\n",
1146                  (int)group, (int)filter->fo_group_count);
1147         LASSERTF(group > 0, "group %d > 0 failed\n", (int)group);
1148
1149         if (filter->fo_subdir_count == 0)
1150                 return filter->fo_groups[group];
1151
1152         return filter->fo_subdirs[group].dentry[objid & (filter->fo_subdir_count - 1)];
1153 }
1154
1155 /* We never dget the object parent, so DON'T dput it either */
1156 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
1157                                   obd_id objid, void **lock)
1158 {
1159         unsigned long now = jiffies;
1160         struct dentry *dparent = filter_parent(obd, group, objid);
1161
1162         if (IS_ERR(dparent))
1163                 return dparent;
1164
1165         LASSERT(dparent);
1166         LASSERT(dparent->d_inode);
1167
1168         *lock = filter_lock_dentry(obd, dparent, objid);
1169         fsfilt_check_slow(now, obd_timeout, "parent lock");
1170         return dparent;
1171 }
1172
1173 /* How to get files, dentries, inodes from object id's.
1174  *
1175  * If dir_dentry is passed, the caller has already locked the parent
1176  * appropriately for this operation (normally a write lock).  If
1177  * dir_dentry is NULL, we do a read lock while we do the lookup to
1178  * avoid races with create/destroy and such changing the directory
1179  * internal to the filesystem code. */
1180 struct dentry *filter_id2dentry(struct obd_device *obd,
1181                                 struct dentry *dir_dentry,
1182                                 obd_gr group, obd_id id)
1183 {
1184         struct dentry *dparent = dir_dentry;
1185         struct dentry *dchild;
1186         void *lock = NULL;
1187         char name[32];
1188         int len;
1189         ENTRY;
1190
1191         if (id == 0) {
1192                 CERROR("fatal: invalid object id 0\n");
1193                 RETURN(ERR_PTR(-ESTALE));
1194         }
1195
1196         len = sprintf(name, LPU64, id);
1197         if (dir_dentry == NULL) {
1198                 dparent = filter_parent_lock(obd, group, id, &lock);
1199                 if (IS_ERR(dparent)) {
1200                         CERROR("%s: error getting object "LPU64":"LPU64
1201                                " parent: rc %ld\n", obd->obd_name,
1202                                id, group, PTR_ERR(dparent));
1203                         RETURN(dparent);
1204                 }
1205         }
1206         CDEBUG(D_INODE, "looking up object O/%.*s/%s\n",
1207                dparent->d_name.len, dparent->d_name.name, name);
1208         dchild = /*ll_*/lookup_one_len(name, dparent, len);
1209         if (dir_dentry == NULL)
1210                 filter_parent_unlock(dparent, lock);
1211         if (IS_ERR(dchild)) {
1212                 CERROR("%s: child lookup error %ld\n", obd->obd_name,
1213                        PTR_ERR(dchild));
1214                 RETURN(dchild);
1215         }
1216
1217         if (dchild->d_inode != NULL && is_bad_inode(dchild->d_inode)) {
1218                 CERROR("%s: got bad inode "LPU64"\n", obd->obd_name, id);
1219                 f_dput(dchild);
1220                 RETURN(ERR_PTR(-ENOENT));
1221         }
1222
1223         CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
1224                name, dchild, atomic_read(&dchild->d_count));
1225
1226         LASSERT(atomic_read(&dchild->d_count) > 0);
1227
1228         RETURN(dchild);
1229 }
1230
1231 static int filter_prepare_destroy(struct obd_device *obd, obd_id objid,
1232                                   obd_id group)
1233 {
1234         struct lustre_handle lockh;
1235         int flags = LDLM_AST_DISCARD_DATA, rc;
1236         struct ldlm_res_id res_id = { .name = { objid, 0, group, 0 } };
1237         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
1238
1239         ENTRY;
1240         /* Tell the clients that the object is gone now and that they should
1241          * throw away any cached pages. */
1242         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
1243                               LDLM_EXTENT, &policy, LCK_PW,
1244                               &flags, filter_blocking_ast, ldlm_completion_ast,
1245                               NULL, NULL, NULL, 0, NULL, &lockh);
1246
1247         /* We only care about the side-effects, just drop the lock. */
1248         if (rc == ELDLM_OK)
1249                 ldlm_lock_decref(&lockh, LCK_PW);
1250
1251         RETURN(rc);
1252 }
1253
1254 /* Caller must hold LCK_PW on parent and push us into kernel context.
1255  * Caller is also required to ensure that dchild->d_inode exists. */
1256 static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
1257                                    struct dentry *dparent,
1258                                    struct dentry *dchild)
1259 {
1260         struct inode *inode = dchild->d_inode;
1261         int rc;
1262         ENTRY;
1263
1264         if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
1265                 CERROR("destroying objid %.*s nlink = %lu, count = %d\n",
1266                        dchild->d_name.len, dchild->d_name.name,
1267                        (unsigned long)inode->i_nlink,
1268                        atomic_read(&inode->i_count));
1269         }
1270
1271         rc = vfs_unlink(dparent->d_inode, dchild);
1272
1273         if (rc)
1274                 CERROR("error unlinking objid %.*s: rc %d\n",
1275                        dchild->d_name.len, dchild->d_name.name, rc);
1276
1277         RETURN(rc);
1278 }
1279
1280 static int filter_intent_policy(struct ldlm_namespace *ns,
1281                                 struct ldlm_lock **lockp, void *req_cookie,
1282                                 ldlm_mode_t mode, int flags, void *data)
1283 {
1284         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1285         struct ptlrpc_request *req = req_cookie;
1286         struct ldlm_lock *lock = *lockp, *l = NULL;
1287         struct ldlm_resource *res = lock->l_resource;
1288         ldlm_processing_policy policy;
1289         struct ost_lvb *res_lvb, *reply_lvb;
1290         struct ldlm_reply *rep;
1291         struct list_head *tmp;
1292         ldlm_error_t err;
1293         int tmpflags = 0, rc, repsize[2] = {sizeof(*rep), sizeof(*reply_lvb)};
1294         ENTRY;
1295
1296         policy = ldlm_get_processing_policy(res);
1297         LASSERT(policy != NULL);
1298         LASSERT(req != NULL);
1299
1300         rc = lustre_pack_reply(req, 2, repsize, NULL);
1301         if (rc)
1302                 RETURN(req->rq_status = rc);
1303
1304         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
1305         LASSERT(rep != NULL);
1306
1307         reply_lvb = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*reply_lvb));
1308         LASSERT(reply_lvb != NULL);
1309
1310         //fixup_handle_for_resent_req(req, lock, &lockh);
1311
1312         /* If we grant any lock at all, it will be a whole-file read lock.
1313          * Call the extent policy function to see if our request can be
1314          * granted, or is blocked. */
1315         lock->l_policy_data.l_extent.start = 0;
1316         lock->l_policy_data.l_extent.end = OBD_OBJECT_EOF;
1317         lock->l_req_mode = LCK_PR;
1318
1319         lock_res_and_lock(lock);
1320         res = lock->l_resource;
1321         rc = policy(lock, &tmpflags, 0, &err, &rpc_list);
1322
1323         /* FIXME: we should change the policy function slightly, to not make
1324          * this list at all, since we just turn around and free it */
1325         while (!list_empty(&rpc_list)) {
1326                 struct ldlm_lock *wlock =
1327                         list_entry(rpc_list.next, struct ldlm_lock, l_cp_ast);
1328                 LASSERT((lock->l_flags & LDLM_FL_AST_SENT) == 0);
1329                 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1330                 lock->l_flags &= ~LDLM_FL_CP_REQD;
1331                 list_del_init(&wlock->l_cp_ast);
1332                 LDLM_LOCK_PUT(wlock);
1333         }
1334
1335         if (rc == LDLM_ITER_CONTINUE) {
1336                 /* The lock met with no resistance; we're finished. */
1337                 unlock_res_and_lock(lock);
1338                 RETURN(ELDLM_LOCK_REPLACED);
1339         }
1340
1341         /* Do not grant any lock, but instead send GL callbacks.  The extent
1342          * policy nicely created a list of all PW locks for us.  We will choose
1343          * the highest of those which are larger than the size in the LVB, if
1344          * any, and perform a glimpse callback. */
1345         res_lvb = res->lr_lvb_data;
1346         LASSERT(res_lvb != NULL);
1347         *reply_lvb = *res_lvb;
1348
1349         list_for_each(tmp, &res->lr_granted) {
1350                 struct ldlm_lock *tmplock =
1351                         list_entry(tmp, struct ldlm_lock, l_res_link);
1352
1353                 if (tmplock->l_granted_mode == LCK_PR)
1354                         continue;
1355
1356                 if (tmplock->l_policy_data.l_extent.end <= reply_lvb->lvb_size)
1357                         continue;
1358
1359                 if (l == NULL) {
1360                         l = LDLM_LOCK_GET(tmplock);
1361                         continue;
1362                 }
1363
1364                 if (l->l_policy_data.l_extent.start >
1365                     tmplock->l_policy_data.l_extent.start)
1366                         continue;
1367
1368                 LDLM_LOCK_PUT(l);
1369                 l = LDLM_LOCK_GET(tmplock);
1370         }
1371         unlock_res(res);
1372
1373         /* There were no PW locks beyond the size in the LVB; finished. */
1374         if (l == NULL)
1375                 RETURN(ELDLM_LOCK_ABORTED);
1376
1377         if (l->l_glimpse_ast == NULL) {
1378                 /* We are racing with unlink(); just return -ENOENT */
1379                 CDEBUG(D_OTHER, "race with unlink: "LPX64"/"LPX64"\n",
1380                        res->lr_name.name[0], res->lr_name.name[2]);
1381                 rep->lock_policy_res1 = -ENOENT;
1382                 goto out;
1383         }
1384
1385         LASSERTF(l->l_glimpse_ast != NULL, "l == %p", l);
1386
1387         rc = l->l_glimpse_ast(l, NULL); /* this will update the LVB */
1388         if (rc != 0 && res->lr_namespace->ns_lvbo &&
1389             res->lr_namespace->ns_lvbo->lvbo_update) {
1390                 res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
1391         }
1392
1393         lock_res(res);
1394         *reply_lvb = *res_lvb;
1395         unlock_res(res);
1396 out:
1397         LDLM_LOCK_PUT(l);
1398
1399         RETURN(ELDLM_LOCK_ABORTED);
1400 }
1401
1402 static int filter_post_fs_cleanup(struct obd_device *obd)
1403 {
1404         int rc = 0;
1405
1406         rc = fsfilt_post_cleanup(obd);
1407
1408         RETURN(rc);
1409 }
1410
1411 static int filter_post_fs_setup(struct obd_device *obd)
1412 {
1413         struct filter_obd *filter = &obd->u.filter;
1414         int rc = 0;
1415         ENTRY;
1416         
1417         rc = fsfilt_post_setup(obd, filter->fo_dentry_O);
1418
1419         RETURN(rc);
1420 }
1421
1422 /* mount the file system (secretly) */
1423 int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
1424                         char *option)
1425 {
1426         struct lustre_cfg *lcfg = buf;
1427         struct filter_obd *filter = &obd->u.filter;
1428         struct lvfs_obd_ctxt *lvfs_ctxt = NULL;
1429         struct vfsmount *mnt;
1430         struct crypto_tfm *tfm;
1431         char *str;
1432         char ns_name[48];
1433         int rc = 0, i;
1434         ENTRY;
1435
1436         if ((LUSTRE_CFG_BUFLEN(lcfg, 1)) < 1 || 
1437             (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1)) 
1438                 RETURN(-EINVAL);
1439
1440         obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
1441         if (IS_ERR(obd->obd_fsops))
1442                 RETURN(PTR_ERR(obd->obd_fsops));
1443
1444         rc = lvfs_mount_fs(lustre_cfg_string(lcfg, 1), 
1445                            lustre_cfg_string(lcfg, 2), 
1446                            option, MS_NOATIME | MS_NODIRATIME, &lvfs_ctxt);
1447         if (rc) {
1448                 CERROR("lvfs_mount_fs failed: rc = %d\n", rc);
1449                 GOTO(err_ops, rc);
1450         }
1451         LASSERT(lvfs_ctxt);
1452
1453         mnt = lvfs_ctxt->loc_mnt;
1454         filter->fo_lvfs_ctxt = lvfs_ctxt;
1455
1456         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && lustre_cfg_buf(lcfg, 3)) {
1457                 str = lustre_cfg_string(lcfg, 3);
1458                 if (*str == 'f') {
1459                         obd->obd_replayable = 1;
1460                         obd_sync_filter = 1;
1461                         CWARN("%s: recovery enabled\n", obd->obd_name);
1462                 } else {
1463                         if (*str != 'n') {
1464                                 CERROR("unrecognised flag '%c'\n",
1465                                        *str);
1466                         }
1467                         // XXX Robert? Why do we get errors here
1468                         // GOTO(err_mntput, rc = -EINVAL);
1469                 }
1470         }
1471
1472         filter->fo_vfsmnt = mnt;
1473         filter->fo_sb = mnt->mnt_sb;
1474         filter->fo_fstype = mnt->mnt_sb->s_type->name;
1475         CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
1476
1477         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1478         obd->obd_lvfs_ctxt.pwdmnt = mnt;
1479         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1480         obd->obd_lvfs_ctxt.fs = get_ds();
1481         obd->obd_lvfs_ctxt.cb_ops = filter_lvfs_ops;
1482
1483         ll_clear_rdonly(ll_sbdev(filter->fo_sb));
1484         
1485         rc = fsfilt_setup(obd, mnt->mnt_sb);
1486         if (rc)
1487                 GOTO(err_mntput, rc);
1488
1489         sema_init(&filter->fo_init_lock, 1);
1490         filter->fo_committed_group = 0;
1491
1492         tfm = crypto_alloc_tfm(CAPA_HMAC_ALG, 0);
1493         if (!tfm)
1494                 GOTO(err_mntput, rc = -ENOSYS);
1495
1496         filter->fo_capa_hmac = tfm;
1497         INIT_LIST_HEAD(&filter->fo_capa_keys);
1498         spin_lock_init(&filter->fo_capa_lock);
1499
1500         rc = filter_prep(obd);
1501         if (rc)
1502                 GOTO(err_capa, rc);
1503
1504         filter->fo_destroys_in_progress = 0;
1505         for (i = 0; i < 32; i++)
1506                 sema_init(&filter->fo_create_locks[i], 1);
1507
1508         spin_lock_init(&filter->fo_translock);
1509         spin_lock_init(&filter->fo_objidlock);
1510         spin_lock_init(&filter->fo_lastidlock);
1511         INIT_LIST_HEAD(&filter->fo_export_list);
1512         sema_init(&filter->fo_alloc_lock, 1);
1513         spin_lock_init(&filter->fo_r_pages.oh_lock);
1514         spin_lock_init(&filter->fo_w_pages.oh_lock);
1515         spin_lock_init(&filter->fo_r_discont_pages.oh_lock);
1516         spin_lock_init(&filter->fo_w_discont_pages.oh_lock);
1517         spin_lock_init(&filter->fo_r_discont_blocks.oh_lock);
1518         spin_lock_init(&filter->fo_w_discont_blocks.oh_lock);
1519         filter->fo_readcache_max_filesize = FILTER_MAX_CACHE_SIZE;
1520
1521         INIT_LIST_HEAD(&filter->fo_llog_list);
1522         spin_lock_init(&filter->fo_llog_list_lock);
1523
1524         spin_lock_init(&filter->fo_denylist_lock);
1525         INIT_LIST_HEAD(&filter->fo_denylist);
1526
1527         sprintf(ns_name, "filter-%s", obd->obd_uuid.uuid);
1528         obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
1529
1530         if (obd->obd_namespace == NULL)
1531                 GOTO(err_post, rc = -ENOMEM);
1532         obd->obd_namespace->ns_lvbp = obd;
1533         obd->obd_namespace->ns_lvbo = &filter_lvbo;
1534         ldlm_register_intent(obd->obd_namespace, filter_intent_policy);
1535
1536         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1537                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1538
1539         rc = obd_llog_cat_initialize(obd, &obd->obd_llogs, 1, CATLIST);
1540         if (rc) {
1541                 CERROR("failed to setup llogging subsystems\n");
1542                 GOTO(err_post, rc);
1543         }
1544         RETURN(0);
1545
1546 err_post:
1547         filter_post(obd);
1548 err_capa:
1549         crypto_free_tfm(filter->fo_capa_hmac);
1550 err_mntput:
1551         unlock_kernel();
1552         lvfs_umount_fs(filter->fo_lvfs_ctxt);
1553         filter->fo_sb = 0;
1554         lock_kernel();
1555 err_ops:
1556         fsfilt_put_ops(obd->obd_fsops);
1557         return rc;
1558 }
1559
1560 static int filter_attach(struct obd_device *obd, obd_count len, void *data)
1561 {
1562         struct lprocfs_static_vars lvars;
1563         int rc;
1564
1565         lprocfs_init_vars(filter, &lvars);
1566         rc = lprocfs_obd_attach(obd, lvars.obd_vars);
1567         if (rc != 0)
1568                 return rc;
1569
1570         rc = lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST);
1571         if (rc != 0)
1572                 return rc;
1573
1574         /* Init obdfilter private stats here */
1575         lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
1576                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
1577         lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
1578                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
1579
1580         return lproc_filter_attach_seqstat(obd);
1581 }
1582
1583 static int filter_detach(struct obd_device *dev)
1584 {
1585         lprocfs_free_obd_stats(dev);
1586         return lprocfs_obd_detach(dev);
1587 }
1588
1589 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
1590 {
1591         struct lustre_cfg *lcfg = buf;
1592         unsigned long page;
1593         int rc;
1594         ENTRY;
1595
1596         /* 2.6.9 selinux wants a full option page for do_kern_mount (bug6471) */
1597         page = get_zeroed_page(GFP_KERNEL);
1598         if (!page)
1599                 RETURN(-ENOMEM);
1600
1601         memcpy((void *)page, lustre_cfg_buf(lcfg, 4),
1602                LUSTRE_CFG_BUFLEN(lcfg, 4));
1603         
1604         /* all mount options including errors=remount-ro and asyncdel are passed
1605          * using 4th lcfg param. And it is good, finally we have got rid of
1606          * hardcoded fs types in the code. */
1607         rc = filter_common_setup(obd, len, buf, (void *)page);
1608         free_page(page);
1609         
1610         if (rc)
1611                 RETURN(rc);
1612         rc = filter_post_fs_setup(obd);
1613         RETURN(rc);
1614 }
1615
1616 static int filter_cleanup(struct obd_device *obd, int flags)
1617 {
1618         struct filter_obd *filter = &obd->u.filter;
1619         ll_sbdev_type save_dev;
1620         ENTRY;
1621
1622         if (flags & OBD_OPT_FAILOVER)
1623                 CERROR("%s: shutting down for failover; client state will"
1624                        " be preserved.\n", obd->obd_name);
1625
1626         if (!list_empty(&obd->obd_exports)) {
1627                 CERROR("%s: still has clients!\n", obd->obd_name);
1628                 class_disconnect_exports(obd, flags);
1629                 if (!list_empty(&obd->obd_exports)) {
1630                         CERROR("still has exports after forced cleanup?\n");
1631                         RETURN(-EBUSY);
1632                 }
1633         }
1634
1635         target_cleanup_recovery(obd);
1636         
1637         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
1638
1639         if (filter->fo_sb == NULL)
1640                 RETURN(0);
1641
1642         save_dev = ll_sbdev(filter->fo_sb);
1643         filter_post_fs_cleanup(obd);
1644         filter_post(obd);
1645
1646         shrink_dcache_parent(filter->fo_sb->s_root);
1647         filter->fo_sb = 0;
1648
1649         spin_lock(&filter->fo_denylist_lock);
1650         while (!list_empty(&filter->fo_denylist)) {
1651                 deny_sec_t *p_deny_sec = list_entry(filter->fo_denylist.next,
1652                                                     deny_sec_t, list);
1653                 list_del(&p_deny_sec->list);
1654                 OBD_FREE(p_deny_sec, sizeof(*p_deny_sec));
1655         }
1656         spin_unlock(&filter->fo_denylist_lock);
1657
1658         unlock_kernel();
1659         lvfs_umount_fs(filter->fo_lvfs_ctxt);
1660         //destroy_buffers(filter->fo_sb->s_dev);
1661         filter->fo_sb = NULL;
1662         fsfilt_put_ops(obd->obd_fsops);
1663         lock_kernel();
1664
1665         ll_clear_rdonly(save_dev);
1666
1667         RETURN(0);
1668 }
1669
1670 static int filter_process_config(struct obd_device *obd, obd_count len, void *buf)
1671 {
1672         struct lustre_cfg *lcfg = buf;
1673         struct filter_obd *filter = &obd->u.filter;
1674         int rc = 0;
1675         ENTRY;
1676
1677         switch(lcfg->lcfg_command) {
1678         case LCFG_SET_SECURITY: {
1679                 if ((LUSTRE_CFG_BUFLEN(lcfg, 1) == 0) ||
1680                     (LUSTRE_CFG_BUFLEN(lcfg, 2) == 0))
1681                         GOTO(out, rc = -EINVAL);
1682
1683                 if (!strcmp(lustre_cfg_string(lcfg, 1), "deny_sec")){
1684                         spin_lock(&filter->fo_denylist_lock);
1685                         rc = add_deny_security(lustre_cfg_string(lcfg, 2),
1686                                                &filter->fo_denylist);
1687                         spin_unlock(&filter->fo_denylist_lock);
1688                 }else {
1689                         CERROR("Unrecognized key\n");
1690                         rc = -EINVAL;
1691                 }
1692                 break;
1693         }
1694         default: {
1695                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1696                 GOTO(out, rc = -EINVAL);
1697
1698         }
1699         }
1700 out:
1701         RETURN(rc);
1702 }
1703
1704 static int filter_connect_post(struct obd_export *exp, unsigned initial,
1705                                unsigned long connect_flags)
1706 {
1707         struct obd_device *obd = exp->exp_obd;
1708         struct filter_export_data *fed;
1709         char str[PTL_NALFMT_SIZE];
1710         struct obd_llogs *llog;
1711         struct llog_ctxt *ctxt;
1712         int rc = 0;
1713         ENTRY;
1714
1715         fed = &exp->exp_filter_data;
1716         if (fed->fed_group < FILTER_MIN_GROUPS)
1717                 RETURN(0);
1718
1719         /* initialize llogs for connections from MDS */
1720         llog = filter_grab_llog_for_group(obd, fed->fed_group, exp);
1721         LASSERT(llog != NULL);
1722
1723         ctxt = llog_get_context(llog, LLOG_UNLINK_REPL_CTXT);
1724         LASSERT(ctxt != NULL);
1725
1726         rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
1727         
1728         portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
1729                         exp->exp_connection->c_peer.peer_id.nid, str);
1730         
1731         CDEBUG(D_OTHER, "%s: init llog ctxt for export "LPX64"/%s, group %d\n",
1732                obd->obd_name, exp->exp_connection->c_peer.peer_id.nid,
1733                str, fed->fed_group);
1734
1735         RETURN(rc);
1736 }
1737
1738 /* nearly identical to mds_connect */
1739 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
1740                           struct obd_uuid *cluuid,
1741                           struct obd_connect_data *data,
1742                           unsigned long connect_flags)
1743 {
1744         struct obd_export *exp;
1745         struct filter_export_data *fed;
1746         struct filter_client_data *fcd = NULL;
1747         struct filter_obd *filter = &obd->u.filter;
1748         struct lvfs_run_ctxt saved;
1749         int rc, group;
1750         ENTRY;
1751
1752         if (conn == NULL || obd == NULL || cluuid == NULL)
1753                 RETURN(-EINVAL);
1754
1755         rc = class_connect(conn, obd, cluuid);
1756         if (rc)
1757                 RETURN(rc);
1758         exp = class_conn2export(conn);
1759         LASSERT(exp != NULL);
1760
1761         fed = &exp->exp_filter_data;
1762
1763         spin_lock_init(&fed->fed_lock);
1764
1765        /* connection from MDS */
1766         group = connect_flags;
1767         if (obd->obd_replayable) {
1768                 OBD_ALLOC(fcd, sizeof(*fcd));
1769                 if (!fcd) {
1770                         CERROR("filter: out of memory for client data\n");
1771                         GOTO(cleanup, rc = -ENOMEM);
1772                 }
1773
1774                 memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
1775                 fed->fed_fcd = fcd;
1776                 fed->fed_fcd->fcd_group = group;
1777                 rc = filter_client_add(obd, filter, fed, -1);
1778                 if (rc)
1779                         GOTO(cleanup, rc);
1780         }
1781         CWARN("%s: Received MDS connection ("LPX64"); group %d\n",
1782               obd->obd_name, exp->exp_handle.h_cookie, group);
1783  
1784         if (group == 0)
1785                 GOTO(cleanup, rc);
1786         
1787         if (fed->fed_group != 0 && fed->fed_group != group) {
1788                 char str[PTL_NALFMT_SIZE];
1789                 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
1790                                 exp->exp_connection->c_peer.peer_id.nid, str);
1791                 CERROR("!!! This export (nid "LPX64"/%s) used object group %d "
1792                        "earlier; now it's trying to use group %d!  This could "
1793                        "be a bug in the MDS.  Tell CFS.\n",
1794                        exp->exp_connection->c_peer.peer_id.nid, str,
1795                        fed->fed_group, group);
1796                 GOTO(cleanup, rc = -EPROTO);
1797         }
1798         fed->fed_group = group;
1799
1800         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1801         rc = filter_read_groups(obd, group, 1);
1802         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1803         if (rc != 0) {
1804                 CERROR("can't read group %u\n", group);
1805                 GOTO(cleanup, rc);
1806         }
1807 cleanup:
1808         if (rc) {
1809                 if (fcd)
1810                         OBD_FREE(fcd, sizeof(*fcd));
1811                 class_disconnect(exp, 0);
1812         } else {
1813                 class_export_put(exp);
1814         }
1815         return rc;
1816 }
1817
1818 static int filter_precleanup(struct obd_device *obd, int flags)
1819 {
1820         struct filter_group_llog *log;
1821         struct filter_obd *filter;
1822         int rc = 0;
1823         ENTRY;
1824
1825         filter = &obd->u.filter;
1826
1827         spin_lock(&filter->fo_llog_list_lock);
1828         while (!list_empty(&filter->fo_llog_list)) {
1829                 log = list_entry(filter->fo_llog_list.next,
1830                                  struct filter_group_llog, list);
1831                 list_del(&log->list);
1832                 spin_unlock(&filter->fo_llog_list_lock);
1833
1834                 rc = obd_llog_finish(obd, log->llogs, 0);
1835                 if (rc)
1836                         CERROR("failed to cleanup llogging subsystem for %u\n",
1837                                 log->group);
1838                 OBD_FREE(log->llogs, sizeof(*(log->llogs)));
1839                 OBD_FREE(log, sizeof(*log));
1840                 spin_lock(&filter->fo_llog_list_lock);
1841         }
1842         spin_unlock(&filter->fo_llog_list_lock);
1843
1844         rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1845         if (rc)
1846                 CERROR("failed to cleanup llogging subsystem\n");
1847
1848         RETURN(rc);
1849 }
1850
1851 /* Do extra sanity checks for grant accounting.  We do this at connect,
1852  * disconnect, and statfs RPC time, so it shouldn't be too bad.  We can
1853  * always get rid of it or turn it off when we know accounting is good. */
1854 static void filter_grant_sanity_check(struct obd_device *obd, const char *func)
1855 {
1856         struct filter_export_data *fed;
1857         struct obd_export *exp;
1858         obd_size maxsize = obd->obd_osfs.os_blocks * obd->obd_osfs.os_bsize;
1859         obd_size tot_dirty = 0, tot_pending = 0, tot_granted = 0;
1860         obd_size fo_tot_dirty, fo_tot_pending, fo_tot_granted;
1861         int level = D_CACHE;
1862
1863         if (list_empty(&obd->obd_exports))
1864                 return;
1865
1866         spin_lock(&obd->obd_osfs_lock);
1867         spin_lock(&obd->obd_dev_lock);
1868         list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1869                 fed = &exp->exp_filter_data;
1870                 if (fed->fed_grant < 0 || fed->fed_pending < 0 ||
1871                     fed->fed_dirty < 0)
1872                         level = D_ERROR;
1873                 if (maxsize > 0) { /* we may not have done a statfs yet */
1874                         LASSERTF(fed->fed_grant + fed->fed_pending <= maxsize,
1875                                  "cli %s/%p %ld+%ld > "LPU64"\n",
1876                                  exp->exp_client_uuid.uuid, exp,
1877                                  fed->fed_grant, fed->fed_pending, maxsize);
1878                         LASSERTF(fed->fed_dirty <= maxsize,
1879                                  "cli %s/%p %ld > "LPU64"\n",
1880                                  exp->exp_client_uuid.uuid, exp,
1881                                  fed->fed_dirty, maxsize);
1882                 }
1883                 CDEBUG(level, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
1884                        obd->obd_name, exp->exp_client_uuid.uuid, exp,
1885                        fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1886                 tot_granted += fed->fed_grant + fed->fed_pending;
1887                 tot_pending += fed->fed_pending;
1888                 tot_dirty += fed->fed_dirty;
1889         }
1890         fo_tot_granted = obd->u.filter.fo_tot_granted;
1891         fo_tot_pending = obd->u.filter.fo_tot_pending;
1892         fo_tot_dirty = obd->u.filter.fo_tot_dirty;
1893         spin_unlock(&obd->obd_dev_lock);
1894         spin_unlock(&obd->obd_osfs_lock);
1895
1896         /* Do these assertions outside the spinlocks so we don't kill system */
1897         if (tot_granted != fo_tot_granted)
1898                 CERROR("%s: tot_granted "LPU64" != fo_tot_granted "LPU64"\n",
1899                        func, tot_granted, fo_tot_granted);
1900         if (tot_pending != fo_tot_pending)
1901                 CERROR("%s: tot_pending "LPU64" != fo_tot_pending "LPU64"\n",
1902                        func, tot_pending, fo_tot_pending);
1903         if (tot_dirty != fo_tot_dirty)
1904                 CERROR("%s: tot_dirty "LPU64" != fo_tot_dirty "LPU64"\n",
1905                        func, tot_dirty, fo_tot_dirty);
1906         if (tot_pending > tot_granted)
1907                 CERROR("%s: tot_pending "LPU64" > tot_granted "LPU64"\n",
1908                        func, tot_pending, tot_granted);
1909         if (tot_granted > maxsize)
1910                 CERROR("%s: tot_granted "LPU64" > maxsize "LPU64"\n",
1911                        func, tot_granted, maxsize);
1912         if (tot_dirty > maxsize)
1913                 CERROR("%s: tot_dirty "LPU64" > maxsize "LPU64"\n",
1914                        func, tot_dirty, maxsize);
1915 }
1916
1917 /* Remove this client from the grant accounting totals.  We also remove
1918  * the export from the obd device under the osfs and dev locks to ensure
1919  * that the filter_grant_sanity_check() calculations are always valid.
1920  * The client should do something similar when it invalidates its import. */
1921 static void filter_grant_discard(struct obd_export *exp)
1922 {
1923         struct obd_device *obd = exp->exp_obd;
1924         struct filter_obd *filter = &obd->u.filter;
1925         struct filter_export_data *fed = &exp->exp_filter_data;
1926         int level = D_CACHE;
1927
1928         spin_lock(&obd->obd_osfs_lock);
1929         spin_lock(&exp->exp_obd->obd_dev_lock);
1930         list_del_init(&exp->exp_obd_chain);
1931         spin_unlock(&exp->exp_obd->obd_dev_lock);
1932
1933         if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0)
1934                 level = D_ERROR;
1935         CDEBUG(level, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
1936                obd->obd_name, exp->exp_client_uuid.uuid, exp,
1937                fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1938
1939         LASSERTF(filter->fo_tot_granted >= fed->fed_grant,
1940                  "%s: tot_granted "LPU64" cli %s/%p fed_grant %ld\n",
1941                  obd->obd_name, filter->fo_tot_granted,
1942                  exp->exp_client_uuid.uuid, exp, fed->fed_grant);
1943         filter->fo_tot_granted -= fed->fed_grant;
1944         LASSERTF(filter->fo_tot_pending >= fed->fed_pending,
1945                  "%s: tot_pending "LPU64" cli %s/%p fed_pending %ld\n",
1946                  obd->obd_name, filter->fo_tot_pending,
1947                  exp->exp_client_uuid.uuid, exp, fed->fed_pending);
1948         LASSERTF(filter->fo_tot_dirty >= fed->fed_dirty,
1949                  "%s: tot_dirty "LPU64" cli %s/%p fed_dirty %ld\n",
1950                  obd->obd_name, filter->fo_tot_dirty,
1951                  exp->exp_client_uuid.uuid, exp, fed->fed_dirty);
1952         filter->fo_tot_dirty -= fed->fed_dirty;
1953         fed->fed_dirty = 0;
1954         fed->fed_grant = 0;
1955
1956         spin_unlock(&obd->obd_osfs_lock);
1957 }
1958
1959 static int filter_destroy_export(struct obd_export *exp)
1960 {
1961         ENTRY;
1962
1963         if (exp->exp_filter_data.fed_pending)
1964                 CERROR("%s: cli %s/%p has %lu pending on destroyed export\n",
1965                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
1966                        exp, exp->exp_filter_data.fed_pending);
1967
1968         target_destroy_export(exp);
1969
1970         if (exp->exp_obd->obd_replayable)
1971                 filter_client_free(exp, exp->exp_flags);
1972
1973         filter_grant_discard(exp);
1974         if (!(exp->exp_flags & OBD_OPT_FORCE))
1975                 filter_grant_sanity_check(exp->exp_obd, __FUNCTION__);
1976
1977         RETURN(0);
1978 }
1979
1980 static void filter_sync_llogs(struct obd_device *obd, struct obd_export *dexp)
1981 {
1982         struct filter_group_llog *fglog, *nlog;
1983         struct filter_obd *filter;
1984         int worked = 0, group;
1985         struct llog_ctxt *ctxt;
1986         ENTRY;
1987
1988         filter = &obd->u.filter;
1989
1990         /* we can't sync log holding spinlock. also, we do not want to get
1991          * into livelock. so we do following: loop over MDS's exports in
1992          * group order and skip already synced llogs -bzzz */
1993         do {
1994                 /* look for group with min. number, but > worked */
1995                 fglog = NULL;
1996                 group = 1 << 30;
1997                 spin_lock(&filter->fo_llog_list_lock);
1998                 list_for_each_entry(nlog, &filter->fo_llog_list, list) {
1999                        
2000                         if (nlog->group <= worked) {
2001                                 /* this group is already synced */
2002                                 continue;
2003                         }
2004         
2005                         if (group < nlog->group) {
2006                                 /* we have group with smaller number to sync */
2007                                 continue;
2008                         }
2009
2010                         /* store current minimal group */
2011                         fglog = nlog;
2012                         group = nlog->group;
2013                 }
2014                 spin_unlock(&filter->fo_llog_list_lock);
2015
2016                 if (fglog == NULL)
2017                         break;
2018
2019                 worked = fglog->group;
2020                 if (fglog->exp && (dexp == fglog->exp || dexp == NULL)) {
2021                         ctxt = llog_get_context(fglog->llogs,
2022                                         LLOG_UNLINK_REPL_CTXT);
2023                         LASSERT(ctxt != NULL);
2024                         llog_sync(ctxt, fglog->exp);
2025                 }
2026         } while (fglog != NULL);
2027 }
2028
2029 /* also incredibly similar to mds_disconnect */
2030 static int filter_disconnect(struct obd_export *exp, unsigned long flags)
2031 {
2032         struct obd_device *obd = exp->exp_obd;
2033         unsigned long irqflags;
2034         int rc;
2035         ENTRY;
2036
2037         LASSERT(exp);
2038         class_export_get(exp);
2039
2040         spin_lock_irqsave(&exp->exp_lock, irqflags);
2041         exp->exp_flags = flags;
2042         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
2043
2044         if (!(flags & OBD_OPT_FORCE))
2045                 filter_grant_sanity_check(obd, __FUNCTION__);
2046         filter_grant_discard(exp);
2047
2048         /* Disconnect early so that clients can't keep using export */
2049         rc = class_disconnect(exp, flags);
2050
2051         ldlm_cancel_locks_for_export(exp);
2052
2053         fsfilt_sync(obd, obd->u.filter.fo_sb);
2054
2055         /* flush any remaining cancel messages out to the target */
2056         filter_sync_llogs(obd, exp);
2057         class_export_put(exp);
2058         RETURN(rc);
2059 }
2060
2061 struct dentry *__filter_oa2dentry(struct obd_device *obd,
2062                                   struct obdo *oa, const char *what)
2063 {
2064         struct dentry *dchild = NULL;
2065         obd_gr group = 0;
2066         ENTRY;
2067
2068         if (oa->o_valid & OBD_MD_FLGROUP)
2069                 group = oa->o_gr;
2070
2071         dchild = filter_id2dentry(obd, NULL, group, oa->o_id);
2072
2073         if (IS_ERR(dchild)) {
2074                 CERROR("%s error looking up object: "LPU64"\n",
2075                        what, oa->o_id);
2076                 RETURN(dchild);
2077         }
2078
2079         if (dchild->d_inode == NULL) {
2080                 CDEBUG(D_INFO, "%s: %s on non-existent object: "
2081                        LPU64"\n", obd->obd_name, what, oa->o_id);
2082                 f_dput(dchild);
2083                 RETURN(ERR_PTR(-ENOENT));
2084         }
2085
2086         RETURN(dchild);
2087 }
2088
2089 static int filter_getattr(struct obd_export *exp, struct obdo *oa,
2090                           struct lov_stripe_md *md)
2091 {
2092         struct dentry *dentry = NULL;
2093         struct obd_device *obd;
2094         int rc = 0;
2095         ENTRY;
2096
2097         obd = class_exp2obd(exp);
2098         if (obd == NULL) {
2099                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2100                        exp->exp_handle.h_cookie);
2101                 RETURN(-EINVAL);
2102         }
2103
2104         dentry = filter_oa2dentry(obd, oa);
2105         if (IS_ERR(dentry))
2106                 RETURN(PTR_ERR(dentry));
2107
2108         /* Limit the valid bits in the return data to what we actually use */
2109         oa->o_valid = OBD_MD_FLID;
2110         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2111
2112         f_dput(dentry);
2113         RETURN(rc);
2114 }
2115
2116 int filter_setattr_internal(struct obd_export *exp, struct dentry *dentry,
2117                             struct obdo *oa, struct obd_trans_info *oti)
2118 {
2119         struct filter_obd *filter;
2120         struct iattr iattr;
2121         void *handle;
2122         int rc, err;
2123         ENTRY;
2124
2125         LASSERT(dentry != NULL);
2126         LASSERT(!IS_ERR(dentry));
2127         LASSERT(dentry->d_inode != NULL);
2128         
2129         filter = &exp->exp_obd->u.filter;
2130         iattr_from_obdo(&iattr, oa, oa->o_valid);
2131
2132         if (iattr.ia_valid & ATTR_SIZE)
2133                 down(&dentry->d_inode->i_sem);
2134         handle = fsfilt_start(exp->exp_obd, dentry->d_inode,
2135                               FSFILT_OP_SETATTR, oti);
2136         if (IS_ERR(handle))
2137                 GOTO(out_unlock, rc = PTR_ERR(handle));
2138
2139         /* XXX this could be a rwsem instead, if filter_preprw played along */
2140         if (iattr.ia_valid & ATTR_ATTR_FLAG)
2141                 rc = fsfilt_iocontrol(exp->exp_obd, dentry->d_inode,
2142                                       NULL, EXT3_IOC_SETFLAGS,
2143                                       (long)&iattr.ia_attr_flags);
2144         else
2145                 rc = fsfilt_setattr(exp->exp_obd, dentry, handle,
2146                                     &iattr, 1);
2147         
2148         rc = filter_finish_transno(exp, oti, rc);
2149         
2150         err = fsfilt_commit(exp->exp_obd, filter->fo_sb,
2151                             dentry->d_inode, handle,
2152                             exp->exp_sync);
2153         if (err) {
2154                 CERROR("error on commit, err = %d\n", err);
2155                 if (!rc)
2156                         rc = err;
2157         }
2158         EXIT;
2159 out_unlock:
2160         if (iattr.ia_valid & ATTR_SIZE)
2161                 up(&dentry->d_inode->i_sem);
2162         return rc;
2163 }
2164
2165 /* this is called from filter_truncate() until we have filter_punch() */
2166 int filter_setattr(struct obd_export *exp, struct obdo *oa,
2167                    struct lov_stripe_md *md, struct obd_trans_info *oti)
2168 {
2169         struct ldlm_res_id res_id = { .name = { oa->o_id, 0, oa->o_gr, 0 } };
2170         struct ldlm_valblock_ops *ns_lvbo;
2171         struct lvfs_run_ctxt saved;
2172         struct filter_obd *filter;
2173         struct ldlm_resource *res;
2174         struct dentry *dentry;
2175         /*obd_uid uid;*/
2176         /*obd_gid gid;*/
2177         int rc;
2178         ENTRY;
2179
2180         LASSERT(oti != NULL);
2181
2182         filter = &exp->exp_obd->u.filter;
2183         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2184         /* pass nid through */
2185         current->user->nid = oti->oti_nid;
2186         
2187         /* make sure that object is allocated. */
2188         dentry = filter_crow_object(exp->exp_obd, oa);
2189         if (IS_ERR(dentry))
2190                 GOTO(out_pop, rc = PTR_ERR(dentry));
2191
2192         lock_kernel();
2193
2194         /* setting objects attributes (including owner/group) */
2195         rc = filter_setattr_internal(exp, dentry, oa, oti);
2196         if (rc)
2197                 GOTO(out_unlock, rc);
2198
2199         res = ldlm_resource_get(exp->exp_obd->obd_namespace, NULL,
2200                                 res_id, LDLM_EXTENT, 0);
2201         
2202         if (res != NULL) {
2203                 ns_lvbo = res->lr_namespace->ns_lvbo;
2204                 if (ns_lvbo && ns_lvbo->lvbo_update)
2205                         rc = ns_lvbo->lvbo_update(res, NULL, 0, 0);
2206                 ldlm_resource_putref(res);
2207         }
2208         
2209         oa->o_valid = OBD_MD_FLID;
2210         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2211
2212         EXIT;
2213 out_unlock:
2214         unlock_kernel();
2215         f_dput(dentry);
2216 out_pop:
2217         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2218         return rc;
2219 }
2220
2221 /* XXX identical to osc_unpackmd */
2222 static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2223                            struct lov_mds_md *lmm, int lmm_bytes)
2224 {
2225         int lsm_size;
2226         ENTRY;
2227
2228         if (lmm != NULL) {
2229                 if (lmm_bytes < sizeof (*lmm)) {
2230                         CERROR("lov_mds_md too small: %d, need %d\n",
2231                                lmm_bytes, (int)sizeof(*lmm));
2232                         RETURN(-EINVAL);
2233                 }
2234                 /* XXX LOV_MAGIC etc check? */
2235
2236                 if (lmm->lmm_object_id == cpu_to_le64(0)) {
2237                         CERROR("lov_mds_md: zero lmm_object_id\n");
2238                         RETURN(-EINVAL);
2239                 }
2240         }
2241
2242         lsm_size = lov_stripe_md_size(1);
2243         if (lsmp == NULL)
2244                 RETURN(lsm_size);
2245
2246         if (*lsmp != NULL && lmm == NULL) {
2247                 OBD_FREE(*lsmp, lsm_size);
2248                 *lsmp = NULL;
2249                 RETURN(0);
2250         }
2251
2252         if (*lsmp == NULL) {
2253                 OBD_ALLOC(*lsmp, lsm_size);
2254                 if (*lsmp == NULL)
2255                         RETURN(-ENOMEM);
2256
2257                 loi_init((*lsmp)->lsm_oinfo);
2258         }
2259
2260         if (lmm != NULL) {
2261                 /* XXX zero *lsmp? */
2262                 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
2263                 LASSERT((*lsmp)->lsm_object_id);
2264         }
2265
2266         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
2267
2268         RETURN(lsm_size);
2269 }
2270
2271 static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2272                          unsigned long max_age)
2273 {
2274         struct filter_obd *filter = &obd->u.filter;
2275         int blockbits = filter->fo_sb->s_blocksize_bits;
2276         int rc;
2277         ENTRY;
2278
2279         /* at least try to account for cached pages.  its still racey and
2280          * might be under-reporting if clients haven't announced their
2281          * caches with brw recently */
2282         spin_lock(&obd->obd_osfs_lock);
2283         rc = fsfilt_statfs(obd, filter->fo_sb, max_age);
2284         memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
2285         spin_unlock(&obd->obd_osfs_lock);
2286
2287         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
2288                " pending "LPU64" free "LPU64" avail "LPU64"\n",
2289                filter->fo_tot_dirty, filter->fo_tot_granted,
2290                filter->fo_tot_pending,
2291                osfs->os_bfree << blockbits, osfs->os_bavail << blockbits);
2292
2293         filter_grant_sanity_check(obd, __FUNCTION__);
2294
2295         osfs->os_bavail -= min(osfs->os_bavail,
2296                                (filter->fo_tot_dirty + filter->fo_tot_pending +
2297                                 osfs->os_bsize -1) >> blockbits);
2298
2299         RETURN(rc);
2300 }
2301
2302 int filter_create_object(struct obd_device *obd, struct obdo *oa)
2303 {
2304         struct dentry *dparent = NULL;
2305         struct dentry *dchild = NULL;
2306         struct lvfs_ucred uc = {0,};
2307         struct lvfs_run_ctxt saved;
2308         struct filter_obd *filter;
2309         int cleanup_phase = 0;
2310         int err = 0, rc = 0;
2311         void *handle = NULL;
2312         void *lock = NULL;
2313         ENTRY;
2314
2315         filter = &obd->u.filter;
2316
2317         down(&filter->fo_create_locks[oa->o_gr]);
2318
2319         if (test_bit(oa->o_gr, &filter->fo_destroys_in_progress)) {
2320                 CWARN("%s: create aborted by destroy\n",
2321                       obd->obd_name);
2322                 GOTO(out, rc = -EALREADY);
2323         }
2324
2325         CDEBUG(D_INFO, "create objid "LPU64"\n", oa->o_id);
2326
2327         dparent = filter_parent_lock(obd, oa->o_gr, oa->o_id, &lock);
2328         if (IS_ERR(dparent))
2329                 GOTO(cleanup, rc = PTR_ERR(dparent));
2330         cleanup_phase = 1;
2331
2332         dchild = filter_id2dentry(obd, dparent, oa->o_gr, oa->o_id);
2333         if (IS_ERR(dchild))
2334                 GOTO(cleanup, rc = PTR_ERR(dchild));
2335         cleanup_phase = 2;
2336
2337         if (dchild->d_inode != NULL)
2338                 GOTO(cleanup, rc = 0);
2339
2340         handle = fsfilt_start_log(obd, dparent->d_inode,
2341                                   FSFILT_OP_CREATE, NULL, 1);
2342         if (IS_ERR(handle))
2343                 GOTO(cleanup, rc = PTR_ERR(handle));
2344         cleanup_phase = 3;
2345
2346         uc.luc_uid = oa->o_uid;
2347         uc.luc_gid = oa->o_gid;
2348         uc.luc_fsuid = oa->o_uid;
2349         uc.luc_fsgid = oa->o_gid;
2350         uc.luc_cap = current->cap_effective;
2351         cap_raise(uc.luc_cap, CAP_SYS_RESOURCE);
2352
2353         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2354         rc = ll_vfs_create(dparent->d_inode, dchild, S_IFREG, NULL);
2355         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2356
2357         if (rc) {
2358                 CERROR("create failed rc = %d\n", rc);
2359                 GOTO(cleanup, rc);
2360         }
2361
2362         /* nobody else is touching this newly created object */
2363         LASSERT(dchild->d_inode);
2364         if (oa->o_valid & OBD_MD_FLIFID) {
2365                 rc = fsfilt_set_md(obd, dchild->d_inode, handle, obdo_id(oa),
2366                                    sizeof(struct lustre_id), EA_SID);
2367                 if (rc) {
2368                         CERROR("store fid in object failed! rc:%d\n", rc);
2369                         GOTO(cleanup, rc);
2370                 }
2371         } else {
2372                 CWARN("create object without lustre id!\n");
2373                 portals_debug_dumpstack(NULL);
2374         }
2375
2376         fsfilt_set_fs_flags(obd, dparent->d_inode, SM_DO_REC);
2377
2378         /* save last created object id */
2379         filter_save_last_id(filter, oa->o_gr, oa->o_id);
2380
2381         rc = filter_update_last_objid(obd, oa->o_gr, 0);
2382         if (rc) {
2383                 CERROR("unable to write lastobjid, but "
2384                        "orphans were deleted, err = %d\n",
2385                        rc);
2386                 rc = 0;
2387         }
2388 cleanup:
2389         switch(cleanup_phase) {
2390         case 3:
2391                 err = fsfilt_commit(obd, filter->fo_sb,
2392                                     dparent->d_inode, handle, 0);
2393                 if (err) {
2394                         CERROR("error on commit, err = %d\n", err);
2395                         if (!rc)
2396                                 rc = err;
2397                 }
2398         case 2:
2399                 f_dput(dchild);
2400         case 1:
2401                 filter_parent_unlock(dparent, lock);
2402         case 0:
2403                 break;
2404         }
2405
2406         if (rc)
2407                 GOTO(out, rc);
2408
2409 out:
2410         up(&filter->fo_create_locks[oa->o_gr]);
2411         RETURN(rc);
2412 }
2413
2414 struct dentry *
2415 filter_crow_object(struct obd_device *obd, struct obdo *oa)
2416 {
2417         struct dentry *dentry;
2418         /* obd_uid uid; */
2419         /* obd_gid gid; */
2420         int rc = 0;
2421         ENTRY;
2422
2423         /* check if object is already allocated */
2424         dentry = filter_id2dentry(obd, NULL, oa->o_gr, oa->o_id);
2425         if (IS_ERR(dentry))
2426                 RETURN(dentry);
2427
2428         if (dentry->d_inode)
2429                 RETURN(dentry);
2430
2431         f_dput(dentry);
2432         CDEBUG(D_INODE, "OSS object "LPU64"/"LPU64
2433                " does not exists - allocate it now\n",
2434                oa->o_id, oa->o_gr);
2435         /*
2436         uid = oa->o_valid & OBD_MD_FLUID ? oa->o_uid : 0;
2437         gid = oa->o_valid & OBD_MD_FLGID ? oa->o_gid : 0;
2438         */
2439         rc = filter_create_object(obd, oa);
2440         if (rc) {
2441                 CERROR("cannot create OSS object "LPU64"/"LPU64
2442                        ", err = %d\n", oa->o_id, oa->o_gr, rc);
2443                 RETURN(ERR_PTR(rc));
2444         }
2445
2446         /* lookup for just created object and return it to caller */
2447         dentry = filter_id2dentry(obd, NULL, oa->o_gr, oa->o_id);
2448         if (IS_ERR(dentry))
2449                 RETURN(dentry);
2450                 
2451         if (dentry->d_inode == NULL) {
2452                 f_dput(dentry);
2453                 dentry = ERR_PTR(-ENOENT);
2454                 CERROR("cannot find just created OSS object "
2455                        LPU64"/"LPU64" err = %d\n", oa->o_id,
2456                        oa->o_gr, (int)PTR_ERR(dentry));
2457                 RETURN(dentry);
2458         }
2459         RETURN(dentry);
2460 }
2461
2462 static int
2463 filter_clear_orphans(struct obd_export *exp, struct obdo *oa)
2464 {
2465         struct obd_device *obd = NULL;
2466         struct filter_obd *filter;
2467         struct obdo *doa = NULL;
2468         int rc = 0, orphans;
2469         __u64 last, id;
2470         ENTRY;
2471         
2472         LASSERT(oa);
2473         LASSERT(oa->o_gr != 0);
2474         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2475
2476         obd = exp->exp_obd;
2477         filter = &obd->u.filter;
2478
2479         last = filter_last_id(filter, oa->o_gr);
2480         orphans = last - oa->o_id;
2481         
2482         if (orphans <= 0)
2483                 RETURN(0);
2484                 
2485         doa = obdo_alloc();
2486         if (doa == NULL)
2487                 RETURN(-ENOMEM);
2488
2489         doa->o_gr = oa->o_gr;
2490         doa->o_mode = S_IFREG;
2491         doa->o_valid = oa->o_valid & (OBD_MD_FLGROUP | OBD_MD_FLID);
2492
2493         set_bit(doa->o_gr, &filter->fo_destroys_in_progress);
2494         down(&filter->fo_create_locks[doa->o_gr]);
2495         if (!test_bit(doa->o_gr, &filter->fo_destroys_in_progress)) {
2496                 CERROR("%s:["LPU64"] destroy_in_progress already cleared\n",
2497                        exp->exp_obd->obd_name, doa->o_gr);
2498                 up(&filter->fo_create_locks[doa->o_gr]);
2499                 GOTO(out_free_doa, 0);
2500         }
2501
2502         CDEBUG(D_ERROR, "%s:["LPU64"] deleting orphan objects from "LPU64" to "
2503               LPU64"\n", exp->exp_obd->obd_name, doa->o_gr,
2504               oa->o_id + 1, last);
2505         
2506         for (id = oa->o_id + 1; id <= last; id++) {
2507                 doa->o_id = id;
2508                 filter_destroy(exp, doa, NULL, NULL);
2509         }
2510
2511         CDEBUG(D_ERROR, "%s:["LPU64"] after destroy: set last_objids = "
2512                LPU64"\n", exp->exp_obd->obd_name, doa->o_gr, last + 1);
2513
2514         /* return next free id to be used as a new start of sequence -bzzz */
2515         oa->o_id = last + 1;
2516
2517         filter_set_last_id(filter, oa->o_gr, oa->o_id);
2518         clear_bit(doa->o_gr, &filter->fo_destroys_in_progress);
2519         up(&filter->fo_create_locks[oa->o_gr]);
2520
2521         EXIT;
2522 out_free_doa:
2523         obdo_free(doa);
2524         return rc;
2525 }
2526
2527 /*
2528  * by now this function is only needed as entry point for deleting orphanes on
2529  * OSS as objects are created on first write attempt. --umka
2530  */
2531 static int
2532 filter_create(struct obd_export *exp, struct obdo *oa, void *acl,
2533               int acl_size, struct lov_stripe_md **ea,
2534               struct obd_trans_info *oti)
2535 {
2536         struct filter_export_data *fed;
2537         struct obd_device *obd = NULL;
2538         int group = oa->o_gr, rc = 0;
2539         struct lvfs_run_ctxt saved;
2540         struct filter_obd *filter;
2541         char str[PTL_NALFMT_SIZE];
2542         ENTRY;
2543
2544         LASSERT(acl == NULL && acl_size == 0);
2545
2546         if (!(oa->o_valid & OBD_MD_FLGROUP) || group == 0) {
2547                 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
2548                                 exp->exp_connection->c_peer.peer_id.nid, str);
2549                 CERROR("!!! nid "LPX64"/%s sent invalid object group %d\n",
2550                        exp->exp_connection->c_peer.peer_id.nid, str, group);
2551                 RETURN(-EINVAL);
2552         }
2553
2554         obd = exp->exp_obd;
2555         fed = &exp->exp_filter_data;
2556         filter = &obd->u.filter;
2557
2558         if (fed->fed_group != group) {
2559                 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
2560                                 exp->exp_connection->c_peer.peer_id.nid, str);
2561                 CERROR("!!! this export (nid "LPX64"/%s) used object group %d "
2562                        "earlier; now it's trying to use group %d!  This could "
2563                        "be a bug in the MDS.  Tell CFS.\n",
2564                        exp->exp_connection->c_peer.peer_id.nid, str,
2565                        fed->fed_group, group);
2566                 RETURN(-ENOTUNIQ);
2567         }
2568
2569         CDEBUG(D_INFO, "filter_create(od->o_gr=%d,od->o_id="LPU64")\n",
2570                group, oa->o_id);
2571
2572         obd = exp->exp_obd;
2573         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2574
2575         LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
2576                 (oa->o_flags == OBD_FL_DELORPHAN));
2577                 
2578         rc = filter_clear_orphans(exp, oa);
2579         if (rc) {
2580                 CERROR("cannot clear orphanes starting from "
2581                        LPU64", err = %d\n", oa->o_id, rc);
2582         } else {
2583                 rc = filter_update_last_objid(obd, group, 0);
2584                 if (rc) {
2585                         CERROR("unable to write lastobjid, but "
2586                                "orphans were deleted, err = %d\n",
2587                                rc);
2588                 }
2589         }
2590         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2591         
2592         RETURN(0);
2593 }
2594
2595 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
2596                           struct lov_stripe_md *ea, struct obd_trans_info *oti)
2597 {
2598         int rc, rc2, cleanup_phase = 0, have_prepared = 0;
2599         struct dentry *dchild = NULL, *dparent = NULL;
2600         struct llog_cookie *fcc = NULL;
2601         struct lvfs_run_ctxt saved;
2602         struct filter_obd *filter;
2603         struct obd_device *obd;
2604         void *handle = NULL;
2605         void *lock = NULL;
2606         struct iattr iattr;
2607         ENTRY;
2608
2609         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2610
2611         obd = exp->exp_obd;
2612         filter = &obd->u.filter;
2613
2614         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2615
2616  acquire_locks:
2617         dparent = filter_parent_lock(obd, oa->o_gr, oa->o_id, &lock);
2618         if (IS_ERR(dparent))
2619                 GOTO(cleanup, rc = PTR_ERR(dparent));
2620         cleanup_phase = 1;
2621
2622         dchild = filter_id2dentry(obd, dparent, oa->o_gr, oa->o_id);
2623         if (IS_ERR(dchild))
2624                 GOTO(cleanup, rc = PTR_ERR(dchild));
2625         cleanup_phase = 2;
2626
2627         if (dchild->d_inode == NULL) {
2628                 CDEBUG(D_INODE, "destroying non-existent object "LPU64"\n",
2629                        oa->o_id);
2630                 if (have_prepared)
2631                         GOTO(cleanup, rc = -ENOENT);
2632         }
2633
2634         if (!have_prepared) {
2635                 /* If we're really going to destroy the object, get ready
2636                  * by getting the clients to discard their cached data.
2637                  *
2638                  * We have to drop the parent lock, because
2639                  * filter_prepare_destroy will acquire a PW on the object, and
2640                  * we don't want to deadlock with an incoming write to the
2641                  * object, which has the extent PW and then wants to get the
2642                  * parent dentry to do the lookup.
2643                  *
2644                  * We dput the child because it's not worth the extra
2645                  * complication of condition the above code to skip it on the
2646                  * second time through. */
2647                 f_dput(dchild);
2648                 filter_parent_unlock(dparent, lock);
2649
2650                 filter_prepare_destroy(obd, oa->o_id, oa->o_gr);
2651                 have_prepared = 1;
2652                 goto acquire_locks;
2653         }
2654
2655         /* Our MDC connection is established by the MDS to us */
2656         if (oa->o_valid & OBD_MD_FLCOOKIE) {
2657                 OBD_ALLOC(fcc, sizeof(*fcc));
2658                 if (fcc != NULL)
2659                         memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
2660         }
2661
2662         /* we're gonna truncate it first in order to avoid possible
2663          * deadlock:
2664          *      P1                      P2
2665          * open trasaction      open transaction
2666          * down(i_zombie)       down(i_zombie)
2667          *                      restart transaction
2668          * (see BUG 4180) -bzzz
2669          */
2670         down(&dchild->d_inode->i_sem);
2671         handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_SETATTR,NULL,1);
2672         if (IS_ERR(handle)) {
2673                 up(&dchild->d_inode->i_sem);
2674                 GOTO(cleanup, rc = PTR_ERR(handle));
2675         }
2676
2677         iattr.ia_valid = ATTR_SIZE;
2678         iattr.ia_size = 0;
2679         rc = fsfilt_setattr(obd, dchild, handle, &iattr, 1);
2680
2681         rc2 = fsfilt_commit(obd, filter->fo_sb, dparent->d_inode, handle, 0);
2682         up(&dchild->d_inode->i_sem);
2683         if (rc)
2684                 GOTO(cleanup, rc);
2685         if (rc2)
2686                 GOTO(cleanup, rc = rc2);
2687
2688         handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_UNLINK,oti,1);
2689         if (IS_ERR(handle))
2690                 GOTO(cleanup, rc = PTR_ERR(handle));
2691
2692         cleanup_phase = 3;
2693
2694         rc = filter_destroy_internal(obd, oa->o_id, dparent, dchild);
2695
2696 cleanup:
2697         switch(cleanup_phase) {
2698         case 3:
2699                 if (fcc != NULL) {
2700                         fsfilt_add_journal_cb(obd, filter->fo_sb, 0,
2701                                               oti ? oti->oti_handle : handle,
2702                                               filter_cancel_cookies_cb, fcc);
2703                 }
2704                 rc = filter_finish_transno(exp, oti, rc);
2705                 rc2 = fsfilt_commit(obd, filter->fo_sb, dparent->d_inode, 
2706                                     handle, exp->exp_sync);
2707                 if (rc2) {
2708                         CERROR("error on commit, err = %d\n", rc2);
2709                         if (!rc)
2710                                 rc = rc2;
2711                 }
2712         case 2:
2713                 f_dput(dchild);
2714         case 1:
2715                 filter_parent_unlock(dparent, lock);
2716         case 0:
2717                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2718                 break;
2719         default:
2720                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2721                 LBUG();
2722         }
2723
2724         RETURN(rc);
2725 }
2726
2727 /* NB start and end are used for punch, but not truncate */
2728 static int filter_truncate(struct obd_export *exp, struct obdo *oa,
2729                            struct lov_stripe_md *lsm, obd_off start,
2730                            obd_off end, struct obd_trans_info *oti,
2731                            struct lustre_capa *capa)
2732 {
2733         int error;
2734         ENTRY;
2735
2736         if (end != OBD_OBJECT_EOF)
2737                 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
2738                        end);
2739
2740         CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = "LPU64", "
2741                "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
2742         error = filter_verify_capa(OBD_BRW_WRITE, exp, capa);
2743         if (error)
2744                 RETURN(error);
2745
2746         oa->o_size = start;
2747         error = filter_setattr(exp, oa, NULL, oti);
2748         RETURN(error);
2749 }
2750
2751 static int filter_sync(struct obd_export *exp, struct obdo *oa,
2752                        struct lov_stripe_md *lsm, obd_off start, obd_off end)
2753 {
2754         struct obd_device *obd = exp->exp_obd;
2755         struct lvfs_run_ctxt saved;
2756         struct filter_obd *filter;
2757         struct dentry *dentry;
2758         int rc, rc2;
2759         ENTRY;
2760
2761         filter = &obd->u.filter;
2762
2763         /* an objid of zero is taken to mean "sync whole filesystem" */
2764         if (!oa || !(oa->o_valid & OBD_MD_FLID)) {
2765                 rc = fsfilt_sync(obd, filter->fo_sb);
2766                 /* flush any remaining cancel messages out to the target */
2767                 filter_sync_llogs(obd, NULL);
2768                 RETURN(rc);
2769         }
2770
2771         dentry = filter_oa2dentry(obd, oa);
2772         if (IS_ERR(dentry))
2773                 RETURN(PTR_ERR(dentry));
2774
2775         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2776
2777         down(&dentry->d_inode->i_sem);
2778         rc = filemap_fdatawrite(dentry->d_inode->i_mapping);
2779         if (rc == 0) {
2780                 /* just any file to grab fsync method - "file" arg unused */
2781                 struct file *file = filter->fo_rcvd_filp;
2782
2783                 if (file->f_op && file->f_op->fsync)
2784                         rc = file->f_op->fsync(NULL, dentry, 1);
2785
2786                 rc2 = filemap_fdatawait(dentry->d_inode->i_mapping);
2787                 if (!rc)
2788                         rc = rc2;
2789         }
2790         up(&dentry->d_inode->i_sem);
2791
2792         oa->o_valid = OBD_MD_FLID;
2793         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2794
2795         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2796
2797         f_dput(dentry);
2798         RETURN(rc);
2799 }
2800
2801 static int filter_set_info(struct obd_export *exp, __u32 keylen,
2802                            void *key, __u32 vallen, void *val)
2803 {
2804         struct obd_device *obd;
2805         int rc = -EINVAL;
2806         ENTRY;
2807
2808         obd = class_exp2obd(exp);
2809         if (obd == NULL) {
2810                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2811                        exp->exp_handle.h_cookie);
2812                 RETURN(-EINVAL);
2813         }
2814
2815         if (keylen == 8 && memcmp(key, "auditlog", 8) == 0) {
2816                 rc = fsfilt_set_info(obd, obd->u.filter.fo_sb, NULL,
2817                                      8, "auditlog", vallen, val);
2818                 RETURN(rc);
2819         } else if (keylen == 5 && strcmp(key, "audit") == 0) {
2820                 /* set audit for whole FS on OSS */
2821                 struct audit_attr_msg * msg = val;
2822
2823                 rc = fsfilt_set_info(obd, obd->u.filter.fo_sb, NULL,
2824                                      5, "audit", sizeof(msg->attr), &msg->attr);
2825                 RETURN(rc);
2826         } else if (keylen == 9 && strcmp(key, "audit_obj") == 0) {
2827                 struct obdo * oa = val;
2828                 struct dentry * dentry;
2829                 __u64 mask = oa->o_fid;
2830                 
2831                 dentry = filter_crow_object(obd, oa);
2832                 if (IS_ERR(dentry))
2833                         RETURN(PTR_ERR(dentry));
2834                 
2835                 rc = fsfilt_set_info(obd, obd->u.filter.fo_sb, dentry->d_inode,
2836                                      5, "audit", sizeof(mask), &mask);
2837
2838                 f_dput(dentry);
2839
2840                 RETURN(rc);
2841         } else if (keylen == 8 && memcmp(key, "capa_key", 8) == 0) {
2842                 struct lustre_capa_key *lkey = val;
2843                 rc = filter_update_capa_key(obd, lkey);
2844                 RETURN(rc);
2845         }
2846
2847         if (rc)
2848                 CDEBUG(D_IOCTL, "invalid key '%s'\n", (char *)key);
2849         
2850         RETURN(rc);
2851 }
2852
2853 static int filter_get_info(struct obd_export *exp, __u32 keylen,
2854                            void *key, __u32 *vallen, void *val)
2855 {
2856         struct filter_export_data *fed = &exp->exp_filter_data;
2857         struct obd_device *obd;
2858         ENTRY;
2859
2860         obd = class_exp2obd(exp);
2861         if (obd == NULL) {
2862                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2863                        exp->exp_handle.h_cookie);
2864                 RETURN(-EINVAL);
2865         }
2866
2867         if (keylen == strlen("blocksize") &&
2868             memcmp(key, "blocksize", keylen) == 0) {
2869                 __u32 *blocksize = val;
2870                 *vallen = sizeof(*blocksize);
2871                 *blocksize = obd->u.filter.fo_sb->s_blocksize;
2872                 RETURN(0);
2873         }
2874
2875         if (keylen == strlen("blocksize_bits") &&
2876             memcmp(key, "blocksize_bits", keylen) == 0) {
2877                 __u32 *blocksize_bits = val;
2878                 *vallen = sizeof(*blocksize_bits);
2879                 *blocksize_bits = obd->u.filter.fo_sb->s_blocksize_bits;
2880                 RETURN(0);
2881         }
2882
2883         if (keylen >= strlen("last_id") && memcmp(key, "last_id", 7) == 0) {
2884                 obd_id *last_id = val;
2885                 *last_id = filter_last_id(&obd->u.filter, fed->fed_group);
2886                 RETURN(0);
2887         }
2888         if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
2889                 /*Get log_context handle*/
2890                 unsigned long *llh_handle = val;
2891                 *vallen = sizeof(unsigned long);
2892                 *llh_handle = (unsigned long)llog_get_context(&obd->obd_llogs,
2893                                                        LLOG_REINT_ORIG_CTXT);
2894                 RETURN(0);
2895         }
2896         if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
2897                 unsigned long *sb = val;
2898                 *vallen = sizeof(unsigned long);
2899                 *sb = (unsigned long)obd->u.filter.fo_sb;
2900                 RETURN(0);
2901         }
2902
2903         CDEBUG(D_IOCTL, "invalid key\n");
2904         RETURN(-EINVAL);
2905 }
2906
2907 struct obd_llogs *filter_grab_llog_for_group(struct obd_device *obd, int group,
2908                                              struct obd_export *export)
2909 {
2910         struct filter_group_llog *fglog, *nlog;
2911         char name[32] = "CATLIST";
2912         struct filter_obd *filter;
2913         struct llog_ctxt *ctxt;
2914         struct list_head *cur;
2915         int rc;
2916
2917         filter = &obd->u.filter;
2918
2919         spin_lock(&filter->fo_llog_list_lock);
2920         list_for_each(cur, &filter->fo_llog_list) {
2921                 fglog = list_entry(cur, struct filter_group_llog, list);
2922                 if (fglog->group == group) {
2923                         if (!(fglog->exp == NULL || fglog->exp == export || export == NULL))
2924                                 CWARN("%s: export for group %d changes: 0x%p -> 0x%p\n",
2925                                       obd->obd_name, group, fglog->exp, export);
2926                         spin_unlock(&filter->fo_llog_list_lock);
2927                         goto init;
2928                 }
2929         }
2930         spin_unlock(&filter->fo_llog_list_lock);
2931
2932         if (export == NULL)
2933                 RETURN(NULL);
2934
2935         OBD_ALLOC(fglog, sizeof(*fglog));
2936         if (fglog == NULL)
2937                 RETURN(NULL);
2938         fglog->group = group;
2939
2940         OBD_ALLOC(fglog->llogs, sizeof(struct obd_llogs));
2941         if (fglog->llogs == NULL) {
2942                 OBD_FREE(fglog, sizeof(*fglog));
2943                 RETURN(NULL);
2944         }
2945
2946         spin_lock(&filter->fo_llog_list_lock);
2947         list_for_each(cur, &filter->fo_llog_list) {
2948                 nlog = list_entry(cur, struct filter_group_llog, list);
2949                 LASSERT(nlog->group != group);
2950         }
2951         list_add(&fglog->list, &filter->fo_llog_list);
2952         spin_unlock(&filter->fo_llog_list_lock);
2953
2954         rc = obd_llog_cat_initialize(obd, fglog->llogs, 1, name);
2955         if (rc) {
2956                 OBD_FREE(fglog->llogs, sizeof(*(fglog->llogs)));
2957                 OBD_FREE(fglog, sizeof(*fglog));
2958                 RETURN(NULL);
2959         }
2960
2961 init:
2962         if (export) {
2963                 fglog->exp = export;
2964                 ctxt = llog_get_context(fglog->llogs, LLOG_UNLINK_REPL_CTXT);
2965                 LASSERT(ctxt != NULL);
2966
2967                 llog_receptor_accept(ctxt, export->exp_imp_reverse);
2968         }
2969         CDEBUG(D_OTHER, "%s: new llog 0x%p for group %u\n",
2970                obd->obd_name, fglog->llogs, group);
2971
2972         RETURN(fglog->llogs);
2973 }
2974
2975 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
2976                      int len, void *karg, void *uarg)
2977 {
2978         struct obd_device *obd = exp->exp_obd;
2979         struct obd_ioctl_data *data = karg;
2980         int rc = 0;
2981
2982         switch (cmd) {
2983         case OBD_IOC_ABORT_RECOVERY:
2984                 target_stop_recovery_thread(obd);
2985                 RETURN(0);
2986
2987         case OBD_IOC_SET_READONLY: {
2988                 void *handle;
2989                 struct super_block *sb = obd->u.filter.fo_sb;
2990                 struct inode *inode = sb->s_root->d_inode;
2991                 BDEVNAME_DECLARE_STORAGE(tmp);
2992                 CERROR("setting device %s read-only\n",
2993                        ll_bdevname(sb, tmp));
2994
2995                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
2996                 LASSERT(handle);
2997                 (void)fsfilt_commit(obd, sb, inode, handle, 1);
2998
2999                 ll_set_rdonly(ll_sbdev(obd->u.filter.fo_sb));
3000                 RETURN(0);
3001         }
3002
3003         case OBD_IOC_CATLOGLIST: {
3004                 rc = llog_catalog_list(obd, 1, data);
3005                 RETURN(rc);
3006         }
3007         case OBD_IOC_LLOG_CANCEL:
3008         case OBD_IOC_LLOG_REMOVE:
3009         case OBD_IOC_LLOG_INFO:
3010         case OBD_IOC_LLOG_PRINT: {
3011                 /* FIXME to be finished */
3012                 RETURN(-EOPNOTSUPP);
3013 /*
3014                 struct llog_ctxt *ctxt = NULL;
3015
3016                 push_ctxt(&saved, &ctxt->loc_ctxt, NULL);
3017                 rc = llog_ioctl(ctxt, cmd, data);
3018                 pop_ctxt(&saved, &ctxt->loc_ctxt, NULL);
3019
3020                 RETURN(rc);
3021 */
3022         }
3023
3024
3025         default:
3026                 RETURN(-EINVAL);
3027         }
3028         RETURN(0);
3029 }
3030
3031 static struct llog_operations filter_unlink_repl_logops;
3032 static struct llog_operations filter_size_orig_logops = {
3033         lop_setup: llog_obd_origin_setup,
3034         lop_cleanup: llog_catalog_cleanup,
3035         lop_add: llog_catalog_add,
3036 };
3037
3038 static int filter_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
3039                             struct obd_device *tgt, int count,
3040                             struct llog_catid *catid)
3041 {
3042         struct llog_ctxt *ctxt;
3043         int rc;
3044         ENTRY;
3045
3046         filter_unlink_repl_logops = llog_client_ops;
3047         filter_unlink_repl_logops.lop_cancel = llog_obd_repl_cancel;
3048         filter_unlink_repl_logops.lop_connect = llog_repl_connect;
3049         filter_unlink_repl_logops.lop_sync = llog_obd_repl_sync;
3050
3051         rc = obd_llog_setup(obd, llogs, LLOG_UNLINK_REPL_CTXT, tgt, 0, NULL,
3052                         &filter_unlink_repl_logops);
3053         if (rc)
3054                 RETURN(rc);
3055         /* FIXME - assign unlink_cb for filter's recovery */
3056         ctxt = llog_get_context(llogs, LLOG_UNLINK_REPL_CTXT);
3057         ctxt->llog_proc_cb = filter_recov_log_unlink_cb;
3058
3059         filter_size_orig_logops = llog_lvfs_ops;
3060 #if 0
3061         filter_size_orig_logops.lop_setup = llog_obd_origin_setup;
3062         filter_size_orig_logops.lop_cleanup = llog_catalog_cleanup;
3063         filter_size_orig_logops.lop_add = llog_catalog_add;
3064 #endif
3065         rc = obd_llog_setup(obd, llogs, LLOG_SIZE_ORIG_CTXT, tgt, 1, 
3066                             &catid->lci_logid, &filter_size_orig_logops);
3067         RETURN(rc);
3068 }
3069
3070 static int filter_llog_finish(struct obd_device *obd,
3071                               struct obd_llogs *llogs, int count)
3072 {
3073         int rc;
3074         ENTRY;
3075
3076         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_UNLINK_REPL_CTXT));
3077         if (rc)
3078                 RETURN(rc);
3079
3080         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_SIZE_ORIG_CTXT));
3081         RETURN(rc);
3082 }
3083
3084 static int filter_llog_connect(struct obd_export *exp,
3085                                struct llogd_conn_body *body) 
3086 {
3087         struct obd_device *obd = exp->exp_obd;
3088         struct llog_ctxt *ctxt;
3089         struct obd_llogs *llog;
3090         int rc;
3091         ENTRY;
3092
3093         CDEBUG(D_OTHER, "handle connect for %s: %u/%u/%u\n", obd->obd_name,
3094                (unsigned) body->lgdc_logid.lgl_ogr,
3095                (unsigned) body->lgdc_logid.lgl_oid,
3096                (unsigned) body->lgdc_logid.lgl_ogen);
3097         llog = filter_grab_llog_for_group(obd, body->lgdc_logid.lgl_ogr, exp);
3098         LASSERT(llog != NULL);
3099         ctxt = llog_get_context(llog, body->lgdc_ctxt_idx);
3100         rc = llog_connect(ctxt, 1, &body->lgdc_logid,
3101                           &body->lgdc_gen, NULL);
3102         if (rc != 0)
3103                 CERROR("failed to connect\n");
3104
3105         RETURN(rc);
3106 }
3107
3108 static struct dentry *filter_lvfs_id2dentry(__u64 id, __u32 gen,
3109                                             __u64 gr, void *data)
3110 {
3111         return filter_id2dentry(data, NULL, gr, id);
3112 }
3113
3114 static struct lvfs_callback_ops filter_lvfs_ops = {
3115         l_id2dentry:     filter_lvfs_id2dentry,
3116 };
3117
3118 static struct obd_ops filter_obd_ops = {
3119         .o_owner          = THIS_MODULE,
3120         .o_attach         = filter_attach,
3121         .o_detach         = filter_detach,
3122         .o_set_info       = filter_set_info,
3123         .o_get_info       = filter_get_info,
3124         .o_setup          = filter_setup,
3125         .o_precleanup     = filter_precleanup,
3126         .o_cleanup        = filter_cleanup,
3127         .o_process_config = filter_process_config,
3128         .o_connect        = filter_connect,
3129         .o_connect_post   = filter_connect_post,
3130         .o_disconnect     = filter_disconnect,
3131         .o_statfs         = filter_statfs,
3132         .o_getattr        = filter_getattr,
3133         .o_unpackmd       = filter_unpackmd,
3134         .o_create         = filter_create,
3135         .o_setattr        = filter_setattr,
3136         .o_destroy        = filter_destroy,
3137         .o_brw            = filter_brw,
3138         .o_punch          = filter_truncate,
3139         .o_sync           = filter_sync,
3140         .o_preprw         = filter_preprw,
3141         .o_commitrw       = filter_commitrw,
3142         .o_do_cow         = filter_do_cow,
3143         .o_write_extents  = filter_write_extents,
3144         .o_destroy_export = filter_destroy_export,
3145         .o_llog_init      = filter_llog_init,
3146         .o_llog_finish    = filter_llog_finish,
3147         .o_llog_connect   = filter_llog_connect,
3148         .o_iocontrol      = filter_iocontrol,
3149 };
3150
3151 static struct obd_ops filter_sanobd_ops = {
3152         .o_owner          = THIS_MODULE,
3153         .o_attach         = filter_attach,
3154         .o_detach         = filter_detach,
3155         .o_get_info       = filter_get_info,
3156         .o_setup          = filter_san_setup,
3157         .o_precleanup     = filter_precleanup,
3158         .o_cleanup        = filter_cleanup,
3159         .o_connect        = filter_connect,
3160         .o_connect_post   = filter_connect_post,
3161         .o_disconnect     = filter_disconnect,
3162         .o_statfs         = filter_statfs,
3163         .o_getattr        = filter_getattr,
3164         .o_unpackmd       = filter_unpackmd,
3165         .o_create         = filter_create,
3166         .o_setattr        = filter_setattr,
3167         .o_destroy        = filter_destroy,
3168         .o_brw            = filter_brw,
3169         .o_punch          = filter_truncate,
3170         .o_sync           = filter_sync,
3171         .o_preprw         = filter_preprw,
3172         .o_commitrw       = filter_commitrw,
3173         .o_do_cow         = filter_do_cow,
3174         .o_write_extents  = filter_write_extents,
3175         .o_san_preprw     = filter_san_preprw,
3176         .o_destroy_export = filter_destroy_export,
3177         .o_llog_init      = filter_llog_init,
3178         .o_llog_finish    = filter_llog_finish,
3179         .o_llog_connect   = filter_llog_connect,
3180         .o_iocontrol      = filter_iocontrol,
3181 };
3182
3183 static int __init obdfilter_init(void)
3184 {
3185         struct lprocfs_static_vars lvars;
3186         int size, rc;
3187
3188         printk(KERN_INFO "Lustre: Filtering OBD driver; info@clusterfs.com\n");
3189
3190         lprocfs_init_vars(filter, &lvars);
3191
3192         size = OBDFILTER_CREATED_SCRATCHPAD_ENTRIES * 
3193                 sizeof(*obdfilter_created_scratchpad);
3194         
3195         OBD_ALLOC(obdfilter_created_scratchpad, size);
3196         if (obdfilter_created_scratchpad == NULL) {
3197                 CERROR ("Can't allocate scratchpad\n");
3198                 return -ENOMEM;
3199         }
3200
3201         rc = class_register_type(&filter_obd_ops, NULL, lvars.module_vars,
3202                                  OBD_FILTER_DEVICENAME);
3203         if (rc) {
3204                 OBD_FREE(obdfilter_created_scratchpad, size);
3205                 return rc;
3206         }
3207
3208         rc = class_register_type(&filter_sanobd_ops, NULL, lvars.module_vars,
3209                                  OBD_FILTER_SAN_DEVICENAME);
3210         if (rc) {
3211                 class_unregister_type(OBD_FILTER_DEVICENAME);
3212                 OBD_FREE(obdfilter_created_scratchpad, size);
3213         }
3214         return rc;
3215 }
3216
3217 static void __exit obdfilter_exit(void)
3218 {
3219         class_unregister_type(OBD_FILTER_SAN_DEVICENAME);
3220         class_unregister_type(OBD_FILTER_DEVICENAME);
3221         OBD_FREE(obdfilter_created_scratchpad,
3222                  OBDFILTER_CREATED_SCRATCHPAD_ENTRIES * 
3223                  sizeof(*obdfilter_created_scratchpad));
3224 }
3225
3226 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3227 MODULE_DESCRIPTION("Lustre Filtering OBD driver");
3228 MODULE_LICENSE("GPL");
3229
3230 module_init(obdfilter_init);
3231 module_exit(obdfilter_exit);