Whamcloud - gitweb
b=12005
[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_mutex 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_mutex 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/sched.h>
45 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
46 # include <linux/mount.h>
47 # include <linux/buffer_head.h>
48 #endif
49
50 #include <obd_class.h>
51 #include <obd_lov.h>
52 #include <lustre_dlm.h>
53 #include <lustre_fsfilt.h>
54 #include <lprocfs_status.h>
55 #include <lustre_log.h>
56 #include <lustre_commit_confd.h>
57 #include <libcfs/list.h>
58 #include <lustre_disk.h>
59 #include <lustre_quota.h>
60 #include <linux/slab.h>
61 #include <lustre_param.h>
62
63 #include "filter_internal.h"
64
65 static struct lvfs_callback_ops filter_lvfs_ops;
66 kmem_cache_t *ll_fmd_cachep;
67
68 static void filter_commit_cb(struct obd_device *obd, __u64 transno,
69                              void *cb_data, int error)
70 {
71         obd_transno_commit_cb(obd, transno, error);
72 }
73
74 /* Assumes caller has already pushed us into the kernel context. */
75 int filter_finish_transno(struct obd_export *exp, struct obd_trans_info *oti,
76                           int rc)
77 {
78         struct filter_obd *filter = &exp->exp_obd->u.filter;
79         struct filter_export_data *fed = &exp->exp_filter_data;
80         struct filter_client_data *fcd = fed->fed_fcd;
81         __u64 last_rcvd;
82         loff_t off;
83         int err, log_pri = D_HA;
84
85         /* Propagate error code. */
86         if (rc)
87                 RETURN(rc);
88
89         if (!exp->exp_obd->obd_replayable || oti == NULL)
90                 RETURN(rc);
91
92         /* we don't allocate new transnos for replayed requests */
93         if (oti->oti_transno == 0) {
94                 spin_lock(&filter->fo_translock);
95                 last_rcvd = le64_to_cpu(filter->fo_fsd->lsd_last_transno) + 1;
96                 filter->fo_fsd->lsd_last_transno = cpu_to_le64(last_rcvd);
97                 spin_unlock(&filter->fo_translock);
98                 oti->oti_transno = last_rcvd;
99         } else {
100                 spin_lock(&filter->fo_translock);
101                 last_rcvd = oti->oti_transno;
102                 if (last_rcvd > le64_to_cpu(filter->fo_fsd->lsd_last_transno))
103                         filter->fo_fsd->lsd_last_transno =
104                                 cpu_to_le64(last_rcvd);
105                 spin_unlock(&filter->fo_translock);
106         }
107         fcd->fcd_last_rcvd = cpu_to_le64(last_rcvd);
108
109         /* could get xid from oti, if it's ever needed */
110         fcd->fcd_last_xid = 0;
111
112         off = fed->fed_lr_off;
113         if (off <= 0) {
114                 CERROR("%s: client idx %d is %lld\n", exp->exp_obd->obd_name,
115                        fed->fed_lr_idx, fed->fed_lr_off);
116                 err = -EINVAL;
117         } else {
118                 fsfilt_add_journal_cb(exp->exp_obd, last_rcvd, oti->oti_handle,
119                                       filter_commit_cb, NULL);
120                 err = fsfilt_write_record(exp->exp_obd, filter->fo_rcvd_filp,
121                                           fcd, sizeof(*fcd), &off,
122                                           exp->exp_need_sync);
123         }
124         if (err) {
125                 log_pri = D_ERROR;
126                 if (rc == 0)
127                         rc = err;
128         }
129
130         CDEBUG(log_pri, "wrote trans "LPU64" for client %s at #%d: err = %d\n",
131                last_rcvd, fcd->fcd_uuid, fed->fed_lr_idx, err);
132
133         RETURN(rc);
134 }
135
136 void f_dput(struct dentry *dentry)
137 {
138         /* Can't go inside filter_ddelete because it can block */
139         CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
140                dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
141         LASSERT(atomic_read(&dentry->d_count) > 0);
142
143         dput(dentry);
144 }
145
146 static void init_brw_stats(struct brw_stats *brw_stats)
147 {
148         int i;
149         for (i = 0; i < BRW_LAST; i++)
150                 spin_lock_init(&brw_stats->hist[i].oh_lock);
151 }
152
153 /* brw_stats are 2128, ops are 3916, ldlm are 204, so 6248 bytes per client,
154    plus the procfs overhead :( */
155 static int filter_export_stats_init(struct obd_device *obd,
156                                     struct obd_export *exp)
157 {
158         struct filter_export_data *fed = &exp->exp_filter_data;
159         struct proc_dir_entry *brw_entry;
160         int rc, num_stats;
161         ENTRY;
162
163         init_brw_stats(&fed->fed_brw_stats);
164
165         if (obd_uuid_equals(&exp->exp_client_uuid, &obd->obd_uuid))
166                 /* Self-export gets no proc entry */
167                 RETURN(0);
168         
169         rc = lprocfs_exp_setup(exp);
170         if (rc) 
171                 RETURN(rc);
172
173         /* Create a per export proc entry for brw_stats */
174         brw_entry = create_proc_entry("brw_stats", 0644, exp->exp_proc);
175         if (brw_entry == NULL)
176                RETURN(-ENOMEM);
177         brw_entry->proc_fops = &filter_per_export_stats_fops;
178         brw_entry->data = fed;
179
180         /* Create a per export proc entry for ops stats */
181         num_stats = (sizeof(*obd->obd_type->typ_ops) / sizeof(void *)) +
182                      LPROC_FILTER_LAST - 1;
183         exp->exp_ops_stats = lprocfs_alloc_stats(num_stats);
184         if (exp->exp_ops_stats == NULL)
185               RETURN(-ENOMEM);
186         lprocfs_init_ops_stats(LPROC_FILTER_LAST, exp->exp_ops_stats);
187         lprocfs_counter_init(exp->exp_ops_stats, LPROC_FILTER_READ_BYTES,
188                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
189         lprocfs_counter_init(exp->exp_ops_stats, LPROC_FILTER_WRITE_BYTES,
190                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
191         lprocfs_register_stats(exp->exp_proc, "stats", exp->exp_ops_stats);
192         RETURN(0);
193 }
194
195 /* Add client data to the FILTER.  We use a bitmap to locate a free space
196  * in the last_rcvd file if cl_idx is -1 (i.e. a new client).
197  * Otherwise, we have just read the data from the last_rcvd file and
198  * we know its offset. */
199 static int filter_client_add(struct obd_device *obd, struct obd_export *exp,
200                              int cl_idx)
201 {
202         struct filter_obd *filter = &obd->u.filter;
203         struct filter_export_data *fed = &exp->exp_filter_data;
204         unsigned long *bitmap = filter->fo_last_rcvd_slots;
205         int new_client = (cl_idx == -1);
206
207         ENTRY;
208
209         LASSERT(bitmap != NULL);
210         LASSERTF(cl_idx > -2, "%d\n", cl_idx);
211
212         /* Self-export */
213         if (strcmp(fed->fed_fcd->fcd_uuid, obd->obd_uuid.uuid) == 0) 
214                 RETURN(0);
215
216         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
217          * there's no need for extra complication here
218          */
219         if (new_client) {
220                 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
221         repeat:
222                 if (cl_idx >= LR_MAX_CLIENTS) {
223                         CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
224                                cl_idx);
225                         RETURN(-EOVERFLOW);
226                 }
227                 if (test_and_set_bit(cl_idx, bitmap)) {
228                         cl_idx = find_next_zero_bit(bitmap, LR_MAX_CLIENTS,
229                                                     cl_idx);
230                         goto repeat;
231                 }
232         } else {
233                 if (test_and_set_bit(cl_idx, bitmap)) {
234                         CERROR("FILTER client %d: bit already set in bitmap!\n",
235                                cl_idx);
236                         LBUG();
237                 }
238         }
239
240         fed->fed_lr_idx = cl_idx;
241         fed->fed_lr_off = le32_to_cpu(filter->fo_fsd->lsd_client_start) +
242                 cl_idx * le16_to_cpu(filter->fo_fsd->lsd_client_size);
243         LASSERTF(fed->fed_lr_off > 0, "fed_lr_off = %llu\n", fed->fed_lr_off);
244
245         CDEBUG(D_INFO, "client at index %d (%llu) with UUID '%s' added\n",
246                fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
247
248         if (new_client) {
249                 struct lvfs_run_ctxt saved;
250                 loff_t off = fed->fed_lr_off;
251                 void *handle;
252                 int rc;
253
254                 CDEBUG(D_INFO, "writing client fcd at idx %u (%llu) (len %u)\n",
255                        fed->fed_lr_idx,off,(unsigned int)sizeof(*fed->fed_fcd));
256
257                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
258                 /* Transaction needed to fix bug 1403 */
259                 handle = fsfilt_start(obd,
260                                       filter->fo_rcvd_filp->f_dentry->d_inode,
261                                       FSFILT_OP_SETATTR, NULL);
262                 if (IS_ERR(handle)) {
263                         rc = PTR_ERR(handle);
264                         CERROR("unable to start transaction: rc %d\n", rc);
265                 } else {
266                         rc = fsfilt_add_journal_cb(obd, 0, handle,
267                                                    target_client_add_cb, exp);
268                         if (rc == 0)
269                                 exp->exp_need_sync = 1;
270                         rc = fsfilt_write_record(obd, filter->fo_rcvd_filp,
271                                                  fed->fed_fcd,
272                                                  sizeof(*fed->fed_fcd),
273                                                  &off, rc /* sync if no cb */);
274                         fsfilt_commit(obd,
275                                       filter->fo_rcvd_filp->f_dentry->d_inode,
276                                       handle, 0);
277                 }
278                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
279
280                 if (rc) {
281                         CERROR("error writing %s client idx %u: rc %d\n",
282                                LAST_RCVD, fed->fed_lr_idx, rc);
283                         RETURN(rc);
284                 }
285         }
286         RETURN(0);
287 }
288
289 static int filter_client_free(struct obd_export *exp)
290 {
291         struct filter_export_data *fed = &exp->exp_filter_data;
292         struct filter_obd *filter = &exp->exp_obd->u.filter;
293         struct obd_device *obd = exp->exp_obd;
294         struct filter_client_data zero_fcd;
295         struct lvfs_run_ctxt saved;
296         int rc;
297         loff_t off;
298         ENTRY;
299
300         if (fed->fed_fcd == NULL)
301                 RETURN(0);
302
303         /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
304         if (strcmp(fed->fed_fcd->fcd_uuid, obd->obd_uuid.uuid ) == 0)
305                 GOTO(free, 0);
306
307         CDEBUG(D_INFO, "freeing client at idx %u, offset %lld with UUID '%s'\n",
308                fed->fed_lr_idx, off, fed->fed_fcd->fcd_uuid);
309
310         LASSERT(filter->fo_last_rcvd_slots != NULL);
311
312         off = fed->fed_lr_off;
313
314         /* Don't clear fed_lr_idx here as it is likely also unset.  At worst
315          * we leak a client slot that will be cleaned on the next recovery. */
316         if (off <= 0) {
317                 CERROR("%s: client idx %d has med_off %lld\n",
318                        obd->obd_name, fed->fed_lr_idx, off);
319                 GOTO(free, rc = -EINVAL);
320         }
321
322         /* Clear the bit _after_ zeroing out the client so we don't
323            race with filter_client_add and zero out new clients.*/
324         if (!test_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
325                 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
326                        fed->fed_lr_idx);
327                 LBUG();
328         }
329
330         if (!(exp->exp_flags & OBD_OPT_FAILOVER)) {
331                 memset(&zero_fcd, 0, sizeof zero_fcd);
332                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
333                 rc = fsfilt_write_record(obd, filter->fo_rcvd_filp, &zero_fcd,
334                                          sizeof(zero_fcd), &off,
335                                          !exp->exp_libclient);
336
337                 if (rc == 0)
338                         /* update server's transno */
339                         filter_update_server_data(obd, filter->fo_rcvd_filp,
340                                                   filter->fo_fsd,
341                                                   !exp->exp_libclient);
342                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
343
344                 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
345                        "zeroing out client %s at idx %u (%llu) in %s rc %d\n",
346                        fed->fed_fcd->fcd_uuid, fed->fed_lr_idx, fed->fed_lr_off,
347                        LAST_RCVD, rc);
348         }
349
350         if (!test_and_clear_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
351                 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
352                        fed->fed_lr_idx);
353                 LBUG();
354         }
355
356         EXIT;
357 free:
358         OBD_FREE(fed->fed_fcd, sizeof(*fed->fed_fcd));
359         fed->fed_fcd = NULL;
360
361         return 0;
362 }
363
364 /* drop fmd reference, free it if last ref. must be called with fed_lock held.*/
365 static inline void filter_fmd_put_nolock(struct filter_export_data *fed,
366                                          struct filter_mod_data *fmd)
367 {
368         LASSERT_SPIN_LOCKED(&fed->fed_lock);
369         if (--fmd->fmd_refcount == 0) {
370                 /* XXX when we have persistent reservations and the handle
371                  * is stored herein we need to drop it here. */
372                 fed->fed_mod_count--;
373                 list_del(&fmd->fmd_list);
374                 OBD_SLAB_FREE(fmd, ll_fmd_cachep, sizeof(*fmd));
375         }
376 }
377
378 /* drop fmd reference, free it if last ref */
379 void filter_fmd_put(struct obd_export *exp, struct filter_mod_data *fmd)
380 {
381         struct filter_export_data *fed;
382
383         if (fmd == NULL)
384                 return;
385
386         fed = &exp->exp_filter_data;
387         spin_lock(&fed->fed_lock);
388         filter_fmd_put_nolock(fed, fmd); /* caller reference */
389         spin_unlock(&fed->fed_lock);
390 }
391
392 /* expire entries from the end of the list if there are too many
393  * or they are too old */
394 static void filter_fmd_expire_nolock(struct filter_obd *filter,
395                                      struct filter_export_data *fed,
396                                      struct filter_mod_data *keep)
397 {
398         struct filter_mod_data *fmd, *tmp;
399
400         list_for_each_entry_safe(fmd, tmp, &fed->fed_mod_list, fmd_list) {
401                 if (fmd == keep)
402                         break;
403
404                 if (time_before(jiffies, fmd->fmd_expire) &&
405                     fed->fed_mod_count < filter->fo_fmd_max_num)
406                         break;
407
408                 list_del_init(&fmd->fmd_list);
409                 filter_fmd_put_nolock(fed, fmd); /* list reference */
410         }
411 }
412
413 void filter_fmd_expire(struct obd_export *exp)
414 {
415         spin_lock(&exp->exp_filter_data.fed_lock);
416         filter_fmd_expire_nolock(&exp->exp_obd->u.filter,
417                                  &exp->exp_filter_data, NULL);
418         spin_unlock(&exp->exp_filter_data.fed_lock);
419 }
420
421 /* find specified objid, group in export fmd list.
422  * caller must hold fed_lock and take fmd reference itself */
423 static struct filter_mod_data *filter_fmd_find_nolock(struct filter_obd *filter,
424                                                 struct filter_export_data *fed,
425                                                 obd_id objid, obd_gr group)
426 {
427         struct filter_mod_data *found = NULL, *fmd;
428
429         LASSERT_SPIN_LOCKED(&fed->fed_lock);
430
431         list_for_each_entry_reverse(fmd, &fed->fed_mod_list, fmd_list) {
432                 if (fmd->fmd_id == objid && fmd->fmd_gr == group) {
433                         found = fmd;
434                         list_del(&fmd->fmd_list);
435                         list_add_tail(&fmd->fmd_list, &fed->fed_mod_list);
436                         fmd->fmd_expire = jiffies + filter->fo_fmd_max_age;
437                         break;
438                 }
439         }
440
441         filter_fmd_expire_nolock(filter, fed, found);
442
443         return found;
444 }
445
446 /* Find fmd based on objid and group, or return NULL if not found. */
447 struct filter_mod_data *filter_fmd_find(struct obd_export *exp,
448                                         obd_id objid, obd_gr group)
449 {
450         struct filter_mod_data *fmd;
451
452         spin_lock(&exp->exp_filter_data.fed_lock);
453         fmd = filter_fmd_find_nolock(&exp->exp_obd->u.filter,
454                                      &exp->exp_filter_data, objid, group);
455         if (fmd)
456                 fmd->fmd_refcount++;    /* caller reference */
457         spin_unlock(&exp->exp_filter_data.fed_lock);
458
459         return fmd;
460 }
461
462 /* Find fmd based on objid and group, or create a new one if none is found.
463  * It is possible for this function to return NULL under memory pressure,
464  * or if objid = 0 is passed (which will only cause old entries to expire).
465  * Currently this is not fatal because any fmd state is transient and
466  * may also be freed when it gets sufficiently old. */
467 struct filter_mod_data *filter_fmd_get(struct obd_export *exp,
468                                        obd_id objid, obd_gr group)
469 {
470         struct filter_export_data *fed = &exp->exp_filter_data;
471         struct filter_mod_data *found = NULL, *fmd_new = NULL;
472
473         OBD_SLAB_ALLOC(fmd_new, ll_fmd_cachep, SLAB_NOFS, sizeof(*fmd_new));
474
475         spin_lock(&fed->fed_lock);
476         found = filter_fmd_find_nolock(&exp->exp_obd->u.filter,fed,objid,group);
477         if (fmd_new) {
478                 if (found == NULL) {
479                         list_add_tail(&fmd_new->fmd_list, &fed->fed_mod_list);
480                         fmd_new->fmd_id = objid;
481                         fmd_new->fmd_gr = group;
482                         fmd_new->fmd_refcount++;   /* list reference */
483                         found = fmd_new;
484                         fed->fed_mod_count++;
485                 } else {
486                         OBD_SLAB_FREE(fmd_new, ll_fmd_cachep, sizeof(*fmd_new));
487                 }
488         }
489         if (found) {
490                 found->fmd_refcount++;          /* caller reference */
491                 found->fmd_expire = jiffies +
492                         exp->exp_obd->u.filter.fo_fmd_max_age;
493         }
494
495         spin_unlock(&fed->fed_lock);
496
497         return found;
498 }
499
500 #ifdef DO_FMD_DROP
501 /* drop fmd list reference so it will disappear when last reference is put.
502  * This isn't so critical because it would in fact only affect the one client
503  * that is doing the unlink and at worst we have an stale entry referencing
504  * an object that should never be used again. */
505 static void filter_fmd_drop(struct obd_export *exp, obd_id objid, obd_gr group)
506 {
507         struct filter_mod_data *found = NULL;
508
509         spin_lock(&exp->exp_filter_data.fed_lock);
510         found = filter_fmd_find_nolock(&exp->exp_filter_data, objid, group);
511         if (found) {
512                 list_del_init(&found->fmd_list);
513                 filter_fmd_put_nolock(&exp->exp_filter_data, found);
514         }
515         spin_unlock(&exp->exp_filter_data.fed_lock);
516 }
517 #else
518 #define filter_fmd_drop(exp, objid, group)
519 #endif
520
521 /* remove all entries from fmd list */
522 static void filter_fmd_cleanup(struct obd_export *exp)
523 {
524         struct filter_export_data *fed = &exp->exp_filter_data;
525         struct filter_mod_data *fmd = NULL, *tmp;
526
527         spin_lock(&fed->fed_lock);
528         list_for_each_entry_safe(fmd, tmp, &fed->fed_mod_list, fmd_list) {
529                 list_del_init(&fmd->fmd_list);
530                 filter_fmd_put_nolock(fed, fmd);
531         }
532         spin_unlock(&fed->fed_lock);
533 }
534
535 static int filter_init_export(struct obd_export *exp)
536 {
537         spin_lock_init(&exp->exp_filter_data.fed_lock);
538         INIT_LIST_HEAD(&exp->exp_filter_data.fed_mod_list);
539         exp->exp_connecting = 1;
540
541         return 0;
542 }
543
544 static int filter_free_server_data(struct filter_obd *filter)
545 {
546         OBD_FREE(filter->fo_fsd, sizeof(*filter->fo_fsd));
547         filter->fo_fsd = NULL;
548         OBD_FREE(filter->fo_last_rcvd_slots, LR_MAX_CLIENTS / 8);
549         filter->fo_last_rcvd_slots = NULL;
550         return 0;
551 }
552
553 /* assumes caller is already in kernel ctxt */
554 int filter_update_server_data(struct obd_device *obd, struct file *filp,
555                               struct lr_server_data *fsd, int force_sync)
556 {
557         loff_t off = 0;
558         int rc;
559         ENTRY;
560
561         CDEBUG(D_INODE, "server uuid      : %s\n", fsd->lsd_uuid);
562         CDEBUG(D_INODE, "server last_rcvd : "LPU64"\n",
563                le64_to_cpu(fsd->lsd_last_transno));
564         CDEBUG(D_INODE, "server last_mount: "LPU64"\n",
565                le64_to_cpu(fsd->lsd_mount_count));
566
567         fsd->lsd_compat14 = fsd->lsd_last_transno;
568         rc = fsfilt_write_record(obd, filp, fsd, sizeof(*fsd), &off,force_sync);
569         if (rc)
570                 CERROR("error writing lr_server_data: rc = %d\n", rc);
571
572         RETURN(rc);
573 }
574
575 int filter_update_last_objid(struct obd_device *obd, obd_gr group,
576                              int force_sync)
577 {
578         struct filter_obd *filter = &obd->u.filter;
579         __u64 tmp;
580         loff_t off = 0;
581         int rc;
582         ENTRY;
583
584         if (filter->fo_last_objid_files[group] == NULL) {
585                 CERROR("Object group "LPU64" not fully setup; not updating "
586                        "last_objid\n", group);
587                 RETURN(-EINVAL);
588         }
589
590         CDEBUG(D_INODE, "%s: server last_objid for group "LPU64": "LPU64"\n",
591                obd->obd_name, group, filter->fo_last_objids[group]);
592
593         tmp = cpu_to_le64(filter->fo_last_objids[group]);
594         rc = fsfilt_write_record(obd, filter->fo_last_objid_files[group],
595                                  &tmp, sizeof(tmp), &off, force_sync);
596         if (rc)
597                 CERROR("error writing group "LPU64" last objid: rc = %d\n",
598                        group, rc);
599         RETURN(rc);
600 }
601
602 /* assumes caller has already in kernel ctxt */
603 static int filter_init_server_data(struct obd_device *obd, struct file * filp)
604 {
605         struct filter_obd *filter = &obd->u.filter;
606         struct lr_server_data *fsd;
607         struct filter_client_data *fcd = NULL;
608         struct inode *inode = filp->f_dentry->d_inode;
609         unsigned long last_rcvd_size = inode->i_size;
610         __u64 mount_count;
611         int cl_idx;
612         loff_t off = 0;
613         int rc;
614
615         /* ensure padding in the struct is the correct size */
616         CLASSERT (offsetof(struct lr_server_data, lsd_padding) +
617                  sizeof(fsd->lsd_padding) == LR_SERVER_SIZE);
618         CLASSERT (offsetof(struct filter_client_data, fcd_padding) +
619                  sizeof(fcd->fcd_padding) == LR_CLIENT_SIZE);
620
621         OBD_ALLOC(fsd, sizeof(*fsd));
622         if (!fsd)
623                 RETURN(-ENOMEM);
624         filter->fo_fsd = fsd;
625
626         OBD_ALLOC(filter->fo_last_rcvd_slots, LR_MAX_CLIENTS / 8);
627         if (filter->fo_last_rcvd_slots == NULL) {
628                 OBD_FREE(fsd, sizeof(*fsd));
629                 RETURN(-ENOMEM);
630         }
631
632         if (last_rcvd_size == 0) {
633                 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
634
635                 memcpy(fsd->lsd_uuid, obd->obd_uuid.uuid,sizeof(fsd->lsd_uuid));
636                 fsd->lsd_last_transno = 0;
637                 mount_count = fsd->lsd_mount_count = 0;
638                 fsd->lsd_server_size = cpu_to_le32(LR_SERVER_SIZE);
639                 fsd->lsd_client_start = cpu_to_le32(LR_CLIENT_START);
640                 fsd->lsd_client_size = cpu_to_le16(LR_CLIENT_SIZE);
641                 fsd->lsd_subdir_count = cpu_to_le16(FILTER_SUBDIR_COUNT);
642                 filter->fo_subdir_count = FILTER_SUBDIR_COUNT;
643                 fsd->lsd_feature_incompat = cpu_to_le32(OBD_INCOMPAT_OST);
644         } else {
645                 rc = fsfilt_read_record(obd, filp, fsd, sizeof(*fsd), &off);
646                 if (rc) {
647                         CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
648                                LAST_RCVD, rc);
649                         GOTO(err_fsd, rc);
650                 }
651                 if (strcmp(fsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
652                         LCONSOLE_ERROR("Trying to start OBD %s using the wrong"
653                                        " disk %s. Were the /dev/ assignments "
654                                        "rearranged?\n",
655                                        obd->obd_uuid.uuid, fsd->lsd_uuid);
656                         GOTO(err_fsd, rc = -EINVAL);
657                 }
658                 mount_count = le64_to_cpu(fsd->lsd_mount_count);
659                 filter->fo_subdir_count = le16_to_cpu(fsd->lsd_subdir_count);
660                 /* COMPAT_146 */
661                 /* Assume old last_rcvd format unless I_C_LR is set */
662                 if (!(fsd->lsd_feature_incompat & 
663                       cpu_to_le32(OBD_INCOMPAT_COMMON_LR)))
664                         fsd->lsd_last_transno = fsd->lsd_compat14;
665                 /* end COMPAT_146 */
666         }
667
668         if (fsd->lsd_feature_incompat & ~cpu_to_le32(FILTER_INCOMPAT_SUPP)) {
669                 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
670                        obd->obd_name, le32_to_cpu(fsd->lsd_feature_incompat) &
671                        ~FILTER_INCOMPAT_SUPP);
672                 GOTO(err_fsd, rc = -EINVAL);
673         }
674         if (fsd->lsd_feature_rocompat & ~cpu_to_le32(FILTER_ROCOMPAT_SUPP)) {
675                 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
676                        obd->obd_name, le32_to_cpu(fsd->lsd_feature_rocompat) &
677                        ~FILTER_ROCOMPAT_SUPP);
678                 /* Do something like remount filesystem read-only */
679                 GOTO(err_fsd, rc = -EINVAL);
680         }
681
682         CDEBUG(D_INODE, "%s: server last_transno : "LPU64"\n",
683                obd->obd_name, le64_to_cpu(fsd->lsd_last_transno));
684         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
685                obd->obd_name, mount_count + 1);
686         CDEBUG(D_INODE, "%s: server data size: %u\n",
687                obd->obd_name, le32_to_cpu(fsd->lsd_server_size));
688         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
689                obd->obd_name, le32_to_cpu(fsd->lsd_client_start));
690         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
691                obd->obd_name, le32_to_cpu(fsd->lsd_client_size));
692         CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
693                obd->obd_name, le16_to_cpu(fsd->lsd_subdir_count));
694         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
695                last_rcvd_size <= le32_to_cpu(fsd->lsd_client_start) ? 0 :
696                (last_rcvd_size - le32_to_cpu(fsd->lsd_client_start)) /
697                 le16_to_cpu(fsd->lsd_client_size));
698
699         if (!obd->obd_replayable) {
700                 CWARN("%s: recovery support OFF\n", obd->obd_name);
701                 GOTO(out, rc = 0);
702         }
703
704         for (cl_idx = 0, off = le32_to_cpu(fsd->lsd_client_start);
705              off < last_rcvd_size; cl_idx++) {
706                 __u64 last_rcvd;
707                 struct obd_export *exp;
708                 struct filter_export_data *fed;
709
710                 if (!fcd) {
711                         OBD_ALLOC(fcd, sizeof(*fcd));
712                         if (!fcd)
713                                 GOTO(err_client, rc = -ENOMEM);
714                 }
715
716                 /* Don't assume off is incremented properly by
717                  * fsfilt_read_record(), in case sizeof(*fcd)
718                  * isn't the same as fsd->lsd_client_size.  */
719                 off = le32_to_cpu(fsd->lsd_client_start) +
720                         cl_idx * le16_to_cpu(fsd->lsd_client_size);
721                 rc = fsfilt_read_record(obd, filp, fcd, sizeof(*fcd), &off);
722                 if (rc) {
723                         CERROR("error reading FILT %s idx %d off %llu: rc %d\n",
724                                LAST_RCVD, cl_idx, off, rc);
725                         break; /* read error shouldn't cause startup to fail */
726                 }
727
728                 if (fcd->fcd_uuid[0] == '\0') {
729                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
730                                cl_idx);
731                         continue;
732                 }
733
734                 last_rcvd = le64_to_cpu(fcd->fcd_last_rcvd);
735
736                 /* These exports are cleaned up by filter_disconnect(), so they
737                  * need to be set up like real exports as filter_connect() does.
738                  */
739                 exp = class_new_export(obd, (struct obd_uuid *)fcd->fcd_uuid);
740                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
741                        " srv lr: "LPU64"\n", fcd->fcd_uuid, cl_idx,
742                        last_rcvd, le64_to_cpu(fsd->lsd_last_transno));
743                 if (IS_ERR(exp)) {
744                         if (PTR_ERR(exp) == -EALREADY) {
745                                 /* export already exists, zero out this one */
746                                 CERROR("Zeroing out duplicate export due to "
747                                        "bug 10479.\n");
748                                 fcd->fcd_uuid[0] = '\0';
749                         } else {
750                                 GOTO(err_client, rc = PTR_ERR(exp));
751                         }
752                 } else {
753                         fed = &exp->exp_filter_data;
754                         fed->fed_fcd = fcd;
755                         filter_export_stats_init(obd, exp);
756                         rc = filter_client_add(obd, exp, cl_idx);
757                         LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
758
759                         fcd = NULL;
760                         exp->exp_replay_needed = 1;
761                         exp->exp_connecting = 0;
762                         obd->obd_recoverable_clients++;
763                         obd->obd_max_recoverable_clients++;
764                         class_export_put(exp);
765                 }
766
767                 /* Need to check last_rcvd even for duplicated exports. */
768                 CDEBUG(D_OTHER, "client at idx %d has last_rcvd = "LPU64"\n",
769                        cl_idx, last_rcvd);
770
771                 if (last_rcvd > le64_to_cpu(fsd->lsd_last_transno))
772                         fsd->lsd_last_transno = cpu_to_le64(last_rcvd);
773         }
774
775         if (fcd)
776                 OBD_FREE(fcd, sizeof(*fcd));
777
778         obd->obd_last_committed = le64_to_cpu(fsd->lsd_last_transno);
779
780         if (obd->obd_recoverable_clients) {
781                 CWARN("RECOVERY: service %s, %d recoverable clients, "
782                       "last_rcvd "LPU64"\n", obd->obd_name,
783                       obd->obd_recoverable_clients,
784                       le64_to_cpu(fsd->lsd_last_transno));
785                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
786                 obd->obd_recovering = 1;
787                 obd->obd_recovery_start = CURRENT_SECONDS;
788                 /* Only used for lprocfs_status */
789                 obd->obd_recovery_end = obd->obd_recovery_start +
790                         OBD_RECOVERY_TIMEOUT;
791         }
792
793 out:
794         filter->fo_mount_count = mount_count + 1;
795         fsd->lsd_mount_count = cpu_to_le64(filter->fo_mount_count);
796
797         /* save it, so mount count and last_transno is current */
798         rc = filter_update_server_data(obd, filp, filter->fo_fsd, 1);
799         if (rc)
800                 GOTO(err_client, rc);
801
802         RETURN(0);
803
804 err_client:
805         class_disconnect_exports(obd);
806 err_fsd:
807         filter_free_server_data(filter);
808         RETURN(rc);
809 }
810
811 static int filter_cleanup_groups(struct obd_device *obd)
812 {
813         struct filter_obd *filter = &obd->u.filter;
814         struct file *filp;
815         struct dentry *dentry;
816         int i;
817         ENTRY;
818
819         if (filter->fo_dentry_O_groups != NULL) {
820                 for (i = 0; i < FILTER_GROUPS; i++) {
821                         dentry = filter->fo_dentry_O_groups[i];
822                         if (dentry != NULL)
823                                 f_dput(dentry);
824                 }
825                 OBD_FREE(filter->fo_dentry_O_groups,
826                          FILTER_GROUPS * sizeof(*filter->fo_dentry_O_groups));
827                 filter->fo_dentry_O_groups = NULL;
828         }
829         if (filter->fo_last_objid_files != NULL) {
830                 for (i = 0; i < FILTER_GROUPS; i++) {
831                         filp = filter->fo_last_objid_files[i];
832                         if (filp != NULL)
833                                 filp_close(filp, 0);
834                 }
835                 OBD_FREE(filter->fo_last_objid_files,
836                          FILTER_GROUPS * sizeof(*filter->fo_last_objid_files));
837                 filter->fo_last_objid_files = NULL;
838         }
839         if (filter->fo_dentry_O_sub != NULL) {
840                 for (i = 0; i < filter->fo_subdir_count; i++) {
841                         dentry = filter->fo_dentry_O_sub[i];
842                         if (dentry != NULL)
843                                 f_dput(dentry);
844                 }
845                 OBD_FREE(filter->fo_dentry_O_sub,
846                          filter->fo_subdir_count *
847                          sizeof(*filter->fo_dentry_O_sub));
848                 filter->fo_dentry_O_sub = NULL;
849         }
850         if (filter->fo_last_objids != NULL) {
851                 OBD_FREE(filter->fo_last_objids,
852                          FILTER_GROUPS * sizeof(*filter->fo_last_objids));
853                 filter->fo_last_objids = NULL;
854         }
855         if (filter->fo_dentry_O != NULL) {
856                 f_dput(filter->fo_dentry_O);
857                 filter->fo_dentry_O = NULL;
858         }
859         RETURN(0);
860 }
861
862 /* FIXME: object groups */
863 static int filter_prep_groups(struct obd_device *obd)
864 {
865         struct filter_obd *filter = &obd->u.filter;
866         struct dentry *dentry, *O_dentry;
867         struct file *filp;
868         int i, rc = 0, cleanup_phase = 0;
869         ENTRY;
870
871         O_dentry = simple_mkdir(current->fs->pwd, "O", 0700, 1);
872         CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
873         if (IS_ERR(O_dentry)) {
874                 rc = PTR_ERR(O_dentry);
875                 CERROR("cannot open/create O: rc = %d\n", rc);
876                 GOTO(cleanup, rc);
877         }
878         filter->fo_dentry_O = O_dentry;
879         cleanup_phase = 1; /* O_dentry */
880         
881         OBD_ALLOC(filter->fo_last_objids, FILTER_GROUPS * sizeof(__u64));
882         if (filter->fo_last_objids == NULL)
883                 GOTO(cleanup, rc = -ENOMEM);
884         cleanup_phase = 2; /* groups */
885
886         OBD_ALLOC(filter->fo_dentry_O_groups, FILTER_GROUPS * sizeof(dentry));
887         if (filter->fo_dentry_O_groups == NULL)
888                 GOTO(cleanup, rc = -ENOMEM);
889         OBD_ALLOC(filter->fo_last_objid_files, FILTER_GROUPS * sizeof(filp));
890         if (filter->fo_last_objid_files == NULL)
891                 GOTO(cleanup, rc = -ENOMEM);
892
893         for (i = 0; i < FILTER_GROUPS; i++) {
894                 char name[25];
895                 loff_t off = 0;
896
897                 sprintf(name, "%d", i);
898                 dentry = simple_mkdir(O_dentry, name, 0700, 1);
899                 CDEBUG(D_INODE, "got/created O/%s: %p\n", name, dentry);
900                 if (IS_ERR(dentry)) {
901                         rc = PTR_ERR(dentry);
902                         CERROR("cannot lookup/create O/%s: rc = %d\n",
903                                name, rc);
904                         GOTO(cleanup, rc);
905                 }
906                 filter->fo_dentry_O_groups[i] = dentry;
907
908                 sprintf(name, "O/%d/LAST_ID", i);
909                 filp = filp_open(name, O_CREAT | O_RDWR, 0700);
910                 if (IS_ERR(filp)) {
911                         rc = PTR_ERR(filp);
912                         CERROR("cannot create %s: rc = %d\n", name, rc);
913                         GOTO(cleanup, rc);
914                 }
915                 filter->fo_last_objid_files[i] = filp;
916
917                 if (filp->f_dentry->d_inode->i_size == 0) {
918                         filter->fo_last_objids[i] = FILTER_INIT_OBJID;
919                         rc = filter_update_last_objid(obd, i, 1);
920                         if (rc)
921                                 GOTO(cleanup, rc);
922                         continue;
923                 }
924
925                 rc = fsfilt_read_record(obd, filp, &filter->fo_last_objids[i],
926                                         sizeof(__u64), &off);
927                 if (rc) {
928                         CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
929                                name, rc);
930                         GOTO(cleanup, rc);
931                 }
932                 filter->fo_last_objids[i] =
933                         le64_to_cpu(filter->fo_last_objids[i]);
934                 CDEBUG(D_HA, "%s: server last_objid group %d: "LPU64"\n",
935                        obd->obd_name, i, filter->fo_last_objids[i]);
936         }
937
938         if (filter->fo_subdir_count) {
939                 O_dentry = filter->fo_dentry_O_groups[0];
940                 OBD_ALLOC(filter->fo_dentry_O_sub,
941                           filter->fo_subdir_count * sizeof(dentry));
942                 if (filter->fo_dentry_O_sub == NULL)
943                         GOTO(cleanup, rc = -ENOMEM);
944
945                 for (i = 0; i < filter->fo_subdir_count; i++) {
946                         char dir[20];
947                         snprintf(dir, sizeof(dir), "d%u", i);
948
949                         dentry = simple_mkdir(O_dentry, dir, 0700, 1);
950                         CDEBUG(D_INODE, "got/created O/0/%s: %p\n", dir,dentry);
951                         if (IS_ERR(dentry)) {
952                                 rc = PTR_ERR(dentry);
953                                 CERROR("can't lookup/create O/0/%s: rc = %d\n",
954                                        dir, rc);
955                                 GOTO(cleanup, rc);
956                         }
957                         filter->fo_dentry_O_sub[i] = dentry;
958                 }
959         }
960         RETURN(0);
961
962  cleanup:
963         filter_cleanup_groups(obd);
964         return rc;
965 }
966
967 /* setup the object store with correct subdirectories */
968 static int filter_prep(struct obd_device *obd)
969 {
970         struct lvfs_run_ctxt saved;
971         struct filter_obd *filter = &obd->u.filter;
972         struct file *file;
973         struct inode *inode;
974         int rc = 0;
975         ENTRY;
976
977         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
978         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
979         if (!file || IS_ERR(file)) {
980                 rc = PTR_ERR(file);
981                 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
982                        LAST_RCVD, rc);
983                 GOTO(out, rc);
984         }
985         filter->fo_rcvd_filp = file;
986         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
987                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
988                        file->f_dentry->d_inode->i_mode);
989                 GOTO(err_filp, rc = -ENOENT);
990         }
991
992         inode = file->f_dentry->d_parent->d_inode;
993         /* We use i_op->unlink directly in filter_vfs_unlink() */
994         if (!inode->i_op || !inode->i_op->create || !inode->i_op->unlink) {
995                 CERROR("%s: filesystem does not support create/unlink ops\n",
996                        obd->obd_name);
997                 GOTO(err_filp, rc = -EOPNOTSUPP);
998         }
999
1000         rc = filter_init_server_data(obd, file);
1001         if (rc) {
1002                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
1003                 GOTO(err_filp, rc);
1004         }
1005         /* open and create health check io file*/
1006         file = filp_open(HEALTH_CHECK, O_RDWR | O_CREAT, 0644);
1007         if (IS_ERR(file)) {
1008                 rc = PTR_ERR(file);
1009                 CERROR("OBD filter: cannot open/create %s rc = %d\n",
1010                        HEALTH_CHECK, rc);
1011                 GOTO(err_filp, rc);
1012         }
1013         filter->fo_health_check_filp = file;
1014         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
1015                 CERROR("%s is not a regular file!: mode = %o\n", HEALTH_CHECK,
1016                        file->f_dentry->d_inode->i_mode);
1017                 GOTO(err_health_check, rc = -ENOENT);
1018         }
1019         rc = lvfs_check_io_health(obd, file);
1020         if (rc)
1021                 GOTO(err_health_check, rc);
1022
1023         rc = filter_prep_groups(obd);
1024         if (rc)
1025                 GOTO(err_server_data, rc);
1026  out:
1027         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1028
1029         return(rc);
1030
1031  err_server_data:
1032         //class_disconnect_exports(obd, 0);
1033         filter_free_server_data(filter);
1034  err_health_check:
1035         if (filp_close(filter->fo_health_check_filp, 0))
1036                 CERROR("can't close %s after error\n", HEALTH_CHECK);
1037         filter->fo_health_check_filp = NULL;
1038  err_filp:
1039         if (filp_close(filter->fo_rcvd_filp, 0))
1040                 CERROR("can't close %s after error\n", LAST_RCVD);
1041         filter->fo_rcvd_filp = NULL;
1042         goto out;
1043 }
1044
1045 /* cleanup the filter: write last used object id to status file */
1046 static void filter_post(struct obd_device *obd)
1047 {
1048         struct lvfs_run_ctxt saved;
1049         struct filter_obd *filter = &obd->u.filter;
1050         int rc, i;
1051
1052         /* XXX: filter_update_lastobjid used to call fsync_dev.  It might be
1053          * best to start a transaction with h_sync, because we removed this
1054          * from lastobjid */
1055
1056         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1057         rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
1058                                        filter->fo_fsd, 0);
1059         if (rc)
1060                 CERROR("error writing server data: rc = %d\n", rc);
1061
1062         for (i = 0; i < FILTER_GROUPS; i++) {
1063                 rc = filter_update_last_objid(obd, i, (i == FILTER_GROUPS - 1));
1064                 if (rc)
1065                         CERROR("error writing group %d lastobjid: rc = %d\n",
1066                                i, rc);
1067         }
1068
1069         rc = filp_close(filter->fo_rcvd_filp, 0);
1070         filter->fo_rcvd_filp = NULL;
1071         if (rc)
1072                 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
1073
1074         rc = filp_close(filter->fo_health_check_filp, 0);
1075         filter->fo_health_check_filp = NULL;
1076         if (rc)
1077                 CERROR("error closing %s: rc = %d\n", HEALTH_CHECK, rc);
1078
1079         filter_cleanup_groups(obd);
1080         filter_free_server_data(filter);
1081         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1082 }
1083
1084 static void filter_set_last_id(struct filter_obd *filter,
1085                                obd_id id, obd_gr group)
1086 {
1087         LASSERT(filter->fo_fsd != NULL);
1088         LASSERT(group <= FILTER_GROUPS);
1089
1090         spin_lock(&filter->fo_objidlock);
1091         filter->fo_last_objids[group] = id;
1092         spin_unlock(&filter->fo_objidlock);
1093 }
1094
1095 obd_id filter_last_id(struct filter_obd *filter, obd_gr group)
1096 {
1097         obd_id id;
1098         LASSERT(filter->fo_fsd != NULL);
1099         LASSERT(group <= FILTER_GROUPS);
1100
1101         /* FIXME: object groups */
1102         spin_lock(&filter->fo_objidlock);
1103         id = filter->fo_last_objids[group];
1104         spin_unlock(&filter->fo_objidlock);
1105
1106         return id;
1107 }
1108
1109 static int filter_lock_dentry(struct obd_device *obd, struct dentry *dparent)
1110 {
1111         LOCK_INODE_MUTEX(dparent->d_inode);
1112         return 0;
1113 }
1114
1115 /* We never dget the object parent, so DON'T dput it either */
1116 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
1117 {
1118         struct filter_obd *filter = &obd->u.filter;
1119         LASSERT(group < FILTER_GROUPS); /* FIXME: object groups */
1120
1121         if (group > 0 || filter->fo_subdir_count == 0)
1122                 return filter->fo_dentry_O_groups[group];
1123
1124         return filter->fo_dentry_O_sub[objid & (filter->fo_subdir_count - 1)];
1125 }
1126
1127 /* We never dget the object parent, so DON'T dput it either */
1128 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
1129                                   obd_id objid)
1130 {
1131         unsigned long now = jiffies;
1132         struct dentry *dparent = filter_parent(obd, group, objid);
1133         int rc;
1134
1135         if (IS_ERR(dparent))
1136                 return dparent;
1137
1138         rc = filter_lock_dentry(obd, dparent);
1139         fsfilt_check_slow(obd, now, obd_timeout, "parent lock");
1140         return rc ? ERR_PTR(rc) : dparent;
1141 }
1142
1143 /* We never dget the object parent, so DON'T dput it either */
1144 static void filter_parent_unlock(struct dentry *dparent)
1145 {
1146         UNLOCK_INODE_MUTEX(dparent->d_inode);
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_fid2dentry(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         char name[32];
1163         int len;
1164         ENTRY;
1165
1166         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT) &&
1167             !obd->u.filter.fo_destroy_in_progress) {
1168                 /* don't fail lookups for orphan recovery, it causes
1169                  * later LBUGs when objects still exist during precreate */
1170                 CDEBUG(D_INFO, "*** obd_fail_loc=%x ***\n",OBD_FAIL_OST_ENOENT);
1171                 RETURN(ERR_PTR(-ENOENT));
1172         }
1173
1174         if (id == 0) {
1175                 CERROR("fatal: invalid object id 0\n");
1176                 RETURN(ERR_PTR(-ESTALE));
1177         }
1178
1179         len = sprintf(name, LPU64, id);
1180         if (dir_dentry == NULL) {
1181                 dparent = filter_parent_lock(obd, group, id);
1182                 if (IS_ERR(dparent)) {
1183                         CERROR("%s: error getting object "LPU64":"LPU64
1184                                " parent: rc %ld\n", obd->obd_name,
1185                                id, group, PTR_ERR(dparent));
1186                         RETURN(dparent);
1187                 }
1188         }
1189         CDEBUG(D_INODE, "looking up object O/%.*s/%s\n",
1190                dparent->d_name.len, dparent->d_name.name, name);
1191         dchild = /*ll_*/lookup_one_len(name, dparent, len);
1192         if (dir_dentry == NULL)
1193                 filter_parent_unlock(dparent);
1194         if (IS_ERR(dchild)) {
1195                 CERROR("%s: child lookup error %ld\n", obd->obd_name,
1196                        PTR_ERR(dchild));
1197                 RETURN(dchild);
1198         }
1199
1200         if (dchild->d_inode != NULL && is_bad_inode(dchild->d_inode)) {
1201                 CERROR("%s: got bad object "LPU64" inode %lu\n",
1202                        obd->obd_name, id, dchild->d_inode->i_ino);
1203                 f_dput(dchild);
1204                 RETURN(ERR_PTR(-ENOENT));
1205         }
1206
1207         CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
1208                name, dchild, atomic_read(&dchild->d_count));
1209
1210         LASSERT(atomic_read(&dchild->d_count) > 0);
1211
1212         RETURN(dchild);
1213 }
1214
1215 static int filter_prepare_destroy(struct obd_device *obd, obd_id objid)
1216 {
1217         struct lustre_handle lockh;
1218         int flags = LDLM_AST_DISCARD_DATA, rc;
1219         struct ldlm_res_id res_id = { .name = { objid } };
1220         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
1221
1222         ENTRY;
1223         /* Tell the clients that the object is gone now and that they should
1224          * throw away any cached pages. */
1225         rc = ldlm_cli_enqueue_local(obd->obd_namespace, res_id, LDLM_EXTENT, 
1226                                     &policy, LCK_PW, &flags, ldlm_blocking_ast, 
1227                                     ldlm_completion_ast, NULL, NULL, 0, NULL, 
1228                                     &lockh);
1229
1230         /* We only care about the side-effects, just drop the lock. */
1231         if (rc == ELDLM_OK)
1232                 ldlm_lock_decref(&lockh, LCK_PW);
1233
1234         RETURN(rc);
1235 }
1236
1237 /* This is vfs_unlink() without down(i_sem).  If we call regular vfs_unlink()
1238  * we have 2.6 lock ordering issues with filter_commitrw_write() as it takes
1239  * i_sem before starting a handle, while filter_destroy() + vfs_unlink do the
1240  * reverse.  Caller must take i_sem before starting the transaction and we
1241  * drop it here before the inode is removed from the dentry.  bug 4180/6984 */
1242 int filter_vfs_unlink(struct inode *dir, struct dentry *dentry)
1243 {
1244         int rc;
1245         ENTRY;
1246
1247         /* don't need dir->i_zombie for 2.4, it is for rename/unlink of dir
1248          * itself we already hold dir->i_mutex for child create/unlink ops */
1249         LASSERT(dentry->d_inode != NULL);
1250         LASSERT(TRYLOCK_INODE_MUTEX(dir) == 0);
1251         LASSERT(TRYLOCK_INODE_MUTEX(dentry->d_inode) == 0);
1252
1253
1254         /* may_delete() */
1255         if (/*!dentry->d_inode ||*/dentry->d_parent->d_inode != dir)
1256                 GOTO(out, rc = -ENOENT);
1257
1258         rc = ll_permission(dir, MAY_WRITE | MAY_EXEC, NULL);
1259         if (rc)
1260                 GOTO(out, rc);
1261
1262         if (IS_APPEND(dir))
1263                 GOTO(out, rc = -EPERM);
1264
1265         /* check_sticky() */
1266         if ((dentry->d_inode->i_uid != current->fsuid && !capable(CAP_FOWNER))||
1267             IS_APPEND(dentry->d_inode) || IS_IMMUTABLE(dentry->d_inode))
1268                 GOTO(out, rc = -EPERM);
1269
1270         /* NOTE: This might need to go outside i_mutex, though it isn't clear if
1271          *       that was done because of journal_start (which is already done
1272          *       here) or some other ordering issue. */
1273         DQUOT_INIT(dir);
1274
1275 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1276         rc = security_inode_unlink(dir, dentry);
1277         if (rc)
1278                 GOTO(out, rc);
1279 #endif
1280
1281         rc = dir->i_op->unlink(dir, dentry);
1282 out:
1283         /* need to drop i_mutex before we lose inode reference */
1284         UNLOCK_INODE_MUTEX(dentry->d_inode);
1285         if (rc == 0)
1286                 d_delete(dentry);
1287
1288         RETURN(rc);
1289 }
1290
1291 /* Caller must hold LCK_PW on parent and push us into kernel context.
1292  * Caller must hold child i_mutex, we drop it always.
1293  * Caller is also required to ensure that dchild->d_inode exists. */
1294 static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
1295                                    obd_gr group, struct dentry *dparent,
1296                                    struct dentry *dchild)
1297 {
1298         struct inode *inode = dchild->d_inode;
1299         int rc;
1300
1301         if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
1302                 CERROR("destroying objid %.*s ino %lu nlink %lu count %d\n",
1303                        dchild->d_name.len, dchild->d_name.name, inode->i_ino,
1304                        (unsigned long)inode->i_nlink,
1305                        atomic_read(&inode->i_count));
1306         }
1307
1308         rc = filter_vfs_unlink(dparent->d_inode, dchild);
1309         if (rc)
1310                 CERROR("error unlinking objid %.*s: rc %d\n",
1311                        dchild->d_name.len, dchild->d_name.name, rc);
1312         return(rc);
1313 }
1314
1315 static int filter_intent_policy(struct ldlm_namespace *ns,
1316                                 struct ldlm_lock **lockp, void *req_cookie,
1317                                 ldlm_mode_t mode, int flags, void *data)
1318 {
1319         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1320         struct ptlrpc_request *req = req_cookie;
1321         struct ldlm_lock *lock = *lockp, *l = NULL;
1322         struct ldlm_resource *res = lock->l_resource;
1323         ldlm_processing_policy policy;
1324         struct ost_lvb *res_lvb, *reply_lvb;
1325         struct ldlm_reply *rep;
1326         struct list_head *tmp;
1327         ldlm_error_t err;
1328         int rc, tmpflags = 0, only_liblustre = 0;
1329         int repsize[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1330                            [DLM_LOCKREPLY_OFF]   = sizeof(*rep),
1331                            [DLM_REPLY_REC_OFF]   = sizeof(*reply_lvb) };
1332         ENTRY;
1333
1334         policy = ldlm_get_processing_policy(res);
1335         LASSERT(policy != NULL);
1336         LASSERT(req != NULL);
1337
1338         rc = lustre_pack_reply(req, 3, repsize, NULL);
1339         if (rc)
1340                 RETURN(req->rq_status = rc);
1341
1342         rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
1343         LASSERT(rep != NULL);
1344
1345         reply_lvb = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
1346                                    sizeof(*reply_lvb));
1347         LASSERT(reply_lvb != NULL);
1348
1349         //fixup_handle_for_resent_req(req, lock, &lockh);
1350
1351         /* If we grant any lock at all, it will be a whole-file read lock.
1352          * Call the extent policy function to see if our request can be
1353          * granted, or is blocked. */
1354         lock->l_policy_data.l_extent.start = 0;
1355         lock->l_policy_data.l_extent.end = OBD_OBJECT_EOF;
1356         lock->l_req_mode = LCK_PR;
1357
1358         LASSERT(ns == res->lr_namespace);
1359         lock_res(res);
1360         rc = policy(lock, &tmpflags, 0, &err, &rpc_list);
1361         check_res_locked(res);
1362
1363         /* FIXME: we should change the policy function slightly, to not make
1364          * this list at all, since we just turn around and free it */
1365         while (!list_empty(&rpc_list)) {
1366                 struct ldlm_lock *wlock =
1367                         list_entry(rpc_list.next, struct ldlm_lock, l_cp_ast);
1368                 LASSERT((lock->l_flags & LDLM_FL_AST_SENT) == 0);
1369                 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1370                 lock->l_flags &= ~LDLM_FL_CP_REQD;
1371                 list_del_init(&wlock->l_cp_ast);
1372                 LDLM_LOCK_PUT(wlock);
1373         }
1374
1375         /* The lock met with no resistance; we're finished. */
1376         if (rc == LDLM_ITER_CONTINUE) {
1377                 /* do not grant locks to the liblustre clients: they cannot
1378                  * handle ASTs robustly.  We need to do this while still
1379                  * holding ns_lock to avoid the lock remaining on the res_link
1380                  * list (and potentially being added to l_pending_list by an
1381                  * AST) when we are going to drop this lock ASAP. */
1382                 if (lock->l_export->exp_libclient ||
1383                     OBD_FAIL_CHECK(OBD_FAIL_LDLM_GLIMPSE)) {
1384                         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1385                         ldlm_resource_unlink_lock(lock);
1386                         err = ELDLM_LOCK_ABORTED;
1387                 } else {
1388                         err = ELDLM_LOCK_REPLACED;
1389                 }
1390                 unlock_res(res);
1391                 RETURN(err);
1392         }
1393
1394         /* Do not grant any lock, but instead send GL callbacks.  The extent
1395          * policy nicely created a list of all PW locks for us.  We will choose
1396          * the highest of those which are larger than the size in the LVB, if
1397          * any, and perform a glimpse callback. */
1398         res_lvb = res->lr_lvb_data;
1399         LASSERT(res_lvb != NULL);
1400         *reply_lvb = *res_lvb;
1401
1402         list_for_each(tmp, &res->lr_granted) {
1403                 struct ldlm_lock *tmplock =
1404                         list_entry(tmp, struct ldlm_lock, l_res_link);
1405
1406                 if (tmplock->l_granted_mode == LCK_PR)
1407                         continue;
1408                 /*
1409                  * ->ns_lock guarantees that no new locks are granted, and,
1410                  * therefore, that res->lr_lvb_data cannot increase beyond the
1411                  * end of already granted lock. As a result, it is safe to
1412                  * check against "stale" reply_lvb->lvb_size value without
1413                  * res->lr_lvb_sem.
1414                  */
1415                 if (tmplock->l_policy_data.l_extent.end <= reply_lvb->lvb_size)
1416                         continue;
1417
1418                 /* Don't send glimpse ASTs to liblustre clients.  They aren't
1419                  * listening for them, and they do entirely synchronous I/O
1420                  * anyways. */
1421                 if (tmplock->l_export == NULL ||
1422                     tmplock->l_export->exp_libclient == 1) {
1423                         only_liblustre = 1;
1424                         continue;
1425                 }
1426
1427                 if (l == NULL) {
1428                         l = LDLM_LOCK_GET(tmplock);
1429                         continue;
1430                 }
1431
1432                 if (l->l_policy_data.l_extent.start >
1433                     tmplock->l_policy_data.l_extent.start)
1434                         continue;
1435
1436                 LDLM_LOCK_PUT(l);
1437                 l = LDLM_LOCK_GET(tmplock);
1438         }
1439         unlock_res(res);
1440
1441         /* There were no PW locks beyond the size in the LVB; finished. */
1442         if (l == NULL) {
1443                 if (only_liblustre) {
1444                         /* If we discovered a liblustre client with a PW lock,
1445                          * however, the LVB may be out of date!  The LVB is
1446                          * updated only on glimpse (which we don't do for
1447                          * liblustre clients) and cancel (which the client
1448                          * obviously has not yet done).  So if it has written
1449                          * data but kept the lock, the LVB is stale and needs
1450                          * to be updated from disk.
1451                          *
1452                          * Of course, this will all disappear when we switch to
1453                          * taking liblustre locks on the OST. */
1454                         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_update)
1455                                 ns->ns_lvbo->lvbo_update(res, NULL, 0, 1);
1456                 }
1457                 RETURN(ELDLM_LOCK_ABORTED);
1458         }
1459
1460         /*
1461          * This check is for lock taken in filter_prepare_destroy() that does
1462          * not have l_glimpse_ast set. So the logic is: if there is a lock
1463          * with no l_glimpse_ast set, this object is being destroyed already.
1464          *
1465          * Hence, if you are grabbing DLM locks on the server, always set
1466          * non-NULL glimpse_ast (e.g., ldlm_request.c:ldlm_glimpse_ast()).
1467          */
1468         if (l->l_glimpse_ast == NULL) {
1469                 /* We are racing with unlink(); just return -ENOENT */
1470                 rep->lock_policy_res1 = -ENOENT;
1471                 goto out;
1472         }
1473
1474         LASSERTF(l->l_glimpse_ast != NULL, "l == %p", l);
1475         rc = l->l_glimpse_ast(l, NULL); /* this will update the LVB */
1476         /* Update the LVB from disk if the AST failed (this is a legal race) */
1477         /*
1478          * XXX nikita: situation when ldlm_server_glimpse_ast() failed before
1479          * sending ast is not handled. This can result in lost client writes.
1480          */
1481         if (rc != 0 && ns->ns_lvbo && ns->ns_lvbo->lvbo_update)
1482                 ns->ns_lvbo->lvbo_update(res, NULL, 0, 1);
1483
1484         lock_res(res);
1485         *reply_lvb = *res_lvb;
1486         unlock_res(res);
1487
1488  out:
1489         LDLM_LOCK_PUT(l);
1490
1491         RETURN(ELDLM_LOCK_ABORTED);
1492 }
1493
1494 /*
1495  * per-obd_device iobuf pool.
1496  *
1497  * To avoid memory deadlocks in low-memory setups, amount of dynamic
1498  * allocations in write-path has to be minimized (see bug 5137).
1499  *
1500  * Pages, niobuf_local's and niobuf_remote's are pre-allocated and attached to
1501  * OST threads (see ost_thread_{init,done}()).
1502  *
1503  * "iobuf's" used by filter cannot be attached to OST thread, however, because
1504  * at the OST layer there are only (potentially) multiple obd_device of type
1505  * unknown at the time of OST thread creation.
1506  *
1507  * Instead array of iobuf's is attached to struct filter_obd (->fo_iobuf_pool
1508  * field). This array has size OST_MAX_THREADS, so that each OST thread uses
1509  * it's very own iobuf.
1510  *
1511  * Functions below
1512  *
1513  *     filter_kiobuf_pool_init()
1514  *
1515  *     filter_kiobuf_pool_done()
1516  *
1517  *     filter_iobuf_get()
1518  *
1519  * operate on this array. They are "generic" in a sense that they don't depend
1520  * on actual type of iobuf's (the latter depending on Linux kernel version).
1521  */
1522
1523 /*
1524  * destroy pool created by filter_iobuf_pool_init
1525  */
1526 static void filter_iobuf_pool_done(struct filter_obd *filter)
1527 {
1528         struct filter_iobuf **pool;
1529         int i;
1530
1531         ENTRY;
1532
1533         pool = filter->fo_iobuf_pool;
1534         if (pool != NULL) {
1535                 for (i = 0; i < filter->fo_iobuf_count; ++ i) {
1536                         if (pool[i] != NULL)
1537                                 filter_free_iobuf(pool[i]);
1538                 }
1539                 OBD_FREE(pool, filter->fo_iobuf_count * sizeof pool[0]);
1540                 filter->fo_iobuf_pool = NULL;
1541         }
1542         EXIT;
1543 }
1544
1545 /*
1546  * pre-allocate pool of iobuf's to be used by filter_{prep,commit}rw_write().
1547  */
1548 static int filter_iobuf_pool_init(struct filter_obd *filter)
1549 {
1550         void **pool;
1551
1552         ENTRY;
1553
1554         OBD_ALLOC_GFP(filter->fo_iobuf_pool, OSS_THREADS_MAX * sizeof(*pool),
1555                       GFP_KERNEL);
1556         if (filter->fo_iobuf_pool == NULL)
1557                 RETURN(-ENOMEM);
1558
1559         filter->fo_iobuf_count = OSS_THREADS_MAX;
1560
1561         RETURN(0);
1562 }
1563
1564 /* Return iobuf allocated for @thread_id.  We don't know in advance how
1565  * many threads there will be so we allocate a large empty array and only
1566  * fill in those slots that are actually in use.
1567  * If we haven't allocated a pool entry for this thread before, do so now. */
1568 void *filter_iobuf_get(struct filter_obd *filter, struct obd_trans_info *oti)
1569 {
1570         int thread_id                    = oti ? oti->oti_thread_id : -1;
1571         struct filter_iobuf  *pool       = NULL;
1572         struct filter_iobuf **pool_place = NULL;
1573
1574         if (thread_id >= 0) {
1575                 LASSERT(thread_id < filter->fo_iobuf_count);
1576                 pool = *(pool_place = &filter->fo_iobuf_pool[thread_id]);
1577         }
1578
1579         if (unlikely(pool == NULL)) {
1580                 pool = filter_alloc_iobuf(filter, OBD_BRW_WRITE,
1581                                           PTLRPC_MAX_BRW_PAGES);
1582                 if (pool_place != NULL)
1583                         *pool_place = pool;
1584         }
1585
1586         return pool;
1587 }
1588
1589 /* mount the file system (secretly).  lustre_cfg parameters are:
1590  * 1 = device
1591  * 2 = fstype
1592  * 3 = flags: failover=f, failout=n
1593  * 4 = mount options
1594  */
1595 int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
1596                         void *option)
1597 {
1598         struct lustre_cfg* lcfg = buf;
1599         struct filter_obd *filter = &obd->u.filter;
1600         struct vfsmount *mnt;
1601         struct lustre_mount_info *lmi;
1602         struct obd_uuid uuid;
1603         __u8 *uuid_ptr;
1604         char *str, *label;
1605         char ns_name[48];
1606         int rc;
1607         ENTRY;
1608
1609         if (lcfg->lcfg_bufcount < 3 ||
1610             LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
1611             LUSTRE_CFG_BUFLEN(lcfg, 2) < 1)
1612                 RETURN(-EINVAL);
1613
1614         lmi = server_get_mount(obd->obd_name);
1615         if (lmi) {
1616                 /* We already mounted in lustre_fill_super.
1617                    lcfg bufs 1, 2, 4 (device, fstype, mount opts) are ignored.*/
1618                 struct lustre_sb_info *lsi = s2lsi(lmi->lmi_sb);
1619                 mnt = lmi->lmi_mnt;
1620                 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
1621         } else {
1622                 /* old path - used by lctl */
1623                 CERROR("Using old MDS mount method\n");
1624                 mnt = ll_kern_mount(lustre_cfg_string(lcfg, 2),
1625                                     MS_NOATIME|MS_NODIRATIME,
1626                                     lustre_cfg_string(lcfg, 1), option);    
1627                 if (IS_ERR(mnt)) {
1628                         rc = PTR_ERR(mnt);
1629                         LCONSOLE_ERROR("Can't mount disk %s (%d)\n",
1630                                        lustre_cfg_string(lcfg, 1), rc);
1631                         RETURN(rc);
1632                 }
1633
1634                 obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
1635         }
1636         if (IS_ERR(obd->obd_fsops))
1637                 GOTO(err_mntput, rc = PTR_ERR(obd->obd_fsops));
1638
1639         rc = filter_iobuf_pool_init(filter);
1640         if (rc != 0)
1641                 GOTO(err_ops, rc);
1642
1643         LASSERT(!lvfs_check_rdonly(lvfs_sbdev(mnt->mnt_sb)));
1644
1645         /* failover is the default */
1646         obd->obd_replayable = 1;
1647
1648         if (lcfg->lcfg_bufcount > 3 && LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
1649                 str = lustre_cfg_string(lcfg, 3);
1650                 if (strchr(str, 'n')) {
1651                         CWARN("%s: recovery disabled\n", obd->obd_name);
1652                         obd->obd_replayable = 0;
1653                 }
1654         }
1655
1656         filter->fo_vfsmnt = mnt;
1657         obd->u.obt.obt_sb = mnt->mnt_sb;
1658         filter->fo_fstype = mnt->mnt_sb->s_type->name;
1659         CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
1660
1661         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1662         obd->obd_lvfs_ctxt.pwdmnt = mnt;
1663         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1664         obd->obd_lvfs_ctxt.fs = get_ds();
1665         obd->obd_lvfs_ctxt.cb_ops = filter_lvfs_ops;
1666
1667         rc = filter_prep(obd);
1668         if (rc)
1669                 GOTO(err_ops, rc);
1670
1671         filter->fo_destroy_in_progress = 0;
1672         sema_init(&filter->fo_create_lock, 1);
1673         spin_lock_init(&filter->fo_translock);
1674         spin_lock_init(&filter->fo_objidlock);
1675         INIT_LIST_HEAD(&filter->fo_export_list);
1676         sema_init(&filter->fo_alloc_lock, 1);
1677         init_brw_stats(&filter->fo_filter_stats);
1678         filter->fo_readcache_max_filesize = FILTER_MAX_CACHE_SIZE;
1679         filter->fo_fmd_max_num = FILTER_FMD_MAX_NUM_DEFAULT;
1680         filter->fo_fmd_max_age = FILTER_FMD_MAX_AGE_DEFAULT;
1681
1682         sprintf(ns_name, "filter-%s", obd->obd_uuid.uuid);
1683         obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
1684         if (obd->obd_namespace == NULL)
1685                 GOTO(err_post, rc = -ENOMEM);
1686         obd->obd_namespace->ns_lvbp = obd;
1687         obd->obd_namespace->ns_lvbo = &filter_lvbo;
1688         ldlm_register_intent(obd->obd_namespace, filter_intent_policy);
1689
1690         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1691                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1692
1693         rc = llog_cat_initialize(obd, 1, NULL);
1694         if (rc) {
1695                 CERROR("failed to setup llogging subsystems\n");
1696                 GOTO(err_post, rc);
1697         }
1698
1699         rc = lquota_setup(filter_quota_interface_ref, obd);
1700         if (rc)
1701                 GOTO(err_post, rc);
1702
1703         uuid_ptr = fsfilt_uuid(obd, obd->u.obt.obt_sb);
1704         if (uuid_ptr != NULL) {
1705                 class_uuid_unparse(uuid_ptr, &uuid);
1706                 str = uuid.uuid;
1707         } else {
1708                 str = "no UUID";
1709         }
1710         
1711         label = fsfilt_get_label(obd, obd->u.obt.obt_sb);
1712
1713         if (obd->obd_recovering) {
1714                 LCONSOLE_WARN("OST %s now serving %s (%s%s%s), but will be in "
1715                               "recovery until %d %s reconnect, or if no clients"
1716                               " reconnect for %d:%.02d; during that time new "
1717                               "clients will not be allowed to connect. "
1718                               "Recovery progress can be monitored by watching "
1719                               "/proc/fs/lustre/obdfilter/%s/recovery_status.\n",
1720                               obd->obd_name, lustre_cfg_string(lcfg, 1),
1721                               label ?: "", label ? "/" : "", str,
1722                               obd->obd_recoverable_clients,
1723                               (obd->obd_recoverable_clients == 1)
1724                               ? "client" : "clients",
1725                               (int)(OBD_RECOVERY_TIMEOUT) / 60,
1726                               (int)(OBD_RECOVERY_TIMEOUT) % 60,
1727                               obd->obd_name);
1728         } else {
1729                 LCONSOLE_INFO("OST %s now serving %s (%s%s%s) with recovery "
1730                               "%s\n", obd->obd_name, lustre_cfg_string(lcfg, 1),
1731                               label ?: "", label ? "/" : "", str,
1732                               obd->obd_replayable ? "enabled" : "disabled");
1733         }
1734
1735         RETURN(0);
1736
1737 err_post:
1738         filter_post(obd);
1739 err_ops:
1740         fsfilt_put_ops(obd->obd_fsops);
1741         filter_iobuf_pool_done(filter);
1742 err_mntput:
1743         server_put_mount(obd->obd_name, mnt);
1744         obd->u.obt.obt_sb = 0;
1745         return rc;
1746 }
1747
1748 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
1749 {
1750         struct lprocfs_static_vars lvars;
1751         struct lustre_cfg* lcfg = buf;
1752         unsigned long page;
1753         int rc;
1754
1755         CLASSERT(offsetof(struct obd_device, u.obt) ==
1756                  offsetof(struct obd_device, u.filter.fo_obt));
1757
1758         if (!LUSTRE_CFG_BUFLEN(lcfg, 1) || !LUSTRE_CFG_BUFLEN(lcfg, 2))
1759                 RETURN(-EINVAL);
1760
1761         /* 2.6.9 selinux wants a full option page for do_kern_mount (bug6471) */
1762         page = get_zeroed_page(GFP_KERNEL);
1763         if (!page)
1764                 RETURN(-ENOMEM);
1765
1766         memcpy((void *)page, lustre_cfg_buf(lcfg, 4),
1767                LUSTRE_CFG_BUFLEN(lcfg, 4));
1768         rc = filter_common_setup(obd, len, buf, (void *)page);
1769         free_page(page);
1770
1771         lprocfs_init_vars(filter, &lvars);
1772         if (rc == 0 && lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
1773             lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST) == 0) {
1774                 /* Init obdfilter private stats here */
1775                 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
1776                                      LPROCFS_CNTR_AVGMINMAX,
1777                                      "read_bytes", "bytes");
1778                 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
1779                                      LPROCFS_CNTR_AVGMINMAX,
1780                                      "write_bytes", "bytes");
1781                 lproc_filter_attach_seqstat(obd);
1782                 obd->obd_proc_exports = proc_mkdir("exports",
1783                                                    obd->obd_proc_entry);
1784         }
1785
1786         return rc;
1787 }
1788
1789 static struct llog_operations filter_mds_ost_repl_logops /* initialized below*/;
1790 static struct llog_operations filter_size_orig_logops = {
1791         lop_setup: llog_obd_origin_setup,
1792         lop_cleanup: llog_obd_origin_cleanup,
1793         lop_add: llog_obd_origin_add
1794 };
1795
1796 static int filter_llog_init(struct obd_device *obd, struct obd_device *tgt,
1797                             int count, struct llog_catid *catid,
1798                             struct obd_uuid *uuid)
1799 {
1800         struct llog_ctxt *ctxt;
1801         int rc;
1802         ENTRY;
1803
1804         filter_mds_ost_repl_logops = llog_client_ops;
1805         filter_mds_ost_repl_logops.lop_cancel = llog_obd_repl_cancel;
1806         filter_mds_ost_repl_logops.lop_connect = llog_repl_connect;
1807         filter_mds_ost_repl_logops.lop_sync = llog_obd_repl_sync;
1808
1809         rc = llog_setup(obd, LLOG_MDS_OST_REPL_CTXT, tgt, 0, NULL,
1810                         &filter_mds_ost_repl_logops);
1811         if (rc)
1812                 RETURN(rc);
1813
1814         /* FIXME - assign unlink_cb for filter's recovery */
1815         ctxt = llog_get_context(obd, LLOG_MDS_OST_REPL_CTXT);
1816         ctxt->llog_proc_cb = filter_recov_log_mds_ost_cb;
1817
1818         rc = llog_setup(obd, LLOG_SIZE_ORIG_CTXT, tgt, 0, NULL,
1819                         &filter_size_orig_logops);
1820         RETURN(rc);
1821 }
1822
1823 static int filter_llog_finish(struct obd_device *obd, int count)
1824 {
1825         struct llog_ctxt *ctxt;
1826         int rc = 0, rc2 = 0;
1827         ENTRY;
1828
1829         ctxt = llog_get_context(obd, LLOG_MDS_OST_REPL_CTXT);
1830         if (ctxt)
1831                 rc = llog_cleanup(ctxt);
1832
1833         ctxt = llog_get_context(obd, LLOG_SIZE_ORIG_CTXT);
1834         if (ctxt)
1835                 rc2 = llog_cleanup(ctxt);
1836         if (!rc)
1837                 rc = rc2;
1838
1839         RETURN(rc);
1840 }
1841
1842 static int filter_precleanup(struct obd_device *obd,
1843                              enum obd_cleanup_stage stage)
1844 {
1845         int rc = 0;
1846         ENTRY;
1847
1848         switch(stage) {
1849         case OBD_CLEANUP_EARLY:
1850                 break;
1851         case OBD_CLEANUP_EXPORTS:
1852                 target_cleanup_recovery(obd);
1853                 break;
1854         case OBD_CLEANUP_SELF_EXP:
1855                 rc = filter_llog_finish(obd, 0);
1856                 break;
1857         case OBD_CLEANUP_OBD:
1858                 break;
1859         }
1860         RETURN(rc);
1861 }
1862
1863 static int filter_cleanup(struct obd_device *obd)
1864 {
1865         struct filter_obd *filter = &obd->u.filter;
1866         ENTRY;
1867
1868         if (obd->obd_fail)
1869                 LCONSOLE_WARN("%s: shutting down for failover; client state "
1870                               "will be preserved.\n", obd->obd_name);
1871
1872         if (!list_empty(&obd->obd_exports)) {
1873                 CERROR("%s: still has clients!\n", obd->obd_name);
1874                 class_disconnect_exports(obd);
1875                 if (!list_empty(&obd->obd_exports)) {
1876                         CERROR("still has exports after forced cleanup?\n");
1877                         RETURN(-EBUSY);
1878                 }
1879         }
1880
1881         lprocfs_obd_cleanup(obd);
1882         lprocfs_free_obd_stats(obd);
1883
1884         lquota_cleanup(filter_quota_interface_ref, obd);
1885
1886         ldlm_namespace_free(obd->obd_namespace, obd->obd_force);
1887
1888         if (obd->u.obt.obt_sb == NULL)
1889                 RETURN(0);
1890
1891         filter_post(obd);
1892
1893         shrink_dcache_parent(obd->u.obt.obt_sb->s_root);
1894
1895         LL_DQUOT_OFF(obd->u.obt.obt_sb);
1896
1897         server_put_mount(obd->obd_name, filter->fo_vfsmnt);
1898         obd->u.obt.obt_sb = NULL;
1899
1900         fsfilt_put_ops(obd->obd_fsops);
1901
1902         filter_iobuf_pool_done(filter);
1903
1904         LCONSOLE_INFO("OST %s has stopped.\n", obd->obd_name);
1905
1906         RETURN(0);
1907 }
1908
1909 static int filter_connect_internal(struct obd_export *exp,
1910                                    struct obd_connect_data *data)
1911 {
1912         if (!data)
1913                 RETURN(0);
1914
1915         CDEBUG(D_RPCTRACE, "%s: cli %s/%p ocd_connect_flags: "LPX64
1916                " ocd_version: %x ocd_grant: %d ocd_index: %u\n",
1917                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
1918                data->ocd_connect_flags, data->ocd_version,
1919                data->ocd_grant, data->ocd_index);
1920
1921         data->ocd_connect_flags &= OST_CONNECT_SUPPORTED;
1922         exp->exp_connect_flags = data->ocd_connect_flags;
1923         data->ocd_version = LUSTRE_VERSION_CODE;
1924
1925         if (exp->exp_connect_flags & OBD_CONNECT_GRANT) {
1926                 obd_size left, want;
1927
1928                 spin_lock(&exp->exp_obd->obd_osfs_lock);
1929                 left = filter_grant_space_left(exp);
1930                 want = data->ocd_grant;
1931                 data->ocd_grant = filter_grant(exp, 0, want, left);
1932                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
1933
1934                 CDEBUG(D_CACHE, "%s: cli %s/%p ocd_grant: %d want: "
1935                        LPU64" left: "LPU64"\n", exp->exp_obd->obd_name,
1936                        exp->exp_client_uuid.uuid, exp,
1937                        data->ocd_grant, want, left);
1938         }
1939
1940         if (data->ocd_connect_flags & OBD_CONNECT_INDEX) {
1941                 struct filter_obd *filter = &exp->exp_obd->u.filter;
1942                 struct lr_server_data *lsd = filter->fo_fsd;
1943                 int index = le32_to_cpu(lsd->lsd_ost_index);
1944                 
1945                 if (!(lsd->lsd_feature_compat &
1946                       cpu_to_le32(OBD_COMPAT_OST))) {
1947                         /* this will only happen on the first connect */
1948                         lsd->lsd_ost_index = cpu_to_le32(data->ocd_index);
1949                         lsd->lsd_feature_compat |= cpu_to_le32(OBD_COMPAT_OST);
1950                         filter_update_server_data(exp->exp_obd,
1951                                                   filter->fo_rcvd_filp, lsd, 1);
1952                 } else if (index != data->ocd_index) {
1953                         LCONSOLE_ERROR("Connection from %s to index %u doesn't "
1954                                        "match actual OST index %u in last_rcvd "
1955                                        "file, bad configuration?\n",
1956                                        obd_export_nid2str(exp), index,
1957                                        data->ocd_index);
1958                         RETURN(-EBADF);
1959                 }
1960         }
1961
1962         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_SIZE)) {
1963                 data->ocd_brw_size = 65536;
1964         } else if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
1965                 data->ocd_brw_size = min(data->ocd_brw_size,
1966                                          (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
1967                 LASSERT(data->ocd_brw_size); 
1968         }
1969
1970         /* FIXME: Do the same with the MDS UUID and fsd_peeruuid.
1971          * FIXME: We don't strictly need the COMPAT flag for that,
1972          * FIXME: as fsd_peeruuid[0] will tell us if that is set.
1973          * FIXME: We needed it for the index, as index 0 is valid. */
1974
1975         RETURN(0);
1976 }
1977
1978 static int filter_reconnect(struct obd_export *exp, struct obd_device *obd,
1979                             struct obd_uuid *cluuid,
1980                             struct obd_connect_data *data)
1981 {
1982         int rc;
1983         ENTRY;
1984
1985         if (exp == NULL || obd == NULL || cluuid == NULL)
1986                 RETURN(-EINVAL);
1987
1988         rc = filter_connect_internal(exp, data);
1989
1990         RETURN(rc);
1991 }
1992
1993 /* nearly identical to mds_connect */
1994 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
1995                           struct obd_uuid *cluuid,
1996                           struct obd_connect_data *data)
1997 {
1998         struct obd_export *exp;
1999         struct filter_export_data *fed;
2000         struct filter_client_data *fcd = NULL;
2001         int rc;
2002         ENTRY;
2003
2004         if (conn == NULL || obd == NULL || cluuid == NULL)
2005                 RETURN(-EINVAL);
2006
2007         rc = class_connect(conn, obd, cluuid);
2008         if (rc)
2009                 RETURN(rc);
2010         exp = class_conn2export(conn);
2011         LASSERT(exp != NULL);
2012
2013         fed = &exp->exp_filter_data;
2014
2015         rc = filter_connect_internal(exp, data);
2016         if (rc)
2017                 GOTO(cleanup, rc);
2018
2019         filter_export_stats_init(obd, exp);
2020
2021         if (!obd->obd_replayable)
2022                 GOTO(cleanup, rc = 0);
2023
2024         OBD_ALLOC(fcd, sizeof(*fcd));
2025         if (!fcd) {
2026                 CERROR("filter: out of memory for client data\n");
2027                 GOTO(cleanup, rc = -ENOMEM);
2028         }
2029
2030         memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
2031         fed->fed_fcd = fcd;
2032
2033         rc = filter_client_add(obd, exp, -1);
2034
2035         GOTO(cleanup, rc);
2036
2037 cleanup:
2038         if (rc) {
2039                 if (fcd) {
2040                         OBD_FREE(fcd, sizeof(*fcd));
2041                         fed->fed_fcd = NULL;
2042                 }
2043                 class_disconnect(exp);
2044         } else {
2045                 class_export_put(exp);
2046         }
2047
2048         RETURN(rc);
2049 }
2050
2051 /* Do extra sanity checks for grant accounting.  We do this at connect,
2052  * disconnect, and statfs RPC time, so it shouldn't be too bad.  We can
2053  * always get rid of it or turn it off when we know accounting is good. */
2054 static void filter_grant_sanity_check(struct obd_device *obd, const char *func)
2055 {
2056         struct filter_export_data *fed;
2057         struct obd_export *exp;
2058         obd_size maxsize = obd->obd_osfs.os_blocks * obd->obd_osfs.os_bsize;
2059         obd_size tot_dirty = 0, tot_pending = 0, tot_granted = 0;
2060         obd_size fo_tot_dirty, fo_tot_pending, fo_tot_granted;
2061
2062         if (list_empty(&obd->obd_exports))
2063                 return;
2064
2065         spin_lock(&obd->obd_osfs_lock);
2066         spin_lock(&obd->obd_dev_lock);
2067         list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
2068                 int error = 0;
2069                 fed = &exp->exp_filter_data;
2070                 if (fed->fed_grant < 0 || fed->fed_pending < 0 ||
2071                     fed->fed_dirty < 0)
2072                         error = 1;
2073                 if (maxsize > 0) { /* we may not have done a statfs yet */
2074                         LASSERTF(fed->fed_grant + fed->fed_pending <= maxsize,
2075                                  "%s: cli %s/%p %ld+%ld > "LPU64"\n", func,
2076                                  exp->exp_client_uuid.uuid, exp,
2077                                  fed->fed_grant, fed->fed_pending, maxsize);
2078                         LASSERTF(fed->fed_dirty <= maxsize,
2079                                  "%s: cli %s/%p %ld > "LPU64"\n", func,
2080                                  exp->exp_client_uuid.uuid, exp,
2081                                  fed->fed_dirty, maxsize);
2082                 }
2083                 if (error)
2084                         CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
2085                                obd->obd_name, exp->exp_client_uuid.uuid, exp,
2086                                fed->fed_dirty, fed->fed_pending,fed->fed_grant);
2087                 else
2088                         CDEBUG(D_CACHE, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
2089                                obd->obd_name, exp->exp_client_uuid.uuid, exp,
2090                                fed->fed_dirty, fed->fed_pending,fed->fed_grant);
2091                 tot_granted += fed->fed_grant + fed->fed_pending;
2092                 tot_pending += fed->fed_pending;
2093                 tot_dirty += fed->fed_dirty;
2094         }
2095         fo_tot_granted = obd->u.filter.fo_tot_granted;
2096         fo_tot_pending = obd->u.filter.fo_tot_pending;
2097         fo_tot_dirty = obd->u.filter.fo_tot_dirty;
2098         spin_unlock(&obd->obd_dev_lock);
2099         spin_unlock(&obd->obd_osfs_lock);
2100
2101         /* Do these assertions outside the spinlocks so we don't kill system */
2102         if (tot_granted != fo_tot_granted)
2103                 CERROR("%s: tot_granted "LPU64" != fo_tot_granted "LPU64"\n",
2104                        func, tot_granted, fo_tot_granted);
2105         if (tot_pending != fo_tot_pending)
2106                 CERROR("%s: tot_pending "LPU64" != fo_tot_pending "LPU64"\n",
2107                        func, tot_pending, fo_tot_pending);
2108         if (tot_dirty != fo_tot_dirty)
2109                 CERROR("%s: tot_dirty "LPU64" != fo_tot_dirty "LPU64"\n",
2110                        func, tot_dirty, fo_tot_dirty);
2111         if (tot_pending > tot_granted)
2112                 CERROR("%s: tot_pending "LPU64" > tot_granted "LPU64"\n",
2113                        func, tot_pending, tot_granted);
2114         if (tot_granted > maxsize)
2115                 CERROR("%s: tot_granted "LPU64" > maxsize "LPU64"\n",
2116                        func, tot_granted, maxsize);
2117         if (tot_dirty > maxsize)
2118                 CERROR("%s: tot_dirty "LPU64" > maxsize "LPU64"\n",
2119                        func, tot_dirty, maxsize);
2120 }
2121
2122 /* Remove this client from the grant accounting totals.  We also remove
2123  * the export from the obd device under the osfs and dev locks to ensure
2124  * that the filter_grant_sanity_check() calculations are always valid.
2125  * The client should do something similar when it invalidates its import. */
2126 static void filter_grant_discard(struct obd_export *exp)
2127 {
2128         struct obd_device *obd = exp->exp_obd;
2129         struct filter_obd *filter = &obd->u.filter;
2130         struct filter_export_data *fed = &exp->exp_filter_data;
2131
2132         spin_lock(&obd->obd_osfs_lock);
2133         spin_lock(&obd->obd_dev_lock);
2134         list_del_init(&exp->exp_obd_chain);
2135         spin_unlock(&obd->obd_dev_lock);
2136
2137         LASSERTF(filter->fo_tot_granted >= fed->fed_grant,
2138                  "%s: tot_granted "LPU64" cli %s/%p fed_grant %ld\n",
2139                  obd->obd_name, filter->fo_tot_granted,
2140                  exp->exp_client_uuid.uuid, exp, fed->fed_grant);
2141         filter->fo_tot_granted -= fed->fed_grant;
2142         LASSERTF(filter->fo_tot_pending >= fed->fed_pending,
2143                  "%s: tot_pending "LPU64" cli %s/%p fed_pending %ld\n",
2144                  obd->obd_name, filter->fo_tot_pending,
2145                  exp->exp_client_uuid.uuid, exp, fed->fed_pending);
2146         /* fo_tot_pending is handled in filter_grant_commit as bulk finishes */
2147         LASSERTF(filter->fo_tot_dirty >= fed->fed_dirty,
2148                  "%s: tot_dirty "LPU64" cli %s/%p fed_dirty %ld\n",
2149                  obd->obd_name, filter->fo_tot_dirty,
2150                  exp->exp_client_uuid.uuid, exp, fed->fed_dirty);
2151         filter->fo_tot_dirty -= fed->fed_dirty;
2152         fed->fed_dirty = 0;
2153         fed->fed_grant = 0;
2154
2155         spin_unlock(&obd->obd_osfs_lock);
2156 }
2157
2158 static int filter_destroy_export(struct obd_export *exp)
2159 {
2160         ENTRY;
2161
2162         if (exp->exp_filter_data.fed_pending)
2163                 CERROR("%s: cli %s/%p has %lu pending on destroyed export\n",
2164                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
2165                        exp, exp->exp_filter_data.fed_pending);
2166
2167         target_destroy_export(exp);
2168
2169         if (obd_uuid_equals(&exp->exp_client_uuid, &exp->exp_obd->obd_uuid))
2170                 RETURN(0);
2171
2172         lprocfs_exp_cleanup(exp);
2173
2174         if (exp->exp_obd->obd_replayable)
2175                 filter_client_free(exp);
2176         else
2177                 fsfilt_sync(exp->exp_obd, exp->exp_obd->u.obt.obt_sb);
2178
2179         filter_grant_discard(exp);
2180         filter_fmd_cleanup(exp);
2181
2182         if (!(exp->exp_flags & OBD_OPT_FORCE))
2183                 filter_grant_sanity_check(exp->exp_obd, __FUNCTION__);
2184
2185         RETURN(0);
2186 }
2187
2188 /* also incredibly similar to mds_disconnect */
2189 static int filter_disconnect(struct obd_export *exp)
2190 {
2191         struct obd_device *obd = exp->exp_obd;
2192         struct llog_ctxt *ctxt;
2193         int rc, err;
2194         ENTRY;
2195
2196         LASSERT(exp);
2197         class_export_get(exp);
2198
2199         if (!(exp->exp_flags & OBD_OPT_FORCE))
2200                 filter_grant_sanity_check(obd, __FUNCTION__);
2201         filter_grant_discard(exp);
2202
2203         /* Disconnect early so that clients can't keep using export */
2204         rc = class_disconnect(exp);
2205         if (exp->exp_obd->obd_namespace != NULL)
2206                 ldlm_cancel_locks_for_export(exp);
2207
2208         /* flush any remaining cancel messages out to the target */
2209         ctxt = llog_get_context(obd, LLOG_MDS_OST_REPL_CTXT);
2210         err = llog_sync(ctxt, exp);
2211         if (err)
2212                 CERROR("error flushing logs to MDS: rc %d\n", err);
2213
2214         class_export_put(exp);
2215         RETURN(rc);
2216 }
2217
2218 static int filter_ping(struct obd_export *exp)
2219 {
2220         filter_fmd_expire(exp);
2221
2222         return 0;
2223 }
2224
2225 struct dentry *__filter_oa2dentry(struct obd_device *obd, struct obdo *oa,
2226                                   const char *what, int quiet)
2227 {
2228         struct dentry *dchild = NULL;
2229
2230         if (!(oa->o_valid & OBD_MD_FLGROUP))
2231                 oa->o_gr = 0;
2232
2233         dchild = filter_fid2dentry(obd, NULL, oa->o_gr, oa->o_id);
2234
2235         if (IS_ERR(dchild)) {
2236                 CERROR("%s error looking up object: "LPU64"\n",
2237                        what, oa->o_id);
2238                 RETURN(dchild);
2239         }
2240
2241         if (dchild->d_inode == NULL) {
2242                 if (!quiet)
2243                         CERROR("%s: %s on non-existent object: "LPU64"\n",
2244                                obd->obd_name, what, oa->o_id);
2245                 f_dput(dchild);
2246                 RETURN(ERR_PTR(-ENOENT));
2247         }
2248
2249         return dchild;
2250 }
2251
2252 static int filter_getattr(struct obd_export *exp, struct obd_info *oinfo)
2253 {
2254         struct dentry *dentry = NULL;
2255         struct obd_device *obd;
2256         int rc = 0;
2257         ENTRY;
2258
2259         obd = class_exp2obd(exp);
2260         if (obd == NULL) {
2261                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
2262                 RETURN(-EINVAL);
2263         }
2264
2265         dentry = filter_oa2dentry(obd, oinfo->oi_oa);
2266         if (IS_ERR(dentry))
2267                 RETURN(PTR_ERR(dentry));
2268
2269         /* Limit the valid bits in the return data to what we actually use */
2270         oinfo->oi_oa->o_valid = OBD_MD_FLID;
2271         obdo_from_inode(oinfo->oi_oa, dentry->d_inode, FILTER_VALID_FLAGS);
2272
2273         f_dput(dentry);
2274         RETURN(rc);
2275 }
2276
2277 /* this should be enabled/disabled in condition to enabled/disabled large
2278  * inodes (fast EAs) in backing store FS. */
2279 int filter_update_fidea(struct obd_export *exp, struct inode *inode,
2280                         void *handle, struct obdo *oa)
2281 {
2282         struct obd_device *obd = exp->exp_obd;
2283         int rc = 0;
2284         ENTRY;
2285
2286         if (oa->o_valid & OBD_MD_FLFID) {
2287                 struct filter_fid ff;
2288
2289                 if (!(oa->o_valid & OBD_MD_FLGROUP))
2290                         oa->o_gr = 0;
2291
2292                 /* packing fid and converting it to LE for storing into EA.
2293                  * Here ->o_stripe_idx should be filled by LOV and rest of
2294                  * fields - by client. */
2295                 ff.ff_fid.id = cpu_to_le64(oa->o_fid);
2296                 ff.ff_fid.f_type = cpu_to_le32(oa->o_stripe_idx);
2297                 ff.ff_fid.generation = cpu_to_le32(oa->o_generation);
2298                 ff.ff_objid = cpu_to_le64(oa->o_id);
2299                 ff.ff_group = cpu_to_le64(oa->o_gr);
2300
2301                 CDEBUG(D_INODE, "storing filter fid EA ("LPU64"/%u/%u"
2302                        LPU64"/"LPU64")\n", oa->o_fid, oa->o_stripe_idx,
2303                        oa->o_generation, oa->o_id, oa->o_gr);
2304
2305                 rc = fsfilt_set_md(obd, inode, handle, &ff, sizeof(ff), "fid");
2306                 if (rc)
2307                         CERROR("store fid in object failed! rc: %d\n", rc);
2308         } else {
2309                 CDEBUG(D_HA, "OSS object without fid info!\n");
2310         }
2311
2312         RETURN(rc);
2313 }
2314
2315 /* this is called from filter_truncate() until we have filter_punch() */
2316 int filter_setattr_internal(struct obd_export *exp, struct dentry *dentry,
2317                             struct obdo *oa, struct obd_trans_info *oti)
2318 {
2319         unsigned int orig_ids[MAXQUOTAS] = {0, 0};
2320         struct llog_cookie *fcc = NULL;
2321         struct filter_obd *filter;
2322         int rc, err, locked = 0;
2323         unsigned int ia_valid;
2324         struct inode *inode;
2325         struct iattr iattr;
2326         void *handle;
2327         ENTRY;
2328
2329         LASSERT(dentry != NULL);
2330         LASSERT(!IS_ERR(dentry));
2331
2332         inode = dentry->d_inode;
2333         LASSERT(inode != NULL);
2334
2335         filter = &exp->exp_obd->u.filter;
2336         iattr_from_obdo(&iattr, oa, oa->o_valid);
2337         ia_valid = iattr.ia_valid;
2338
2339         if (oa->o_valid & OBD_MD_FLCOOKIE) {
2340                 OBD_ALLOC(fcc, sizeof(*fcc));
2341                 if (fcc != NULL)
2342                         memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
2343         }
2344
2345         if (ia_valid & ATTR_SIZE || ia_valid & (ATTR_UID | ATTR_GID)) {
2346                 DQUOT_INIT(inode);
2347                 LOCK_INODE_MUTEX(inode);
2348                 locked = 1;
2349         }
2350
2351         /* If the inode still has SUID+SGID bits set (see filter_precreate())
2352          * then we will accept the UID+GID sent by the client during write for
2353          * initializing the ownership of this inode.  We only allow this to
2354          * happen once so clear these bits in setattr. In 2.6 kernels it is
2355          * possible to get ATTR_UID and ATTR_GID separately, so we only clear
2356          * the flags that are actually being set. */
2357         if (ia_valid & (ATTR_UID | ATTR_GID)) {
2358                 CDEBUG(D_INODE, "update UID/GID to %lu/%lu\n",
2359                        (unsigned long)oa->o_uid, (unsigned long)oa->o_gid);
2360
2361                 if ((inode->i_mode & S_ISUID) && (ia_valid & ATTR_UID)) {
2362                         if (!(ia_valid & ATTR_MODE)) {
2363                                 iattr.ia_mode = inode->i_mode;
2364                                 iattr.ia_valid |= ATTR_MODE;
2365                         }
2366                         iattr.ia_mode &= ~S_ISUID;
2367                 }
2368                 if ((inode->i_mode & S_ISGID) && (ia_valid & ATTR_GID)) {
2369                         if (!(iattr.ia_valid & ATTR_MODE)) {
2370                                 iattr.ia_mode = inode->i_mode;
2371                                 iattr.ia_valid |= ATTR_MODE;
2372                         }
2373                         iattr.ia_mode &= ~S_ISGID;
2374                 }
2375
2376                 orig_ids[USRQUOTA] = inode->i_uid;
2377                 orig_ids[GRPQUOTA] = inode->i_gid;
2378                 handle = fsfilt_start_log(exp->exp_obd, inode,
2379                                           FSFILT_OP_SETATTR, oti, 1);
2380
2381                 /* update inode EA only once when inode is suid bit marked. As
2382                  * on 2.6.x UID and GID may be set separately, we check here
2383                  * only one of them to avoid double setting. */
2384                 if (inode->i_mode & S_ISUID)
2385                         filter_update_fidea(exp, inode, handle, oa);
2386         } else {
2387                 handle = fsfilt_start(exp->exp_obd, inode,
2388                                       FSFILT_OP_SETATTR, oti);
2389         }
2390
2391         if (IS_ERR(handle))
2392                 GOTO(out_unlock, rc = PTR_ERR(handle));
2393
2394         if (oa->o_valid & OBD_MD_FLFLAGS) {
2395                 rc = fsfilt_iocontrol(exp->exp_obd, inode, NULL,
2396                                       EXT3_IOC_SETFLAGS, (long)&oa->o_flags);
2397         } else {
2398                 rc = fsfilt_setattr(exp->exp_obd, dentry, handle, &iattr, 1);
2399                 if (fcc != NULL) {
2400                         /* set cancel cookie callback function */
2401                         if (fsfilt_add_journal_cb(exp->exp_obd, 0, handle,
2402                                                   filter_cancel_cookies_cb,
2403                                                   fcc))
2404                                 exp->exp_need_sync = 1;
2405                         else
2406                                 fcc = NULL;
2407                 }
2408         }
2409
2410         if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_CREDITS))
2411                 fsfilt_extend(exp->exp_obd, inode, 0, handle);
2412
2413         /* The truncate might have used up our transaction credits.  Make
2414          * sure we have one left for the last_rcvd update. */
2415         err = fsfilt_extend(exp->exp_obd, inode, 1, handle);
2416         rc = filter_finish_transno(exp, oti, rc);
2417
2418         err = fsfilt_commit(exp->exp_obd, inode, handle, 0);
2419         if (err) {
2420                 CERROR("error on commit, err = %d\n", err);
2421                 if (!rc)
2422                         rc = err;
2423         }
2424
2425         if (locked) {
2426                 /* Let's flush truncated page on disk immediately, then we can
2427                  * avoid need to search for page aliases before directio writes
2428                  * and this sort of stuff at expense of somewhat slower
2429                  * truncates not on a page boundary. I believe this is the only
2430                  * place in filter code that can lead to pages getting to
2431                  * pagecache so far. */
2432                 filter_clear_truncated_page(inode);
2433                 UNLOCK_INODE_MUTEX(inode);
2434                 locked = 0;
2435         }
2436
2437         EXIT;
2438 out_unlock:
2439         if (locked)
2440                 UNLOCK_INODE_MUTEX(inode);
2441
2442         if (fcc)
2443                 OBD_FREE(fcc, sizeof(*fcc));
2444
2445         /* trigger quota release */
2446         if (ia_valid & (ATTR_SIZE | ATTR_UID | ATTR_GID)) {
2447                 unsigned int cur_ids[MAXQUOTAS] = {oa->o_uid, oa->o_gid};
2448                 int rc2 = lquota_adjust(filter_quota_interface_ref, exp->exp_obd, cur_ids,
2449                                         orig_ids, rc, FSFILT_OP_SETATTR);
2450                 CDEBUG(rc2 ? D_ERROR : D_QUOTA,
2451                        "filter adjust qunit. (rc:%d)\n", rc2);
2452         }
2453         return rc;
2454 }
2455
2456 /* this is called from filter_truncate() until we have filter_punch() */
2457 int filter_setattr(struct obd_export *exp, struct obd_info *oinfo,
2458                    struct obd_trans_info *oti)
2459 {
2460         struct ldlm_res_id res_id = { .name = { oinfo->oi_oa->o_id } };
2461         struct ldlm_valblock_ops *ns_lvbo;
2462         struct filter_mod_data *fmd;
2463         struct lvfs_run_ctxt saved;
2464         struct filter_obd *filter;
2465         struct ldlm_resource *res;
2466         struct dentry *dentry;
2467         int rc;
2468         ENTRY;
2469
2470         dentry = __filter_oa2dentry(exp->exp_obd, oinfo->oi_oa,
2471                                     __FUNCTION__, 1);
2472         if (IS_ERR(dentry))
2473                 RETURN(PTR_ERR(dentry));
2474
2475         filter = &exp->exp_obd->u.filter;
2476         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2477         lock_kernel();
2478
2479         if (oinfo->oi_oa->o_valid &
2480             (OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME)) {
2481                 fmd = filter_fmd_get(exp,oinfo->oi_oa->o_id,oinfo->oi_oa->o_gr);
2482                 if (fmd && fmd->fmd_mactime_xid < oti->oti_xid)
2483                         fmd->fmd_mactime_xid = oti->oti_xid;
2484                 filter_fmd_put(exp, fmd);
2485         }
2486
2487         /* setting objects attributes (including owner/group) */
2488         rc = filter_setattr_internal(exp, dentry, oinfo->oi_oa, oti);
2489         if (rc)
2490                 GOTO(out_unlock, rc);
2491
2492         res = ldlm_resource_get(exp->exp_obd->obd_namespace, NULL,
2493                                 res_id, LDLM_EXTENT, 0);
2494
2495         if (res != NULL) {
2496                 ns_lvbo = res->lr_namespace->ns_lvbo;
2497                 if (ns_lvbo && ns_lvbo->lvbo_update)
2498                         rc = ns_lvbo->lvbo_update(res, NULL, 0, 0);
2499                 ldlm_resource_putref(res);
2500         }
2501
2502         oinfo->oi_oa->o_valid = OBD_MD_FLID;
2503
2504         /* Quota release need uid/gid info */
2505         obdo_from_inode(oinfo->oi_oa, dentry->d_inode,
2506                         FILTER_VALID_FLAGS | OBD_MD_FLUID | OBD_MD_FLGID);
2507
2508         EXIT;
2509 out_unlock:
2510         unlock_kernel();
2511         f_dput(dentry);
2512         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2513         return rc;
2514 }
2515
2516 /* XXX identical to osc_unpackmd */
2517 static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2518                            struct lov_mds_md *lmm, int lmm_bytes)
2519 {
2520         int lsm_size;
2521         ENTRY;
2522
2523         if (lmm != NULL) {
2524                 if (lmm_bytes < sizeof (*lmm)) {
2525                         CERROR("lov_mds_md too small: %d, need %d\n",
2526                                lmm_bytes, (int)sizeof(*lmm));
2527                         RETURN(-EINVAL);
2528                 }
2529                 /* XXX LOV_MAGIC etc check? */
2530
2531                 if (lmm->lmm_object_id == cpu_to_le64(0)) {
2532                         CERROR("lov_mds_md: zero lmm_object_id\n");
2533                         RETURN(-EINVAL);
2534                 }
2535         }
2536
2537         lsm_size = lov_stripe_md_size(1);
2538         if (lsmp == NULL)
2539                 RETURN(lsm_size);
2540
2541         if (*lsmp != NULL && lmm == NULL) {
2542                 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
2543                 OBD_FREE(*lsmp, lsm_size);
2544                 *lsmp = NULL;
2545                 RETURN(0);
2546         }
2547
2548         if (*lsmp == NULL) {
2549                 OBD_ALLOC(*lsmp, lsm_size);
2550                 if (*lsmp == NULL)
2551                         RETURN(-ENOMEM);
2552                 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
2553                 if ((*lsmp)->lsm_oinfo[0] == NULL) {
2554                         OBD_FREE(*lsmp, lsm_size);
2555                         RETURN(-ENOMEM);
2556                 }
2557                 loi_init((*lsmp)->lsm_oinfo[0]);
2558         }
2559
2560         if (lmm != NULL) {
2561                 /* XXX zero *lsmp? */
2562                 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
2563                 LASSERT((*lsmp)->lsm_object_id);
2564         }
2565
2566         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
2567
2568         RETURN(lsm_size);
2569 }
2570
2571 /* caller must hold fo_create_lock */
2572 static int filter_destroy_precreated(struct obd_export *exp, struct obdo *oa,
2573                                       struct filter_obd *filter)
2574 {
2575         struct obdo doa; /* XXX obdo on stack */
2576         obd_id last, id;
2577         int rc;
2578         ENTRY;
2579
2580         LASSERT(oa);
2581         LASSERT(down_trylock(&filter->fo_create_lock) != 0);
2582
2583         memset(&doa, 0, sizeof(doa));
2584         if (oa->o_valid & OBD_MD_FLGROUP) {
2585                 doa.o_valid |= OBD_MD_FLGROUP;
2586                 doa.o_gr = oa->o_gr;
2587         } else {
2588                 doa.o_gr = 0;
2589         }
2590         doa.o_mode = S_IFREG;
2591
2592         if (!filter->fo_destroy_in_progress) {
2593                 CERROR("%s: destroy_in_progress already cleared\n",
2594                         exp->exp_obd->obd_name);
2595                 RETURN(0);
2596         }
2597
2598         last = filter_last_id(filter, doa.o_gr);
2599         CWARN("%s: deleting orphan objects from "LPU64" to "LPU64"\n",
2600                exp->exp_obd->obd_name, oa->o_id + 1, last);
2601         for (id = last; id > oa->o_id; id--) {
2602                 doa.o_id = id;
2603                 rc = filter_destroy(exp, &doa, NULL, NULL, NULL);
2604                 if (rc && rc != -ENOENT) /* this is pretty fatal... */
2605                         CEMERG("error destroying precreate objid "LPU64": %d\n",
2606                                id, rc);
2607                 filter_set_last_id(filter, id - 1, doa.o_gr);
2608                 /* update last_id on disk periodically so that if we restart
2609                  * we don't need to re-scan all of the just-deleted objects. */
2610                 if ((id & 511) == 0)
2611                         filter_update_last_objid(exp->exp_obd, doa.o_gr, 0);
2612         }
2613
2614         CDEBUG(D_HA, "%s: after destroy: set last_objids["LPU64"] = "LPU64"\n",
2615                exp->exp_obd->obd_name, doa.o_gr, oa->o_id);
2616
2617         rc = filter_update_last_objid(exp->exp_obd, doa.o_gr, 1);
2618         filter->fo_destroy_in_progress = 0;
2619
2620         RETURN(rc);
2621 }
2622
2623 static int filter_precreate(struct obd_device *obd, struct obdo *oa,
2624                             obd_gr group, int *num);
2625 /* returns a negative error or a nonnegative number of files to create */
2626 static int filter_handle_precreate(struct obd_export *exp, struct obdo *oa,
2627                                    obd_gr group, struct obd_trans_info *oti)
2628 {
2629         struct obd_device *obd = exp->exp_obd;
2630         struct filter_obd *filter = &obd->u.filter;
2631         int diff, rc;
2632         ENTRY;
2633
2634         /* delete orphans request */
2635         if ((oa->o_valid & OBD_MD_FLFLAGS) && (oa->o_flags & OBD_FL_DELORPHAN)){
2636                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
2637                         CERROR("%s: dropping old orphan cleanup request\n",
2638                                obd->obd_name);
2639                         RETURN(0);
2640                 }
2641
2642                 /* This causes inflight precreates to abort and drop lock */
2643                 filter->fo_destroy_in_progress = 1;
2644                 down(&filter->fo_create_lock);
2645                 diff = oa->o_id - filter_last_id(filter, group);
2646                 CDEBUG(D_HA, "filter_last_id() = "LPU64" -> diff = %d\n",
2647                        filter_last_id(filter, group), diff);
2648
2649                 if (-diff > OST_MAX_PRECREATE) {
2650                         CERROR("%s: ignoring bogus orphan destroy request: "
2651                                "obdid "LPU64" last_id "LPU64"\n", obd->obd_name,
2652                                oa->o_id, filter_last_id(filter, group));
2653                         /* FIXME: should reset precreate_next_id on MDS */
2654                         GOTO(out, rc = -EINVAL);
2655                 }
2656                 if (diff < 0) {
2657                         rc = filter_destroy_precreated(exp, oa, filter);
2658                         if (rc)
2659                                 CERROR("%s: unable to write lastobjid, but "
2660                                        "orphans were deleted\n", obd->obd_name);
2661                         GOTO(out, rc);
2662                 } else {
2663                         /*XXX used by MDS for the first time! */
2664                         filter->fo_destroy_in_progress = 0;
2665                 }
2666         } else {
2667                 down(&filter->fo_create_lock);
2668                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
2669                         CERROR("%s: dropping old precreate request\n",
2670                                obd->obd_name);
2671                         GOTO(out, rc = 0);
2672                 }
2673                 /* only precreate if group == 0 and o_id is specfied */
2674                 if (group != 0 || oa->o_id == 0)
2675                         diff = 1;
2676                 else
2677                         diff = oa->o_id - filter_last_id(filter, group);
2678                 CDEBUG(D_HA, "filter_last_id() = "LPU64" -> diff = %d\n",
2679                        filter_last_id(filter, group), diff);
2680
2681                 LASSERTF(diff >= 0,"%s: "LPU64" - "LPU64" = %d\n",obd->obd_name,
2682                          oa->o_id, filter_last_id(filter, group), diff);
2683         }
2684
2685         if (diff > 0) {
2686                 oa->o_id = filter_last_id(&obd->u.filter, group);
2687                 rc = filter_precreate(obd, oa, group, &diff);
2688                 oa->o_id = filter_last_id(&obd->u.filter, group);
2689                 oa->o_valid = OBD_MD_FLID;
2690                 GOTO(out, rc);
2691         }
2692         /* else diff == 0 */
2693         GOTO(out, rc = 0);
2694 out:
2695         up(&filter->fo_create_lock);
2696         return rc;
2697 }
2698
2699 static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2700                          __u64 max_age)
2701 {
2702         struct filter_obd *filter = &obd->u.filter;
2703         int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
2704         int rc;
2705         ENTRY;
2706
2707         /* at least try to account for cached pages.  its still racey and
2708          * might be under-reporting if clients haven't announced their
2709          * caches with brw recently */
2710         spin_lock(&obd->obd_osfs_lock);
2711         rc = fsfilt_statfs(obd, obd->u.obt.obt_sb, max_age);
2712         memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
2713         spin_unlock(&obd->obd_osfs_lock);
2714
2715         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
2716                " pending "LPU64" free "LPU64" avail "LPU64"\n",
2717                filter->fo_tot_dirty, filter->fo_tot_granted,
2718                filter->fo_tot_pending,
2719                osfs->os_bfree << blockbits, osfs->os_bavail << blockbits);
2720
2721         filter_grant_sanity_check(obd, __FUNCTION__);
2722
2723         osfs->os_bavail -= min(osfs->os_bavail, GRANT_FOR_LLOG(obd) +
2724                                ((filter->fo_tot_dirty + filter->fo_tot_pending +
2725                                  osfs->os_bsize - 1) >> blockbits));
2726
2727         /* set EROFS to state field if FS is mounted as RDONLY. The goal is to
2728          * stop creating files on MDS if OST is not good shape to create
2729          * objects.*/
2730         osfs->os_state = (filter->fo_obt.obt_sb->s_flags & MS_RDONLY) ?
2731                 EROFS : 0;
2732         RETURN(rc);
2733 }
2734
2735 /* We rely on the fact that only one thread will be creating files in a given
2736  * group at a time, which is why we don't need an atomic filter_get_new_id.
2737  * Even if we had that atomic function, the following race would exist:
2738  *
2739  * thread 1: gets id x from filter_next_id
2740  * thread 2: gets id (x + 1) from filter_next_id
2741  * thread 2: creates object (x + 1)
2742  * thread 1: tries to create object x, gets -ENOSPC
2743  *
2744  * Caller must hold fo_create_lock
2745  */
2746 static int filter_precreate(struct obd_device *obd, struct obdo *oa,
2747                             obd_gr group, int *num)
2748 {
2749         struct dentry *dchild = NULL, *dparent = NULL;
2750         struct filter_obd *filter;
2751         int err = 0, rc = 0, recreate_obj = 0, i;
2752         unsigned long enough_time = jiffies + min(obd_timeout * HZ / 4, 10U*HZ);
2753         obd_id next_id;
2754         void *handle = NULL;
2755         ENTRY;
2756
2757         filter = &obd->u.filter;
2758
2759         LASSERT(down_trylock(&filter->fo_create_lock) != 0);
2760
2761         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
2762             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
2763                 recreate_obj = 1;
2764         } else {
2765                 struct obd_statfs *osfs;
2766
2767                 OBD_ALLOC(osfs, sizeof(*osfs));
2768                 if (osfs == NULL)
2769                         RETURN(-ENOMEM);
2770                 rc = filter_statfs(obd, osfs, cfs_time_current_64() - HZ);
2771                 if (rc == 0 && osfs->os_bavail < (osfs->os_blocks >> 10)) {
2772                         CDEBUG(D_HA,"%s: not enough space for create "LPU64"\n",
2773                                obd->obd_name, osfs->os_bavail <<
2774                                filter->fo_vfsmnt->mnt_sb->s_blocksize_bits);
2775                         *num = 0;
2776                         rc = -ENOSPC;
2777                 }
2778                 OBD_FREE(osfs, sizeof(*osfs));
2779                 if (rc)
2780                         RETURN(rc);
2781         }
2782
2783         CDEBUG(D_HA, "%s: precreating %d objects in group "LPU64" at "LPU64"\n",
2784                obd->obd_name, *num, group, oa->o_id);
2785
2786         for (i = 0; i < *num && err == 0; i++) {
2787                 int cleanup_phase = 0;
2788
2789                 if (filter->fo_destroy_in_progress) {
2790                         CWARN("%s: precreate aborted by destroy\n",
2791                               obd->obd_name);
2792                         rc = -EAGAIN;
2793                         break;
2794                 }
2795
2796                 if (recreate_obj) {
2797                         __u64 last_id;
2798                         next_id = oa->o_id;
2799                         last_id = filter_last_id(filter, group);
2800                         if (next_id > last_id) {
2801                                 CERROR("Error: Trying to recreate obj greater"
2802                                        "than last id "LPD64" > "LPD64"\n",
2803                                        next_id, last_id);
2804                                 GOTO(cleanup, rc = -EINVAL);
2805                         }
2806                 } else
2807                         next_id = filter_last_id(filter, group) + 1;
2808
2809                 CDEBUG(D_INFO, "precreate objid "LPU64"\n", next_id);
2810
2811                 dparent = filter_parent_lock(obd, group, next_id);
2812                 if (IS_ERR(dparent))
2813                         GOTO(cleanup, rc = PTR_ERR(dparent));
2814                 cleanup_phase = 1;      /* filter_parent_unlock(dparent) */
2815
2816                 dchild = filter_fid2dentry(obd, dparent, group, next_id);
2817                 if (IS_ERR(dchild))
2818                         GOTO(cleanup, rc = PTR_ERR(dchild));
2819                 cleanup_phase = 2;      /* f_dput(dchild) */
2820
2821                 if (dchild->d_inode != NULL) {
2822                         /* This would only happen if lastobjid was bad on disk*/
2823                         /* Could also happen if recreating missing obj but
2824                          * already exists
2825                          */
2826                         if (recreate_obj) {
2827                                 CERROR("%s: recreating existing object %.*s?\n",
2828                                        obd->obd_name, dchild->d_name.len,
2829                                        dchild->d_name.name);
2830                         } else {
2831                                 CERROR("%s: Serious error: objid %.*s already "
2832                                        "exists; is this filesystem corrupt?\n",
2833                                        obd->obd_name, dchild->d_name.len,
2834                                        dchild->d_name.name);
2835                                 LBUG();
2836                         }
2837                         GOTO(cleanup, rc = -EEXIST);
2838                 }
2839
2840                 handle = fsfilt_start_log(obd, dparent->d_inode,
2841                                           FSFILT_OP_CREATE, NULL, 1);
2842                 if (IS_ERR(handle))
2843                         GOTO(cleanup, rc = PTR_ERR(handle));
2844                 cleanup_phase = 3;
2845
2846                 /* We mark object SUID+SGID to flag it for accepting UID+GID
2847                  * from client on first write.  Currently the permission bits
2848                  * on the OST are never used, so this is OK. */
2849                 rc = ll_vfs_create(dparent->d_inode, dchild,
2850                                    S_IFREG |  S_ISUID | S_ISGID | 0666, NULL);
2851                 if (rc) {
2852                         CERROR("create failed rc = %d\n", rc);
2853                         GOTO(cleanup, rc);
2854                 }
2855
2856                 if (!recreate_obj) {
2857                         filter_set_last_id(filter, next_id, group);
2858                         err = filter_update_last_objid(obd, group, 0);
2859                         if (err)
2860                                 CERROR("unable to write lastobjid "
2861                                        "but file created\n");
2862                 }
2863
2864         cleanup:
2865                 switch(cleanup_phase) {
2866                 case 3:
2867                         err = fsfilt_commit(obd, dparent->d_inode, handle, 0);
2868                         if (err) {
2869                                 CERROR("error on commit, err = %d\n", err);
2870                                 if (!rc)
2871                                         rc = err;
2872                         }
2873                 case 2:
2874                         f_dput(dchild);
2875                 case 1:
2876                         filter_parent_unlock(dparent);
2877                 case 0:
2878                         break;
2879                 }
2880
2881                 if (rc)
2882                         break;
2883                 if (time_after(jiffies, enough_time)) {
2884                         CDEBUG(D_HA, "%s: precreate slow - want %d got %d \n",
2885                                obd->obd_name, *num, i);
2886                         break;
2887                 }
2888         }
2889         *num = i;
2890
2891         CDEBUG(D_HA, "%s: created %d objects for group "LPU64": "LPU64" rc %d\n",
2892                obd->obd_name, i, group, filter->fo_last_objids[group], rc);
2893
2894         RETURN(rc);
2895 }
2896
2897 static int filter_create(struct obd_export *exp, struct obdo *oa,
2898                          struct lov_stripe_md **ea, struct obd_trans_info *oti)
2899 {
2900         struct obd_device *obd = NULL;
2901         struct lvfs_run_ctxt saved;
2902         struct lov_stripe_md *lsm = NULL;
2903         int rc = 0;
2904         ENTRY;
2905
2906         if (!(oa->o_valid & OBD_MD_FLGROUP))
2907                 oa->o_gr = 0;
2908
2909         CDEBUG(D_INFO, "filter_create(od->o_gr="LPU64",od->o_id="LPU64")\n",
2910                oa->o_gr, oa->o_id);
2911         if (ea != NULL) {
2912                 lsm = *ea;
2913                 if (lsm == NULL) {
2914                         rc = obd_alloc_memmd(exp, &lsm);
2915                         if (rc < 0)
2916                                 RETURN(rc);
2917                 }
2918         }
2919
2920         obd = exp->exp_obd;
2921         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2922
2923         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
2924             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
2925                 if (oa->o_id > filter_last_id(&obd->u.filter, oa->o_gr)) {
2926                         CERROR("recreate objid "LPU64" > last id "LPU64"\n",
2927                                oa->o_id, filter_last_id(&obd->u.filter,
2928                                                         oa->o_gr));
2929                         rc = -EINVAL;
2930                 } else {
2931                         struct filter_obd *filter = &obd->u.filter;
2932                         int diff = 1;
2933
2934                         down(&filter->fo_create_lock);
2935                         rc = filter_precreate(obd, oa, oa->o_gr, &diff);
2936                         up(&filter->fo_create_lock);
2937                 }
2938         } else {
2939                 rc = filter_handle_precreate(exp, oa, oa->o_gr, oti);
2940         }
2941
2942         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2943         if (rc && ea != NULL && *ea != lsm) {
2944                 obd_free_memmd(exp, &lsm);
2945         } else if (rc == 0 && ea != NULL) {
2946                 /* XXX LOV STACKING: the lsm that is passed to us from
2947                  * LOV does not have valid lsm_oinfo data structs, so
2948                  * don't go touching that.  This needs to be fixed in a
2949                  * big way. */
2950                 lsm->lsm_object_id = oa->o_id;
2951                 *ea = lsm;
2952         }
2953
2954         RETURN(rc);
2955 }
2956
2957 int filter_destroy(struct obd_export *exp, struct obdo *oa,
2958                    struct lov_stripe_md *md, struct obd_trans_info *oti,
2959                    struct obd_export *md_exp)
2960 {
2961         unsigned int qcids[MAXQUOTAS] = {0, 0};
2962         struct obd_device *obd;
2963         struct filter_obd *filter;
2964         struct dentry *dchild = NULL, *dparent = NULL;
2965         struct lvfs_run_ctxt saved;
2966         void *handle = NULL;
2967         struct llog_cookie *fcc = NULL;
2968         int rc, rc2, cleanup_phase = 0;
2969         struct iattr iattr;
2970         ENTRY;
2971
2972         if (!(oa->o_valid & OBD_MD_FLGROUP))
2973                 oa->o_gr = 0;
2974
2975         obd = exp->exp_obd;
2976         filter = &obd->u.filter;
2977
2978         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2979         cleanup_phase = 1;
2980
2981         dchild = filter_fid2dentry(obd, NULL, oa->o_gr, oa->o_id);
2982         if (IS_ERR(dchild))
2983                 GOTO(cleanup, rc = PTR_ERR(dchild));
2984         cleanup_phase = 2;
2985
2986         if (dchild->d_inode == NULL) {
2987                 CDEBUG(D_INODE, "destroying non-existent object "LPU64"\n",
2988                        oa->o_id);
2989                 /* If object already gone, cancel cookie right now */
2990                 if (oa->o_valid & OBD_MD_FLCOOKIE) {
2991                         fcc = obdo_logcookie(oa);
2992                         llog_cancel(llog_get_context(obd, fcc->lgc_subsys + 1),
2993                                     NULL, 1, fcc, 0);
2994                         fcc = NULL; /* we didn't allocate fcc, don't free it */
2995                 }
2996                 GOTO(cleanup, rc = -ENOENT);
2997         }
2998
2999         filter_prepare_destroy(obd, oa->o_id);
3000
3001         /* Our MDC connection is established by the MDS to us */
3002         if (oa->o_valid & OBD_MD_FLCOOKIE) {
3003                 OBD_ALLOC(fcc, sizeof(*fcc));
3004                 if (fcc != NULL)
3005                         memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
3006         }
3007         DQUOT_INIT(dchild->d_inode);
3008
3009         /* we're gonna truncate it first in order to avoid possible deadlock:
3010          *      P1                      P2
3011          * open trasaction      open transaction
3012          * down(i_zombie)       down(i_zombie)
3013          *                      restart transaction
3014          * (see BUG 4180) -bzzz
3015          */
3016         LOCK_INODE_MUTEX(dchild->d_inode);
3017         handle = fsfilt_start_log(obd, dchild->d_inode, FSFILT_OP_SETATTR,
3018                                   NULL, 1);
3019         if (IS_ERR(handle)) {
3020                 UNLOCK_INODE_MUTEX(dchild->d_inode);
3021                 GOTO(cleanup, rc = PTR_ERR(handle));
3022         }
3023
3024         iattr.ia_valid = ATTR_SIZE;
3025         iattr.ia_size = 0;
3026         rc = fsfilt_setattr(obd, dchild, handle, &iattr, 1);
3027         rc2 = fsfilt_commit(obd, dchild->d_inode, handle, 0);
3028         UNLOCK_INODE_MUTEX(dchild->d_inode);
3029         if (rc)
3030                 GOTO(cleanup, rc);
3031         if (rc2)
3032                 GOTO(cleanup, rc = rc2);
3033
3034         /* We don't actually need to lock the parent until we are unlinking
3035          * here, and not while truncating above.  That avoids holding the
3036          * parent lock for a long time during truncate, which can block other
3037          * threads from doing anything to objects in that directory. bug 7171 */
3038         dparent = filter_parent_lock(obd, oa->o_gr, oa->o_id);
3039         if (IS_ERR(dparent))
3040                 GOTO(cleanup, rc = PTR_ERR(dparent));
3041         cleanup_phase = 3; /* filter_parent_unlock */
3042
3043         LOCK_INODE_MUTEX(dchild->d_inode);
3044         handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_UNLINK,oti,1);
3045         if (IS_ERR(handle)) {
3046                 UNLOCK_INODE_MUTEX(dchild->d_inode);
3047                 GOTO(cleanup, rc = PTR_ERR(handle));
3048         }
3049         cleanup_phase = 4; /* fsfilt_commit */
3050
3051         /* Quota release need uid/gid of inode */
3052         obdo_from_inode(oa, dchild->d_inode, OBD_MD_FLUID|OBD_MD_FLGID);
3053
3054         filter_fmd_drop(exp, oa->o_id, oa->o_gr);
3055
3056         /* this drops dchild->d_inode->i_mutex unconditionally */
3057         rc = filter_destroy_internal(obd, oa->o_id, oa->o_gr, dparent, dchild);
3058
3059         EXIT;
3060 cleanup:
3061         switch(cleanup_phase) {
3062         case 4:
3063                 if (fcc != NULL) {
3064                         if (fsfilt_add_journal_cb(obd, 0, oti ?
3065                                                   oti->oti_handle : handle,
3066                                                   filter_cancel_cookies_cb,
3067                                                   fcc) == 0)
3068                                 fcc = NULL;
3069                 }
3070                 rc = filter_finish_transno(exp, oti, rc);
3071                 rc2 = fsfilt_commit(obd, dparent->d_inode, handle, 0);
3072                 if (rc2) {
3073                         CERROR("error on commit, err = %d\n", rc2);
3074                         if (!rc)
3075                                 rc = rc2;
3076                 }
3077         case 3:
3078                 filter_parent_unlock(dparent);
3079         case 2:
3080                 f_dput(dchild);
3081                 if (fcc != NULL)
3082                         OBD_FREE(fcc, sizeof(*fcc));
3083         case 1:
3084                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3085                 break;
3086         default:
3087                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3088                 LBUG();
3089         }
3090
3091         /* trigger quota release */
3092         qcids[USRQUOTA] = oa->o_uid;
3093         qcids[GRPQUOTA] = oa->o_gid;
3094         rc2 = lquota_adjust(filter_quota_interface_ref, obd, qcids, NULL, rc,
3095                             FSFILT_OP_UNLINK);
3096         if (rc2)
3097                 CERROR("filter adjust qunit! (rc:%d)\n", rc2);
3098         return rc;
3099 }
3100
3101 /* NB start and end are used for punch, but not truncate */
3102 static int filter_truncate(struct obd_export *exp, struct obd_info *oinfo,
3103                            struct obd_trans_info *oti,
3104                            struct ptlrpc_request_set *rqset)
3105 {
3106         int rc;
3107         ENTRY;
3108
3109         if (oinfo->oi_policy.l_extent.end != OBD_OBJECT_EOF) {
3110                 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
3111                        oinfo->oi_policy.l_extent.end);
3112                 RETURN(-EFAULT);
3113         }
3114
3115         CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = "LPX64
3116                ", o_size = "LPD64"\n", oinfo->oi_oa->o_id,
3117                oinfo->oi_oa->o_valid, oinfo->oi_policy.l_extent.start);
3118
3119         oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.start;
3120         rc = filter_setattr(exp, oinfo, oti);
3121         RETURN(rc);
3122 }
3123
3124 static int filter_sync(struct obd_export *exp, struct obdo *oa,
3125                        struct lov_stripe_md *lsm, obd_off start, obd_off end)
3126 {
3127         struct lvfs_run_ctxt saved;
3128         struct filter_obd *filter;
3129         struct dentry *dentry;
3130         struct llog_ctxt *ctxt;
3131         int rc, rc2;
3132         ENTRY;
3133
3134         filter = &exp->exp_obd->u.filter;
3135
3136         /* an objid of zero is taken to mean "sync whole filesystem" */
3137         if (!oa || !(oa->o_valid & OBD_MD_FLID)) {
3138                 rc = fsfilt_sync(exp->exp_obd, filter->fo_obt.obt_sb);
3139                 /* flush any remaining cancel messages out to the target */
3140                 ctxt = llog_get_context(exp->exp_obd, LLOG_MDS_OST_REPL_CTXT);
3141                 llog_sync(ctxt, exp);
3142                 RETURN(rc);
3143         }
3144
3145         dentry = filter_oa2dentry(exp->exp_obd, oa);
3146         if (IS_ERR(dentry))
3147                 RETURN(PTR_ERR(dentry));
3148
3149         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
3150
3151         LOCK_INODE_MUTEX(dentry->d_inode);
3152
3153         rc = filemap_fdatawrite(dentry->d_inode->i_mapping);
3154         if (rc == 0) {
3155                 /* just any file to grab fsync method - "file" arg unused */
3156                 struct file *file = filter->fo_rcvd_filp;
3157
3158                 if (file->f_op && file->f_op->fsync)
3159                         rc = file->f_op->fsync(NULL, dentry, 1);
3160
3161                 rc2 = filemap_fdatawait(dentry->d_inode->i_mapping);
3162                 if (!rc)
3163                         rc = rc2;
3164         }
3165         UNLOCK_INODE_MUTEX(dentry->d_inode);
3166
3167         oa->o_valid = OBD_MD_FLID;
3168         obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
3169
3170         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
3171
3172         f_dput(dentry);
3173         RETURN(rc);
3174 }
3175
3176 static int filter_get_info(struct obd_export *exp, __u32 keylen,
3177                            void *key, __u32 *vallen, void *val)
3178 {
3179         struct obd_device *obd;
3180         ENTRY;
3181
3182         obd = class_exp2obd(exp);
3183         if (obd == NULL) {
3184                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
3185                 RETURN(-EINVAL);
3186         }
3187
3188         if (keylen == strlen("blocksize") &&
3189             memcmp(key, "blocksize", keylen) == 0) {
3190                 __u32 *blocksize = val;
3191                 *vallen = sizeof(*blocksize);
3192                 *blocksize = obd->u.obt.obt_sb->s_blocksize;
3193                 RETURN(0);
3194         }
3195
3196         if (keylen == strlen("blocksize_bits") &&
3197             memcmp(key, "blocksize_bits", keylen) == 0) {
3198                 __u32 *blocksize_bits = val;
3199                 *vallen = sizeof(*blocksize_bits);
3200                 *blocksize_bits = obd->u.obt.obt_sb->s_blocksize_bits;
3201                 RETURN(0);
3202         }
3203
3204         if (keylen >= strlen("last_id") && memcmp(key, "last_id", 7) == 0) {
3205                 obd_id *last_id = val;
3206                 /* FIXME: object groups */
3207                 *last_id = filter_last_id(&obd->u.filter, 0);
3208                 RETURN(0);
3209         }
3210         CDEBUG(D_IOCTL, "invalid key\n");
3211         RETURN(-EINVAL);
3212 }
3213
3214 static int filter_set_info_async(struct obd_export *exp, __u32 keylen,
3215                                  void *key, __u32 vallen, void *val,
3216                                  struct ptlrpc_request_set *set)
3217 {
3218         struct obd_device *obd;
3219         struct llog_ctxt *ctxt;
3220         int rc = 0;
3221         ENTRY;
3222
3223         obd = exp->exp_obd;
3224         if (obd == NULL) {
3225                 CDEBUG(D_IOCTL, "invalid export %p\n", exp);
3226                 RETURN(-EINVAL);
3227         }
3228
3229         if (keylen < strlen(KEY_MDS_CONN) ||
3230             memcmp(key, KEY_MDS_CONN, keylen) != 0)
3231                 RETURN(-EINVAL);
3232
3233         LCONSOLE_WARN("%s: received MDS connection from %s\n", obd->obd_name,
3234                       obd_export_nid2str(exp));
3235         obd->u.filter.fo_mdc_conn.cookie = exp->exp_handle.h_cookie;
3236
3237         /* setup llog imports */
3238         ctxt = llog_get_context(obd, LLOG_MDS_OST_REPL_CTXT);
3239         rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
3240
3241         lquota_setinfo(filter_quota_interface_ref, exp, obd);
3242
3243         RETURN(rc);
3244 }
3245
3246 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
3247                      int len, void *karg, void *uarg)
3248 {
3249         struct obd_device *obd = exp->exp_obd;
3250         struct obd_ioctl_data *data = karg;
3251         int rc = 0;
3252
3253         switch (cmd) {
3254         case OBD_IOC_ABORT_RECOVERY: {
3255                 CERROR("aborting recovery for device %s\n", obd->obd_name);
3256                 target_abort_recovery(obd);
3257                 RETURN(0);
3258         }
3259
3260         case OBD_IOC_SYNC: {
3261                 CDEBUG(D_HA, "syncing ost %s\n", obd->obd_name);
3262                 rc = fsfilt_sync(obd, obd->u.obt.obt_sb);
3263                 RETURN(rc);
3264         }
3265
3266         case OBD_IOC_SET_READONLY: {
3267                 void *handle;
3268                 struct super_block *sb = obd->u.obt.obt_sb;
3269                 struct inode *inode = sb->s_root->d_inode;
3270                 BDEVNAME_DECLARE_STORAGE(tmp);
3271                 CERROR("*** setting device %s read-only ***\n",
3272                        ll_bdevname(sb, tmp));
3273
3274                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
3275                 if (!IS_ERR(handle))
3276                         rc = fsfilt_commit(obd, inode, handle, 1);
3277
3278                 CDEBUG(D_HA, "syncing ost %s\n", obd->obd_name);
3279                 rc = fsfilt_sync(obd, obd->u.obt.obt_sb);
3280
3281                 lvfs_set_rdonly(obd, obd->u.obt.obt_sb);
3282                 RETURN(0);
3283         }
3284
3285         case OBD_IOC_CATLOGLIST: {
3286                 rc = llog_catalog_list(obd, 1, data);
3287                 RETURN(rc);
3288         }
3289
3290         case OBD_IOC_LLOG_CANCEL:
3291         case OBD_IOC_LLOG_REMOVE:
3292         case OBD_IOC_LLOG_INFO:
3293         case OBD_IOC_LLOG_PRINT: {
3294                 /* FIXME to be finished */
3295                 RETURN(-EOPNOTSUPP);
3296 /*
3297                 struct llog_ctxt *ctxt = NULL;
3298
3299                 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
3300                 rc = llog_ioctl(ctxt, cmd, data);
3301                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
3302
3303                 RETURN(rc);
3304 */
3305         }
3306
3307
3308         default:
3309                 RETURN(-EINVAL);
3310         }
3311         RETURN(0);
3312 }
3313
3314 static int filter_health_check(struct obd_device *obd)
3315 {
3316         struct filter_obd *filter = &obd->u.filter;
3317         int rc = 0;
3318
3319         /*
3320          * health_check to return 0 on healthy
3321          * and 1 on unhealthy.
3322          */
3323         if (obd->u.obt.obt_sb->s_flags & MS_RDONLY)
3324                 rc = 1;
3325
3326         LASSERT(filter->fo_health_check_filp != NULL);
3327         rc |= !!lvfs_check_io_health(obd, filter->fo_health_check_filp);
3328
3329         return rc;
3330 }
3331
3332 static struct dentry *filter_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
3333                                              void *data)
3334 {
3335         return filter_fid2dentry(data, NULL, gr, id);
3336 }
3337
3338 static int filter_process_config(struct obd_device *obd, obd_count len, void *buf)
3339 {
3340         struct lustre_cfg *lcfg = buf;
3341         struct lprocfs_static_vars lvars;
3342         int rc = 0;
3343
3344         lprocfs_init_vars(filter, &lvars);
3345         
3346         rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, lcfg, obd);
3347
3348         return(rc);
3349 }
3350
3351 static struct lvfs_callback_ops filter_lvfs_ops = {
3352         l_fid2dentry:     filter_lvfs_fid2dentry,
3353 };
3354
3355 static struct obd_ops filter_obd_ops = {
3356         .o_owner          = THIS_MODULE,
3357         .o_get_info       = filter_get_info,
3358         .o_set_info_async = filter_set_info_async,
3359         .o_setup          = filter_setup,
3360         .o_precleanup     = filter_precleanup,
3361         .o_cleanup        = filter_cleanup,
3362         .o_connect        = filter_connect,
3363         .o_reconnect      = filter_reconnect,
3364         .o_disconnect     = filter_disconnect,
3365         .o_ping           = filter_ping,
3366         .o_init_export    = filter_init_export,
3367         .o_destroy_export = filter_destroy_export,
3368         .o_statfs         = filter_statfs,
3369         .o_getattr        = filter_getattr,
3370         .o_unpackmd       = filter_unpackmd,
3371         .o_create         = filter_create,
3372         .o_setattr        = filter_setattr,
3373         .o_destroy        = filter_destroy,
3374         .o_brw            = filter_brw,
3375         .o_punch          = filter_truncate,
3376         .o_sync           = filter_sync,
3377         .o_preprw         = filter_preprw,
3378         .o_commitrw       = filter_commitrw,
3379         .o_llog_init      = filter_llog_init,
3380         .o_llog_finish    = filter_llog_finish,
3381         .o_iocontrol      = filter_iocontrol,
3382         .o_health_check   = filter_health_check,
3383         .o_process_config = filter_process_config,
3384 };
3385
3386 quota_interface_t *filter_quota_interface_ref;
3387 extern quota_interface_t filter_quota_interface;
3388
3389 static int __init obdfilter_init(void)
3390 {
3391         struct lprocfs_static_vars lvars;
3392         int rc;
3393
3394         printk(KERN_INFO "Lustre: Filtering OBD driver; info@clusterfs.com\n");
3395
3396         lprocfs_init_vars(filter, &lvars);
3397
3398         request_module("lquota");
3399         OBD_ALLOC(obdfilter_created_scratchpad,
3400                   OBDFILTER_CREATED_SCRATCHPAD_ENTRIES *
3401                   sizeof(*obdfilter_created_scratchpad));
3402         if (obdfilter_created_scratchpad == NULL)
3403                 return -ENOMEM;
3404
3405         ll_fmd_cachep = kmem_cache_create("ll_fmd_cache",
3406                                          sizeof(struct filter_mod_data),
3407                                          0, 0, NULL, NULL);
3408         if (!ll_fmd_cachep)
3409                 GOTO(out, rc = -ENOMEM);
3410
3411         filter_quota_interface_ref = PORTAL_SYMBOL_GET(filter_quota_interface);
3412         init_obd_quota_ops(filter_quota_interface_ref, &filter_obd_ops);
3413
3414         rc = class_register_type(&filter_obd_ops, lvars.module_vars,
3415                                  LUSTRE_OST_NAME);
3416         if (rc) {
3417                 int err;
3418
3419                 err = kmem_cache_destroy(ll_fmd_cachep);
3420                 LASSERTF(err == 0, "Cannot destroy ll_fmd_cachep: rc %d\n",err);
3421                 ll_fmd_cachep = NULL;
3422 out:
3423                 if (filter_quota_interface_ref)
3424                         PORTAL_SYMBOL_PUT(filter_quota_interface);
3425
3426                 OBD_FREE(obdfilter_created_scratchpad,
3427                          OBDFILTER_CREATED_SCRATCHPAD_ENTRIES *
3428                          sizeof(*obdfilter_created_scratchpad));
3429         }
3430
3431         return rc;
3432 }
3433
3434 static void __exit obdfilter_exit(void)
3435 {
3436         if (filter_quota_interface_ref)
3437                 PORTAL_SYMBOL_PUT(filter_quota_interface);
3438
3439         if (ll_fmd_cachep) {
3440                 int rc = kmem_cache_destroy(ll_fmd_cachep);
3441                 LASSERTF(rc == 0, "Cannot destroy ll_fmd_cachep: rc %d\n", rc);
3442                 ll_fmd_cachep = NULL;
3443         }
3444
3445         class_unregister_type(LUSTRE_OST_NAME);
3446         OBD_FREE(obdfilter_created_scratchpad,
3447                  OBDFILTER_CREATED_SCRATCHPAD_ENTRIES *
3448                  sizeof(*obdfilter_created_scratchpad));
3449 }
3450
3451 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3452 MODULE_DESCRIPTION("Lustre Filtering OBD driver");
3453 MODULE_LICENSE("GPL");
3454
3455 module_init(obdfilter_init);
3456 module_exit(obdfilter_exit);