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