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