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