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