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