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