Whamcloud - gitweb
86a7f5fc5307538cb3a3156d4fbec4b4cb4781ba
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdfilter/filter.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  */
41
42 /*
43  * Invariant: Get O/R i_mutex for lookup, if needed, before any journal ops
44  *            (which need to get journal_lock, may block if journal full).
45  *
46  * Invariant: Call filter_start_transno() before any journal ops to avoid the
47  *            same deadlock problem.  We can (and want) to get rid of the
48  *            transno sem in favour of the dir/inode i_mutex to avoid single
49  *            threaded operation on the OST.
50  */
51
52 #define DEBUG_SUBSYSTEM S_FILTER
53 #ifndef AUTOCONF_INCLUDED
54 #include <linux/config.h>
55 #endif
56 #include <linux/module.h>
57 #include <linux/fs.h>
58 #include <linux/dcache.h>
59 #include <linux/init.h>
60 #include <linux/version.h>
61 #include <linux/sched.h>
62 #include <linux/mount.h>
63 #include <linux/buffer_head.h>
64
65 #include <obd_class.h>
66 #include <obd_lov.h>
67 #include <lustre_dlm.h>
68 #include <lustre_fsfilt.h>
69 #include <lprocfs_status.h>
70 #include <lustre_log.h>
71 #include <libcfs/list.h>
72 #include <lustre_disk.h>
73 #include <lustre_quota.h>
74 #include <linux/slab.h>
75 #include <lustre_param.h>
76 #include <lustre/ll_fiemap.h>
77
78 #include "filter_internal.h"
79
80 static struct lvfs_callback_ops filter_lvfs_ops;
81 cfs_mem_cache_t *ll_fmd_cachep;
82
83 static void filter_commit_cb(struct obd_device *obd, __u64 transno,
84                              void *cb_data, int error)
85 {
86         struct obd_export *exp = cb_data;
87         LASSERTF(exp->exp_obd == obd,
88                  "%s: bad export (%p), obd (%p) != exp->exp_obd (%p)\n",
89                  obd->obd_name, exp, obd, exp->exp_obd);
90         obd_transno_commit_cb(obd, transno, exp, error);
91         class_export_put(exp);
92 }
93
94 int filter_version_get_check(struct obd_export *exp,
95                              struct obd_trans_info *oti, struct inode *inode)
96 {
97         __u64 curr_version;
98
99         if (inode == NULL || oti == NULL)
100                 RETURN(0);
101
102         curr_version = fsfilt_get_version(exp->exp_obd, inode);
103         if ((__s64)curr_version == -EOPNOTSUPP)
104                 RETURN(0);
105         /* VBR: version is checked always because costs nothing */
106         if (oti->oti_pre_version != 0 &&
107             oti->oti_pre_version != curr_version) {
108                 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
109                        oti->oti_pre_version, curr_version);
110                 spin_lock(&exp->exp_lock);
111                 exp->exp_vbr_failed = 1;
112                 spin_unlock(&exp->exp_lock);
113                 RETURN (-EOVERFLOW);
114         }
115         oti->oti_pre_version = curr_version;
116         RETURN(0);
117 }
118
119 /* Assumes caller has already pushed us into the kernel context. */
120 int filter_finish_transno(struct obd_export *exp, struct inode *inode,
121                           struct obd_trans_info *oti,
122                           int rc, int force_sync)
123 {
124         struct filter_obd *filter = &exp->exp_obd->u.filter;
125         struct filter_export_data *fed = &exp->exp_filter_data;
126         struct lsd_client_data *lcd = fed->fed_lcd;
127         __u64 last_rcvd;
128         loff_t off;
129         int err, log_pri = D_RPCTRACE;
130
131         /* Propagate error code. */
132         if (rc)
133                 RETURN(rc);
134
135         if (!exp->exp_obd->obd_replayable || oti == NULL)
136                 RETURN(rc);
137
138         /* we don't allocate new transnos for replayed requests */
139         spin_lock(&filter->fo_translock);
140         if (oti->oti_transno == 0) {
141                 last_rcvd = le64_to_cpu(filter->fo_fsd->lsd_last_transno) + 1;
142                 filter->fo_fsd->lsd_last_transno = cpu_to_le64(last_rcvd);
143         } else {
144                 last_rcvd = oti->oti_transno;
145                 if (last_rcvd > le64_to_cpu(filter->fo_fsd->lsd_last_transno))
146                         filter->fo_fsd->lsd_last_transno = cpu_to_le64(last_rcvd);
147         }
148         oti->oti_transno = last_rcvd;
149         if (last_rcvd <= le64_to_cpu(lcd->lcd_last_transno)) {
150                 spin_unlock(&filter->fo_translock);
151                 LBUG();
152         }
153         lcd->lcd_last_transno = cpu_to_le64(last_rcvd);
154         lcd->lcd_pre_versions[0] = cpu_to_le64(oti->oti_pre_version);
155         lcd->lcd_last_xid = cpu_to_le64(oti->oti_xid);
156         target_trans_table_update(exp, last_rcvd);
157
158         spin_unlock(&filter->fo_translock);
159
160         if (inode)
161                 fsfilt_set_version(exp->exp_obd, inode, last_rcvd);
162
163         off = fed->fed_lr_off;
164         if (off <= 0) {
165                 CERROR("%s: client idx %d is %lld\n", exp->exp_obd->obd_name,
166                        fed->fed_lr_idx, fed->fed_lr_off);
167                 err = -EINVAL;
168         } else {
169                 class_export_get(exp); /* released when the cb is called */
170                 if (!force_sync)
171                         force_sync = fsfilt_add_journal_cb(exp->exp_obd,
172                                                            last_rcvd,
173                                                            oti->oti_handle,
174                                                            filter_commit_cb,
175                                                            exp);
176
177                 err = fsfilt_write_record(exp->exp_obd, filter->fo_rcvd_filp,
178                                           lcd, sizeof(*lcd), &off,
179                                           force_sync | exp->exp_need_sync);
180                 if (force_sync)
181                         filter_commit_cb(exp->exp_obd, last_rcvd, exp, err);
182         }
183         if (err) {
184                 log_pri = D_ERROR;
185                 if (rc == 0)
186                         rc = err;
187         }
188
189         CDEBUG(log_pri, "wrote trans "LPU64" for client %s at #%d: err = %d\n",
190                last_rcvd, lcd->lcd_uuid, fed->fed_lr_idx, err);
191
192         RETURN(rc);
193 }
194
195 void f_dput(struct dentry *dentry)
196 {
197         /* Can't go inside filter_ddelete because it can block */
198         CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
199                dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
200         LASSERT(atomic_read(&dentry->d_count) > 0);
201
202         dput(dentry);
203 }
204
205 static void init_brw_stats(struct brw_stats *brw_stats)
206 {
207         int i;
208         for (i = 0; i < BRW_LAST; i++)
209                 spin_lock_init(&brw_stats->hist[i].oh_lock);
210 }
211
212 static int lprocfs_init_rw_stats(struct obd_device *obd,
213                                  struct lprocfs_stats **stats)
214 {
215         int num_stats;
216
217         num_stats = (sizeof(*obd->obd_type->typ_ops) / sizeof(void *)) +
218                                                         LPROC_FILTER_LAST - 1;
219         *stats = lprocfs_alloc_stats(num_stats, LPROCFS_STATS_FLAG_NOPERCPU);
220         if (*stats == NULL)
221                 return -ENOMEM;
222
223         lprocfs_init_ops_stats(LPROC_FILTER_LAST, *stats);
224         lprocfs_counter_init(*stats, LPROC_FILTER_READ_BYTES,
225                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
226         lprocfs_counter_init(*stats, LPROC_FILTER_WRITE_BYTES,
227                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
228
229         return(0);
230 }
231
232 /* brw_stats are 2128, ops are 3916, ldlm are 204, so 6248 bytes per client,
233    plus the procfs overhead :( */
234 static int filter_export_stats_init(struct obd_device *obd,
235                                     struct obd_export *exp,
236                                     int reconnect,
237                                     void *client_nid)
238 {
239         struct proc_dir_entry *brw_entry;
240         int rc, newnid = 0;
241         ENTRY;
242
243         if (obd_uuid_equals(&exp->exp_client_uuid, &obd->obd_uuid))
244                 /* Self-export gets no proc entry */
245                 RETURN(0);
246         rc = lprocfs_exp_setup(exp, (lnet_nid_t *)client_nid,
247                                reconnect, &newnid);
248         if (rc) {
249                 /* Mask error for already created
250                  * /proc entries */
251                 if (rc == -EALREADY)
252                         rc = 0;
253
254                 RETURN(rc);
255         }
256
257         if (newnid) {
258                 struct nid_stat *tmp = exp->exp_nid_stats;
259                 LASSERT(tmp != NULL);
260
261                 OBD_ALLOC(tmp->nid_brw_stats, sizeof(struct brw_stats));
262                 if (tmp->nid_brw_stats == NULL)
263                         RETURN(-ENOMEM);
264
265                 init_brw_stats(tmp->nid_brw_stats);
266
267                 brw_entry = create_proc_entry("brw_stats", 0644,
268                                               exp->exp_nid_stats->nid_proc);
269                 if (brw_entry == NULL)
270                        RETURN(-ENOMEM);
271
272                 brw_entry->proc_fops = &filter_per_nid_stats_fops;
273                 brw_entry->data = exp->exp_nid_stats;
274
275                 rc = lprocfs_init_rw_stats(obd, &exp->exp_nid_stats->nid_stats);
276                 if (rc)
277                         RETURN(rc);
278
279                 rc = lprocfs_register_stats(tmp->nid_proc, "stats",
280                                             tmp->nid_stats);
281                 if (rc)
282                         RETURN(rc);
283
284                 /* Always add in ldlm_stats */
285                 tmp->nid_ldlm_stats =
286                         lprocfs_alloc_stats(LDLM_LAST_OPC - LDLM_FIRST_OPC,
287                                             LPROCFS_STATS_FLAG_NOPERCPU);
288                 if (tmp->nid_ldlm_stats == NULL)
289                         return -ENOMEM;
290
291                 lprocfs_init_ldlm_stats(tmp->nid_ldlm_stats);
292
293                 rc = lprocfs_register_stats(tmp->nid_proc, "ldlm_stats",
294                                             tmp->nid_ldlm_stats);
295         }
296
297         RETURN(0);
298 }
299
300 /* VBR: to determine the delayed client the lcd should be updated for each new
301  * epoch */
302 static int filter_update_client_epoch(struct obd_export *exp)
303 {
304         struct filter_export_data *fed = &exp->exp_filter_data;
305         struct filter_obd *filter = &exp->exp_obd->u.filter;
306         struct lvfs_run_ctxt saved;
307         loff_t off = fed->fed_lr_off;
308         int rc = 0;
309
310         /* VBR: set client last_epoch to current epoch */
311         if (le32_to_cpu(fed->fed_lcd->lcd_last_epoch) >=
312                         le32_to_cpu(filter->fo_fsd->lsd_start_epoch))
313                 return rc;
314         fed->fed_lcd->lcd_last_epoch = filter->fo_fsd->lsd_start_epoch;
315         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
316         rc = fsfilt_write_record(exp->exp_obd, filter->fo_rcvd_filp,
317                                  fed->fed_lcd, sizeof(*fed->fed_lcd), &off,
318                                  exp->exp_delayed);
319         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
320
321         CDEBUG(D_INFO, "update client idx %u last_epoch %#x (%#x)\n",
322                fed->fed_lr_idx, le32_to_cpu(fed->fed_lcd->lcd_last_epoch),
323                le32_to_cpu(filter->fo_fsd->lsd_start_epoch));
324
325         return rc;
326 }
327
328 /* Called after recovery is done on server */
329 static void filter_update_last_epoch(struct obd_device *obd)
330 {
331         struct ptlrpc_request *req;
332         struct filter_obd *filter = &obd->u.filter;
333         struct lr_server_data *fsd = filter->fo_fsd;
334         __u32 start_epoch;
335
336         /* Increase server epoch after recovery */
337         spin_lock(&filter->fo_translock);
338         /* VBR: increase the epoch and store it in lsd */
339         start_epoch = lr_epoch(le64_to_cpu(fsd->lsd_last_transno)) + 1;
340         fsd->lsd_last_transno = cpu_to_le64((__u64)start_epoch << LR_EPOCH_BITS);
341         fsd->lsd_start_epoch = cpu_to_le32(start_epoch);
342         spin_unlock(&filter->fo_translock);
343
344         /* go through delayed reply queue to find all exports participate in
345          * recovery and set new epoch for them */
346         list_for_each_entry(req, &obd->obd_delayed_reply_queue, rq_list) {
347                 LASSERT(!req->rq_export->exp_delayed);
348                 filter_update_client_epoch(req->rq_export);
349         }
350         filter_update_server_data(obd, filter->fo_rcvd_filp, fsd, 1);
351 }
352
353 static int filter_postrecov(struct obd_device *obd)
354 {
355         ENTRY;
356
357         if (obd->obd_fail)
358                 RETURN(0);
359
360         LASSERT(!obd->obd_recovering);
361         /* VBR: update start_epoch on server */
362         filter_update_last_epoch(obd);
363
364         RETURN(0);
365 }
366
367 /* Add client data to the FILTER.  We use a bitmap to locate a free space
368  * in the last_rcvd file if cl_idx is -1 (i.e. a new client).
369  * Otherwise, we have just read the data from the last_rcvd file and
370  * we know its offset. */
371 static int filter_client_add(struct obd_device *obd, struct obd_export *exp,
372                              int cl_idx)
373 {
374         struct filter_obd *filter = &obd->u.filter;
375         struct filter_export_data *fed = &exp->exp_filter_data;
376         unsigned long *bitmap = filter->fo_last_rcvd_slots;
377         int new_client = (cl_idx == -1);
378
379         ENTRY;
380
381         LASSERT(bitmap != NULL);
382         LASSERTF(cl_idx > -2, "%d\n", cl_idx);
383
384         /* Self-export */
385         if (strcmp(fed->fed_lcd->lcd_uuid, obd->obd_uuid.uuid) == 0)
386                 RETURN(0);
387
388         /* VBR: remove expired exports before searching for free slot */
389         if (new_client)
390                 class_disconnect_expired_exports(obd);
391
392         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
393          * there's no need for extra complication here
394          */
395         if (new_client) {
396                 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
397         repeat:
398                 if (cl_idx >= LR_MAX_CLIENTS) {
399                         CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
400                                cl_idx);
401                         RETURN(-EOVERFLOW);
402                 }
403                 if (test_and_set_bit(cl_idx, bitmap)) {
404                         cl_idx = find_next_zero_bit(bitmap, LR_MAX_CLIENTS,
405                                                     cl_idx);
406                         goto repeat;
407                 }
408         } else {
409                 if (test_and_set_bit(cl_idx, bitmap)) {
410                         CERROR("FILTER client %d: bit already set in bitmap!\n",
411                                cl_idx);
412                         LBUG();
413                 }
414         }
415
416         fed->fed_lr_idx = cl_idx;
417         fed->fed_lr_off = le32_to_cpu(filter->fo_fsd->lsd_client_start) +
418                 cl_idx * le16_to_cpu(filter->fo_fsd->lsd_client_size);
419         LASSERTF(fed->fed_lr_off > 0, "fed_lr_off = %llu\n", fed->fed_lr_off);
420
421         CDEBUG(D_INFO, "client at index %d (%llu) with UUID '%s' added\n",
422                fed->fed_lr_idx, fed->fed_lr_off, fed->fed_lcd->lcd_uuid);
423
424         if (new_client) {
425                 struct lvfs_run_ctxt saved;
426                 loff_t off = fed->fed_lr_off;
427                 void *handle;
428                 int rc;
429
430                 CDEBUG(D_INFO, "writing client lcd at idx %u (%llu) (len %u)\n",
431                        fed->fed_lr_idx,off,(unsigned int)sizeof(*fed->fed_lcd));
432
433                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
434                 /* Transaction needed to fix bug 1403 */
435                 handle = fsfilt_start(obd,
436                                       filter->fo_rcvd_filp->f_dentry->d_inode,
437                                       FSFILT_OP_SETATTR, NULL);
438                 if (IS_ERR(handle)) {
439                         rc = PTR_ERR(handle);
440                         CERROR("unable to start transaction: rc %d\n", rc);
441                 } else {
442                         fed->fed_lcd->lcd_last_epoch =
443                                               filter->fo_fsd->lsd_start_epoch;
444                         exp->exp_last_request_time = cfs_time_current_sec();
445                         rc = fsfilt_add_journal_cb(obd, 0, handle,
446                                                    target_client_add_cb, exp);
447                         if (rc == 0) {
448                                 spin_lock(&exp->exp_lock);
449                                 exp->exp_need_sync = 1;
450                                 spin_unlock(&exp->exp_lock);
451                         }
452                         rc = fsfilt_write_record(obd, filter->fo_rcvd_filp,
453                                                  fed->fed_lcd,
454                                                  sizeof(*fed->fed_lcd),
455                                                  &off, rc /* sync if no cb */);
456                         fsfilt_commit(obd,
457                                       filter->fo_rcvd_filp->f_dentry->d_inode,
458                                       handle, 0);
459                 }
460                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
461
462                 if (rc) {
463                         CERROR("error writing %s client idx %u: rc %d\n",
464                                LAST_RCVD, fed->fed_lr_idx, rc);
465                         RETURN(rc);
466                 }
467         }
468         RETURN(0);
469 }
470
471 struct lsd_client_data zero_lcd; /* globals are implicitly zeroed */
472
473 static int filter_client_free(struct obd_export *exp)
474 {
475         struct filter_export_data *fed = &exp->exp_filter_data;
476         struct filter_obd *filter = &exp->exp_obd->u.filter;
477         struct obd_device *obd = exp->exp_obd;
478         struct lvfs_run_ctxt saved;
479         int rc;
480         loff_t off;
481         ENTRY;
482
483         if (fed->fed_lcd == NULL)
484                 RETURN(0);
485
486         /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
487         if (strcmp(fed->fed_lcd->lcd_uuid, obd->obd_uuid.uuid ) == 0)
488                 GOTO(free, 0);
489
490         LASSERT(filter->fo_last_rcvd_slots != NULL);
491
492         off = fed->fed_lr_off;
493
494         CDEBUG(D_INFO, "freeing client at idx %u, offset %lld with UUID '%s'\n",
495                fed->fed_lr_idx, fed->fed_lr_off, fed->fed_lcd->lcd_uuid);
496
497         /* Don't clear fed_lr_idx here as it is likely also unset.  At worst
498          * we leak a client slot that will be cleaned on the next recovery. */
499         if (off <= 0) {
500                 CERROR("%s: client idx %d has med_off %lld\n",
501                        obd->obd_name, fed->fed_lr_idx, off);
502                 GOTO(free, rc = -EINVAL);
503         }
504
505         /* Clear the bit _after_ zeroing out the client so we don't
506            race with filter_client_add and zero out new clients.*/
507         if (!test_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
508                 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
509                        fed->fed_lr_idx);
510                 LBUG();
511         }
512
513         if (!(exp->exp_flags & OBD_OPT_FAILOVER)) {
514                 /* Don't force sync on disconnect if aborting recovery,
515                  * or it does num_clients * num_osts.  b=17194 */
516                 int need_sync = exp->exp_need_sync &&
517                                 !(exp->exp_flags&OBD_OPT_ABORT_RECOV);
518
519                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
520                 rc = fsfilt_write_record(obd, filter->fo_rcvd_filp, &zero_lcd,
521                                          sizeof(zero_lcd), &off, 0);
522
523                 /* Make sure the server's last_transno is up to date. Do this
524                  * after the client is freed so we know all the client's
525                  * transactions have been committed. */
526                 if (rc == 0)
527                         filter_update_server_data(obd, filter->fo_rcvd_filp,
528                                                   filter->fo_fsd, need_sync);
529                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
530
531                 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
532                        "zero out client %s at idx %u/%llu in %s %ssync rc %d\n",
533                        fed->fed_lcd->lcd_uuid, fed->fed_lr_idx, fed->fed_lr_off,
534                        LAST_RCVD, need_sync ? "" : "a", rc);
535         }
536
537         if (!test_and_clear_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
538                 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
539                        fed->fed_lr_idx);
540                 LBUG();
541         }
542
543         EXIT;
544 free:
545         OBD_FREE_PTR(fed->fed_lcd);
546         fed->fed_lcd = NULL;
547
548         return 0;
549 }
550
551 /* drop fmd reference, free it if last ref. must be called with fed_lock held.*/
552 static inline void filter_fmd_put_nolock(struct filter_export_data *fed,
553                                          struct filter_mod_data *fmd)
554 {
555         LASSERT_SPIN_LOCKED(&fed->fed_lock);
556         if (--fmd->fmd_refcount == 0) {
557                 /* XXX when we have persistent reservations and the handle
558                  * is stored herein we need to drop it here. */
559                 fed->fed_mod_count--;
560                 list_del(&fmd->fmd_list);
561                 OBD_SLAB_FREE(fmd, ll_fmd_cachep, sizeof(*fmd));
562         }
563 }
564
565 /* drop fmd reference, free it if last ref */
566 void filter_fmd_put(struct obd_export *exp, struct filter_mod_data *fmd)
567 {
568         struct filter_export_data *fed;
569
570         if (fmd == NULL)
571                 return;
572
573         fed = &exp->exp_filter_data;
574         spin_lock(&fed->fed_lock);
575         filter_fmd_put_nolock(fed, fmd); /* caller reference */
576         spin_unlock(&fed->fed_lock);
577 }
578
579 /* expire entries from the end of the list if there are too many
580  * or they are too old */
581 static void filter_fmd_expire_nolock(struct filter_obd *filter,
582                                      struct filter_export_data *fed,
583                                      struct filter_mod_data *keep)
584 {
585         struct filter_mod_data *fmd, *tmp;
586
587         list_for_each_entry_safe(fmd, tmp, &fed->fed_mod_list, fmd_list) {
588                 if (fmd == keep)
589                         break;
590
591                 if (time_before(jiffies, fmd->fmd_expire) &&
592                     fed->fed_mod_count < filter->fo_fmd_max_num)
593                         break;
594
595                 list_del_init(&fmd->fmd_list);
596                 filter_fmd_put_nolock(fed, fmd); /* list reference */
597         }
598 }
599
600 void filter_fmd_expire(struct obd_export *exp)
601 {
602         spin_lock(&exp->exp_filter_data.fed_lock);
603         filter_fmd_expire_nolock(&exp->exp_obd->u.filter,
604                                  &exp->exp_filter_data, NULL);
605         spin_unlock(&exp->exp_filter_data.fed_lock);
606 }
607
608 /* find specified objid, group in export fmd list.
609  * caller must hold fed_lock and take fmd reference itself */
610 static struct filter_mod_data *filter_fmd_find_nolock(struct filter_obd *filter,
611                                                 struct filter_export_data *fed,
612                                                 obd_id objid, obd_gr group)
613 {
614         struct filter_mod_data *found = NULL, *fmd;
615
616         LASSERT_SPIN_LOCKED(&fed->fed_lock);
617
618         list_for_each_entry_reverse(fmd, &fed->fed_mod_list, fmd_list) {
619                 if (fmd->fmd_id == objid && fmd->fmd_gr == group) {
620                         found = fmd;
621                         list_del(&fmd->fmd_list);
622                         list_add_tail(&fmd->fmd_list, &fed->fed_mod_list);
623                         fmd->fmd_expire = jiffies + filter->fo_fmd_max_age;
624                         break;
625                 }
626         }
627
628         filter_fmd_expire_nolock(filter, fed, found);
629
630         return found;
631 }
632
633 /* Find fmd based on objid and group, or return NULL if not found. */
634 struct filter_mod_data *filter_fmd_find(struct obd_export *exp,
635                                         obd_id objid, obd_gr group)
636 {
637         struct filter_mod_data *fmd;
638
639         spin_lock(&exp->exp_filter_data.fed_lock);
640         fmd = filter_fmd_find_nolock(&exp->exp_obd->u.filter,
641                                      &exp->exp_filter_data, objid, group);
642         if (fmd)
643                 fmd->fmd_refcount++;    /* caller reference */
644         spin_unlock(&exp->exp_filter_data.fed_lock);
645
646         return fmd;
647 }
648
649 /* Find fmd based on objid and group, or create a new one if none is found.
650  * It is possible for this function to return NULL under memory pressure,
651  * or if objid = 0 is passed (which will only cause old entries to expire).
652  * Currently this is not fatal because any fmd state is transient and
653  * may also be freed when it gets sufficiently old. */
654 struct filter_mod_data *filter_fmd_get(struct obd_export *exp,
655                                        obd_id objid, obd_gr group)
656 {
657         struct filter_export_data *fed = &exp->exp_filter_data;
658         struct filter_mod_data *found = NULL, *fmd_new = NULL;
659
660         OBD_SLAB_ALLOC(fmd_new, ll_fmd_cachep, CFS_ALLOC_IO, sizeof(*fmd_new));
661
662         spin_lock(&fed->fed_lock);
663         found = filter_fmd_find_nolock(&exp->exp_obd->u.filter,fed,objid,group);
664         if (fmd_new) {
665                 if (found == NULL) {
666                         list_add_tail(&fmd_new->fmd_list, &fed->fed_mod_list);
667                         fmd_new->fmd_id = objid;
668                         fmd_new->fmd_gr = group;
669                         fmd_new->fmd_refcount++;   /* list reference */
670                         found = fmd_new;
671                         fed->fed_mod_count++;
672                 } else {
673                         OBD_SLAB_FREE(fmd_new, ll_fmd_cachep, sizeof(*fmd_new));
674                 }
675         }
676         if (found) {
677                 found->fmd_refcount++;          /* caller reference */
678                 found->fmd_expire = jiffies +
679                         exp->exp_obd->u.filter.fo_fmd_max_age;
680         }
681
682         spin_unlock(&fed->fed_lock);
683
684         return found;
685 }
686
687 #ifdef DO_FMD_DROP
688 /* drop fmd list reference so it will disappear when last reference is put.
689  * This isn't so critical because it would in fact only affect the one client
690  * that is doing the unlink and at worst we have an stale entry referencing
691  * an object that should never be used again. */
692 static void filter_fmd_drop(struct obd_export *exp, obd_id objid, obd_gr group)
693 {
694         struct filter_mod_data *found = NULL;
695
696         spin_lock(&exp->exp_filter_data.fed_lock);
697         found = filter_fmd_find_nolock(&exp->exp_filter_data, objid, group);
698         if (found) {
699                 list_del_init(&found->fmd_list);
700                 filter_fmd_put_nolock(&exp->exp_filter_data, found);
701         }
702         spin_unlock(&exp->exp_filter_data.fed_lock);
703 }
704 #else
705 #define filter_fmd_drop(exp, objid, group)
706 #endif
707
708 /* remove all entries from fmd list */
709 static void filter_fmd_cleanup(struct obd_export *exp)
710 {
711         struct filter_export_data *fed = &exp->exp_filter_data;
712         struct filter_mod_data *fmd = NULL, *tmp;
713
714         spin_lock(&fed->fed_lock);
715         list_for_each_entry_safe(fmd, tmp, &fed->fed_mod_list, fmd_list) {
716                 list_del_init(&fmd->fmd_list);
717                 filter_fmd_put_nolock(fed, fmd);
718         }
719         spin_unlock(&fed->fed_lock);
720 }
721
722 static int filter_init_export(struct obd_export *exp)
723 {
724         spin_lock_init(&exp->exp_filter_data.fed_lock);
725         INIT_LIST_HEAD(&exp->exp_filter_data.fed_mod_list);
726
727         spin_lock(&exp->exp_lock);
728         exp->exp_connecting = 1;
729         spin_unlock(&exp->exp_lock);
730
731         return ldlm_init_export(exp);
732 }
733
734 static int filter_free_server_data(struct filter_obd *filter)
735 {
736         OBD_FREE(filter->fo_fsd, sizeof(*filter->fo_fsd));
737         filter->fo_fsd = NULL;
738         OBD_FREE(filter->fo_last_rcvd_slots, LR_MAX_CLIENTS / 8);
739         filter->fo_last_rcvd_slots = NULL;
740         return 0;
741 }
742
743 /* assumes caller is already in kernel ctxt */
744 int filter_update_server_data(struct obd_device *obd, struct file *filp,
745                               struct lr_server_data *fsd, int force_sync)
746 {
747         loff_t off = 0;
748         int rc;
749         ENTRY;
750
751         CDEBUG(D_INODE, "server uuid      : %s\n", fsd->lsd_uuid);
752         CDEBUG(D_INODE, "server last_rcvd : "LPU64"\n",
753                le64_to_cpu(fsd->lsd_last_transno));
754         CDEBUG(D_INODE, "server last_mount: "LPU64"\n",
755                le64_to_cpu(fsd->lsd_mount_count));
756
757         fsd->lsd_compat14 = fsd->lsd_last_transno;
758         rc = fsfilt_write_record(obd, filp, fsd, sizeof(*fsd), &off,force_sync);
759         if (rc)
760                 CERROR("error writing lr_server_data: rc = %d\n", rc);
761
762         RETURN(rc);
763 }
764
765 int filter_update_last_objid(struct obd_device *obd, obd_gr group,
766                              int force_sync)
767 {
768         struct filter_obd *filter = &obd->u.filter;
769         __u64 tmp;
770         loff_t off = 0;
771         int rc;
772         ENTRY;
773
774         if (filter->fo_last_objid_files[group] == NULL) {
775                 CERROR("Object group "LPU64" not fully setup; not updating "
776                        "last_objid\n", group);
777                 RETURN(-EINVAL);
778         }
779
780         CDEBUG(D_INODE, "%s: server last_objid for group "LPU64": "LPU64"\n",
781                obd->obd_name, group, filter->fo_last_objids[group]);
782
783         tmp = cpu_to_le64(filter->fo_last_objids[group]);
784         rc = fsfilt_write_record(obd, filter->fo_last_objid_files[group],
785                                  &tmp, sizeof(tmp), &off, force_sync);
786         if (rc)
787                 CERROR("error writing group "LPU64" last objid: rc = %d\n",
788                        group, rc);
789         RETURN(rc);
790 }
791
792 /* assumes caller has already in kernel ctxt */
793 static int filter_init_server_data(struct obd_device *obd, struct file * filp)
794 {
795         struct filter_obd *filter = &obd->u.filter;
796         struct lr_server_data *fsd;
797         struct lsd_client_data *lcd = NULL;
798         struct inode *inode = filp->f_dentry->d_inode;
799         unsigned long last_rcvd_size = i_size_read(inode);
800         struct lustre_mount_info *lmi;
801         __u64 mount_count;
802         __u32 start_epoch;
803         int cl_idx;
804         loff_t off = 0;
805         int rc;
806
807         /* ensure padding in the struct is the correct size */
808         CLASSERT (offsetof(struct lr_server_data, lsd_padding) +
809                  sizeof(fsd->lsd_padding) == LR_SERVER_SIZE);
810         CLASSERT (offsetof(struct lsd_client_data, lcd_padding) +
811                  sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
812
813         OBD_ALLOC(fsd, sizeof(*fsd));
814         if (!fsd)
815                 RETURN(-ENOMEM);
816         filter->fo_fsd = fsd;
817
818         OBD_ALLOC(filter->fo_last_rcvd_slots, LR_MAX_CLIENTS / 8);
819         if (filter->fo_last_rcvd_slots == NULL) {
820                 OBD_FREE(fsd, sizeof(*fsd));
821                 RETURN(-ENOMEM);
822         }
823
824         if (last_rcvd_size == 0) {
825                 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
826
827                 memcpy(fsd->lsd_uuid, obd->obd_uuid.uuid,sizeof(fsd->lsd_uuid));
828                 fsd->lsd_last_transno = 0;
829                 mount_count = fsd->lsd_mount_count = 0;
830                 fsd->lsd_server_size = cpu_to_le32(LR_SERVER_SIZE);
831                 fsd->lsd_client_start = cpu_to_le32(LR_CLIENT_START);
832                 fsd->lsd_client_size = cpu_to_le16(LR_CLIENT_SIZE);
833                 fsd->lsd_subdir_count = cpu_to_le16(FILTER_SUBDIR_COUNT);
834                 filter->fo_subdir_count = FILTER_SUBDIR_COUNT;
835                 fsd->lsd_feature_incompat = cpu_to_le32(OBD_INCOMPAT_OST);
836         } else {
837                 rc = fsfilt_read_record(obd, filp, fsd, sizeof(*fsd), &off);
838                 if (rc) {
839                         CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
840                                LAST_RCVD, rc);
841                         GOTO(err_fsd, rc);
842                 }
843                 if (strcmp(fsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
844                         LCONSOLE_ERROR_MSG(0x134, "Trying to start OBD %s using"
845                                            " the wrong disk %s. Were the /dev/ "
846                                            "assignments rearranged?\n",
847                                            obd->obd_uuid.uuid, fsd->lsd_uuid);
848                         GOTO(err_fsd, rc = -EINVAL);
849                 }
850                 mount_count = le64_to_cpu(fsd->lsd_mount_count);
851                 filter->fo_subdir_count = le16_to_cpu(fsd->lsd_subdir_count);
852                 /* COMPAT_146 */
853                 /* Assume old last_rcvd format unless I_C_LR is set */
854                 if (!(fsd->lsd_feature_incompat &
855                       cpu_to_le32(OBD_INCOMPAT_COMMON_LR)))
856                         fsd->lsd_last_transno = fsd->lsd_compat14;
857                 /* end COMPAT_146 */
858         }
859
860         if (fsd->lsd_feature_incompat & ~cpu_to_le32(FILTER_INCOMPAT_SUPP)) {
861                 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
862                        obd->obd_name, le32_to_cpu(fsd->lsd_feature_incompat) &
863                        ~FILTER_INCOMPAT_SUPP);
864                 GOTO(err_fsd, rc = -EINVAL);
865         }
866         if (fsd->lsd_feature_rocompat & ~cpu_to_le32(FILTER_ROCOMPAT_SUPP)) {
867                 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
868                        obd->obd_name, le32_to_cpu(fsd->lsd_feature_rocompat) &
869                        ~FILTER_ROCOMPAT_SUPP);
870                 /* Do something like remount filesystem read-only */
871                 GOTO(err_fsd, rc = -EINVAL);
872         }
873
874         target_trans_table_init(obd);
875         start_epoch = le32_to_cpu(fsd->lsd_start_epoch);
876
877         CDEBUG(D_INODE, "%s: server start_epoch : %#x\n",
878                obd->obd_name, start_epoch);
879         CDEBUG(D_INODE, "%s: server last_transno : "LPX64"\n",
880                obd->obd_name, le64_to_cpu(fsd->lsd_last_transno));
881         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
882                obd->obd_name, mount_count + 1);
883         CDEBUG(D_INODE, "%s: server data size: %u\n",
884                obd->obd_name, le32_to_cpu(fsd->lsd_server_size));
885         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
886                obd->obd_name, le32_to_cpu(fsd->lsd_client_start));
887         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
888                obd->obd_name, le32_to_cpu(fsd->lsd_client_size));
889         CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
890                obd->obd_name, le16_to_cpu(fsd->lsd_subdir_count));
891         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
892                last_rcvd_size <= le32_to_cpu(fsd->lsd_client_start) ? 0 :
893                (last_rcvd_size - le32_to_cpu(fsd->lsd_client_start)) /
894                 le16_to_cpu(fsd->lsd_client_size));
895
896         if (!obd->obd_replayable) {
897                 CWARN("%s: recovery support OFF\n", obd->obd_name);
898                 GOTO(out, rc = 0);
899         }
900
901         for (cl_idx = 0, off = le32_to_cpu(fsd->lsd_client_start);
902              off < last_rcvd_size; cl_idx++) {
903                 __u64 last_rcvd;
904                 struct obd_export *exp;
905                 struct filter_export_data *fed;
906
907                 if (!lcd) {
908                         OBD_ALLOC_PTR(lcd);
909                         if (!lcd)
910                                 GOTO(err_client, rc = -ENOMEM);
911                 }
912
913                 /* Don't assume off is incremented properly by
914                  * fsfilt_read_record(), in case sizeof(*lcd)
915                  * isn't the same as fsd->lsd_client_size.  */
916                 off = le32_to_cpu(fsd->lsd_client_start) +
917                         cl_idx * le16_to_cpu(fsd->lsd_client_size);
918                 rc = fsfilt_read_record(obd, filp, lcd, sizeof(*lcd), &off);
919                 if (rc) {
920                         CERROR("error reading FILT %s idx %d off %llu: rc %d\n",
921                                LAST_RCVD, cl_idx, off, rc);
922                         break; /* read error shouldn't cause startup to fail */
923                 }
924
925                 if (lcd->lcd_uuid[0] == '\0') {
926                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
927                                cl_idx);
928                         continue;
929                 }
930
931                 check_lcd(obd->obd_name, cl_idx, lcd);
932
933                 last_rcvd = le64_to_cpu(lcd->lcd_last_transno);
934
935                 /* These exports are cleaned up by filter_disconnect(), so they
936                  * need to be set up like real exports as filter_connect() does.
937                  */
938                 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
939                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
940                        " srv lr: "LPU64"\n", lcd->lcd_uuid, cl_idx,
941                        last_rcvd, le64_to_cpu(fsd->lsd_last_transno));
942                 if (IS_ERR(exp)) {
943                         if (PTR_ERR(exp) == -EALREADY) {
944                                 /* export already exists, zero out this one */
945                                 CERROR("Zeroing out duplicate export due to "
946                                        "bug 10479.\n");
947                                 lcd->lcd_uuid[0] = '\0';
948                         } else {
949                                 GOTO(err_client, rc = PTR_ERR(exp));
950                         }
951                 } else {
952                         fed = &exp->exp_filter_data;
953                         fed->fed_lcd = lcd;
954                         filter_export_stats_init(obd, exp, 0, NULL);
955                         rc = filter_client_add(obd, exp, cl_idx);
956                         /* can't fail for existing client */
957                         LASSERTF(rc == 0, "rc = %d\n", rc);
958
959                         /* VBR: set export last committed */
960                         exp->exp_last_committed = last_rcvd;
961                         /* read last time from disk */
962                         exp->exp_last_request_time = target_trans_table_last_time(exp);
963
964                         spin_lock(&exp->exp_lock);
965                         exp->exp_replay_needed = 1;
966                         exp->exp_connecting = 0;
967                         exp->exp_in_recovery = 0;
968                         spin_unlock(&exp->exp_lock);
969
970                         spin_lock_bh(&obd->obd_processing_task_lock);
971                         obd->obd_recoverable_clients++;
972                         obd->obd_max_recoverable_clients++;
973                         spin_unlock_bh(&obd->obd_processing_task_lock);
974
975                         /* VBR: if epoch too old mark export as delayed,
976                          * if epoch is zero then client is pre-vbr one */
977                         if (start_epoch > le32_to_cpu(lcd->lcd_last_epoch) &&
978                             le32_to_cpu(lcd->lcd_last_epoch) != 0)
979                                 class_set_export_delayed(exp);
980
981                         lcd = NULL;
982                         class_export_put(exp);
983                 }
984
985                 /* Need to check last_rcvd even for duplicated exports. */
986                 CDEBUG(D_OTHER, "client at idx %d has last_rcvd = "LPX64"\n",
987                        cl_idx, last_rcvd);
988
989                 if (last_rcvd > le64_to_cpu(fsd->lsd_last_transno))
990                         fsd->lsd_last_transno = cpu_to_le64(last_rcvd);
991         }
992
993         if (lcd)
994                 OBD_FREE_PTR(lcd);
995
996         obd->obd_last_committed = le64_to_cpu(fsd->lsd_last_transno);
997
998         if (obd->obd_recoverable_clients) {
999                 CWARN("RECOVERY: service %s, %d recoverable clients, "
1000                       "%d delayed clients, last_rcvd "LPU64"\n",
1001                       obd->obd_name, obd->obd_recoverable_clients,
1002                       obd->obd_delayed_clients,
1003                       le64_to_cpu(fsd->lsd_last_transno));
1004                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
1005                 obd->obd_recovering = 1;
1006                 obd->obd_recovery_start = 0;
1007                 obd->obd_recovery_end = 0;
1008         } else {
1009                 LASSERT(!obd->obd_recovering);
1010                 /* VBR: update boot epoch after recovery */
1011                 filter_update_last_epoch(obd);
1012         }
1013
1014         obd->obd_recovery_timeout = OBD_RECOVERY_TIME_SOFT;
1015         obd->obd_recovery_time_hard = OBD_RECOVERY_TIME_HARD;
1016
1017         lmi = server_find_mount_locked(obd->obd_name);
1018         if (lmi) {
1019                 struct lustre_sb_info *lsi = s2lsi(lmi->lmi_sb);
1020
1021                 if (lsi->lsi_lmd && lsi->lsi_lmd->lmd_recovery_time_soft)
1022                         obd->obd_recovery_timeout =
1023                                 lsi->lsi_lmd->lmd_recovery_time_soft;
1024
1025                 if (lsi->lsi_lmd && lsi->lsi_lmd->lmd_recovery_time_hard)
1026                         obd->obd_recovery_time_hard =
1027                                 lsi->lsi_lmd->lmd_recovery_time_hard;
1028         }
1029
1030 out:
1031         filter->fo_mount_count = mount_count + 1;
1032         fsd->lsd_mount_count = cpu_to_le64(filter->fo_mount_count);
1033
1034         /* save it, so mount count and last_transno is current */
1035         rc = filter_update_server_data(obd, filp, filter->fo_fsd, 1);
1036         if (rc)
1037                 GOTO(err_client, rc);
1038
1039         RETURN(0);
1040
1041 err_client:
1042         class_disconnect_exports(obd);
1043 err_fsd:
1044         filter_free_server_data(filter);
1045         RETURN(rc);
1046 }
1047
1048 static int filter_cleanup_groups(struct obd_device *obd)
1049 {
1050         struct filter_obd *filter = &obd->u.filter;
1051         struct file *filp;
1052         struct dentry *dentry;
1053         int i;
1054         ENTRY;
1055
1056         if (filter->fo_dentry_O_groups != NULL) {
1057                 for (i = 0; i < FILTER_GROUPS; i++) {
1058                         dentry = filter->fo_dentry_O_groups[i];
1059                         if (dentry != NULL)
1060                                 f_dput(dentry);
1061                 }
1062                 OBD_FREE(filter->fo_dentry_O_groups,
1063                          FILTER_GROUPS * sizeof(*filter->fo_dentry_O_groups));
1064                 filter->fo_dentry_O_groups = NULL;
1065         }
1066         if (filter->fo_last_objid_files != NULL) {
1067                 for (i = 0; i < FILTER_GROUPS; i++) {
1068                         filp = filter->fo_last_objid_files[i];
1069                         if (filp != NULL)
1070                                 filp_close(filp, 0);
1071                 }
1072                 OBD_FREE(filter->fo_last_objid_files,
1073                          FILTER_GROUPS * sizeof(*filter->fo_last_objid_files));
1074                 filter->fo_last_objid_files = NULL;
1075         }
1076         if (filter->fo_dentry_O_sub != NULL) {
1077                 for (i = 0; i < filter->fo_subdir_count; i++) {
1078                         dentry = filter->fo_dentry_O_sub[i];
1079                         if (dentry != NULL)
1080                                 f_dput(dentry);
1081                 }
1082                 OBD_FREE(filter->fo_dentry_O_sub,
1083                          filter->fo_subdir_count *
1084                          sizeof(*filter->fo_dentry_O_sub));
1085                 filter->fo_dentry_O_sub = NULL;
1086         }
1087         if (filter->fo_last_objids != NULL) {
1088                 OBD_FREE(filter->fo_last_objids,
1089                          FILTER_GROUPS * sizeof(*filter->fo_last_objids));
1090                 filter->fo_last_objids = NULL;
1091         }
1092         if (filter->fo_dentry_O != NULL) {
1093                 f_dput(filter->fo_dentry_O);
1094                 filter->fo_dentry_O = NULL;
1095         }
1096         RETURN(0);
1097 }
1098
1099 /* FIXME: object groups */
1100 static int filter_prep_groups(struct obd_device *obd)
1101 {
1102         struct filter_obd *filter = &obd->u.filter;
1103         struct dentry *dentry, *O_dentry;
1104         struct file *filp;
1105         int i, rc = 0, cleanup_phase = 0;
1106         ENTRY;
1107
1108         O_dentry = simple_mkdir(cfs_fs_pwd(current->fs), filter->fo_vfsmnt,
1109                                 "O", 0700, 1);
1110         CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
1111         if (IS_ERR(O_dentry)) {
1112                 rc = PTR_ERR(O_dentry);
1113                 CERROR("cannot open/create O: rc = %d\n", rc);
1114                 GOTO(cleanup, rc);
1115         }
1116         filter->fo_dentry_O = O_dentry;
1117         cleanup_phase = 1; /* O_dentry */
1118
1119         OBD_ALLOC(filter->fo_last_objids, FILTER_GROUPS * sizeof(__u64));
1120         if (filter->fo_last_objids == NULL)
1121                 GOTO(cleanup, rc = -ENOMEM);
1122         cleanup_phase = 2; /* groups */
1123
1124         OBD_ALLOC(filter->fo_dentry_O_groups, FILTER_GROUPS * sizeof(dentry));
1125         if (filter->fo_dentry_O_groups == NULL)
1126                 GOTO(cleanup, rc = -ENOMEM);
1127         OBD_ALLOC(filter->fo_last_objid_files, FILTER_GROUPS * sizeof(filp));
1128         if (filter->fo_last_objid_files == NULL)
1129                 GOTO(cleanup, rc = -ENOMEM);
1130
1131         for (i = 0; i < FILTER_GROUPS; i++) {
1132                 char name[25];
1133                 loff_t off = 0;
1134
1135                 sprintf(name, "%d", i);
1136                 dentry = simple_mkdir(O_dentry, filter->fo_vfsmnt,
1137                                       name, 0700, 1);
1138                 CDEBUG(D_INODE, "got/created O/%s: %p\n", name, dentry);
1139                 if (IS_ERR(dentry)) {
1140                         rc = PTR_ERR(dentry);
1141                         CERROR("cannot lookup/create O/%s: rc = %d\n",
1142                                name, rc);
1143                         GOTO(cleanup, rc);
1144                 }
1145                 filter->fo_dentry_O_groups[i] = dentry;
1146
1147                 sprintf(name, "O/%d/LAST_ID", i);
1148                 filp = filp_open(name, O_CREAT | O_RDWR, 0700);
1149                 if (IS_ERR(filp)) {
1150                         rc = PTR_ERR(filp);
1151                         CERROR("cannot create %s: rc = %d\n", name, rc);
1152                         GOTO(cleanup, rc);
1153                 }
1154                 filter->fo_last_objid_files[i] = filp;
1155
1156                 if (i_size_read(filp->f_dentry->d_inode) == 0) {
1157                         filter->fo_last_objids[i] = FILTER_INIT_OBJID;
1158                         rc = filter_update_last_objid(obd, i, 1);
1159                         if (rc)
1160                                 GOTO(cleanup, rc);
1161                         continue;
1162                 }
1163
1164                 rc = fsfilt_read_record(obd, filp, &filter->fo_last_objids[i],
1165                                         sizeof(__u64), &off);
1166                 if (rc) {
1167                         CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
1168                                name, rc);
1169                         GOTO(cleanup, rc);
1170                 }
1171                 filter->fo_last_objids[i] =
1172                         le64_to_cpu(filter->fo_last_objids[i]);
1173                 CDEBUG(D_HA, "%s: server last_objid group %d: "LPU64"\n",
1174                        obd->obd_name, i, filter->fo_last_objids[i]);
1175         }
1176
1177         if (filter->fo_subdir_count) {
1178                 O_dentry = filter->fo_dentry_O_groups[0];
1179                 OBD_ALLOC(filter->fo_dentry_O_sub,
1180                           filter->fo_subdir_count * sizeof(dentry));
1181                 if (filter->fo_dentry_O_sub == NULL)
1182                         GOTO(cleanup, rc = -ENOMEM);
1183
1184                 for (i = 0; i < filter->fo_subdir_count; i++) {
1185                         char dir[20];
1186                         snprintf(dir, sizeof(dir), "d%u", i);
1187
1188                         dentry = simple_mkdir(O_dentry, filter->fo_vfsmnt,
1189                                               dir, 0700, 1);
1190                         CDEBUG(D_INODE, "got/created O/0/%s: %p\n", dir,dentry);
1191                         if (IS_ERR(dentry)) {
1192                                 rc = PTR_ERR(dentry);
1193                                 CERROR("can't lookup/create O/0/%s: rc = %d\n",
1194                                        dir, rc);
1195                                 GOTO(cleanup, rc);
1196                         }
1197                         filter->fo_dentry_O_sub[i] = dentry;
1198                 }
1199         }
1200         RETURN(0);
1201
1202  cleanup:
1203         filter_cleanup_groups(obd);
1204         return rc;
1205 }
1206
1207 /* setup the object store with correct subdirectories */
1208 static int filter_prep(struct obd_device *obd)
1209 {
1210         struct lvfs_run_ctxt saved;
1211         struct filter_obd *filter = &obd->u.filter;
1212         struct file *file;
1213         struct inode *inode;
1214         int rc = 0;
1215         ENTRY;
1216
1217         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1218         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
1219         if (!file || IS_ERR(file)) {
1220                 rc = PTR_ERR(file);
1221                 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
1222                        LAST_RCVD, rc);
1223                 GOTO(out, rc);
1224         }
1225         filter->fo_rcvd_filp = file;
1226         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
1227                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
1228                        file->f_dentry->d_inode->i_mode);
1229                 GOTO(err_filp, rc = -ENOENT);
1230         }
1231
1232         inode = file->f_dentry->d_parent->d_inode;
1233         /* We use i_op->unlink directly in filter_vfs_unlink() */
1234         if (!inode->i_op || !inode->i_op->create || !inode->i_op->unlink) {
1235                 CERROR("%s: filesystem does not support create/unlink ops\n",
1236                        obd->obd_name);
1237                 GOTO(err_filp, rc = -EOPNOTSUPP);
1238         }
1239
1240         rc = filter_init_server_data(obd, file);
1241         if (rc) {
1242                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
1243                 GOTO(err_filp, rc);
1244         }
1245         /* open and create health check io file*/
1246         file = filp_open(HEALTH_CHECK, O_RDWR | O_CREAT, 0644);
1247         if (IS_ERR(file)) {
1248                 rc = PTR_ERR(file);
1249                 CERROR("OBD filter: cannot open/create %s rc = %d\n",
1250                        HEALTH_CHECK, rc);
1251                 GOTO(err_filp, rc);
1252         }
1253         filter->fo_obt.obt_health_check_filp = file;
1254         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
1255                 CERROR("%s is not a regular file!: mode = %o\n", HEALTH_CHECK,
1256                        file->f_dentry->d_inode->i_mode);
1257                 GOTO(err_health_check, rc = -ENOENT);
1258         }
1259         rc = lvfs_check_io_health(obd, file);
1260         if (rc)
1261                 GOTO(err_health_check, rc);
1262
1263         rc = filter_prep_groups(obd);
1264         if (rc)
1265                 GOTO(err_server_data, rc);
1266  out:
1267         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1268
1269         return(rc);
1270
1271  err_server_data:
1272         //class_disconnect_exports(obd, 0);
1273         filter_free_server_data(filter);
1274  err_health_check:
1275         if (filp_close(filter->fo_obt.obt_health_check_filp, 0))
1276                 CERROR("can't close %s after error\n", HEALTH_CHECK);
1277         filter->fo_obt.obt_health_check_filp = NULL;
1278  err_filp:
1279         if (filp_close(filter->fo_rcvd_filp, 0))
1280                 CERROR("can't close %s after error\n", LAST_RCVD);
1281         filter->fo_rcvd_filp = NULL;
1282         goto out;
1283 }
1284
1285 /* cleanup the filter: write last used object id to status file */
1286 static void filter_post(struct obd_device *obd)
1287 {
1288         struct lvfs_run_ctxt saved;
1289         struct filter_obd *filter = &obd->u.filter;
1290         int rc, i;
1291
1292         /* XXX: filter_update_lastobjid used to call fsync_dev.  It might be
1293          * best to start a transaction with h_sync, because we removed this
1294          * from lastobjid */
1295
1296         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1297         rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
1298                                        filter->fo_fsd, 0);
1299         if (rc)
1300                 CERROR("error writing server data: rc = %d\n", rc);
1301
1302         for (i = 0; i < FILTER_GROUPS; i++) {
1303                 rc = filter_update_last_objid(obd, i, (i == FILTER_GROUPS - 1));
1304                 if (rc)
1305                         CERROR("error writing group %d lastobjid: rc = %d\n",
1306                                i, rc);
1307         }
1308
1309         rc = filp_close(filter->fo_rcvd_filp, 0);
1310         filter->fo_rcvd_filp = NULL;
1311         if (rc)
1312                 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
1313
1314         rc = filp_close(filter->fo_obt.obt_health_check_filp, 0);
1315         filter->fo_obt.obt_health_check_filp = NULL;
1316         if (rc)
1317                 CERROR("error closing %s: rc = %d\n", HEALTH_CHECK, rc);
1318
1319         filter_cleanup_groups(obd);
1320         filter_free_server_data(filter);
1321         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1322 }
1323
1324 static void filter_set_last_id(struct filter_obd *filter,
1325                                obd_id id, obd_gr group)
1326 {
1327         LASSERT(filter->fo_fsd != NULL);
1328         LASSERT(group <= FILTER_GROUPS);
1329
1330         spin_lock(&filter->fo_objidlock);
1331         filter->fo_last_objids[group] = id;
1332         spin_unlock(&filter->fo_objidlock);
1333 }
1334
1335 obd_id filter_last_id(struct filter_obd *filter, obd_gr group)
1336 {
1337         obd_id id;
1338         LASSERT(filter->fo_fsd != NULL);
1339         LASSERT(group <= FILTER_GROUPS);
1340         LASSERT(filter->fo_last_objids != NULL);
1341
1342         /* FIXME: object groups */
1343         spin_lock(&filter->fo_objidlock);
1344         id = filter->fo_last_objids[group];
1345         spin_unlock(&filter->fo_objidlock);
1346
1347         return id;
1348 }
1349
1350 static int filter_lock_dentry(struct obd_device *obd, struct dentry *dparent)
1351 {
1352         LOCK_INODE_MUTEX(dparent->d_inode);
1353         return 0;
1354 }
1355
1356 /* We never dget the object parent, so DON'T dput it either */
1357 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
1358 {
1359         struct filter_obd *filter = &obd->u.filter;
1360         LASSERT(group < FILTER_GROUPS); /* FIXME: object groups */
1361
1362         if (group > 0 || filter->fo_subdir_count == 0)
1363                 return filter->fo_dentry_O_groups[group];
1364
1365         return filter->fo_dentry_O_sub[objid & (filter->fo_subdir_count - 1)];
1366 }
1367
1368 /* We never dget the object parent, so DON'T dput it either */
1369 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
1370                                   obd_id objid)
1371 {
1372         unsigned long now = jiffies;
1373         struct dentry *dparent = filter_parent(obd, group, objid);
1374         int rc;
1375
1376         if (IS_ERR(dparent))
1377                 return dparent;
1378
1379         rc = filter_lock_dentry(obd, dparent);
1380         fsfilt_check_slow(obd, now, "parent lock");
1381         return rc ? ERR_PTR(rc) : dparent;
1382 }
1383
1384 /* We never dget the object parent, so DON'T dput it either */
1385 static void filter_parent_unlock(struct dentry *dparent)
1386 {
1387         UNLOCK_INODE_MUTEX(dparent->d_inode);
1388 }
1389
1390 /* How to get files, dentries, inodes from object id's.
1391  *
1392  * If dir_dentry is passed, the caller has already locked the parent
1393  * appropriately for this operation (normally a write lock).  If
1394  * dir_dentry is NULL, we do a read lock while we do the lookup to
1395  * avoid races with create/destroy and such changing the directory
1396  * internal to the filesystem code. */
1397 struct dentry *filter_fid2dentry(struct obd_device *obd,
1398                                  struct dentry *dir_dentry,
1399                                  obd_gr group, obd_id id)
1400 {
1401         struct dentry *dparent = dir_dentry;
1402         struct dentry *dchild;
1403         char name[32];
1404         int len;
1405         ENTRY;
1406
1407         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT) &&
1408             !obd->u.filter.fo_destroy_in_progress) {
1409                 /* don't fail lookups for orphan recovery, it causes
1410                  * later LBUGs when objects still exist during precreate */
1411                 CDEBUG(D_INFO, "*** obd_fail_loc=%x ***\n",OBD_FAIL_OST_ENOENT);
1412                 RETURN(ERR_PTR(-ENOENT));
1413         }
1414
1415         if (id == 0) {
1416                 CERROR("fatal: invalid object id 0\n");
1417                 RETURN(ERR_PTR(-ESTALE));
1418         }
1419
1420         len = sprintf(name, LPU64, id);
1421         if (dir_dentry == NULL) {
1422                 dparent = filter_parent_lock(obd, group, id);
1423                 if (IS_ERR(dparent)) {
1424                         CERROR("%s: error getting object "LPU64":"LPU64
1425                                " parent: rc %ld\n", obd->obd_name,
1426                                id, group, PTR_ERR(dparent));
1427                         RETURN(dparent);
1428                 }
1429         }
1430         CDEBUG(D_INODE, "looking up object O/%.*s/%s\n",
1431                dparent->d_name.len, dparent->d_name.name, name);
1432         dchild = /*ll_*/lookup_one_len(name, dparent, len);
1433         if (dir_dentry == NULL)
1434                 filter_parent_unlock(dparent);
1435         if (IS_ERR(dchild)) {
1436                 CERROR("%s: child lookup error %ld\n", obd->obd_name,
1437                        PTR_ERR(dchild));
1438                 RETURN(dchild);
1439         }
1440
1441         if (dchild->d_inode != NULL && is_bad_inode(dchild->d_inode)) {
1442                 CERROR("%s: got bad object "LPU64" inode %lu\n",
1443                        obd->obd_name, id, dchild->d_inode->i_ino);
1444                 f_dput(dchild);
1445                 RETURN(ERR_PTR(-ENOENT));
1446         }
1447
1448         CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
1449                name, dchild, atomic_read(&dchild->d_count));
1450
1451         LASSERT(atomic_read(&dchild->d_count) > 0);
1452
1453         RETURN(dchild);
1454 }
1455
1456 static int filter_prepare_destroy(struct obd_device *obd, obd_id objid,
1457                                   struct lustre_handle *lockh)
1458 {
1459         int flags = LDLM_AST_DISCARD_DATA, rc;
1460         struct ldlm_res_id res_id = { .name = { objid } };
1461         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
1462
1463         ENTRY;
1464         /* Tell the clients that the object is gone now and that they should
1465          * throw away any cached pages. */
1466         rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_EXTENT,
1467                                     &policy, LCK_PW, &flags, ldlm_blocking_ast,
1468                                     ldlm_completion_ast, NULL, NULL, 0, NULL,
1469                                     lockh);
1470
1471         if (rc != ELDLM_OK) {
1472                 lockh->cookie = 0;
1473                 CERROR("%s: failed to get lock to destroy objid "LPU64" (%d)\n",
1474                        obd->obd_name, objid, rc);
1475         }
1476         RETURN(rc);
1477 }
1478
1479 static void filter_fini_destroy(struct obd_device *obd,
1480                                 struct lustre_handle *lockh)
1481 {
1482         if (lustre_handle_is_used(lockh))
1483                 ldlm_lock_decref(lockh, LCK_PW);
1484 }
1485
1486 /* This is vfs_unlink() without down(i_sem).  If we call regular vfs_unlink()
1487  * we have 2.6 lock ordering issues with filter_commitrw_write() as it takes
1488  * i_sem before starting a handle, while filter_destroy() + vfs_unlink do the
1489  * reverse.  Caller must take i_sem before starting the transaction and we
1490  * drop it here before the inode is removed from the dentry.  bug 4180/6984 */
1491 int filter_vfs_unlink(struct inode *dir, struct dentry *dentry,
1492                       struct vfsmount *mnt)
1493 {
1494         int rc;
1495         ENTRY;
1496
1497         /* don't need dir->i_zombie for 2.4, it is for rename/unlink of dir
1498          * itself we already hold dir->i_mutex for child create/unlink ops */
1499         LASSERT(dentry->d_inode != NULL);
1500         LASSERT(TRYLOCK_INODE_MUTEX(dir) == 0);
1501         LASSERT(TRYLOCK_INODE_MUTEX(dentry->d_inode) == 0);
1502
1503
1504         /* may_delete() */
1505         if (/*!dentry->d_inode ||*/dentry->d_parent->d_inode != dir)
1506                 GOTO(out, rc = -ENOENT);
1507
1508         rc = ll_permission(dir, MAY_WRITE | MAY_EXEC, NULL);
1509         if (rc)
1510                 GOTO(out, rc);
1511
1512         if (IS_APPEND(dir))
1513                 GOTO(out, rc = -EPERM);
1514
1515         /* check_sticky() */
1516         if ((dentry->d_inode->i_uid != current->fsuid &&
1517              !cfs_capable(CFS_CAP_FOWNER)) || IS_APPEND(dentry->d_inode) ||
1518             IS_IMMUTABLE(dentry->d_inode))
1519                 GOTO(out, rc = -EPERM);
1520
1521         /* NOTE: This might need to go outside i_mutex, though it isn't clear if
1522          *       that was done because of journal_start (which is already done
1523          *       here) or some other ordering issue. */
1524         DQUOT_INIT(dir);
1525
1526         rc = ll_security_inode_unlink(dir, dentry, mnt);
1527         if (rc)
1528                 GOTO(out, rc);
1529         rc = dir->i_op->unlink(dir, dentry);
1530 out:
1531         /* need to drop i_mutex before we lose inode reference */
1532         UNLOCK_INODE_MUTEX(dentry->d_inode);
1533         if (rc == 0)
1534                 d_delete(dentry);
1535
1536         RETURN(rc);
1537 }
1538
1539 /* Caller must hold LCK_PW on parent and push us into kernel context.
1540  * Caller must hold child i_mutex, we drop it always.
1541  * Caller is also required to ensure that dchild->d_inode exists. */
1542 static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
1543                                    obd_gr group, struct dentry *dparent,
1544                                    struct dentry *dchild)
1545 {
1546         struct inode *inode = dchild->d_inode;
1547         struct filter_obd *filter = &obd->u.filter;
1548         int rc;
1549
1550         if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
1551                 CERROR("destroying objid %.*s ino %lu nlink %lu count %d\n",
1552                        dchild->d_name.len, dchild->d_name.name, inode->i_ino,
1553                        (unsigned long)inode->i_nlink,
1554                        atomic_read(&inode->i_count));
1555         }
1556
1557         rc = filter_vfs_unlink(dparent->d_inode, dchild, filter->fo_vfsmnt);
1558         if (rc)
1559                 CERROR("error unlinking objid %.*s: rc %d\n",
1560                        dchild->d_name.len, dchild->d_name.name, rc);
1561         return(rc);
1562 }
1563
1564 struct filter_intent_args {
1565         struct ldlm_lock **victim;
1566         __u64 size;
1567         int *liblustre;
1568 };
1569
1570 static enum interval_iter filter_intent_cb(struct interval_node *n,
1571                                            void *args)
1572 {
1573         struct ldlm_interval *node = (struct ldlm_interval *)n;
1574         struct filter_intent_args *arg = (struct filter_intent_args*)args;
1575         __u64 size = arg->size;
1576         struct ldlm_lock **v = arg->victim;
1577         struct ldlm_lock *lck;
1578
1579         /* If the interval is lower than the current file size,
1580          * just break. */
1581         if (interval_high(n) <= size)
1582                 return INTERVAL_ITER_STOP;
1583
1584         list_for_each_entry(lck, &node->li_group, l_sl_policy) {
1585                 /* Don't send glimpse ASTs to liblustre clients.
1586                  * They aren't listening for them, and they do
1587                  * entirely synchronous I/O anyways. */
1588                 if (lck->l_export == NULL ||
1589                     lck->l_export->exp_libclient == 1)
1590                         continue;
1591
1592                 if (*arg->liblustre)
1593                         *arg->liblustre = 0;
1594
1595                 if (*v == NULL) {
1596                         *v = LDLM_LOCK_GET(lck);
1597                 } else if ((*v)->l_policy_data.l_extent.start <
1598                            lck->l_policy_data.l_extent.start) {
1599                         LDLM_LOCK_PUT(*v);
1600                         *v = LDLM_LOCK_GET(lck);
1601                 }
1602
1603                 /* the same policy group - every lock has the
1604                  * same extent, so needn't do it any more */
1605                 break;
1606         }
1607
1608         return INTERVAL_ITER_CONT;
1609 }
1610
1611 static int filter_intent_policy(struct ldlm_namespace *ns,
1612                                 struct ldlm_lock **lockp, void *req_cookie,
1613                                 ldlm_mode_t mode, int flags, void *data)
1614 {
1615         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1616         struct ptlrpc_request *req = req_cookie;
1617         struct ldlm_lock *lock = *lockp, *l = NULL;
1618         struct ldlm_resource *res = lock->l_resource;
1619         ldlm_processing_policy policy;
1620         struct ost_lvb *res_lvb, *reply_lvb;
1621         struct ldlm_reply *rep;
1622         ldlm_error_t err;
1623         int idx, rc, tmpflags = 0, only_liblustre = 1;
1624         struct ldlm_interval_tree *tree;
1625         struct filter_intent_args arg;
1626         __u32 repsize[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1627                            [DLM_LOCKREPLY_OFF]   = sizeof(*rep),
1628                            [DLM_REPLY_REC_OFF]   = sizeof(*reply_lvb) };
1629         ENTRY;
1630
1631         policy = ldlm_get_processing_policy(res);
1632         LASSERT(policy != NULL);
1633         LASSERT(req != NULL);
1634
1635         rc = lustre_pack_reply(req, 3, repsize, NULL);
1636         if (rc)
1637                 RETURN(req->rq_status = rc);
1638
1639         rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
1640         LASSERT(rep != NULL);
1641
1642         reply_lvb = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
1643                                    sizeof(*reply_lvb));
1644         LASSERT(reply_lvb != NULL);
1645
1646         //fixup_handle_for_resent_req(req, lock, &lockh);
1647
1648         /* Call the extent policy function to see if our request can be
1649          * granted, or is blocked.
1650          * If the OST lock has LDLM_FL_HAS_INTENT set, it means a glimpse
1651          * lock, and should not be granted if the lock will be blocked.
1652          */
1653
1654         LASSERT(ns == res->lr_namespace);
1655         lock_res(res);
1656         rc = policy(lock, &tmpflags, 0, &err, &rpc_list);
1657         check_res_locked(res);
1658
1659         /* FIXME: we should change the policy function slightly, to not make
1660          * this list at all, since we just turn around and free it */
1661         while (!list_empty(&rpc_list)) {
1662                 struct ldlm_lock *wlock =
1663                         list_entry(rpc_list.next, struct ldlm_lock, l_cp_ast);
1664                 LASSERT((lock->l_flags & LDLM_FL_AST_SENT) == 0);
1665                 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1666                 lock->l_flags &= ~LDLM_FL_CP_REQD;
1667                 list_del_init(&wlock->l_cp_ast);
1668                 LDLM_LOCK_PUT(wlock);
1669         }
1670
1671         /* The lock met with no resistance; we're finished. */
1672         if (rc == LDLM_ITER_CONTINUE) {
1673                 /* do not grant locks to the liblustre clients: they cannot
1674                  * handle ASTs robustly.  We need to do this while still
1675                  * holding ns_lock to avoid the lock remaining on the res_link
1676                  * list (and potentially being added to l_pending_list by an
1677                  * AST) when we are going to drop this lock ASAP. */
1678                 if (lock->l_export->exp_libclient ||
1679                     OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2)) {
1680                         ldlm_resource_unlink_lock(lock);
1681                         err = ELDLM_LOCK_ABORTED;
1682                 } else {
1683                         err = ELDLM_LOCK_REPLACED;
1684                 }
1685                 unlock_res(res);
1686                 RETURN(err);
1687         }
1688
1689         /* Do not grant any lock, but instead send GL callbacks.  The extent
1690          * policy nicely created a list of all PW locks for us.  We will choose
1691          * the highest of those which are larger than the size in the LVB, if
1692          * any, and perform a glimpse callback. */
1693         res_lvb = res->lr_lvb_data;
1694         LASSERT(res_lvb != NULL);
1695         *reply_lvb = *res_lvb;
1696
1697         /*
1698          * ->ns_lock guarantees that no new locks are granted, and,
1699          * therefore, that res->lr_lvb_data cannot increase beyond the
1700          * end of already granted lock. As a result, it is safe to
1701          * check against "stale" reply_lvb->lvb_size value without
1702          * res->lr_lvb_sem.
1703          */
1704         arg.size = reply_lvb->lvb_size;
1705         arg.victim = &l;
1706         arg.liblustre = &only_liblustre;
1707         for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1708                 tree = &res->lr_itree[idx];
1709                 if (tree->lit_mode == LCK_PR)
1710                         continue;
1711
1712                 interval_iterate_reverse(tree->lit_root,
1713                                          filter_intent_cb, &arg);
1714         }
1715         unlock_res(res);
1716
1717         /* There were no PW locks beyond the size in the LVB; finished. */
1718         if (l == NULL) {
1719                 if (only_liblustre) {
1720                         /* If we discovered a liblustre client with a PW lock,
1721                          * however, the LVB may be out of date!  The LVB is
1722                          * updated only on glimpse (which we don't do for
1723                          * liblustre clients) and cancel (which the client
1724                          * obviously has not yet done).  So if it has written
1725                          * data but kept the lock, the LVB is stale and needs
1726                          * to be updated from disk.
1727                          *
1728                          * Of course, this will all disappear when we switch to
1729                          * taking liblustre locks on the OST. */
1730                         ldlm_res_lvbo_update(res, NULL, 0, 1);
1731                 }
1732                 RETURN(ELDLM_LOCK_ABORTED);
1733         }
1734
1735         /*
1736          * This check is for lock taken in filter_prepare_destroy() that does
1737          * not have l_glimpse_ast set. So the logic is: if there is a lock
1738          * with no l_glimpse_ast set, this object is being destroyed already.
1739          *
1740          * Hence, if you are grabbing DLM locks on the server, always set
1741          * non-NULL glimpse_ast (e.g., ldlm_request.c:ldlm_glimpse_ast()).
1742          */
1743         if (l->l_glimpse_ast == NULL) {
1744                 /* We are racing with unlink(); just return -ENOENT */
1745                 rep->lock_policy_res1 = -ENOENT;
1746                 goto out;
1747         }
1748
1749         LASSERTF(l->l_glimpse_ast != NULL, "l == %p", l);
1750         rc = l->l_glimpse_ast(l, NULL); /* this will update the LVB */
1751         /* Update the LVB from disk if the AST failed (this is a legal race) */
1752         /*
1753          * XXX nikita: situation when ldlm_server_glimpse_ast() failed before
1754          * sending ast is not handled. This can result in lost client writes.
1755          */
1756         if (rc != 0)
1757                 ldlm_res_lvbo_update(res, NULL, 0, 1);
1758
1759         lock_res(res);
1760         *reply_lvb = *res_lvb;
1761         unlock_res(res);
1762
1763  out:
1764         LDLM_LOCK_PUT(l);
1765
1766         RETURN(ELDLM_LOCK_ABORTED);
1767 }
1768
1769 /*
1770  * per-obd_device iobuf pool.
1771  *
1772  * To avoid memory deadlocks in low-memory setups, amount of dynamic
1773  * allocations in write-path has to be minimized (see bug 5137).
1774  *
1775  * Pages, niobuf_local's and niobuf_remote's are pre-allocated and attached to
1776  * OST threads (see ost_thread_{init,done}()).
1777  *
1778  * "iobuf's" used by filter cannot be attached to OST thread, however, because
1779  * at the OST layer there are only (potentially) multiple obd_device of type
1780  * unknown at the time of OST thread creation.
1781  *
1782  * Instead array of iobuf's is attached to struct filter_obd (->fo_iobuf_pool
1783  * field). This array has size OST_MAX_THREADS, so that each OST thread uses
1784  * it's very own iobuf.
1785  *
1786  * Functions below
1787  *
1788  *     filter_kiobuf_pool_init()
1789  *
1790  *     filter_kiobuf_pool_done()
1791  *
1792  *     filter_iobuf_get()
1793  *
1794  * operate on this array. They are "generic" in a sense that they don't depend
1795  * on actual type of iobuf's (the latter depending on Linux kernel version).
1796  */
1797
1798 /*
1799  * destroy pool created by filter_iobuf_pool_init
1800  */
1801 static void filter_iobuf_pool_done(struct filter_obd *filter)
1802 {
1803         struct filter_iobuf **pool;
1804         int i;
1805
1806         ENTRY;
1807
1808         pool = filter->fo_iobuf_pool;
1809         if (pool != NULL) {
1810                 for (i = 0; i < filter->fo_iobuf_count; ++ i) {
1811                         if (pool[i] != NULL)
1812                                 filter_free_iobuf(pool[i]);
1813                 }
1814                 OBD_FREE(pool, filter->fo_iobuf_count * sizeof pool[0]);
1815                 filter->fo_iobuf_pool = NULL;
1816         }
1817         EXIT;
1818 }
1819
1820 /*
1821  * pre-allocate pool of iobuf's to be used by filter_{prep,commit}rw_write().
1822  */
1823 static int filter_iobuf_pool_init(struct filter_obd *filter)
1824 {
1825         void **pool;
1826
1827         ENTRY;
1828
1829         OBD_ALLOC_GFP(filter->fo_iobuf_pool, OSS_THREADS_MAX * sizeof(*pool),
1830                       GFP_KERNEL);
1831         if (filter->fo_iobuf_pool == NULL)
1832                 RETURN(-ENOMEM);
1833
1834         filter->fo_iobuf_count = OSS_THREADS_MAX;
1835
1836         RETURN(0);
1837 }
1838
1839 /* Return iobuf allocated for @thread_id.  We don't know in advance how
1840  * many threads there will be so we allocate a large empty array and only
1841  * fill in those slots that are actually in use.
1842  * If we haven't allocated a pool entry for this thread before, do so now. */
1843 void *filter_iobuf_get(struct filter_obd *filter, struct obd_trans_info *oti)
1844 {
1845         int thread_id                    = (oti && oti->oti_thread) ?
1846                                            oti->oti_thread->t_id : -1;
1847         struct filter_iobuf  *pool       = NULL;
1848         struct filter_iobuf **pool_place = NULL;
1849
1850         if (thread_id >= 0) {
1851                 LASSERT(thread_id < filter->fo_iobuf_count);
1852                 pool = *(pool_place = &filter->fo_iobuf_pool[thread_id]);
1853         }
1854
1855         if (unlikely(pool == NULL)) {
1856                 pool = filter_alloc_iobuf(filter, OBD_BRW_WRITE,
1857                                           PTLRPC_MAX_BRW_PAGES);
1858                 if (pool_place != NULL)
1859                         *pool_place = pool;
1860         }
1861
1862         return pool;
1863 }
1864
1865 /* mount the file system (secretly).  lustre_cfg parameters are:
1866  * 1 = device
1867  * 2 = fstype
1868  * 3 = flags: failover=f, failout=n
1869  * 4 = mount options
1870  */
1871 int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
1872                         void *option)
1873 {
1874         struct lustre_cfg* lcfg = buf;
1875         struct filter_obd *filter = &obd->u.filter;
1876         struct vfsmount *mnt;
1877         struct lustre_mount_info *lmi;
1878         struct obd_uuid uuid;
1879         __u8 *uuid_ptr;
1880         char *str, *label;
1881         char ns_name[48];
1882 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9)
1883         struct request_queue *q;
1884 #endif
1885         int rc;
1886         ENTRY;
1887
1888         if (lcfg->lcfg_bufcount < 3 ||
1889             LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
1890             LUSTRE_CFG_BUFLEN(lcfg, 2) < 1)
1891                 RETURN(-EINVAL);
1892
1893         lmi = server_get_mount(obd->obd_name);
1894         if (lmi) {
1895                 /* We already mounted in lustre_fill_super.
1896                    lcfg bufs 1, 2, 4 (device, fstype, mount opts) are ignored.*/
1897                 struct lustre_sb_info *lsi = s2lsi(lmi->lmi_sb);
1898                 mnt = lmi->lmi_mnt;
1899                 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
1900         } else {
1901                 /* old path - used by lctl */
1902                 CERROR("Using old MDS mount method\n");
1903                 mnt = ll_kern_mount(lustre_cfg_string(lcfg, 2),
1904                                     MS_NOATIME|MS_NODIRATIME,
1905                                     lustre_cfg_string(lcfg, 1), option);
1906                 if (IS_ERR(mnt)) {
1907                         rc = PTR_ERR(mnt);
1908                         LCONSOLE_ERROR_MSG(0x135, "Can't mount disk %s (%d)\n",
1909                                            lustre_cfg_string(lcfg, 1), rc);
1910                         RETURN(rc);
1911                 }
1912
1913                 obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
1914         }
1915         if (IS_ERR(obd->obd_fsops))
1916                 GOTO(err_mntput, rc = PTR_ERR(obd->obd_fsops));
1917
1918         rc = filter_iobuf_pool_init(filter);
1919         if (rc != 0)
1920                 GOTO(err_ops, rc);
1921
1922         if (lvfs_check_rdonly(lvfs_sbdev(mnt->mnt_sb))) {
1923                 CERROR("%s: Underlying device is marked as read-only. "
1924                        "Setup failed\n", obd->obd_name);
1925                 GOTO(err_ops, rc = -EROFS);
1926         }
1927
1928         /* failover is the default */
1929         obd->obd_replayable = 1;
1930
1931         if (lcfg->lcfg_bufcount > 3 && LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
1932                 str = lustre_cfg_string(lcfg, 3);
1933                 if (strchr(str, 'n')) {
1934                         CWARN("%s: recovery disabled\n", obd->obd_name);
1935                         obd->obd_replayable = 0;
1936                 }
1937         }
1938
1939         filter->fo_vfsmnt = mnt;
1940         obd->u.obt.obt_sb = mnt->mnt_sb;
1941         obd->u.obt.obt_stale_export_age = STALE_EXPORT_MAXTIME_DEFAULT;
1942         spin_lock_init(&obd->u.obt.obt_trans_table_lock);
1943
1944         filter->fo_fstype = mnt->mnt_sb->s_type->name;
1945         CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
1946
1947         rc = fsfilt_setup(obd, obd->u.obt.obt_sb);
1948         if (rc)
1949                 GOTO(err_ops, rc);
1950
1951         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1952         obd->obd_lvfs_ctxt.pwdmnt = mnt;
1953         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1954         obd->obd_lvfs_ctxt.fs = get_ds();
1955         obd->obd_lvfs_ctxt.cb_ops = filter_lvfs_ops;
1956
1957         filter->fo_destroy_in_progress = 0;
1958         sema_init(&filter->fo_create_lock, 1);
1959         spin_lock_init(&filter->fo_translock);
1960         spin_lock_init(&filter->fo_objidlock);
1961         INIT_LIST_HEAD(&filter->fo_export_list);
1962         sema_init(&filter->fo_alloc_lock, 1);
1963         init_brw_stats(&filter->fo_filter_stats);
1964         filter->fo_read_cache = 1; /* enable read-only cache by default */
1965         filter->fo_writethrough_cache = 1; /* enable writethrough cache */
1966         filter->fo_readcache_max_filesize = FILTER_MAX_CACHE_SIZE;
1967         filter->fo_fmd_max_num = FILTER_FMD_MAX_NUM_DEFAULT;
1968         filter->fo_fmd_max_age = FILTER_FMD_MAX_AGE_DEFAULT;
1969         filter->fo_syncjournal = 1; /* Sync journals on i/o by default b=19128 */
1970
1971         rc = filter_prep(obd);
1972         if (rc)
1973                 GOTO(err_ops, rc);
1974
1975         sprintf(ns_name, "filter-%s", obd->obd_uuid.uuid);
1976         obd->obd_namespace = ldlm_namespace_new(obd, ns_name, LDLM_NAMESPACE_SERVER,
1977                                                 LDLM_NAMESPACE_GREEDY);
1978         if (obd->obd_namespace == NULL)
1979                 GOTO(err_post, rc = -ENOMEM);
1980         obd->obd_namespace->ns_lvbp = obd;
1981         obd->obd_namespace->ns_lvbo = &filter_lvbo;
1982         ldlm_register_intent(obd->obd_namespace, filter_intent_policy);
1983
1984         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1985                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1986
1987         rc = obd_llog_init(obd, obd, NULL);
1988         if (rc) {
1989                 CERROR("failed to setup llogging subsystems\n");
1990                 GOTO(err_post, rc);
1991         }
1992
1993         rc = lquota_setup(filter_quota_interface_ref, obd);
1994         if (rc)
1995                 GOTO(err_post, rc);
1996
1997 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9)
1998         q = bdev_get_queue(mnt->mnt_sb->s_bdev);
1999         if (q->max_sectors < q->max_hw_sectors &&
2000             q->max_sectors < PTLRPC_MAX_BRW_SIZE >> 9)
2001                 LCONSOLE_INFO("%s: underlying device %s should be tuned "
2002                               "for larger I/O requests: max_sectors = %u "
2003                               "could be up to max_hw_sectors=%u\n",
2004                               obd->obd_name, mnt->mnt_sb->s_id,
2005                               q->max_sectors, q->max_hw_sectors);
2006 #endif
2007
2008         uuid_ptr = fsfilt_uuid(obd, obd->u.obt.obt_sb);
2009         if (uuid_ptr != NULL) {
2010                 class_uuid_unparse(uuid_ptr, &uuid);
2011                 str = uuid.uuid;
2012         } else {
2013                 str = "no UUID";
2014         }
2015
2016         label = fsfilt_get_label(obd, obd->u.obt.obt_sb);
2017
2018         if (obd->obd_recovering) {
2019                 LCONSOLE_WARN("OST %s now serving %s (%s%s%s), but will be in "
2020                               "recovery for at least %d:%.02d, or until %d "
2021                               "client%s reconnect%s.\n",
2022                               obd->obd_name, lustre_cfg_string(lcfg, 1),
2023                               label ?: "", label ? "/" : "", str,
2024                               obd->obd_recovery_timeout / 60,
2025                               obd->obd_recovery_timeout % 60,
2026                               obd->obd_recoverable_clients,
2027                               obd->obd_recoverable_clients == 1 ? "":"s",
2028                               obd->obd_recoverable_clients == 1 ? "s":"");
2029         } else {
2030                 LCONSOLE_INFO("OST %s now serving %s (%s%s%s) with recovery "
2031                               "%s\n", obd->obd_name, lustre_cfg_string(lcfg, 1),
2032                               label ?: "", label ? "/" : "", str,
2033                               obd->obd_replayable ? "enabled" : "disabled");
2034         }
2035
2036         RETURN(0);
2037
2038 err_post:
2039         filter_post(obd);
2040 err_ops:
2041         fsfilt_put_ops(obd->obd_fsops);
2042         filter_iobuf_pool_done(filter);
2043 err_mntput:
2044         server_put_mount(obd->obd_name, mnt);
2045         obd->u.obt.obt_sb = 0;
2046         return rc;
2047 }
2048
2049 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
2050 {
2051         struct lprocfs_static_vars lvars;
2052         struct lustre_cfg* lcfg = buf;
2053         unsigned long addr;
2054         struct page *page;
2055         int rc;
2056
2057         CLASSERT(offsetof(struct obd_device, u.obt) ==
2058                  offsetof(struct obd_device, u.filter.fo_obt));
2059
2060         if (!LUSTRE_CFG_BUFLEN(lcfg, 1) || !LUSTRE_CFG_BUFLEN(lcfg, 2))
2061                 RETURN(-EINVAL);
2062
2063         /* 2.6.9 selinux wants a full option page for do_kern_mount (bug6471) */
2064         OBD_PAGE_ALLOC(page, CFS_ALLOC_STD);
2065         if (!page)
2066                 RETURN(-ENOMEM);
2067         addr = (unsigned long)cfs_page_address(page);
2068         clear_page((void *)addr);
2069
2070         /* lprocfs must be setup before the filter so state can be safely added
2071          * to /proc incrementally as the filter is setup */
2072         lprocfs_filter_init_vars(&lvars);
2073         if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
2074             lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST) == 0) {
2075                 /* Init obdfilter private stats here */
2076                 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
2077                                      LPROCFS_CNTR_AVGMINMAX,
2078                                      "read_bytes", "bytes");
2079                 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
2080                                      LPROCFS_CNTR_AVGMINMAX,
2081                                      "write_bytes", "bytes");
2082                 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_GET_PAGE,
2083                                      LPROCFS_CNTR_AVGMINMAX|LPROCFS_CNTR_STDDEV,
2084                                      "get_page", "usec");
2085                 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_NO_PAGE,
2086                                      LPROCFS_CNTR_AVGMINMAX,
2087                                      "get_page failures", "num");
2088                 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_CACHE_ACCESS,
2089                                      LPROCFS_CNTR_AVGMINMAX,
2090                                      "cache_access", "pages");
2091                 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_CACHE_HIT,
2092                                      LPROCFS_CNTR_AVGMINMAX,
2093                                      "cache_hit", "pages");
2094                 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_CACHE_MISS,
2095                                      LPROCFS_CNTR_AVGMINMAX,
2096                                      "cache_miss", "pages");
2097                 lproc_filter_attach_seqstat(obd);
2098 #ifdef HAVE_DELAYED_RECOVERY
2099                 lprocfs_obd_attach_stale_exports(obd);
2100 #endif
2101                 obd->obd_proc_exports_entry = proc_mkdir("exports",
2102                                                          obd->obd_proc_entry);
2103         }
2104         if (obd->obd_proc_exports_entry)
2105                 lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
2106                                    lprocfs_nid_stats_clear_read,
2107                                    lprocfs_nid_stats_clear_write, obd, NULL);
2108
2109         memcpy((void *)addr, lustre_cfg_buf(lcfg, 4),
2110                LUSTRE_CFG_BUFLEN(lcfg, 4));
2111         rc = filter_common_setup(obd, len, buf, (void *)addr);
2112         OBD_PAGE_FREE(page);
2113
2114         if (rc) {
2115                 remove_proc_entry("clear", obd->obd_proc_exports_entry);
2116                 lprocfs_free_per_client_stats(obd);
2117                 lprocfs_free_obd_stats(obd);
2118                 lprocfs_obd_cleanup(obd);
2119         }
2120
2121         return rc;
2122 }
2123
2124 static struct llog_operations filter_mds_ost_repl_logops /* initialized below*/;
2125 static struct llog_operations filter_size_orig_logops = {
2126         lop_setup: llog_obd_origin_setup,
2127         lop_cleanup: llog_obd_origin_cleanup,
2128         lop_add: llog_obd_origin_add
2129 };
2130
2131 static int filter_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
2132                             int *index)
2133 {
2134         struct filter_obd *filter = &obd->u.filter;
2135         struct llog_ctxt *ctxt;
2136         int rc;
2137         ENTRY;
2138
2139         filter->fo_lcm = llog_recov_thread_init(obd->obd_name);
2140         if (!filter->fo_lcm)
2141                 RETURN(-ENOMEM);
2142
2143         filter_mds_ost_repl_logops = llog_client_ops;
2144         filter_mds_ost_repl_logops.lop_cancel = llog_obd_repl_cancel;
2145         filter_mds_ost_repl_logops.lop_connect = llog_obd_repl_connect;
2146         filter_mds_ost_repl_logops.lop_sync = llog_obd_repl_sync;
2147
2148         rc = llog_setup(obd, LLOG_MDS_OST_REPL_CTXT, disk_obd, 0, NULL,
2149                         &filter_mds_ost_repl_logops);
2150         if (rc)
2151                 GOTO(cleanup_lcm, rc);
2152
2153         /* FIXME - assign unlink_cb for filter's recovery */
2154         ctxt = llog_get_context(obd, LLOG_MDS_OST_REPL_CTXT);
2155         ctxt->llog_proc_cb = filter_recov_log_mds_ost_cb;
2156         ctxt->loc_lcm = filter->fo_lcm;
2157         llog_ctxt_put(ctxt);
2158
2159         rc = llog_setup(obd, LLOG_SIZE_ORIG_CTXT, disk_obd, 0, NULL,
2160                         &filter_size_orig_logops);
2161         if (rc)
2162                 GOTO(cleanup_ctxt, rc);
2163         RETURN(rc);
2164 cleanup_ctxt:
2165         ctxt = llog_get_context(obd, LLOG_MDS_OST_REPL_CTXT);
2166         if (ctxt)
2167                 llog_cleanup(ctxt);
2168 cleanup_lcm:
2169         llog_recov_thread_fini(filter->fo_lcm, 1);
2170         filter->fo_lcm = NULL;
2171         return rc;
2172 }
2173
2174 static int filter_llog_finish(struct obd_device *obd, int count)
2175 {
2176         struct filter_obd *filter = &obd->u.filter;
2177         struct llog_ctxt *ctxt;
2178         int rc = 0, rc2 = 0;
2179         ENTRY;
2180
2181         ctxt = llog_get_context(obd, LLOG_MDS_OST_REPL_CTXT);
2182         if (ctxt) {
2183                 /*
2184                  * Make sure that no cached llcds left in recov_thread. We
2185                  * actually do sync in disconnect time, but disconnect may
2186                  * not come being marked rq_no_resend = 1.
2187                  */
2188                 llog_sync(ctxt, NULL);
2189
2190                 /*
2191                  * Balance class_import_get() called in llog_receptor_accept().
2192                  * This is safe to do here, as llog is already synchronized and
2193                  * its import may go.
2194                  */
2195                 mutex_down(&ctxt->loc_sem);
2196                 if (ctxt->loc_imp) {
2197                         class_import_put(ctxt->loc_imp);
2198                         ctxt->loc_imp = NULL;
2199                 }
2200                 mutex_up(&ctxt->loc_sem);
2201         }
2202
2203         if (filter->fo_lcm) {
2204                 llog_recov_thread_fini(filter->fo_lcm, obd->obd_force);
2205                 filter->fo_lcm = NULL;
2206         }
2207
2208         if (ctxt)
2209                 rc = llog_cleanup(ctxt);
2210
2211         ctxt = llog_get_context(obd, LLOG_SIZE_ORIG_CTXT);
2212         if (ctxt)
2213                 rc2 = llog_cleanup(ctxt);
2214         if (!rc)
2215                 rc = rc2;
2216
2217         RETURN(rc);
2218 }
2219
2220 static int filter_precleanup(struct obd_device *obd,
2221                              enum obd_cleanup_stage stage)
2222 {
2223         int rc = 0;
2224         ENTRY;
2225
2226         switch(stage) {
2227         case OBD_CLEANUP_EARLY:
2228                 break;
2229         case OBD_CLEANUP_EXPORTS:
2230                 target_cleanup_recovery(obd);
2231                 rc = filter_llog_finish(obd, 0);
2232                 break;
2233         case OBD_CLEANUP_SELF_EXP:
2234                 break;
2235         case OBD_CLEANUP_OBD:
2236                 break;
2237         }
2238         RETURN(rc);
2239 }
2240
2241 static int filter_cleanup(struct obd_device *obd)
2242 {
2243         struct filter_obd *filter = &obd->u.filter;
2244         ENTRY;
2245
2246         if (obd->obd_fail)
2247                 LCONSOLE_WARN("%s: shutting down for failover; client state "
2248                               "will be preserved.\n", obd->obd_name);
2249
2250         if (!list_empty(&obd->obd_exports)) {
2251                 CERROR("%s: still has clients!\n", obd->obd_name);
2252                 class_disconnect_exports(obd);
2253                 if (!list_empty(&obd->obd_exports)) {
2254                         CERROR("still has exports after forced cleanup?\n");
2255                         RETURN(-EBUSY);
2256                 }
2257         }
2258
2259        /* some exports may still be in the zombie queue, so we make sure that
2260         * all the exports have been processed, otherwise the last_rcvd slot
2261         * may not be updated on time */
2262         obd_zombie_barrier();
2263
2264         remove_proc_entry("clear", obd->obd_proc_exports_entry);
2265         lprocfs_free_per_client_stats(obd);
2266         lprocfs_free_obd_stats(obd);
2267         lprocfs_obd_cleanup(obd);
2268
2269         lquota_cleanup(filter_quota_interface_ref, obd);
2270
2271         ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
2272         obd->obd_namespace = NULL;
2273
2274         if (obd->u.obt.obt_sb == NULL)
2275                 RETURN(0);
2276
2277         filter_post(obd);
2278
2279         LL_DQUOT_OFF(obd->u.obt.obt_sb, 0);
2280         shrink_dcache_sb(obd->u.obt.obt_sb);
2281
2282         server_put_mount(obd->obd_name, filter->fo_vfsmnt);
2283         obd->u.obt.obt_sb = NULL;
2284
2285         fsfilt_put_ops(obd->obd_fsops);
2286
2287         filter_iobuf_pool_done(filter);
2288
2289         LCONSOLE_INFO("OST %s has stopped.\n", obd->obd_name);
2290
2291         RETURN(0);
2292 }
2293
2294 static int filter_connect_internal(struct obd_export *exp,
2295                                    struct obd_connect_data *data)
2296 {
2297         if (!data)
2298                 RETURN(0);
2299
2300         CDEBUG(D_RPCTRACE, "%s: cli %s/%p ocd_connect_flags: "LPX64
2301                " ocd_version: %x ocd_grant: %d ocd_index: %u\n",
2302                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
2303                data->ocd_connect_flags, data->ocd_version,
2304                data->ocd_grant, data->ocd_index);
2305
2306         data->ocd_connect_flags &= OST_CONNECT_SUPPORTED;
2307         exp->exp_connect_flags = data->ocd_connect_flags;
2308         data->ocd_version = LUSTRE_VERSION_CODE;
2309
2310         /* Kindly make sure the SKIP_ORPHAN flag is from MDS. */
2311         if (!ergo(data->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN,
2312                   data->ocd_connect_flags & OBD_CONNECT_MDS))
2313                 RETURN(-EPROTO);
2314
2315         if (exp->exp_connect_flags & OBD_CONNECT_GRANT) {
2316                 struct filter_obd *filter = &exp->exp_obd->u.filter;
2317                 struct filter_export_data *fed = &exp->exp_filter_data;
2318                 obd_size left, want;
2319
2320                 spin_lock(&exp->exp_obd->obd_osfs_lock);
2321                 left = filter_grant_space_left(exp);
2322                 want = data->ocd_grant;
2323                 filter_grant(exp, fed->fed_grant, want, left);
2324                 data->ocd_grant = fed->fed_grant;
2325                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
2326
2327                 CDEBUG(D_CACHE, "%s: cli %s/%p ocd_grant: %d want: "
2328                        LPU64" left: "LPU64"\n", exp->exp_obd->obd_name,
2329                        exp->exp_client_uuid.uuid, exp,
2330                        data->ocd_grant, want, left);
2331                 
2332                 filter->fo_tot_granted_clients ++;
2333         }
2334
2335         if (data->ocd_connect_flags & OBD_CONNECT_INDEX) {
2336                 struct filter_obd *filter = &exp->exp_obd->u.filter;
2337                 struct lr_server_data *lsd = filter->fo_fsd;
2338                 int index = le32_to_cpu(lsd->lsd_ost_index);
2339
2340                 if (!(lsd->lsd_feature_compat &
2341                       cpu_to_le32(OBD_COMPAT_OST))) {
2342                         /* this will only happen on the first connect */
2343                         lsd->lsd_ost_index = cpu_to_le32(data->ocd_index);
2344                         lsd->lsd_feature_compat |= cpu_to_le32(OBD_COMPAT_OST);
2345                         filter_update_server_data(exp->exp_obd,
2346                                                   filter->fo_rcvd_filp, lsd, 1);
2347                 } else if (index != data->ocd_index) {
2348                         LCONSOLE_ERROR_MSG(0x136, "Connection from %s to index "
2349                                            "%u doesn't match actual OST index "
2350                                            "%u in last_rcvd file, bad "
2351                                            "configuration?\n",
2352                                            obd_export_nid2str(exp), index,
2353                                            data->ocd_index);
2354                         RETURN(-EBADF);
2355                 }
2356         }
2357
2358         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_SIZE)) {
2359                 data->ocd_brw_size = 65536;
2360         } else if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
2361                 data->ocd_brw_size = min(data->ocd_brw_size,
2362                                          (__u32)(PTLRPC_MAX_BRW_PAGES <<
2363                                                  CFS_PAGE_SHIFT));
2364                 LASSERT(data->ocd_brw_size);
2365         }
2366
2367         if (data->ocd_connect_flags & OBD_CONNECT_CKSUM) {
2368                 __u32 cksum_types = data->ocd_cksum_types;
2369
2370                 /* The client set in ocd_cksum_types the checksum types it
2371                  * supports. We have to mask off the algorithms that we don't
2372                  * support */
2373                 if (cksum_types & OBD_CKSUM_ALL)
2374                         data->ocd_cksum_types &= OBD_CKSUM_ALL;
2375                 else
2376                         data->ocd_cksum_types = OBD_CKSUM_CRC32;
2377
2378                 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
2379                                    "%x\n", exp->exp_obd->obd_name,
2380                                    obd_export_nid2str(exp), cksum_types,
2381                                    data->ocd_cksum_types);
2382         } else {
2383                 /* This client does not support OBD_CONNECT_CKSUM
2384                  * fall back to CRC32 */
2385                 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
2386                                    "OBD_CONNECT_CKSUM, CRC32 will be used\n",
2387                                    exp->exp_obd->obd_name,
2388                                    obd_export_nid2str(exp));
2389         }
2390
2391         /* FIXME: Do the same with the MDS UUID and fsd_peeruuid.
2392          * FIXME: We don't strictly need the COMPAT flag for that,
2393          * FIXME: as fsd_peeruuid[0] will tell us if that is set.
2394          * FIXME: We needed it for the index, as index 0 is valid. */
2395
2396         RETURN(0);
2397 }
2398
2399 static int filter_reconnect(struct obd_export *exp, struct obd_device *obd,
2400                             struct obd_uuid *cluuid,
2401                             struct obd_connect_data *data,
2402                             void *localdata)
2403 {
2404         int rc;
2405         ENTRY;
2406
2407         if (exp == NULL || obd == NULL || cluuid == NULL)
2408                 RETURN(-EINVAL);
2409
2410         rc = filter_connect_internal(exp, data);
2411         if (rc == 0)
2412                 filter_export_stats_init(obd, exp, 1, localdata);
2413
2414         RETURN(rc);
2415 }
2416
2417 /* nearly identical to mds_connect */
2418 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
2419                           struct obd_uuid *cluuid,
2420                           struct obd_connect_data *data,
2421                           void *localdata)
2422 {
2423         struct obd_export *exp;
2424         struct filter_export_data *fed;
2425         struct lsd_client_data *lcd = NULL;
2426         int rc;
2427         ENTRY;
2428
2429         if (conn == NULL || obd == NULL || cluuid == NULL)
2430                 RETURN(-EINVAL);
2431
2432         /* Check for aborted recovery. */
2433         target_recovery_check_and_stop(obd);
2434
2435         rc = class_connect(conn, obd, cluuid);
2436         if (rc)
2437                 RETURN(rc);
2438         exp = class_conn2export(conn);
2439         LASSERT(exp != NULL);
2440
2441         fed = &exp->exp_filter_data;
2442
2443         rc = filter_connect_internal(exp, data);
2444         if (rc)
2445                 GOTO(cleanup, rc);
2446
2447         filter_export_stats_init(obd, exp, 0, localdata);
2448
2449         if (!obd->obd_replayable)
2450                 GOTO(cleanup, rc = 0);
2451
2452         OBD_ALLOC_PTR(lcd);
2453         if (!lcd) {
2454                 CERROR("filter: out of memory for client data\n");
2455                 GOTO(cleanup, rc = -ENOMEM);
2456         }
2457
2458         memcpy(lcd->lcd_uuid, cluuid, sizeof(lcd->lcd_uuid));
2459         fed->fed_lcd = lcd;
2460
2461         rc = filter_client_add(obd, exp, -1);
2462
2463         GOTO(cleanup, rc);
2464
2465 cleanup:
2466         if (rc) {
2467                 if (lcd) {
2468                         OBD_FREE_PTR(lcd);
2469                         fed->fed_lcd = NULL;
2470                 }
2471                 class_disconnect(exp);
2472                 lprocfs_exp_cleanup(exp);
2473         } else {
2474                 class_export_put(exp);
2475         }
2476
2477         RETURN(rc);
2478 }
2479
2480 /* Do extra sanity checks for grant accounting.  We do this at connect,
2481  * disconnect, and statfs RPC time, so it shouldn't be too bad.  We can
2482  * always get rid of it or turn it off when we know accounting is good. */
2483 static void filter_grant_sanity_check(struct obd_device *obd, const char *func)
2484 {
2485         struct filter_export_data *fed;
2486         struct obd_export *exp;
2487         obd_size maxsize = obd->obd_osfs.os_blocks * obd->obd_osfs.os_bsize;
2488         obd_size tot_dirty = 0, tot_pending = 0, tot_granted = 0;
2489         obd_size fo_tot_dirty, fo_tot_pending, fo_tot_granted;
2490
2491         if (list_empty(&obd->obd_exports))
2492                 return;
2493
2494         /* We don't want to do this for large machines that do lots of
2495            mounts or unmounts.  It burns... */
2496         if (obd->obd_num_exports > 100)
2497                 return;
2498
2499         spin_lock(&obd->obd_osfs_lock);
2500         spin_lock(&obd->obd_dev_lock);
2501         list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
2502                 int error = 0;
2503                 fed = &exp->exp_filter_data;
2504                 if (fed->fed_grant < 0 || fed->fed_pending < 0 ||
2505                     fed->fed_dirty < 0)
2506                         error = 1;
2507                 if (maxsize > 0) { /* we may not have done a statfs yet */
2508                         LASSERTF(fed->fed_grant + fed->fed_pending <= maxsize,
2509                                  "%s: cli %s/%p %ld+%ld > "LPU64"\n", func,
2510                                  exp->exp_client_uuid.uuid, exp,
2511                                  fed->fed_grant, fed->fed_pending, maxsize);
2512                         LASSERTF(fed->fed_dirty <= maxsize,
2513                                  "%s: cli %s/%p %ld > "LPU64"\n", func,
2514                                  exp->exp_client_uuid.uuid, exp,
2515                                  fed->fed_dirty, maxsize);
2516                 }
2517                 if (error)
2518                         CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
2519                                obd->obd_name, exp->exp_client_uuid.uuid, exp,
2520                                fed->fed_dirty, fed->fed_pending,fed->fed_grant);
2521                 else
2522                         CDEBUG(D_CACHE, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
2523                                obd->obd_name, exp->exp_client_uuid.uuid, exp,
2524                                fed->fed_dirty, fed->fed_pending,fed->fed_grant);
2525                 tot_granted += fed->fed_grant + fed->fed_pending;
2526                 tot_pending += fed->fed_pending;
2527                 tot_dirty += fed->fed_dirty;
2528         }
2529         fo_tot_granted = obd->u.filter.fo_tot_granted;
2530         fo_tot_pending = obd->u.filter.fo_tot_pending;
2531         fo_tot_dirty = obd->u.filter.fo_tot_dirty;
2532         spin_unlock(&obd->obd_dev_lock);
2533         spin_unlock(&obd->obd_osfs_lock);
2534
2535         /* Do these assertions outside the spinlocks so we don't kill system */
2536         if (tot_granted != fo_tot_granted)
2537                 CERROR("%s: tot_granted "LPU64" != fo_tot_granted "LPU64"\n",
2538                        func, tot_granted, fo_tot_granted);
2539         if (tot_pending != fo_tot_pending)
2540                 CERROR("%s: tot_pending "LPU64" != fo_tot_pending "LPU64"\n",
2541                        func, tot_pending, fo_tot_pending);
2542         if (tot_dirty != fo_tot_dirty)
2543                 CERROR("%s: tot_dirty "LPU64" != fo_tot_dirty "LPU64"\n",
2544                        func, tot_dirty, fo_tot_dirty);
2545         if (tot_pending > tot_granted)
2546                 CERROR("%s: tot_pending "LPU64" > tot_granted "LPU64"\n",
2547                        func, tot_pending, tot_granted);
2548         if (tot_granted > maxsize)
2549                 CERROR("%s: tot_granted "LPU64" > maxsize "LPU64"\n",
2550                        func, tot_granted, maxsize);
2551         if (tot_dirty > maxsize)
2552                 CERROR("%s: tot_dirty "LPU64" > maxsize "LPU64"\n",
2553                        func, tot_dirty, maxsize);
2554 }
2555
2556 /* Remove this client from the grant accounting totals.  We also remove
2557  * the export from the obd device under the osfs and dev locks to ensure
2558  * that the filter_grant_sanity_check() calculations are always valid.
2559  * The client should do something similar when it invalidates its import. */
2560 static void filter_grant_discard(struct obd_export *exp)
2561 {
2562         struct obd_device *obd = exp->exp_obd;
2563         struct filter_obd *filter = &obd->u.filter;
2564         struct filter_export_data *fed = &exp->exp_filter_data;
2565
2566         spin_lock(&obd->obd_osfs_lock);
2567         spin_lock(&obd->obd_dev_lock);
2568         list_del_init(&exp->exp_obd_chain);
2569         spin_unlock(&obd->obd_dev_lock);
2570
2571         LASSERTF(filter->fo_tot_granted >= fed->fed_grant,
2572                  "%s: tot_granted "LPU64" cli %s/%p fed_grant %ld\n",
2573                  obd->obd_name, filter->fo_tot_granted,
2574                  exp->exp_client_uuid.uuid, exp, fed->fed_grant);
2575         filter->fo_tot_granted -= fed->fed_grant;
2576         LASSERTF(filter->fo_tot_pending >= fed->fed_pending,
2577                  "%s: tot_pending "LPU64" cli %s/%p fed_pending %ld\n",
2578                  obd->obd_name, filter->fo_tot_pending,
2579                  exp->exp_client_uuid.uuid, exp, fed->fed_pending);
2580         /* fo_tot_pending is handled in filter_grant_commit as bulk finishes */
2581         LASSERTF(filter->fo_tot_dirty >= fed->fed_dirty,
2582                  "%s: tot_dirty "LPU64" cli %s/%p fed_dirty %ld\n",
2583                  obd->obd_name, filter->fo_tot_dirty,
2584                  exp->exp_client_uuid.uuid, exp, fed->fed_dirty);
2585         filter->fo_tot_dirty -= fed->fed_dirty;
2586         fed->fed_dirty = 0;
2587         fed->fed_grant = 0;
2588
2589         spin_unlock(&obd->obd_osfs_lock);
2590 }
2591
2592 static int filter_destroy_export(struct obd_export *exp)
2593 {
2594         ENTRY;
2595
2596         if (exp->exp_filter_data.fed_pending)
2597                 CERROR("%s: cli %s/%p has %lu pending on destroyed export\n",
2598                        exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
2599                        exp, exp->exp_filter_data.fed_pending);
2600
2601         lquota_clearinfo(filter_quota_interface_ref, exp, exp->exp_obd);
2602
2603         target_destroy_export(exp);
2604         ldlm_destroy_export(exp);
2605
2606         if (obd_uuid_equals(&exp->exp_client_uuid, &exp->exp_obd->obd_uuid))
2607                 RETURN(0);
2608
2609
2610         if (exp->exp_obd->obd_replayable)
2611                 filter_client_free(exp);
2612         else
2613                 fsfilt_sync(exp->exp_obd, exp->exp_obd->u.obt.obt_sb);
2614
2615         filter_grant_discard(exp);
2616         filter_fmd_cleanup(exp);
2617
2618         if (!(exp->exp_flags & OBD_OPT_FORCE))
2619                 filter_grant_sanity_check(exp->exp_obd, __FUNCTION__);
2620
2621         RETURN(0);
2622 }
2623
2624 /* also incredibly similar to mds_disconnect */
2625 static int filter_disconnect(struct obd_export *exp)
2626 {
2627         struct obd_device *obd = exp->exp_obd;
2628         struct llog_ctxt *ctxt;
2629         int rc;
2630         ENTRY;
2631
2632         LASSERT(exp);
2633         class_export_get(exp);
2634
2635         /* Flush any remaining cancel messages out to the target */
2636         ctxt = llog_get_context(obd, LLOG_MDS_OST_REPL_CTXT);
2637         if (ctxt) {
2638                 if (ctxt->loc_imp == exp->exp_imp_reverse)
2639                         CDEBUG(D_RPCTRACE, "Reverse import disconnect\n");
2640                 llog_sync(ctxt, exp);
2641                 llog_ctxt_put(ctxt);
2642         }
2643
2644         if (exp->exp_connect_flags & OBD_CONNECT_GRANT_SHRINK) {
2645                 struct filter_obd *filter = &exp->exp_obd->u.filter;
2646                 if (filter->fo_tot_granted_clients > 0)
2647                         filter->fo_tot_granted_clients --;
2648         }
2649
2650         if (!(exp->exp_flags & OBD_OPT_FORCE))
2651                 filter_grant_sanity_check(obd, __FUNCTION__);
2652         filter_grant_discard(exp);
2653
2654         lquota_clearinfo(filter_quota_interface_ref, exp, exp->exp_obd);
2655
2656         /* Disconnect early so that clients can't keep using export */
2657         rc = class_disconnect(exp);
2658         if (exp->exp_obd->obd_namespace != NULL)
2659                 ldlm_cancel_locks_for_export(exp);
2660
2661         lprocfs_exp_cleanup(exp);
2662         class_export_put(exp);
2663         RETURN(rc);
2664 }
2665
2666 static int filter_ping(struct obd_export *exp)
2667 {
2668         filter_fmd_expire(exp);
2669
2670         if (exp->exp_delayed)
2671                 filter_update_client_epoch(exp);
2672
2673         return 0;
2674 }
2675
2676 struct dentry *__filter_oa2dentry(struct obd_device *obd, struct obdo *oa,
2677                                   const char *what, int quiet)
2678 {
2679         struct dentry *dchild = NULL;
2680
2681         if (!(oa->o_valid & OBD_MD_FLGROUP))
2682                 oa->o_gr = 0;
2683
2684         dchild = filter_fid2dentry(obd, NULL, oa->o_gr, oa->o_id);
2685
2686         if (IS_ERR(dchild)) {
2687                 CERROR("%s error looking up object: "LPU64"\n",
2688                        what, oa->o_id);
2689                 RETURN(dchild);
2690         }
2691
2692         if (dchild->d_inode == NULL) {
2693                 if (!quiet)
2694                         CERROR("%s: %s on non-existent object: "LPU64"\n",
2695                                obd->obd_name, what, oa->o_id);
2696                 f_dput(dchild);
2697                 RETURN(ERR_PTR(-ENOENT));
2698         }
2699
2700         return dchild;
2701 }
2702
2703 static int filter_getattr(struct obd_export *exp, struct obd_info *oinfo)
2704 {
2705         struct dentry *dentry = NULL;
2706         struct obd_device *obd;
2707         int rc = 0;
2708         ENTRY;
2709
2710         obd = class_exp2obd(exp);
2711         if (obd == NULL) {
2712                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
2713                 RETURN(-EINVAL);
2714         }
2715
2716         dentry = filter_oa2dentry(obd, oinfo->oi_oa);
2717         if (IS_ERR(dentry))
2718                 RETURN(PTR_ERR(dentry));
2719
2720         /* Limit the valid bits in the return data to what we actually use */
2721         oinfo->oi_oa->o_valid = OBD_MD_FLID;
2722         obdo_from_inode(oinfo->oi_oa, dentry->d_inode, FILTER_VALID_FLAGS);
2723
2724         f_dput(dentry);
2725         RETURN(rc);
2726 }
2727
2728 /* this should be enabled/disabled in condition to enabled/disabled large
2729  * inodes (fast EAs) in backing store FS. */
2730 int filter_update_fidea(struct obd_export *exp, struct inode *inode,
2731                         void *handle, struct obdo *oa)
2732 {
2733         struct obd_device *obd = exp->exp_obd;
2734         int rc = 0;
2735         ENTRY;
2736
2737         if (oa->o_valid & OBD_MD_FLFID) {
2738                 struct filter_fid ff;
2739
2740                 if (!(oa->o_valid & OBD_MD_FLGROUP))
2741                         oa->o_gr = 0;
2742
2743                 /* packing fid and converting it to LE for storing into EA.
2744                  * Here ->o_stripe_idx should be filled by LOV and rest of
2745                  * fields - by client. */
2746                 ff.ff_fid.id = cpu_to_le64(oa->o_fid);
2747                 ff.ff_fid.f_type = cpu_to_le32(oa->o_stripe_idx);
2748                 ff.ff_fid.generation = cpu_to_le32(oa->o_generation);
2749                 ff.ff_objid = cpu_to_le64(oa->o_id);
2750                 ff.ff_group = cpu_to_le64(oa->o_gr);
2751
2752                 CDEBUG(D_INODE, "storing filter fid EA ("LPU64"/%u/%u"
2753                        LPU64"/"LPU64")\n", oa->o_fid, oa->o_stripe_idx,
2754                        oa->o_generation, oa->o_id, oa->o_gr);
2755
2756                 rc = fsfilt_set_md(obd, inode, handle, &ff, sizeof(ff), "fid");
2757                 if (rc)
2758                         CERROR("store fid in object failed! rc: %d\n", rc);
2759         } else {
2760                 CDEBUG(D_HA, "OSS object without fid info!\n");
2761         }
2762
2763         RETURN(rc);
2764 }
2765
2766 /* this is called from filter_truncate() until we have filter_punch() */
2767 int filter_setattr_internal(struct obd_export *exp, struct dentry *dentry,
2768                             struct obdo *oa, struct obd_trans_info *oti)
2769 {
2770         unsigned int orig_ids[MAXQUOTAS] = {0, 0};
2771         struct llog_cookie *fcc = NULL;
2772         struct filter_obd *filter;
2773         int rc, err, sync = 0;
2774         loff_t old_size = 0;
2775         unsigned int ia_valid;
2776         struct inode *inode;
2777         struct page *page = NULL;
2778         struct iattr iattr;
2779         void *handle;
2780         ENTRY;
2781
2782         LASSERT(dentry != NULL);
2783         LASSERT(!IS_ERR(dentry));
2784
2785         inode = dentry->d_inode;
2786         LASSERT(inode != NULL);
2787
2788         filter = &exp->exp_obd->u.filter;
2789         iattr_from_obdo(&iattr, oa, oa->o_valid);
2790         ia_valid = iattr.ia_valid;
2791
2792         if (oa->o_valid & OBD_MD_FLCOOKIE) {
2793                 OBD_ALLOC(fcc, sizeof(*fcc));
2794                 if (fcc != NULL)
2795                         *fcc = oa->o_lcookie;
2796         }
2797         if (ia_valid & (ATTR_SIZE | ATTR_UID | ATTR_GID)) {
2798                 DQUOT_INIT(inode);
2799                 /* Filter truncates and writes are serialized by
2800                  * i_alloc_sem, see the comment in
2801                  * filter_preprw_write.*/
2802                 if (ia_valid & ATTR_SIZE)
2803                         down_write(&inode->i_alloc_sem);
2804                 LOCK_INODE_MUTEX(inode);
2805                 old_size = i_size_read(inode);
2806         }
2807
2808         /* VBR: version recovery check */
2809         rc = filter_version_get_check(exp, oti, inode);
2810         if (rc)
2811                 GOTO(out_unlock, rc);
2812
2813         /* Let's pin the last page so that ldiskfs_truncate
2814          * should not start GFP_FS allocation (20008). */
2815         if (ia_valid & ATTR_SIZE) {
2816                 page = grab_cache_page(inode->i_mapping,
2817                                        iattr.ia_size >> PAGE_CACHE_SHIFT);
2818                 if (page == NULL)
2819                         GOTO(out_unlock, rc = -ENOMEM);
2820
2821                 unlock_page(page);
2822         }
2823
2824         /* If the inode still has SUID+SGID bits set (see filter_precreate())
2825          * then we will accept the UID+GID sent by the client during write for
2826          * initializing the ownership of this inode.  We only allow this to
2827          * happen once so clear these bits in setattr. In 2.6 kernels it is
2828          * possible to get ATTR_UID and ATTR_GID separately, so we only clear
2829          * the flags that are actually being set. */
2830         if (ia_valid & (ATTR_UID | ATTR_GID)) {
2831                 CDEBUG(D_INODE, "update UID/GID to %lu/%lu\n",
2832                        (unsigned long)oa->o_uid, (unsigned long)oa->o_gid);
2833
2834                 if ((inode->i_mode & S_ISUID) && (ia_valid & ATTR_UID)) {
2835                         if (!(ia_valid & ATTR_MODE)) {
2836                                 iattr.ia_mode = inode->i_mode;
2837                                 iattr.ia_valid |= ATTR_MODE;
2838                         }
2839                         iattr.ia_mode &= ~S_ISUID;
2840                 }
2841                 if ((inode->i_mode & S_ISGID) && (ia_valid & ATTR_GID)) {
2842                         if (!(iattr.ia_valid & ATTR_MODE)) {
2843                                 iattr.ia_mode = inode->i_mode;
2844                                 iattr.ia_valid |= ATTR_MODE;
2845                         }
2846                         iattr.ia_mode &= ~S_ISGID;
2847                 }
2848
2849                 orig_ids[USRQUOTA] = inode->i_uid;
2850                 orig_ids[GRPQUOTA] = inode->i_gid;
2851                 handle = fsfilt_start_log(exp->exp_obd, inode,
2852                                           FSFILT_OP_SETATTR, oti, 1);
2853
2854                 if (IS_ERR(handle))
2855                         GOTO(out_unlock, rc = PTR_ERR(handle));
2856
2857                 /* update inode EA only once when inode is suid bit marked. As
2858                  * on 2.6.x UID and GID may be set separately, we check here
2859                  * only one of them to avoid double setting. */
2860                 if (inode->i_mode & S_ISUID)
2861                         filter_update_fidea(exp, inode, handle, oa);
2862         } else {
2863                 handle = fsfilt_start(exp->exp_obd, inode,
2864                                       FSFILT_OP_SETATTR, oti);
2865
2866                 if (IS_ERR(handle))
2867                         GOTO(out_unlock, rc = PTR_ERR(handle));
2868         }
2869
2870         if (oa->o_valid & OBD_MD_FLFLAGS) {
2871                 rc = fsfilt_iocontrol(exp->exp_obd, dentry,
2872                                       FSFILT_IOC_SETFLAGS, (long)&oa->o_flags);
2873         } else {
2874                 rc = fsfilt_setattr(exp->exp_obd, dentry, handle, &iattr, 1);
2875                 if (fcc != NULL)
2876                         /* set cancel cookie callback function */
2877                         sync = fsfilt_add_journal_cb(exp->exp_obd, 0, handle,
2878                                                      filter_cancel_cookies_cb,
2879                                                      fcc);
2880         }
2881
2882         if (OBD_FAIL_CHECK(OBD_FAIL_OST_SETATTR_CREDITS))
2883                 fsfilt_extend(exp->exp_obd, inode, 0, handle);
2884
2885         /* The truncate might have used up our transaction credits.  Make
2886          * sure we have one left for the last_rcvd update. */
2887         err = fsfilt_extend(exp->exp_obd, inode, 1, handle);
2888         rc = filter_finish_transno(exp, inode, oti, rc, sync);
2889         if (sync) {
2890                 filter_cancel_cookies_cb(exp->exp_obd, 0, fcc, rc);
2891                 fcc = NULL;
2892         }
2893
2894         err = fsfilt_commit(exp->exp_obd, inode, handle, 0);
2895         if (err) {
2896                 CERROR("error on commit, err = %d\n", err);
2897                 if (!rc)
2898                         rc = err;
2899         } else {
2900                 fcc = NULL;
2901         }
2902
2903         /* For a partial-page truncate flush the page to disk immediately
2904          * to avoid data corruption during direct disk write. b=17397 */
2905         if (!sync && (iattr.ia_valid & ATTR_SIZE) &&
2906             old_size != iattr.ia_size && (iattr.ia_size & ~CFS_PAGE_MASK)) {
2907                 err = filemap_fdatawrite_range(inode->i_mapping, iattr.ia_size,
2908                                                iattr.ia_size + 1);
2909                 if (!rc)
2910                         rc = err;
2911         }
2912
2913         EXIT;
2914
2915 out_unlock:
2916         if (page)
2917                 page_cache_release(page);
2918
2919         if (ia_valid & (ATTR_SIZE | ATTR_UID | ATTR_GID))
2920                 UNLOCK_INODE_MUTEX(inode);
2921         if (ia_valid & ATTR_SIZE)
2922                 up_write(&inode->i_alloc_sem);
2923         if (fcc)
2924                 OBD_FREE(fcc, sizeof(*fcc));
2925
2926         /* trigger quota release */
2927         if (ia_valid & (ATTR_SIZE | ATTR_UID | ATTR_GID)) {
2928                 unsigned int cur_ids[MAXQUOTAS] = {oa->o_uid, oa->o_gid};
2929                 int rc2 = lquota_adjust(filter_quota_interface_ref,exp->exp_obd,
2930                                         cur_ids, orig_ids,rc,FSFILT_OP_SETATTR);
2931                 CDEBUG(rc2 ? D_ERROR : D_QUOTA,
2932                        "filter adjust qunit. (rc:%d)\n", rc2);
2933         }
2934         return rc;
2935 }
2936
2937 /* this is called from filter_truncate() until we have filter_punch() */
2938 int filter_setattr(struct obd_export *exp, struct obd_info *oinfo,
2939                    struct obd_trans_info *oti)
2940 {
2941         struct ldlm_res_id res_id = { .name = { oinfo->oi_oa->o_id } };
2942         struct filter_mod_data *fmd;
2943         struct lvfs_run_ctxt saved;
2944         struct filter_obd *filter;
2945         struct ldlm_resource *res;
2946         struct dentry *dentry;
2947         int rc = 0;
2948         ENTRY;
2949
2950         dentry = __filter_oa2dentry(exp->exp_obd, oinfo->oi_oa,
2951                                     __FUNCTION__, 1);
2952         if (IS_ERR(dentry))
2953                 RETURN(PTR_ERR(dentry));
2954
2955         filter = &exp->exp_obd->u.filter;
2956         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2957
2958         /* 
2959          * We need to be atomic against a concurrent write
2960          * (which takes the semaphore for reading). fmd_mactime_xid
2961          * checks will have no effect if a write request with lower
2962          * xid starts just before a setattr and finishes later than
2963          * the setattr (see bug 21489, comment 27).
2964          */
2965         if (oinfo->oi_oa->o_valid &
2966             (OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME)) {
2967                 down_write(&dentry->d_inode->i_alloc_sem);
2968                 fmd = filter_fmd_get(exp,oinfo->oi_oa->o_id,oinfo->oi_oa->o_gr);
2969                 if (fmd && fmd->fmd_mactime_xid < oti->oti_xid)
2970                         fmd->fmd_mactime_xid = oti->oti_xid;
2971                 filter_fmd_put(exp, fmd);
2972                 up_write(&dentry->d_inode->i_alloc_sem);
2973         }
2974
2975         /* setting objects attributes (including owner/group) */
2976         rc = filter_setattr_internal(exp, dentry, oinfo->oi_oa, oti);
2977         if (rc)
2978                 GOTO(out_unlock, rc);
2979
2980         res = ldlm_resource_get(exp->exp_obd->obd_namespace, NULL,
2981                                 res_id, LDLM_EXTENT, 0);
2982
2983         if (res != NULL) {
2984                 rc = ldlm_res_lvbo_update(res, NULL, 0, 0);
2985                 ldlm_resource_putref(res);
2986         }
2987
2988         oinfo->oi_oa->o_valid = OBD_MD_FLID;
2989
2990         /* Quota release need uid/gid info */
2991         obdo_from_inode(oinfo->oi_oa, dentry->d_inode,
2992                         FILTER_VALID_FLAGS | OBD_MD_FLUID | OBD_MD_FLGID);
2993
2994         EXIT;
2995 out_unlock:
2996         f_dput(dentry);
2997         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2998         return rc;
2999 }
3000
3001 /* XXX identical to osc_unpackmd */
3002 static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
3003                            struct lov_mds_md *lmm, int lmm_bytes)
3004 {
3005         int lsm_size;
3006         ENTRY;
3007
3008         if (lmm != NULL) {
3009                 if (lmm_bytes < sizeof (*lmm)) {
3010                         CERROR("lov_mds_md too small: %d, need %d\n",
3011                                lmm_bytes, (int)sizeof(*lmm));
3012                         RETURN(-EINVAL);
3013                 }
3014                 /* XXX LOV_MAGIC etc check? */
3015
3016                 if (lmm->lmm_object_id == cpu_to_le64(0)) {
3017                         CERROR("lov_mds_md: zero lmm_object_id\n");
3018                         RETURN(-EINVAL);
3019                 }
3020         }
3021
3022         lsm_size = lov_stripe_md_size(1);
3023         if (lsmp == NULL)
3024                 RETURN(lsm_size);
3025
3026         if (*lsmp != NULL && lmm == NULL) {
3027                 OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
3028                 OBD_FREE(*lsmp, lsm_size);
3029                 *lsmp = NULL;
3030                 RETURN(0);
3031         }
3032
3033         if (*lsmp == NULL) {
3034                 OBD_ALLOC(*lsmp, lsm_size);
3035                 if (*lsmp == NULL)
3036                         RETURN(-ENOMEM);
3037                 OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
3038                 if ((*lsmp)->lsm_oinfo[0] == NULL) {
3039                         OBD_FREE(*lsmp, lsm_size);
3040                         RETURN(-ENOMEM);
3041                 }
3042                 loi_init((*lsmp)->lsm_oinfo[0]);
3043         }
3044
3045         if (lmm != NULL) {
3046                 /* XXX zero *lsmp? */
3047                 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
3048                 LASSERT((*lsmp)->lsm_object_id);
3049         }
3050
3051         (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
3052
3053         RETURN(lsm_size);
3054 }
3055
3056 /* caller must hold fo_create_lock */
3057 static int filter_destroy_precreated(struct obd_export *exp, struct obdo *oa,
3058                                      struct filter_obd *filter)
3059 {
3060         struct obdo doa = { 0 }; /* XXX obdo on stack */
3061         obd_id last, id;
3062         int rc = 0;
3063         int skip_orphan;
3064         ENTRY;
3065
3066         LASSERT(oa);
3067         LASSERT(down_trylock(&filter->fo_create_lock) != 0);
3068
3069         memset(&doa, 0, sizeof(doa));
3070         if (oa->o_valid & OBD_MD_FLGROUP) {
3071                 doa.o_valid |= OBD_MD_FLGROUP;
3072                 doa.o_gr = oa->o_gr;
3073         } else {
3074                 doa.o_gr = 0;
3075         }
3076         doa.o_mode = S_IFREG;
3077
3078         if (!filter->fo_destroy_in_progress) {
3079                 CERROR("%s: destroy_in_progress already cleared\n",
3080                         exp->exp_obd->obd_name);
3081                 RETURN(0);
3082         }
3083
3084         last = filter_last_id(filter, doa.o_gr);
3085         skip_orphan = !!(exp->exp_connect_flags & OBD_CONNECT_SKIP_ORPHAN);
3086
3087         CWARN("%s: deleting orphan objects from "LPU64" to "LPU64"%s\n",
3088                exp->exp_obd->obd_name, oa->o_id + 1, last,
3089                skip_orphan ? ", orphan objids won't be reused any more." : ".");
3090
3091         for (id = last; id > oa->o_id; id--) {
3092                 doa.o_id = id;
3093                 rc = filter_destroy(exp, &doa, NULL, NULL, NULL);
3094                 if (rc && rc != -ENOENT) /* this is pretty fatal... */
3095                         CEMERG("error destroying precreate objid "LPU64": %d\n",
3096                                id, rc);
3097                 /* update last_id on disk periodically so that if we restart
3098                  * we don't need to re-scan all of the just-deleted objects. */
3099                 if ((id & 511) == 0 && !skip_orphan) {
3100                         filter_set_last_id(filter, id - 1, doa.o_gr);
3101                         filter_update_last_objid(exp->exp_obd, doa.o_gr, 0);
3102                 }
3103         }
3104
3105         CDEBUG(D_HA, "%s: after destroy: set last_objids["LPU64"] = "LPU64"\n",
3106                exp->exp_obd->obd_name, doa.o_gr, oa->o_id);
3107
3108         if (!skip_orphan) {
3109                 filter_set_last_id(filter, id, doa.o_gr);
3110                 rc = filter_update_last_objid(exp->exp_obd, doa.o_gr, 1);
3111         } else {
3112                 /* don't reuse orphan object, return last used objid */
3113                 oa->o_id = last;
3114                 rc = 0;
3115         }
3116         filter->fo_destroy_in_progress = 0;
3117
3118         RETURN(rc);
3119 }
3120
3121 static int filter_precreate(struct obd_device *obd, struct obdo *oa,
3122                             obd_gr group, int *num);
3123 /* returns a negative error or a nonnegative number of files to create */
3124 static int filter_handle_precreate(struct obd_export *exp, struct obdo *oa,
3125                                    obd_gr group, struct obd_trans_info *oti)
3126 {
3127         struct obd_device *obd = exp->exp_obd;
3128         struct filter_obd *filter = &obd->u.filter;
3129         int diff, rc;
3130         ENTRY;
3131
3132         /* delete orphans request */
3133         if ((oa->o_valid & OBD_MD_FLFLAGS) && (oa->o_flags & OBD_FL_DELORPHAN)){
3134                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
3135                         CERROR("%s: dropping old orphan cleanup request\n",
3136                                obd->obd_name);
3137                         RETURN(0);
3138                 }
3139
3140                 /* This causes inflight precreates to abort and drop lock */
3141                 filter->fo_destroy_in_progress = 1;
3142                 down(&filter->fo_create_lock);
3143                 diff = oa->o_id - filter_last_id(filter, group);
3144                 CDEBUG(D_HA, "filter_last_id() = "LPU64" -> diff = %d\n",
3145                        filter_last_id(filter, group), diff);
3146
3147                 if (-diff > OST_MAX_PRECREATE) {
3148                         CERROR("%s: ignoring bogus orphan destroy request: "
3149                                "obdid "LPU64" last_id "LPU64"\n", obd->obd_name,
3150                                oa->o_id, filter_last_id(filter, group));
3151                         /* FIXME: should reset precreate_next_id on MDS */
3152                         GOTO(out, rc = -EINVAL);
3153                 }
3154                 if (diff < 0) {
3155                         rc = filter_destroy_precreated(exp, oa, filter);
3156                         if (rc)
3157                                 CERROR("%s: unable to write lastobjid, but "
3158                                        "orphans were deleted\n", obd->obd_name);
3159                         GOTO(out, rc);
3160                 } else {
3161                         /*XXX used by MDS for the first time! */
3162                         filter->fo_destroy_in_progress = 0;
3163                 }
3164         } else {
3165                 down(&filter->fo_create_lock);
3166                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
3167                         CERROR("%s: dropping old precreate request\n",
3168                                obd->obd_name);
3169                         GOTO(out, rc = 0);
3170                 }
3171                 /* only precreate if group == 0 and o_id is specfied */
3172                 if (group != 0 || oa->o_id == 0)
3173                         diff = 1;
3174                 else
3175                         diff = oa->o_id - filter_last_id(filter, group);
3176                 CDEBUG(D_RPCTRACE, "filter_last_id() = "LPU64" -> diff = %d\n",
3177                        filter_last_id(filter, group), diff);
3178
3179                 LASSERTF(diff >= 0,"%s: "LPU64" - "LPU64" = %d\n",obd->obd_name,
3180                          oa->o_id, filter_last_id(filter, group), diff);
3181         }
3182
3183         if (diff > 0) {
3184                 oa->o_id = filter_last_id(&obd->u.filter, group);
3185                 rc = filter_precreate(obd, oa, group, &diff);
3186                 oa->o_id = filter_last_id(&obd->u.filter, group);
3187                 oa->o_valid = OBD_MD_FLID;
3188                 GOTO(out, rc);
3189         }
3190         /* else diff == 0 */
3191         GOTO(out, rc = 0);
3192 out:
3193         up(&filter->fo_create_lock);
3194         return rc;
3195 }
3196
3197 static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
3198                          __u64 max_age, __u32 flags)
3199 {
3200         struct filter_obd *filter = &obd->u.filter;
3201         int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
3202         int rc;
3203         ENTRY;
3204
3205         /* at least try to account for cached pages.  its still racey and
3206          * might be under-reporting if clients haven't announced their
3207          * caches with brw recently */
3208         spin_lock(&obd->obd_osfs_lock);
3209         rc = fsfilt_statfs(obd, obd->u.obt.obt_sb, max_age);
3210         memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
3211         spin_unlock(&obd->obd_osfs_lock);
3212
3213         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
3214                " pending "LPU64" free "LPU64" avail "LPU64"\n",
3215                filter->fo_tot_dirty, filter->fo_tot_granted,
3216                filter->fo_tot_pending,
3217                osfs->os_bfree << blockbits, osfs->os_bavail << blockbits);
3218
3219         filter_grant_sanity_check(obd, __FUNCTION__);
3220
3221         osfs->os_bavail -= min(osfs->os_bavail, GRANT_FOR_LLOG(obd) +
3222                                ((filter->fo_tot_dirty + filter->fo_tot_pending +
3223                                  osfs->os_bsize - 1) >> blockbits));
3224
3225         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOSPC)) {
3226                 struct lr_server_data *lsd = filter->fo_fsd;
3227                 int index = le32_to_cpu(lsd->lsd_ost_index);
3228
3229                 if (obd_fail_val == -1 ||
3230                     index == obd_fail_val)
3231                         osfs->os_bfree = osfs->os_bavail = 2;
3232                 else if (obd_fail_loc & OBD_FAIL_ONCE)
3233                         obd_fail_loc &= ~OBD_FAILED; /* reset flag */
3234         }
3235
3236         /* set EROFS to state field if FS is mounted as RDONLY. The goal is to
3237          * stop creating files on MDS if OST is not good shape to create
3238          * objects.*/
3239         osfs->os_state = 0;
3240
3241         if (filter->fo_obt.obt_sb->s_flags & MS_RDONLY)
3242                 osfs->os_state = OS_STATE_READONLY;
3243
3244         if (filter->fo_raid_degraded)
3245                 osfs->os_state |= OS_STATE_DEGRADED;
3246         RETURN(rc);
3247 }
3248
3249 static int filter_use_existing_obj(struct obd_device *obd,
3250                                    struct dentry *dchild, void **handle,
3251                                    int *cleanup_phase)
3252 {
3253         struct inode *inode = dchild->d_inode;
3254         struct iattr iattr;
3255         int rc;
3256
3257         if ((inode->i_mode & (S_ISUID | S_ISGID)) == (S_ISUID|S_ISGID))
3258                 return 0;
3259
3260         *handle = fsfilt_start_log(obd, inode, FSFILT_OP_SETATTR, NULL, 1);
3261         if (IS_ERR(*handle))
3262                 return PTR_ERR(*handle);
3263
3264         iattr.ia_valid = ATTR_MODE;
3265         iattr.ia_mode = S_ISUID | S_ISGID |0666;
3266         rc = fsfilt_setattr(obd, dchild, *handle, &iattr, 1);
3267         if (rc == 0)
3268                 *cleanup_phase = 3;
3269
3270         return rc;
3271 }
3272
3273
3274 /* We rely on the fact that only one thread will be creating files in a given
3275  * group at a time, which is why we don't need an atomic filter_get_new_id.
3276  * Even if we had that atomic function, the following race would exist:
3277  *
3278  * thread 1: gets id x from filter_next_id
3279  * thread 2: gets id (x + 1) from filter_next_id
3280  * thread 2: creates object (x + 1)
3281  * thread 1: tries to create object x, gets -ENOSPC
3282  *
3283  * Caller must hold fo_create_lock
3284  */
3285 static int filter_precreate(struct obd_device *obd, struct obdo *oa,
3286                             obd_gr group, int *num)
3287 {
3288         struct dentry *dchild = NULL, *dparent = NULL;
3289         struct filter_obd *filter;
3290         int err = 0, rc = 0, recreate_obj = 0, i;
3291         cfs_time_t enough_time = cfs_time_shift(DISK_TIMEOUT/2);
3292         obd_id next_id;
3293         void *handle = NULL;
3294         ENTRY;
3295
3296         filter = &obd->u.filter;
3297
3298         LASSERT(down_trylock(&filter->fo_create_lock) != 0);
3299
3300         OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_PRECREATE, obd_timeout / 2);
3301
3302         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
3303             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
3304                 recreate_obj = 1;
3305         } else {
3306                 struct obd_statfs *osfs;
3307
3308                 OBD_ALLOC(osfs, sizeof(*osfs));
3309                 if (osfs == NULL)
3310                         RETURN(-ENOMEM);
3311                 rc = filter_statfs(obd, osfs,
3312                                    cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
3313                                    0);
3314                 if (rc == 0 && osfs->os_bavail < (osfs->os_blocks >> 10)) {
3315                         CDEBUG(D_RPCTRACE,"%s: not enough space for create "
3316                                LPU64"\n", obd->obd_name, osfs->os_bavail <<
3317                                filter->fo_vfsmnt->mnt_sb->s_blocksize_bits);
3318                         *num = 0;
3319                         rc = -ENOSPC;
3320                 }
3321                 OBD_FREE(osfs, sizeof(*osfs));
3322                 if (rc)
3323                         RETURN(rc);
3324         }
3325
3326         CDEBUG(D_RPCTRACE, "%s: precreating %d objects in group "LPU64
3327                " at "LPU64"\n", obd->obd_name, *num, group, oa->o_id);
3328
3329         for (i = 0; i < *num && err == 0; i++) {
3330                 int cleanup_phase = 0;
3331
3332                 if (recreate_obj) {
3333                         __u64 last_id;
3334                         next_id = oa->o_id;
3335                         last_id = filter_last_id(filter, group);
3336                         if (next_id > last_id) {
3337                                 CERROR("%s: trying to recreate obj greater"
3338                                        "than last id "LPD64" > "LPD64"\n",
3339                                        obd->obd_name, next_id, last_id);
3340                                 GOTO(cleanup, rc = -EINVAL);
3341                         }
3342                 } else if (filter->fo_destroy_in_progress) {
3343                         CWARN("%s: precreate aborted by destroy\n",
3344                               obd->obd_name);
3345                         rc = -EAGAIN;
3346                         break;
3347                 } else
3348                         next_id = filter_last_id(filter, group) + 1;
3349
3350                 dparent = filter_parent_lock(obd, group, next_id);
3351                 if (IS_ERR(dparent))
3352                         GOTO(cleanup, rc = PTR_ERR(dparent));
3353                 cleanup_phase = 1;      /* filter_parent_unlock(dparent) */
3354
3355                 dchild = filter_fid2dentry(obd, dparent, group, next_id);
3356                 if (IS_ERR(dchild))
3357                         GOTO(cleanup, rc = PTR_ERR(dchild));
3358                 cleanup_phase = 2;      /* f_dput(dchild) */
3359
3360                 if (dchild->d_inode != NULL) {
3361                         /* This would only happen if lastobjid was bad on disk*/
3362                         /* Could also happen if recreating missing obj but it
3363                          * already exists. */
3364                         if (recreate_obj) {
3365                                 CERROR("%s: recreating existing object %.*s?\n",
3366                                        obd->obd_name, dchild->d_name.len,
3367                                        dchild->d_name.name);
3368                         } else {
3369                                 /* Use these existing objects if they are
3370                                  * zero length. */
3371                                 if (dchild->d_inode->i_size == 0) {
3372                                         rc = filter_use_existing_obj(obd,dchild,
3373                                                       &handle, &cleanup_phase);
3374                                         if (rc == 0)
3375                                                 goto set_last_id;
3376                                         else
3377                                                 GOTO(cleanup, rc);
3378                                 }
3379
3380                                 CERROR("%s: Serious error: objid %.*s already "
3381                                        "exists; is this filesystem corrupt?\n",
3382                                        obd->obd_name, dchild->d_name.len,
3383                                        dchild->d_name.name);
3384                                 LBUG();
3385                         }
3386                         GOTO(cleanup, rc = -EEXIST);
3387                 }
3388
3389                 handle = fsfilt_start_log(obd, dparent->d_inode,
3390                                           FSFILT_OP_CREATE, NULL, 1);
3391                 if (IS_ERR(handle))
3392                         GOTO(cleanup, rc = PTR_ERR(handle));
3393                 cleanup_phase = 3;
3394
3395                 CDEBUG(D_INODE, "%s: filter_precreate(od->o_gr="LPU64
3396                        ",od->o_id="LPU64")\n", obd->obd_name, group,
3397                        next_id);
3398
3399                 /* We mark object SUID+SGID to flag it for accepting UID+GID
3400                  * from client on first write.  Currently the permission bits
3401                  * on the OST are never used, so this is OK. */
3402                 rc = ll_vfs_create(dparent->d_inode, dchild,
3403                                    S_IFREG |  S_ISUID | S_ISGID | 0666, NULL);
3404                 if (rc) {
3405                         CERROR("create failed rc = %d\n", rc);
3406                         GOTO(cleanup, rc);
3407                 }
3408                 if (dchild->d_inode)
3409                         CDEBUG(D_INFO, "objid "LPU64" got inum %lu\n", next_id,
3410                                        dchild->d_inode->i_ino);
3411
3412 set_last_id:
3413                 if (!recreate_obj) {
3414                         filter_set_last_id(filter, next_id, group);
3415                         err = filter_update_last_objid(obd, group, 0);
3416                         if (err)
3417                                 CERROR("unable to write lastobjid "
3418                                        "but file created\n");
3419                 }
3420
3421         cleanup:
3422                 switch(cleanup_phase) {
3423                 case 3:
3424                         err = fsfilt_commit(obd, dparent->d_inode, handle, 0);
3425                         if (err) {
3426                                 CERROR("error on commit, err = %d\n", err);
3427                                 if (!rc)
3428                                         rc = err;
3429                         }
3430                 case 2:
3431                         f_dput(dchild);
3432                 case 1:
3433                         filter_parent_unlock(dparent);
3434                 case 0:
3435                         break;
3436                 }
3437
3438                 if (rc)
3439                         break;
3440                 if (cfs_time_after(cfs_time_current(), enough_time)) {
3441                         CDEBUG(D_RPCTRACE,
3442                                "%s: precreate slow - want %d got %d \n",
3443                                obd->obd_name, *num, i);
3444                         break;
3445                 }
3446         }
3447         *num = i;
3448
3449         CDEBUG(D_RPCTRACE,
3450                "%s: created %d objects for group "LPU64": "LPU64" rc %d\n",
3451                obd->obd_name, i, group, filter->fo_last_objids[group], rc);
3452
3453         RETURN(rc);
3454 }
3455
3456 int filter_recreate(struct obd_device *obd, struct obdo *oa)
3457 {
3458         struct ldlm_res_id res_id = { .name = { oa->o_id } };
3459         struct ldlm_valblock_ops *ns_lvbo;
3460         struct ldlm_resource *res;
3461         obd_valid old_valid = oa->o_valid;
3462         obd_flag old_flags = oa->o_flags;
3463         int diff = 1, rc;
3464         ENTRY;
3465
3466         if (oa->o_id > filter_last_id(&obd->u.filter, oa->o_gr)) {
3467                 if (!obd->obd_recovering ||
3468                     oa->o_id > filter_last_id(&obd->u.filter, oa->o_gr) +
3469                     OST_MAX_PRECREATE) {
3470                         CERROR("recreate objid "LPU64" > last id "LPU64"\n",
3471                                oa->o_id, filter_last_id(&obd->u.filter,
3472                                oa->o_gr));
3473                         RETURN(-EINVAL);
3474                 }
3475                 diff = oa->o_id - filter_last_id(&obd->u.filter, oa->o_gr);
3476         } else {
3477                 if ((oa->o_valid & OBD_MD_FLFLAGS) == 0) {
3478                         oa->o_valid |= OBD_MD_FLFLAGS;
3479                         oa->o_flags = OBD_FL_RECREATE_OBJS;
3480                 } else {
3481                         oa->o_flags |= OBD_FL_RECREATE_OBJS;
3482                 }
3483         }
3484
3485         down(&obd->u.filter.fo_create_lock);
3486         rc = filter_precreate(obd, oa, oa->o_gr, &diff);
3487         up(&obd->u.filter.fo_create_lock);
3488
3489         res = ldlm_resource_get(obd->obd_namespace, NULL,
3490                                 res_id, LDLM_EXTENT, 0);
3491         if (res != NULL) {
3492                 /* Update lvb->lvb_blocks for the recreated object */
3493                 ns_lvbo = res->lr_namespace->ns_lvbo;
3494                 if (ns_lvbo && ns_lvbo->lvbo_update) {
3495                         rc = ns_lvbo->lvbo_update(res, NULL, 0, 1);
3496                         if (rc)
3497                                 RETURN(rc);
3498                 }
3499                 ldlm_resource_putref(res);
3500         }
3501
3502         if (rc == 0)
3503                 CWARN("%s: recreated missing object "LPU64"/"LPU64"\n",
3504                       obd->obd_name, oa->o_id, oa->o_gr);
3505
3506         oa->o_valid = old_valid;
3507         oa->o_flags = old_flags;
3508         RETURN(rc);
3509 }
3510
3511 static int filter_create(struct obd_export *exp, struct obdo *oa,
3512                          struct lov_stripe_md **ea, struct obd_trans_info *oti)
3513 {
3514         struct obd_device *obd = exp->exp_obd;
3515         struct lvfs_run_ctxt saved;
3516         struct lov_stripe_md *lsm = NULL;
3517         struct ldlm_res_id res_id = { .name = { oa->o_id } };
3518         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
3519         struct lustre_handle lockh;
3520         int flags = 0;
3521         int rc = 0;
3522         ENTRY;
3523
3524         CDEBUG(D_INODE, "%s: filter_create(od->o_gr="LPU64",od->o_id="
3525                LPU64")\n", obd->obd_name, oa->o_gr, oa->o_id);
3526
3527         if (!(oa->o_valid & OBD_MD_FLGROUP))
3528                 oa->o_gr = 0;
3529
3530         CDEBUG(D_INFO, "object "LPU64"/"LPU64"\n", oa->o_id, oa->o_gr);
3531         if (ea != NULL) {
3532                 lsm = *ea;
3533                 if (lsm == NULL) {
3534                         rc = obd_alloc_memmd(exp, &lsm);
3535                         if (rc < 0)
3536                                 RETURN(rc);
3537                 }
3538         }
3539
3540         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3541
3542         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
3543             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
3544                 /* Cancel all conflicting extent locks on recreating object,
3545                  * thus object's metadata will be updated on the clients */
3546                 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id,
3547                                             LDLM_EXTENT, &policy, LCK_PW,
3548                                             &flags, ldlm_blocking_ast,
3549                                             ldlm_completion_ast,
3550                                             ldlm_glimpse_ast, NULL, 0,
3551                                             NULL, &lockh);
3552                 rc = filter_recreate(obd, oa);
3553                 ldlm_lock_decref(&lockh, LCK_PW);
3554         } else {
3555                 rc = filter_handle_precreate(exp, oa, oa->o_gr, oti);
3556         }
3557
3558         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3559         if (rc && ea != NULL && *ea != lsm) {
3560                 obd_free_memmd(exp, &lsm);
3561         } else if (rc == 0 && ea != NULL) {
3562                 /* XXX LOV STACKING: the lsm that is passed to us from
3563                  * LOV does not have valid lsm_oinfo data structs, so
3564                  * don't go touching that.  This needs to be fixed in a
3565                  * big way. */
3566                 lsm->lsm_object_id = oa->o_id;
3567                 *ea = lsm;
3568         }
3569
3570         RETURN(rc);
3571 }
3572
3573 int filter_destroy(struct obd_export *exp, struct obdo *oa,
3574                    struct lov_stripe_md *md, struct obd_trans_info *oti,
3575                    struct obd_export *md_exp)
3576 {
3577         unsigned int qcids[MAXQUOTAS] = {0, 0};
3578         struct obd_device *obd;
3579         struct filter_obd *filter;
3580         struct dentry *dchild = NULL, *dparent = NULL;
3581         struct lustre_handle lockh = { 0 };
3582         struct lvfs_run_ctxt saved;
3583         void *handle = NULL;
3584         struct llog_cookie *fcc = NULL;
3585         int rc, rc2, cleanup_phase = 0, sync = 0;
3586         struct iattr iattr;
3587         ENTRY;
3588
3589         if (!(oa->o_valid & OBD_MD_FLGROUP))
3590                 oa->o_gr = 0;
3591
3592         obd = exp->exp_obd;
3593         filter = &obd->u.filter;
3594
3595         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3596         cleanup_phase = 1;
3597
3598         CDEBUG(D_INODE, "%s: filter_destroy(od->o_gr="LPU64",od->o_id="
3599                LPU64")\n", obd->obd_name, oa->o_gr, oa->o_id);
3600
3601         dchild = filter_fid2dentry(obd, NULL, oa->o_gr, oa->o_id);
3602         if (IS_ERR(dchild))
3603                 GOTO(cleanup, rc = PTR_ERR(dchild));
3604         cleanup_phase = 2;
3605
3606         if (dchild->d_inode == NULL) {
3607                 CDEBUG(D_INODE, "destroying non-existent object "LPU64"\n",
3608                        oa->o_id);
3609                 /* If object already gone, cancel cookie right now */
3610                 if (oa->o_valid & OBD_MD_FLCOOKIE) {
3611                         struct llog_ctxt *ctxt;
3612                         fcc = &oa->o_lcookie;
3613                         ctxt = llog_get_context(obd, fcc->lgc_subsys + 1);
3614                         llog_cancel(ctxt, NULL, 1, fcc, 0);
3615                         llog_ctxt_put(ctxt);
3616                         fcc = NULL; /* we didn't allocate fcc, don't free it */
3617                 }
3618                 GOTO(cleanup, rc = -ENOENT);
3619         }
3620
3621         rc = filter_prepare_destroy(obd, oa->o_id, &lockh);
3622         if (rc)
3623                 GOTO(cleanup, rc);
3624
3625         /* Our MDC connection is established by the MDS to us */
3626         if (oa->o_valid & OBD_MD_FLCOOKIE) {
3627                 OBD_ALLOC(fcc, sizeof(*fcc));
3628                 if (fcc != NULL)
3629                         *fcc = oa->o_lcookie;
3630         }
3631         DQUOT_INIT(dchild->d_inode);
3632
3633         /* we're gonna truncate it first in order to avoid possible deadlock:
3634          *      P1                      P2
3635          * open trasaction      open transaction
3636          * down(i_zombie)       down(i_zombie)
3637          *                      restart transaction
3638          * (see BUG 4180) -bzzz
3639          *
3640          * take i_alloc_sem too to prevent other threads from writing to the
3641          * file while we are truncating it. This can cause lock ordering issue
3642          * between page lock, i_mutex & starting new journal handle.
3643          * (see bug 20321) -johann
3644          */
3645         down_write(&dchild->d_inode->i_alloc_sem);
3646         LOCK_INODE_MUTEX(dchild->d_inode);
3647
3648         /* VBR: version recovery check */
3649         rc = filter_version_get_check(exp, oti, dchild->d_inode);
3650         if (rc) {
3651                 UNLOCK_INODE_MUTEX(dchild->d_inode);
3652                 up_write(&dchild->d_inode->i_alloc_sem);
3653                 GOTO(cleanup, rc);
3654         }
3655
3656         handle = fsfilt_start_log(obd, dchild->d_inode, FSFILT_OP_SETATTR,
3657                                   NULL, 1);
3658         if (IS_ERR(handle)) {
3659                 UNLOCK_INODE_MUTEX(dchild->d_inode);
3660                 up_write(&dchild->d_inode->i_alloc_sem);
3661                 GOTO(cleanup, rc = PTR_ERR(handle));
3662         }
3663
3664         iattr.ia_valid = ATTR_SIZE;
3665         iattr.ia_size = 0;
3666         rc = fsfilt_setattr(obd, dchild, handle, &iattr, 1);
3667         rc2 = fsfilt_commit(obd, dchild->d_inode, handle, 0);
3668         UNLOCK_INODE_MUTEX(dchild->d_inode);
3669         up_write(&dchild->d_inode->i_alloc_sem);
3670         if (rc)
3671                 GOTO(cleanup, rc);
3672         if (rc2)
3673                 GOTO(cleanup, rc = rc2);
3674
3675         /* We don't actually need to lock the parent until we are unlinking
3676          * here, and not while truncating above.  That avoids holding the
3677          * parent lock for a long time during truncate, which can block other
3678          * threads from doing anything to objects in that directory. bug 7171 */
3679         dparent = filter_parent_lock(obd, oa->o_gr, oa->o_id);
3680         if (IS_ERR(dparent))
3681                 GOTO(cleanup, rc = PTR_ERR(dparent));
3682         cleanup_phase = 3; /* filter_parent_unlock */
3683
3684         LOCK_INODE_MUTEX(dchild->d_inode);
3685         handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_UNLINK,oti,1);
3686         if (IS_ERR(handle)) {
3687                 UNLOCK_INODE_MUTEX(dchild->d_inode);
3688                 GOTO(cleanup, rc = PTR_ERR(handle));
3689         }
3690         cleanup_phase = 4; /* fsfilt_commit */
3691
3692         /* Quota release need uid/gid of inode */
3693         obdo_from_inode(oa, dchild->d_inode, OBD_MD_FLUID|OBD_MD_FLGID);
3694
3695         filter_fmd_drop(exp, oa->o_id, oa->o_gr);
3696
3697         /* this drops dchild->d_inode->i_mutex unconditionally */
3698         rc = filter_destroy_internal(obd, oa->o_id, oa->o_gr, dparent, dchild);
3699
3700         EXIT;
3701 cleanup:
3702         switch(cleanup_phase) {
3703         case 4:
3704                 if (fcc != NULL)
3705                         sync = fsfilt_add_journal_cb(obd, 0, oti ?
3706                                                      oti->oti_handle : handle,
3707                                                      filter_cancel_cookies_cb,
3708                                                      fcc);
3709                 /* If add_journal_cb failed, then filter_finish_transno
3710                  * will commit the handle and we will do a sync
3711                  * on commit. then we call callback directly to free
3712                  * the fcc.
3713                  */
3714                 rc = filter_finish_transno(exp, NULL, oti, rc, sync);
3715                 if (sync) {
3716                         filter_cancel_cookies_cb(obd, 0, fcc, rc);
3717                         fcc = NULL;
3718                 }
3719                 rc2 = fsfilt_commit(obd, dparent->d_inode, handle, 0);
3720                 if (rc2) {
3721                         CERROR("error on commit, err = %d\n", rc2);
3722                         if (!rc)
3723                                 rc = rc2;
3724                 } else {
3725                         fcc = NULL;
3726                 }
3727         case 3:
3728                 filter_parent_unlock(dparent);
3729         case 2:
3730                 filter_fini_destroy(obd, &lockh);
3731
3732                 f_dput(dchild);
3733                 if (fcc != NULL)
3734                         OBD_FREE(fcc, sizeof(*fcc));
3735         case 1:
3736                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3737                 break;
3738         default:
3739                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3740                 LBUG();
3741         }
3742
3743         /* trigger quota release */
3744         qcids[USRQUOTA] = oa->o_uid;
3745         qcids[GRPQUOTA] = oa->o_gid;
3746         rc2 = lquota_adjust(filter_quota_interface_ref, obd, qcids, NULL, rc,
3747                             FSFILT_OP_UNLINK);
3748         if (rc2)
3749                 CERROR("filter adjust qunit! (rc:%d)\n", rc2);
3750         return rc;
3751 }
3752
3753 /* NB start and end are used for punch, but not truncate */
3754 static int filter_truncate(struct obd_export *exp, struct obd_info *oinfo,
3755                            struct obd_trans_info *oti,
3756                            struct ptlrpc_request_set *rqset)
3757 {
3758         int rc;
3759         ENTRY;
3760
3761         if (oinfo->oi_policy.l_extent.end != OBD_OBJECT_EOF) {
3762                 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
3763                        oinfo->oi_policy.l_extent.end);
3764                 RETURN(-EFAULT);
3765         }
3766         
3767         CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = "LPX64
3768                ", o_size = "LPD64"\n", oinfo->oi_oa->o_id,
3769                oinfo->oi_oa->o_valid, oinfo->oi_policy.l_extent.start);
3770
3771         oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.start;
3772         rc = filter_setattr(exp, oinfo, oti);
3773
3774         RETURN(rc);
3775 }
3776
3777 static int filter_sync(struct obd_export *exp, struct obd_info *oinfo,
3778                        obd_off start, obd_off end,
3779                        struct ptlrpc_request_set *set)
3780 {
3781         struct lvfs_run_ctxt saved;
3782         struct filter_obd *filter;
3783         struct dentry *dentry;
3784         struct llog_ctxt *ctxt;
3785         int rc, rc2;
3786         ENTRY;
3787
3788         filter = &exp->exp_obd->u.filter;
3789
3790         /* An objid of zero is taken to mean "sync whole filesystem" */
3791         if (!oinfo->oi_oa || !(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
3792                 rc = fsfilt_sync(exp->exp_obd, filter->fo_obt.obt_sb);
3793
3794                 /* Flush any remaining cancel messages out to the target */
3795                 ctxt = llog_get_context(exp->exp_obd, LLOG_MDS_OST_REPL_CTXT);
3796                 if (ctxt) {
3797                         llog_sync(ctxt, exp);
3798                         llog_ctxt_put(ctxt);
3799                 } else {
3800                         CERROR("No LLOG_MDS_OST_REPL_CTXT found in obd %p\n",
3801                                exp->exp_obd);
3802                 }
3803                 RETURN(rc);
3804         }
3805
3806         dentry = filter_oa2dentry(exp->exp_obd, oinfo->oi_oa);
3807         if (IS_ERR(dentry))
3808                 RETURN(PTR_ERR(dentry));
3809
3810         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
3811
3812         LOCK_INODE_MUTEX(dentry->d_inode);
3813
3814         rc = filemap_fdatawrite(dentry->d_inode->i_mapping);
3815         if (rc == 0) {
3816                 /* just any file to grab fsync method - "file" arg unused */
3817                 struct file *file = filter->fo_rcvd_filp;
3818
3819                 if (file->f_op && file->f_op->fsync)
3820                         rc = file->f_op->fsync(NULL, dentry, 1);
3821
3822                 rc2 = filemap_fdatawait(dentry->d_inode->i_mapping);
3823                 if (!rc)
3824                         rc = rc2;
3825         }
3826         UNLOCK_INODE_MUTEX(dentry->d_inode);
3827
3828         oinfo->oi_oa->o_valid = OBD_MD_FLID;
3829         obdo_from_inode(oinfo->oi_oa, dentry->d_inode, FILTER_VALID_FLAGS);
3830
3831         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
3832
3833         f_dput(dentry);
3834         RETURN(rc);
3835 }
3836
3837 static int filter_get_info(struct obd_export *exp, __u32 keylen,
3838                            void *key, __u32 *vallen, void *val,
3839                            struct lov_stripe_md *lsm)
3840 {
3841         struct obd_device *obd;
3842         ENTRY;
3843
3844         obd = class_exp2obd(exp);
3845         if (obd == NULL) {
3846                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
3847                 RETURN(-EINVAL);
3848         }
3849
3850         if (KEY_IS(KEY_BLOCKSIZE)) {
3851                 __u32 *blocksize = val;
3852                 if (blocksize) {
3853                         if (*vallen < sizeof(*blocksize))
3854                                 RETURN(-EOVERFLOW);
3855                         *blocksize = obd->u.obt.obt_sb->s_blocksize;
3856                 }
3857                 *vallen = sizeof(*blocksize);
3858                 RETURN(0);
3859         }
3860
3861         if (KEY_IS(KEY_BLOCKSIZE_BITS)) {
3862                 __u32 *blocksize_bits = val;
3863                 if (blocksize_bits) {
3864                         if (*vallen < sizeof(*blocksize_bits))
3865                                 RETURN(-EOVERFLOW);
3866                         *blocksize_bits = obd->u.obt.obt_sb->s_blocksize_bits;
3867                 }
3868                 *vallen = sizeof(*blocksize_bits);
3869                 RETURN(0);
3870         }
3871
3872         if (KEY_IS(KEY_LAST_ID)) {
3873                 obd_id *last_id = val;
3874                 /* FIXME: object groups */
3875                 if (last_id) {
3876                         if (*vallen < sizeof(*last_id))
3877                                 RETURN(-EOVERFLOW);
3878                         *last_id = filter_last_id(&obd->u.filter, 0);
3879                 }
3880                 *vallen = sizeof(*last_id);
3881                 RETURN(0);
3882         }
3883
3884         if (KEY_IS(KEY_FIEMAP)) {
3885                 struct ll_fiemap_info_key *fm_key = key;
3886                 struct dentry *dentry;
3887                 struct ll_user_fiemap *fiemap = val;
3888                 struct lvfs_run_ctxt saved;
3889                 int rc;
3890
3891                 if (fiemap == NULL) {
3892                         *vallen = fiemap_count_to_size(
3893                                                 fm_key->fiemap.fm_extent_count);
3894                         RETURN(0);
3895                 }
3896
3897                 dentry = __filter_oa2dentry(exp->exp_obd, &fm_key->oa,
3898                                             __FUNCTION__, 1);
3899                 if (IS_ERR(dentry))
3900                         RETURN(PTR_ERR(dentry));
3901
3902                 memcpy(fiemap, &fm_key->fiemap, sizeof(*fiemap));
3903                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3904
3905                 rc = fsfilt_iocontrol(obd, dentry, FSFILT_IOC_FIEMAP,
3906                                       (long)fiemap);
3907                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3908
3909                 f_dput(dentry);
3910                 RETURN(rc);
3911         }
3912
3913         CDEBUG(D_IOCTL, "invalid key\n");
3914         RETURN(-EINVAL);
3915 }
3916
3917 static int filter_set_info_async(struct obd_export *exp, __u32 keylen,
3918                                  void *key, __u32 vallen, void *val,
3919                                  struct ptlrpc_request_set *set)
3920 {
3921         struct obd_device *obd;
3922         struct llog_ctxt *ctxt;
3923         int rc = 0;
3924         ENTRY;
3925
3926         obd = exp->exp_obd;
3927         if (obd == NULL) {
3928                 CDEBUG(D_IOCTL, "invalid export %p\n", exp);
3929                 RETURN(-EINVAL);
3930         }
3931
3932         if (KEY_IS(KEY_GRANT_SHRINK)) {
3933                 struct ost_body *body = (struct ost_body *)val;
3934                 /* handle shrink grant */
3935                 spin_lock(&exp->exp_obd->obd_osfs_lock);
3936                 filter_grant_incoming(exp, &body->oa);
3937                 spin_unlock(&exp->exp_obd->obd_osfs_lock);
3938                 RETURN(rc);
3939         }
3940
3941         if (KEY_IS(KEY_CAPA_KEY)) {
3942                 RETURN(0);
3943         }
3944
3945         if (!KEY_IS(KEY_MDS_CONN))
3946                 RETURN(-EINVAL);
3947
3948         LCONSOLE_WARN("%s: received MDS connection from %s\n", obd->obd_name,
3949                       obd_export_nid2str(exp));
3950         obd->u.filter.fo_mdc_conn.cookie = exp->exp_handle.h_cookie;
3951
3952         /* setup llog imports */
3953         ctxt = llog_get_context(obd, LLOG_MDS_OST_REPL_CTXT);
3954         rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
3955         llog_ctxt_put(ctxt);
3956
3957         lquota_setinfo(filter_quota_interface_ref, exp, obd);
3958
3959         RETURN(rc);
3960 }
3961
3962 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
3963                      int len, void *karg, void *uarg)
3964 {
3965         struct obd_device *obd = exp->exp_obd;
3966         struct obd_ioctl_data *data = karg;
3967         int rc = 0;
3968
3969         switch (cmd) {
3970         case OBD_IOC_ABORT_RECOVERY: {
3971                 CERROR("aborting recovery for device %s\n", obd->obd_name);
3972                 target_abort_recovery(obd);
3973                 RETURN(0);
3974         }
3975
3976         case OBD_IOC_SYNC: {
3977                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
3978                 rc = fsfilt_sync(obd, obd->u.obt.obt_sb);
3979                 RETURN(rc);
3980         }
3981
3982         case OBD_IOC_SET_READONLY: {
3983                 void *handle;
3984                 struct super_block *sb = obd->u.obt.obt_sb;
3985                 struct inode *inode = sb->s_root->d_inode;
3986                 LCONSOLE_WARN("*** setting obd %s device '%s' read-only ***\n",
3987                               obd->obd_name, sb->s_id);
3988
3989                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
3990                 if (!IS_ERR(handle))
3991                         rc = fsfilt_commit(obd, inode, handle, 1);
3992
3993                 CDEBUG(D_HA, "syncing ost %s\n", obd->obd_name);
3994                 rc = fsfilt_sync(obd, obd->u.obt.obt_sb);
3995
3996                 lvfs_set_rdonly(obd, obd->u.obt.obt_sb);
3997                 RETURN(0);
3998         }
3999
4000         case OBD_IOC_CATLOGLIST: {
4001                 rc = llog_catalog_list(obd, 1, data);
4002                 RETURN(rc);
4003         }
4004
4005         case OBD_IOC_LLOG_CANCEL:
4006         case OBD_IOC_LLOG_REMOVE:
4007         case OBD_IOC_LLOG_INFO:
4008         case OBD_IOC_LLOG_PRINT: {
4009                 /* FIXME to be finished */
4010                 RETURN(-EOPNOTSUPP);
4011 /*
4012                 struct llog_ctxt *ctxt = NULL;
4013
4014                 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
4015                 rc = llog_ioctl(ctxt, cmd, data);
4016                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
4017
4018                 RETURN(rc);
4019 */
4020         }
4021         default:
4022                 RETURN(-EINVAL);
4023         }
4024         RETURN(0);
4025 }
4026
4027 static int filter_health_check(struct obd_device *obd)
4028 {
4029 #ifdef USE_HEALTH_CHECK_WRITE
4030         struct filter_obd *filter = &obd->u.filter;
4031 #endif
4032         int rc = 0;
4033
4034         /*
4035          * health_check to return 0 on healthy
4036          * and 1 on unhealthy.
4037          */
4038         if (obd->u.obt.obt_sb->s_flags & MS_RDONLY)
4039                 rc = 1;
4040
4041 #ifdef USE_HEALTH_CHECK_WRITE
4042         LASSERT(filter->fo_obt.obt_health_check_filp != NULL);
4043         rc |= !!lvfs_check_io_health(obd, filter->fo_obt.obt_health_check_filp);
4044 #endif
4045
4046         return rc;
4047 }
4048
4049 static struct dentry *filter_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
4050                                              void *data)
4051 {
4052         return filter_fid2dentry(data, NULL, gr, id);
4053 }
4054
4055 static int filter_process_config(struct obd_device *obd,obd_count len,void *buf)
4056 {
4057         struct lustre_cfg *lcfg = buf;
4058         struct lprocfs_static_vars lvars;
4059         int rc = 0;
4060
4061         lprocfs_filter_init_vars(&lvars);
4062
4063         rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, lcfg, obd);
4064
4065         return(rc);
4066 }
4067
4068 static struct lvfs_callback_ops filter_lvfs_ops = {
4069         l_fid2dentry:     filter_lvfs_fid2dentry,
4070 };
4071
4072 static int filter_notify(struct obd_device *obd, struct obd_device *watched,
4073                          enum obd_notify_event ev, void *data)
4074 {
4075         ENTRY;
4076
4077         CDEBUG(D_CONFIG, "notify %s ev=%d\n", watched->obd_name, ev);
4078
4079         switch (ev) {
4080         case OBD_NOTIFY_CONFIG:
4081                 /* call this only when config is processed and stale_export_age
4082                  * value is configured */
4083                 class_disconnect_expired_exports(obd);
4084         default:
4085                 RETURN(0);
4086         }
4087 }
4088
4089 static struct obd_ops filter_obd_ops = {
4090         .o_owner          = THIS_MODULE,
4091         .o_get_info       = filter_get_info,
4092         .o_set_info_async = filter_set_info_async,
4093         .o_setup          = filter_setup,
4094         .o_precleanup     = filter_precleanup,
4095         .o_cleanup        = filter_cleanup,
4096         .o_connect        = filter_connect,
4097         .o_reconnect      = filter_reconnect,
4098         .o_disconnect     = filter_disconnect,
4099         .o_ping           = filter_ping,
4100         .o_init_export    = filter_init_export,
4101         .o_destroy_export = filter_destroy_export,
4102         .o_statfs         = filter_statfs,
4103         .o_getattr        = filter_getattr,
4104         .o_unpackmd       = filter_unpackmd,
4105         .o_create         = filter_create,
4106         .o_setattr        = filter_setattr,
4107         .o_destroy        = filter_destroy,
4108         .o_brw            = filter_brw,
4109         .o_punch          = filter_truncate,
4110         .o_sync           = filter_sync,
4111         .o_preprw         = filter_preprw,
4112         .o_commitrw       = filter_commitrw,
4113         .o_llog_init      = filter_llog_init,
4114         .o_llog_finish    = filter_llog_finish,
4115         .o_iocontrol      = filter_iocontrol,
4116         .o_health_check   = filter_health_check,
4117         .o_process_config = filter_process_config,
4118         .o_postrecov      = filter_postrecov,
4119         .o_notify         = filter_notify,
4120 };
4121
4122 quota_interface_t *filter_quota_interface_ref;
4123 extern quota_interface_t filter_quota_interface;
4124
4125 static int __init obdfilter_init(void)
4126 {
4127         struct lprocfs_static_vars lvars;
4128         int rc;
4129
4130         printk(KERN_INFO "Lustre: Filtering OBD driver; http://www.lustre.org/\n");
4131
4132         lprocfs_filter_init_vars(&lvars);
4133
4134         request_module("lquota");
4135         OBD_ALLOC(obdfilter_created_scratchpad,
4136                   OBDFILTER_CREATED_SCRATCHPAD_ENTRIES *
4137                   sizeof(*obdfilter_created_scratchpad));
4138         if (obdfilter_created_scratchpad == NULL)
4139                 return -ENOMEM;
4140
4141         ll_fmd_cachep = cfs_mem_cache_create("ll_fmd_cache",
4142                                              sizeof(struct filter_mod_data),
4143                                              0, 0);
4144         if (!ll_fmd_cachep)
4145                 GOTO(out, rc = -ENOMEM);
4146
4147         filter_quota_interface_ref = PORTAL_SYMBOL_GET(filter_quota_interface);
4148         init_obd_quota_ops(filter_quota_interface_ref, &filter_obd_ops);
4149
4150         rc = class_register_type(&filter_obd_ops, lvars.module_vars,
4151                                  LUSTRE_OST_NAME);
4152         if (rc) {
4153                 int err;
4154
4155                 err = cfs_mem_cache_destroy(ll_fmd_cachep);
4156                 LASSERTF(err == 0, "Cannot destroy ll_fmd_cachep: rc %d\n",err);
4157                 ll_fmd_cachep = NULL;
4158 out:
4159                 if (filter_quota_interface_ref)
4160                         PORTAL_SYMBOL_PUT(filter_quota_interface);
4161
4162                 OBD_FREE(obdfilter_created_scratchpad,
4163                          OBDFILTER_CREATED_SCRATCHPAD_ENTRIES *
4164                          sizeof(*obdfilter_created_scratchpad));
4165         }
4166
4167         return rc;
4168 }
4169
4170 static void __exit obdfilter_exit(void)
4171 {
4172         if (filter_quota_interface_ref)
4173                 PORTAL_SYMBOL_PUT(filter_quota_interface);
4174
4175         if (ll_fmd_cachep) {
4176                 int rc = cfs_mem_cache_destroy(ll_fmd_cachep);
4177                 LASSERTF(rc == 0, "Cannot destroy ll_fmd_cachep: rc %d\n", rc);
4178                 ll_fmd_cachep = NULL;
4179         }
4180
4181         class_unregister_type(LUSTRE_OST_NAME);
4182         OBD_FREE(obdfilter_created_scratchpad,
4183                  OBDFILTER_CREATED_SCRATCHPAD_ENTRIES *
4184                  sizeof(*obdfilter_created_scratchpad));
4185 }
4186
4187 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
4188 MODULE_DESCRIPTION("Lustre Filtering OBD driver");
4189 MODULE_LICENSE("GPL");
4190
4191 module_init(obdfilter_init);
4192 module_exit(obdfilter_exit);