1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/fs/obdfilter/filter.c
6 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
7 * Author: Peter Braam <braam@clusterfs.com>
8 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
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).
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.
36 #define DEBUG_SUBSYSTEM S_FILTER
38 #include <linux/config.h>
39 #include <linux/module.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>
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>
57 #include "filter_internal.h"
59 static struct lvfs_callback_ops filter_lvfs_ops;
61 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
62 struct lov_stripe_md *ea, struct obd_trans_info *);
64 static void filter_commit_cb(struct obd_device *obd, __u64 transno,
65 void *cb_data, int error)
67 obd_transno_commit_cb(obd, transno, error);
71 /* Assumes caller has already pushed us into the kernel context. */
72 int filter_finish_transno(struct obd_export *exp, struct obd_trans_info *oti,
75 struct filter_obd *filter = &exp->exp_obd->u.filter;
76 struct filter_export_data *fed = &exp->exp_filter_data;
77 struct filter_client_data *fcd = fed->fed_fcd;
80 int err, log_pri = D_HA;
82 /* Propagate error code. */
86 if (!exp->exp_obd->obd_replayable || oti == NULL)
89 /* we don't allocate new transnos for replayed requests */
90 if (oti->oti_transno == 0) {
91 spin_lock(&filter->fo_translock);
92 last_rcvd = le64_to_cpu(filter->fo_fsd->fsd_last_transno) + 1;
93 filter->fo_fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
94 spin_unlock(&filter->fo_translock);
95 oti->oti_transno = last_rcvd;
97 spin_lock(&filter->fo_translock);
98 last_rcvd = oti->oti_transno;
99 if (last_rcvd > le64_to_cpu(filter->fo_fsd->fsd_last_transno))
100 filter->fo_fsd->fsd_last_transno =
101 cpu_to_le64(last_rcvd);
102 spin_unlock(&filter->fo_translock);
104 fcd->fcd_last_rcvd = cpu_to_le64(last_rcvd);
106 /* could get xid from oti, if it's ever needed */
107 fcd->fcd_last_xid = 0;
109 off = fed->fed_lr_off;
110 fsfilt_add_journal_cb(exp->exp_obd, last_rcvd, oti->oti_handle,
111 filter_commit_cb, NULL);
112 err = fsfilt_write_record(exp->exp_obd, filter->fo_rcvd_filp, fcd,
113 sizeof(*fcd), &off, 0);
120 CDEBUG(log_pri, "wrote trans "LPU64" for client %s at #%d: err = %d\n",
121 last_rcvd, fcd->fcd_uuid, fed->fed_lr_idx, err);
126 void f_dput(struct dentry *dentry)
128 /* Can't go inside filter_ddelete because it can block */
129 CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
130 dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
131 LASSERT(atomic_read(&dentry->d_count) > 0);
136 /* Add client data to the FILTER. We use a bitmap to locate a free space
137 * in the last_rcvd file if cl_idx is -1 (i.e. a new client).
138 * Otherwise, we have just read the data from the last_rcvd file and
139 * we know its offset. */
140 static int filter_client_add(struct obd_device *obd, struct filter_obd *filter,
141 struct filter_export_data *fed, int cl_idx)
143 unsigned long *bitmap = filter->fo_last_rcvd_slots;
144 int new_client = (cl_idx == -1);
147 LASSERT(bitmap != NULL);
149 /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
150 if (!strcmp(fed->fed_fcd->fcd_uuid, obd->obd_uuid.uuid))
153 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
154 * there's no need for extra complication here
157 cl_idx = find_first_zero_bit(bitmap, FILTER_LR_MAX_CLIENTS);
159 if (cl_idx >= FILTER_LR_MAX_CLIENTS) {
160 CERROR("no client slots - fix FILTER_LR_MAX_CLIENTS\n");
163 if (test_and_set_bit(cl_idx, bitmap)) {
164 CERROR("FILTER client %d: found bit is set in bitmap\n",
166 cl_idx = find_next_zero_bit(bitmap,
167 FILTER_LR_MAX_CLIENTS,
172 if (test_and_set_bit(cl_idx, bitmap)) {
173 CERROR("FILTER client %d: bit already set in bitmap!\n",
179 fed->fed_lr_idx = cl_idx;
180 fed->fed_lr_off = le32_to_cpu(filter->fo_fsd->fsd_client_start) +
181 cl_idx * le16_to_cpu(filter->fo_fsd->fsd_client_size);
183 CDEBUG(D_INFO, "client at index %d (%llu) with UUID '%s' added\n",
184 fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
187 struct obd_run_ctxt saved;
188 loff_t off = fed->fed_lr_off;
192 CDEBUG(D_INFO, "writing client fcd at idx %u (%llu) (len %u)\n",
193 fed->fed_lr_idx,off,(unsigned int)sizeof(*fed->fed_fcd));
195 push_ctxt(&saved, &obd->obd_ctxt, NULL);
196 /* Transaction needed to fix bug 1403 */
197 handle = fsfilt_start(obd,
198 filter->fo_rcvd_filp->f_dentry->d_inode,
199 FSFILT_OP_SETATTR, NULL);
200 if (IS_ERR(handle)) {
201 err = PTR_ERR(handle);
202 CERROR("unable to start transaction: rc %d\n", err);
204 err = fsfilt_write_record(obd, filter->fo_rcvd_filp,
206 sizeof(*fed->fed_fcd),
209 filter->fo_rcvd_filp->f_dentry->d_inode,
212 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
215 CERROR("error writing %s client idx %u: rc %d\n",
216 LAST_RCVD, fed->fed_lr_idx, err);
223 static int filter_client_free(struct obd_export *exp, int flags)
225 struct filter_export_data *fed = &exp->exp_filter_data;
226 struct filter_obd *filter = &exp->exp_obd->u.filter;
227 struct obd_device *obd = exp->exp_obd;
228 struct filter_client_data zero_fcd;
229 struct obd_run_ctxt saved;
234 if (fed->fed_fcd == NULL)
237 if (flags & OBD_OPT_FAILOVER)
240 /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
241 if (strcmp(fed->fed_fcd->fcd_uuid, obd->obd_uuid.uuid ) == 0)
244 LASSERT(filter->fo_last_rcvd_slots != NULL);
246 off = fed->fed_lr_off;
248 CDEBUG(D_INFO, "freeing client at idx %u (%lld) with UUID '%s'\n",
249 fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
251 /* Clear the bit _after_ zeroing out the client so we don't
252 race with filter_client_add and zero out new clients.*/
253 if (!test_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
254 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
259 memset(&zero_fcd, 0, sizeof zero_fcd);
260 push_ctxt(&saved, &obd->obd_ctxt, NULL);
261 rc = fsfilt_write_record(obd, filter->fo_rcvd_filp, &zero_fcd,
262 sizeof(zero_fcd), &off, 1);
263 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
265 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
266 "zeroing disconnecting client %s at idx %u (%llu) in %s rc %d\n",
267 fed->fed_fcd->fcd_uuid, fed->fed_lr_idx, fed->fed_lr_off,
270 if (!test_and_clear_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
271 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
277 OBD_FREE(fed->fed_fcd, sizeof(*fed->fed_fcd));
282 static int filter_free_server_data(struct filter_obd *filter)
284 OBD_FREE(filter->fo_fsd, sizeof(*filter->fo_fsd));
285 filter->fo_fsd = NULL;
286 OBD_FREE(filter->fo_last_rcvd_slots,
287 FILTER_LR_MAX_CLIENT_WORDS * sizeof(unsigned long));
288 filter->fo_last_rcvd_slots = NULL;
292 /* assumes caller is already in kernel ctxt */
293 int filter_update_server_data(struct obd_device *obd, struct file *filp,
294 struct filter_server_data *fsd, int force_sync)
300 CDEBUG(D_INODE, "server uuid : %s\n", fsd->fsd_uuid);
301 CDEBUG(D_INODE, "server last_rcvd : "LPU64"\n",
302 le64_to_cpu(fsd->fsd_last_transno));
303 CDEBUG(D_INODE, "server last_mount: "LPU64"\n",
304 le64_to_cpu(fsd->fsd_mount_count));
306 rc = fsfilt_write_record(obd, filp, fsd, sizeof(*fsd), &off,force_sync);
308 CERROR("error writing filter_server_data: rc = %d\n", rc);
313 int filter_update_last_objid(struct obd_device *obd, obd_gr group,
316 struct filter_obd *filter = &obd->u.filter;
322 CDEBUG(D_INODE, "server last_objid for group "LPU64": "LPU64"\n",
323 group, filter->fo_last_objids[group]);
325 tmp = cpu_to_le64(filter->fo_last_objids[group]);
326 rc = fsfilt_write_record(obd, filter->fo_last_objid_files[group],
327 &tmp, sizeof(tmp), &off, force_sync);
329 CERROR("error writing group "LPU64" last objid: rc = %d\n",
334 /* assumes caller has already in kernel ctxt */
335 static int filter_init_server_data(struct obd_device *obd, struct file * filp)
337 struct filter_obd *filter = &obd->u.filter;
338 struct filter_server_data *fsd;
339 struct filter_client_data *fcd = NULL;
340 struct inode *inode = filp->f_dentry->d_inode;
341 unsigned long last_rcvd_size = inode->i_size;
347 /* ensure padding in the struct is the correct size */
348 LASSERT (offsetof(struct filter_server_data, fsd_padding) +
349 sizeof(fsd->fsd_padding) == FILTER_LR_SERVER_SIZE);
350 LASSERT (offsetof(struct filter_client_data, fcd_padding) +
351 sizeof(fcd->fcd_padding) == FILTER_LR_CLIENT_SIZE);
353 OBD_ALLOC(fsd, sizeof(*fsd));
356 filter->fo_fsd = fsd;
358 OBD_ALLOC(filter->fo_last_rcvd_slots,
359 FILTER_LR_MAX_CLIENT_WORDS * sizeof(unsigned long));
360 if (filter->fo_last_rcvd_slots == NULL) {
361 OBD_FREE(fsd, sizeof(*fsd));
365 if (last_rcvd_size == 0) {
366 CWARN("%s: initializing new %s\n", obd->obd_name, LAST_RCVD);
368 memcpy(fsd->fsd_uuid, obd->obd_uuid.uuid,sizeof(fsd->fsd_uuid));
369 fsd->fsd_last_transno = 0;
370 mount_count = fsd->fsd_mount_count = 0;
371 fsd->fsd_server_size = cpu_to_le32(FILTER_LR_SERVER_SIZE);
372 fsd->fsd_client_start = cpu_to_le32(FILTER_LR_CLIENT_START);
373 fsd->fsd_client_size = cpu_to_le16(FILTER_LR_CLIENT_SIZE);
374 fsd->fsd_subdir_count = cpu_to_le16(FILTER_SUBDIR_COUNT);
375 filter->fo_subdir_count = FILTER_SUBDIR_COUNT;
377 rc = fsfilt_read_record(obd, filp, fsd, sizeof(*fsd), &off);
379 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
383 if (strcmp(fsd->fsd_uuid, obd->obd_uuid.uuid) != 0) {
384 CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
385 obd->obd_uuid.uuid, fsd->fsd_uuid);
386 GOTO(err_fsd, rc = -EINVAL);
388 mount_count = le64_to_cpu(fsd->fsd_mount_count);
389 filter->fo_subdir_count = le16_to_cpu(fsd->fsd_subdir_count);
392 if (fsd->fsd_feature_incompat & ~cpu_to_le32(FILTER_INCOMPAT_SUPP)) {
393 CERROR("unsupported feature %x\n",
394 le32_to_cpu(fsd->fsd_feature_incompat) &
395 ~FILTER_INCOMPAT_SUPP);
396 GOTO(err_fsd, rc = -EINVAL);
398 if (fsd->fsd_feature_rocompat & ~cpu_to_le32(FILTER_ROCOMPAT_SUPP)) {
399 CERROR("read-only feature %x\n",
400 le32_to_cpu(fsd->fsd_feature_rocompat) &
401 ~FILTER_ROCOMPAT_SUPP);
402 /* Do something like remount filesystem read-only */
403 GOTO(err_fsd, rc = -EINVAL);
406 CDEBUG(D_INODE, "%s: server last_rcvd : "LPU64"\n",
407 obd->obd_name, le64_to_cpu(fsd->fsd_last_transno));
408 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
409 obd->obd_name, mount_count + 1);
410 CDEBUG(D_INODE, "%s: server data size: %u\n",
411 obd->obd_name, le32_to_cpu(fsd->fsd_server_size));
412 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
413 obd->obd_name, le32_to_cpu(fsd->fsd_client_start));
414 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
415 obd->obd_name, le32_to_cpu(fsd->fsd_client_size));
416 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
417 obd->obd_name, le16_to_cpu(fsd->fsd_subdir_count));
418 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
419 last_rcvd_size <= FILTER_LR_CLIENT_START ? 0 :
420 (last_rcvd_size-FILTER_LR_CLIENT_START) /FILTER_LR_CLIENT_SIZE);
422 if (!obd->obd_replayable) {
423 CWARN("%s: recovery support OFF\n", obd->obd_name);
427 for (cl_idx = 0, off = le32_to_cpu(fsd->fsd_client_start);
428 off < last_rcvd_size; cl_idx++) {
430 struct obd_export *exp;
431 struct filter_export_data *fed;
434 OBD_ALLOC(fcd, sizeof(*fcd));
436 GOTO(err_client, rc = -ENOMEM);
439 /* Don't assume off is incremented properly by
440 * fsfilt_read_record(), in case sizeof(*fcd)
441 * isn't the same as fsd->fsd_client_size. */
442 off = le32_to_cpu(fsd->fsd_client_start) +
443 cl_idx * le16_to_cpu(fsd->fsd_client_size);
444 rc = fsfilt_read_record(obd, filp, fcd, sizeof(*fcd), &off);
446 CERROR("error reading FILT %s idx %d off %llu: rc %d\n",
447 LAST_RCVD, cl_idx, off, rc);
448 break; /* read error shouldn't cause startup to fail */
451 if (fcd->fcd_uuid[0] == '\0') {
452 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
457 last_rcvd = le64_to_cpu(fcd->fcd_last_rcvd);
459 /* These exports are cleaned up by filter_disconnect(), so they
460 * need to be set up like real exports as filter_connect() does.
462 exp = class_new_export(obd);
463 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
464 " srv lr: "LPU64"\n", fcd->fcd_uuid, cl_idx,
465 last_rcvd, le64_to_cpu(fsd->fsd_last_transno));
467 GOTO(err_client, rc = -ENOMEM);
469 memcpy(&exp->exp_client_uuid.uuid, fcd->fcd_uuid,
470 sizeof exp->exp_client_uuid.uuid);
471 fed = &exp->exp_filter_data;
473 filter_client_add(obd, filter, fed, cl_idx);
474 /* create helper if export init gets more complex */
475 spin_lock_init(&fed->fed_lock);
478 obd->obd_recoverable_clients++;
479 class_export_put(exp);
481 CDEBUG(D_OTHER, "client at idx %d has last_rcvd = "LPU64"\n",
484 if (last_rcvd > le64_to_cpu(fsd->fsd_last_transno))
485 fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
489 obd->obd_last_committed = le64_to_cpu(fsd->fsd_last_transno);
491 if (obd->obd_recoverable_clients) {
492 CWARN("RECOVERY: %d recoverable clients, last_rcvd "
493 LPU64"\n", obd->obd_recoverable_clients,
494 le64_to_cpu(fsd->fsd_last_transno));
495 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
496 obd->obd_recovering = 1;
500 OBD_FREE(fcd, sizeof(*fcd));
503 filter->fo_mount_count = mount_count + 1;
504 fsd->fsd_mount_count = cpu_to_le64(filter->fo_mount_count);
506 /* save it, so mount count and last_transno is current */
507 rc = filter_update_server_data(obd, filp, filter->fo_fsd, 1);
512 class_disconnect_exports(obd, 0);
514 filter_free_server_data(filter);
518 static int filter_cleanup_groups(struct obd_device *obd)
520 struct filter_obd *filter = &obd->u.filter;
524 if (filter->fo_dentry_O_groups != NULL &&
525 filter->fo_last_objids != NULL &&
526 filter->fo_last_objid_files != NULL) {
527 for (i = 0; i < FILTER_GROUPS; i++) {
528 struct dentry *dentry = filter->fo_dentry_O_groups[i];
529 struct file *filp = filter->fo_last_objid_files[i];
530 if (dentry != NULL) {
532 filter->fo_dentry_O_groups[i] = NULL;
536 filter->fo_last_objid_files[i] = NULL;
540 if (filter->fo_dentry_O_sub != NULL && filter->fo_subdir_count) {
541 for (i = 0; i < filter->fo_subdir_count; i++) {
542 struct dentry *dentry = filter->fo_dentry_O_sub[i];
543 if (dentry != NULL) {
545 filter->fo_dentry_O_sub[i] = NULL;
548 OBD_FREE(filter->fo_dentry_O_sub,
549 filter->fo_subdir_count *
550 sizeof(*filter->fo_dentry_O_sub));
552 if (filter->fo_dentry_O_groups != NULL)
553 OBD_FREE(filter->fo_dentry_O_groups,
554 FILTER_GROUPS * sizeof(struct dentry *));
555 if (filter->fo_last_objids != NULL)
556 OBD_FREE(filter->fo_last_objids,
557 FILTER_GROUPS * sizeof(__u64));
558 if (filter->fo_last_objid_files != NULL)
559 OBD_FREE(filter->fo_last_objid_files,
560 FILTER_GROUPS * sizeof(struct file *));
564 /* FIXME: object groups */
565 static int filter_prep_groups(struct obd_device *obd)
567 struct filter_obd *filter = &obd->u.filter;
568 struct dentry *dentry, *O_dentry;
570 int i, rc = 0, cleanup_phase = 0;
573 O_dentry = simple_mkdir(current->fs->pwd, "O", 0700, 1);
574 CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
575 if (IS_ERR(O_dentry)) {
576 rc = PTR_ERR(O_dentry);
577 CERROR("cannot open/create O: rc = %d\n", rc);
580 filter->fo_dentry_O = O_dentry;
581 cleanup_phase = 1; /* O_dentry */
583 /* Lookup "R" to tell if we're on an old OST FS and need to convert
584 * from O/R/<dir>/<objid> to O/0/<dir>/<objid>. This can be removed
585 * some time post 1.0 when all old-style OSTs have converted along
586 * with the init_objid hack. */
587 dentry = ll_lookup_one_len("R", O_dentry, 1);
589 GOTO(cleanup, rc = PTR_ERR(dentry));
590 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
591 struct dentry *O0_dentry = lookup_one_len("0", O_dentry, 1);
594 CWARN("converting OST to new object layout\n");
595 if (IS_ERR(O0_dentry)) {
596 rc = PTR_ERR(O0_dentry);
597 CERROR("error looking up O/0: rc %d\n", rc);
601 if (O0_dentry->d_inode) {
602 CERROR("Both O/R and O/0 exist. Fix manually.\n");
603 GOTO(cleanup_O0, rc = -EEXIST);
606 down(&O_dentry->d_inode->i_sem);
607 rc = vfs_rename(O_dentry->d_inode, dentry,
608 O_dentry->d_inode, O0_dentry);
609 up(&O_dentry->d_inode->i_sem);
612 CERROR("error renaming O/R to O/0: rc %d\n", rc);
613 GOTO(cleanup_O0, rc);
615 filter->fo_fsd->fsd_feature_incompat |=
616 cpu_to_le32(FILTER_INCOMPAT_GROUPS);
617 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
619 GOTO(cleanup_O0, rc);
631 OBD_ALLOC(filter->fo_last_objids, FILTER_GROUPS * sizeof(__u64));
632 if (filter->fo_last_objids == NULL)
633 GOTO(cleanup, rc = -ENOMEM);
634 cleanup_phase = 2; /* groups */
636 OBD_ALLOC(filter->fo_dentry_O_groups, FILTER_GROUPS * sizeof(dentry));
637 if (filter->fo_dentry_O_groups == NULL)
638 GOTO(cleanup, rc = -ENOMEM);
639 OBD_ALLOC(filter->fo_last_objid_files, FILTER_GROUPS * sizeof(filp));
640 if (filter->fo_last_objid_files == NULL)
641 GOTO(cleanup, rc = -ENOMEM);
643 for (i = 0; i < FILTER_GROUPS; i++) {
647 sprintf(name, "%d", i);
648 dentry = simple_mkdir(O_dentry, name, 0700, 1);
649 CDEBUG(D_INODE, "got/created O/%s: %p\n", name, dentry);
650 if (IS_ERR(dentry)) {
651 rc = PTR_ERR(dentry);
652 CERROR("cannot lookup/create O/%s: rc = %d\n",
656 filter->fo_dentry_O_groups[i] = dentry;
658 sprintf(name, "O/%d/LAST_ID", i);
659 filp = filp_open(name, O_CREAT | O_RDWR, 0700);
660 if (IS_ERR(dentry)) {
661 rc = PTR_ERR(dentry);
662 CERROR("cannot create %s: rc = %d\n", name, rc);
665 filter->fo_last_objid_files[i] = filp;
667 if (filp->f_dentry->d_inode->i_size == 0) {
668 if (i == 0 && filter->fo_fsd->fsd_unused != 0) {
669 /* OST conversion, remove sometime post 1.0 */
670 filter->fo_last_objids[i] =
671 le64_to_cpu(filter->fo_fsd->fsd_unused);
672 CWARN("saving old objid "LPU64" to LAST_ID\n",
673 filter->fo_last_objids[i]);
674 rc = filter_update_last_objid(obd, 0, 1);
678 filter->fo_last_objids[i] = FILTER_INIT_OBJID;
683 rc = fsfilt_read_record(obd, filp, &filter->fo_last_objids[i],
684 sizeof(__u64), &off);
686 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
690 filter->fo_last_objids[i] =
691 le64_to_cpu(filter->fo_last_objids[i]);
692 CDEBUG(D_INODE, "%s: server last_objid group %d: "LPU64"\n",
693 obd->obd_name, i, filter->fo_last_objids[i]);
696 if (filter->fo_subdir_count) {
697 O_dentry = filter->fo_dentry_O_groups[0];
698 OBD_ALLOC(filter->fo_dentry_O_sub,
699 filter->fo_subdir_count * sizeof(dentry));
700 if (filter->fo_dentry_O_sub == NULL)
701 GOTO(cleanup, rc = -ENOMEM);
703 for (i = 0; i < filter->fo_subdir_count; i++) {
705 snprintf(dir, sizeof(dir), "d%u", i);
707 dentry = simple_mkdir(O_dentry, dir, 0700, 1);
708 CDEBUG(D_INODE, "got/created O/0/%s: %p\n", dir,dentry);
709 if (IS_ERR(dentry)) {
710 rc = PTR_ERR(dentry);
711 CERROR("can't lookup/create O/0/%s: rc = %d\n",
715 filter->fo_dentry_O_sub[i] = dentry;
721 switch (cleanup_phase) {
723 filter_cleanup_groups(obd);
725 f_dput(filter->fo_dentry_O);
726 filter->fo_dentry_O = NULL;
733 /* setup the object store with correct subdirectories */
734 static int filter_prep(struct obd_device *obd)
736 struct obd_run_ctxt saved;
737 struct filter_obd *filter = &obd->u.filter;
743 push_ctxt(&saved, &obd->obd_ctxt, NULL);
744 file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
745 if (!file || IS_ERR(file)) {
747 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
752 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
753 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
754 file->f_dentry->d_inode->i_mode);
755 GOTO(err_filp, rc = -ENOENT);
758 /* steal operations */
759 inode = file->f_dentry->d_inode;
760 filter->fo_fop = file->f_op;
761 filter->fo_iop = inode->i_op;
762 filter->fo_aops = inode->i_mapping->a_ops;
764 rc = filter_init_server_data(obd, file);
766 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
769 filter->fo_rcvd_filp = file;
771 rc = filter_prep_groups(obd);
773 GOTO(err_server_data, rc);
776 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
781 //class_disconnect_exports(obd, 0);
782 filter_free_server_data(filter);
784 if (filp_close(file, 0))
785 CERROR("can't close %s after error\n", LAST_RCVD);
786 filter->fo_rcvd_filp = NULL;
790 /* cleanup the filter: write last used object id to status file */
791 static void filter_post(struct obd_device *obd)
793 struct obd_run_ctxt saved;
794 struct filter_obd *filter = &obd->u.filter;
797 /* XXX: filter_update_lastobjid used to call fsync_dev. It might be
798 * best to start a transaction with h_sync, because we removed this
801 push_ctxt(&saved, &obd->obd_ctxt, NULL);
802 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
805 CERROR("error writing server data: rc = %d\n", rc);
807 for (i = 0; i < FILTER_GROUPS; i++) {
808 rc = filter_update_last_objid(obd, i, (i == FILTER_GROUPS - 1));
810 CERROR("error writing group %d lastobjid: rc = %d\n",
814 filp_close(filter->fo_rcvd_filp, 0);
815 filter->fo_rcvd_filp = NULL;
817 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
819 filter_cleanup_groups(obd);
820 f_dput(filter->fo_dentry_O);
821 filter_free_server_data(filter);
822 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
825 static void filter_set_last_id(struct filter_obd *filter, struct obdo *oa,
829 LASSERT(filter->fo_fsd != NULL);
832 LASSERT(oa->o_gr <= FILTER_GROUPS);
836 spin_lock(&filter->fo_objidlock);
837 filter->fo_last_objids[group] = id;
838 spin_unlock(&filter->fo_objidlock);
841 __u64 filter_last_id(struct filter_obd *filter, struct obdo *oa)
845 LASSERT(filter->fo_fsd != NULL);
848 LASSERT(oa->o_gr <= FILTER_GROUPS);
852 /* FIXME: object groups */
853 spin_lock(&filter->fo_objidlock);
854 id = filter->fo_last_objids[group];
855 spin_unlock(&filter->fo_objidlock);
860 /* direct cut-n-paste of mds_blocking_ast() */
861 static int filter_blocking_ast(struct ldlm_lock *lock,
862 struct ldlm_lock_desc *desc,
863 void *data, int flag)
868 if (flag == LDLM_CB_CANCELING) {
869 /* Don't need to do anything here. */
873 /* XXX layering violation! -phil */
874 l_lock(&lock->l_resource->lr_namespace->ns_lock);
875 /* Get this: if filter_blocking_ast is racing with ldlm_intent_policy,
876 * such that filter_blocking_ast is called just before l_i_p takes the
877 * ns_lock, then by the time we get the lock, we might not be the
878 * correct blocking function anymore. So check, and return early, if
880 if (lock->l_blocking_ast != filter_blocking_ast) {
881 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
885 lock->l_flags |= LDLM_FL_CBPENDING;
886 do_ast = (!lock->l_readers && !lock->l_writers);
887 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
890 struct lustre_handle lockh;
893 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
894 ldlm_lock2handle(lock, &lockh);
895 rc = ldlm_cli_cancel(&lockh);
897 CERROR("ldlm_cli_cancel: %d\n", rc);
899 LDLM_DEBUG(lock, "Lock still has references, will be "
905 static int filter_lock_dentry(struct obd_device *obd, struct dentry *dparent)
907 down(&dparent->d_inode->i_sem);
911 /* We never dget the object parent, so DON'T dput it either */
912 static void filter_parent_unlock(struct dentry *dparent)
914 up(&dparent->d_inode->i_sem);
917 /* We never dget the object parent, so DON'T dput it either */
918 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
920 struct filter_obd *filter = &obd->u.filter;
921 LASSERT(group < FILTER_GROUPS); /* FIXME: object groups */
923 if (group > 0 || filter->fo_subdir_count == 0)
924 return filter->fo_dentry_O_groups[group];
926 return filter->fo_dentry_O_sub[objid & (filter->fo_subdir_count - 1)];
929 /* We never dget the object parent, so DON'T dput it either */
930 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
933 unsigned long now = jiffies;
934 struct dentry *dparent = filter_parent(obd, group, objid);
940 rc = filter_lock_dentry(obd, dparent);
941 if (time_after(jiffies, now + 15 * HZ))
942 CERROR("slow parent lock %lus\n", (jiffies - now) / HZ);
943 return rc ? ERR_PTR(rc) : dparent;
946 /* How to get files, dentries, inodes from object id's.
948 * If dir_dentry is passed, the caller has already locked the parent
949 * appropriately for this operation (normally a write lock). If
950 * dir_dentry is NULL, we do a read lock while we do the lookup to
951 * avoid races with create/destroy and such changing the directory
952 * internal to the filesystem code. */
953 struct dentry *filter_fid2dentry(struct obd_device *obd,
954 struct dentry *dir_dentry,
955 obd_gr group, obd_id id)
957 struct dentry *dparent = dir_dentry;
958 struct dentry *dchild;
964 CERROR("fatal: invalid object id 0\n");
965 RETURN(ERR_PTR(-ESTALE));
968 len = sprintf(name, LPU64, id);
969 if (dir_dentry == NULL) {
970 dparent = filter_parent_lock(obd, group, id);
974 CDEBUG(D_INODE, "looking up object O/%*s/%s\n",
975 dparent->d_name.len, dparent->d_name.name, name);
976 dchild = /*ll_*/lookup_one_len(name, dparent, len);
977 if (dir_dentry == NULL)
978 filter_parent_unlock(dparent);
979 if (IS_ERR(dchild)) {
980 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
984 CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
985 name, dchild, atomic_read(&dchild->d_count));
987 LASSERT(atomic_read(&dchild->d_count) > 0);
992 static int filter_prepare_destroy(struct obd_device *obd, obd_id objid)
994 struct lustre_handle lockh;
995 int flags = LDLM_AST_DISCARD_DATA, rc;
996 struct ldlm_res_id res_id = { .name = { objid } };
997 ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
1000 /* Tell the clients that the object is gone now and that they should
1001 * throw away any cached pages. */
1002 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
1003 LDLM_EXTENT, &policy, LCK_PW,
1004 &flags, filter_blocking_ast, ldlm_completion_ast,
1005 NULL, NULL, NULL, 0, NULL, &lockh);
1007 /* We only care about the side-effects, just drop the lock. */
1009 ldlm_lock_decref(&lockh, LCK_PW);
1014 /* Caller must hold LCK_PW on parent and push us into kernel context.
1015 * Caller is also required to ensure that dchild->d_inode exists. */
1016 static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
1017 struct dentry *dparent,
1018 struct dentry *dchild)
1020 struct inode *inode = dchild->d_inode;
1024 if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
1025 CERROR("destroying objid %*s nlink = %lu, count = %d\n",
1026 dchild->d_name.len, dchild->d_name.name,
1027 (unsigned long)inode->i_nlink,
1028 atomic_read(&inode->i_count));
1031 rc = vfs_unlink(dparent->d_inode, dchild);
1034 CERROR("error unlinking objid %*s: rc %d\n",
1035 dchild->d_name.len, dchild->d_name.name, rc);
1040 static int filter_intent_policy(struct ldlm_namespace *ns,
1041 struct ldlm_lock **lockp, void *req_cookie,
1042 ldlm_mode_t mode, int flags, void *data)
1044 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1045 struct ptlrpc_request *req = req_cookie;
1046 struct ldlm_lock *lock = *lockp, *l = NULL;
1047 struct ldlm_resource *res = lock->l_resource;
1048 ldlm_processing_policy policy;
1049 struct ost_lvb *res_lvb, *reply_lvb;
1050 struct list_head *tmp;
1052 int tmpflags = 0, rc, repsize[2] = {sizeof(struct ldlm_reply),
1053 sizeof(struct ost_lvb) };
1056 policy = ldlm_get_processing_policy(res);
1057 LASSERT(policy != NULL);
1058 LASSERT(req != NULL);
1060 rc = lustre_pack_reply(req, 2, repsize, NULL);
1062 RETURN(req->rq_status = rc);
1064 reply_lvb = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*reply_lvb));
1065 LASSERT(reply_lvb != NULL);
1067 //fixup_handle_for_resent_req(req, lock, &lockh);
1069 /* If we grant any lock at all, it will be a whole-file read lock.
1070 * Call the extent policy function to see if our request can be
1071 * granted, or is blocked. */
1072 lock->l_policy_data.l_extent.start = 0;
1073 lock->l_policy_data.l_extent.end = OBD_OBJECT_EOF;
1074 lock->l_req_mode = LCK_PR;
1076 l_lock(&res->lr_namespace->ns_lock);
1078 res->lr_tmp = &rpc_list;
1079 rc = policy(lock, &tmpflags, 0, &err);
1082 /* FIXME: we should change the policy function slightly, to not make
1083 * this list at all, since we just turn around and free it */
1084 while (!list_empty(&rpc_list)) {
1085 struct ldlm_ast_work *w =
1086 list_entry(rpc_list.next, struct ldlm_ast_work, w_list);
1087 list_del(&w->w_list);
1088 LDLM_LOCK_PUT(w->w_lock);
1089 OBD_FREE(w, sizeof(*w));
1092 if (rc == LDLM_ITER_CONTINUE) {
1093 /* The lock met with no resistance; we're finished. */
1094 l_unlock(&res->lr_namespace->ns_lock);
1095 RETURN(ELDLM_LOCK_REPLACED);
1098 /* Do not grant any lock, but instead send GL callbacks. The extent
1099 * policy nicely created a list of all PW locks for us. We will choose
1100 * the highest of those which are larger than the size in the LVB, if
1101 * any, and perform a glimpse callback. */
1102 down(&res->lr_lvb_sem);
1103 res_lvb = res->lr_lvb_data;
1104 LASSERT(res_lvb != NULL);
1105 reply_lvb->lvb_size = res_lvb->lvb_size;
1106 up(&res->lr_lvb_sem);
1108 list_for_each(tmp, &res->lr_granted) {
1109 struct ldlm_lock *tmplock =
1110 list_entry(tmp, struct ldlm_lock, l_res_link);
1112 if (tmplock->l_granted_mode == LCK_PR)
1115 if (tmplock->l_policy_data.l_extent.end <=
1116 reply_lvb->lvb_size)
1120 l = LDLM_LOCK_GET(tmplock);
1124 if (l->l_policy_data.l_extent.start >
1125 tmplock->l_policy_data.l_extent.start)
1129 l = LDLM_LOCK_GET(tmplock);
1131 l_unlock(&res->lr_namespace->ns_lock);
1133 /* There were no PW locks beyond the size in the LVB; finished. */
1135 RETURN(ELDLM_LOCK_ABORTED);
1137 LASSERT(l->l_glimpse_ast != NULL);
1138 rc = l->l_glimpse_ast(l, NULL); /* this will update the LVB */
1140 down(&res->lr_lvb_sem);
1142 if (res_lvb->lvb_size == reply_lvb->lvb_size)
1143 LDLM_ERROR(l, "we lost the glimpse race!");
1145 reply_lvb->lvb_size = res_lvb->lvb_size;
1146 up(&res->lr_lvb_sem);
1150 RETURN(ELDLM_LOCK_ABORTED);
1153 /* mount the file system (secretly) */
1154 int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
1157 struct lustre_cfg* lcfg = buf;
1158 struct filter_obd *filter = &obd->u.filter;
1159 struct vfsmount *mnt;
1163 dev_clear_rdonly(2);
1165 if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
1168 obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
1169 if (IS_ERR(obd->obd_fsops))
1170 RETURN(PTR_ERR(obd->obd_fsops));
1172 mnt = do_kern_mount(lcfg->lcfg_inlbuf2, MS_NOATIME | MS_NODIRATIME,
1173 lcfg->lcfg_inlbuf1, option);
1178 if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3) {
1179 if (*lcfg->lcfg_inlbuf3 == 'f') {
1180 obd->obd_replayable = 1;
1181 obd_sync_filter = 1;
1182 CWARN("%s: recovery enabled\n", obd->obd_name);
1184 if (*lcfg->lcfg_inlbuf3 != 'n') {
1185 CERROR("unrecognised flag '%c'\n",
1186 *lcfg->lcfg_inlbuf3);
1188 // XXX Robert? Why do we get errors here
1189 // GOTO(err_mntput, rc = -EINVAL);
1193 filter->fo_vfsmnt = mnt;
1194 filter->fo_sb = mnt->mnt_sb;
1195 filter->fo_fstype = mnt->mnt_sb->s_type->name;
1196 CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
1198 OBD_SET_CTXT_MAGIC(&obd->obd_ctxt);
1199 obd->obd_ctxt.pwdmnt = mnt;
1200 obd->obd_ctxt.pwd = mnt->mnt_root;
1201 obd->obd_ctxt.fs = get_ds();
1202 obd->obd_ctxt.cb_ops = filter_lvfs_ops;
1204 rc = filter_prep(obd);
1206 GOTO(err_mntput, rc);
1208 spin_lock_init(&filter->fo_translock);
1209 spin_lock_init(&filter->fo_objidlock);
1210 INIT_LIST_HEAD(&filter->fo_export_list);
1211 sema_init(&filter->fo_alloc_lock, 1);
1212 spin_lock_init(&filter->fo_r_pages.oh_lock);
1213 spin_lock_init(&filter->fo_w_pages.oh_lock);
1214 spin_lock_init(&filter->fo_r_discont_pages.oh_lock);
1215 spin_lock_init(&filter->fo_w_discont_pages.oh_lock);
1216 spin_lock_init(&filter->fo_r_discont_blocks.oh_lock);
1217 spin_lock_init(&filter->fo_w_discont_blocks.oh_lock);
1218 filter->fo_readcache_max_filesize = FILTER_MAX_CACHE_SIZE;
1220 obd->obd_namespace = ldlm_namespace_new("filter-tgt",
1221 LDLM_NAMESPACE_SERVER);
1222 if (obd->obd_namespace == NULL)
1223 GOTO(err_post, rc = -ENOMEM);
1224 obd->obd_namespace->ns_lvbp = obd;
1225 obd->obd_namespace->ns_lvbo = &filter_lvbo;
1226 ldlm_register_intent(obd->obd_namespace, filter_intent_policy);
1228 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1229 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1231 rc = llog_cat_initialize(obd, 1);
1233 CERROR("failed to setup llogging subsystems\n");
1247 fsfilt_put_ops(obd->obd_fsops);
1251 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
1253 struct lustre_cfg* lcfg = buf;
1254 const char *str = NULL;
1255 char *option = NULL;
1259 if (!strcmp(lcfg->lcfg_inlbuf2, "ext3")) {
1260 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1261 /* bug 1577: implement async-delete for 2.5 */
1262 str = "errors=remount-ro,asyncdel";
1264 str = "errors=remount-ro";
1266 n = strlen(str) + 1;
1267 OBD_ALLOC(option, n);
1270 strcpy(option, str);
1273 rc = filter_common_setup(obd, len, buf, option);
1275 OBD_FREE(option, n);
1279 static int filter_cleanup(struct obd_device *obd, int flags)
1281 struct filter_obd *filter = &obd->u.filter;
1284 if (flags & OBD_OPT_FAILOVER)
1285 CERROR("%s: shutting down for failover; client state will"
1286 " be preserved.\n", obd->obd_name);
1288 if (!list_empty(&obd->obd_exports)) {
1289 CERROR("%s: still has clients!\n", obd->obd_name);
1290 class_disconnect_exports(obd, flags);
1291 if (!list_empty(&obd->obd_exports)) {
1292 CERROR("still has exports after forced cleanup?\n");
1297 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
1299 if (filter->fo_sb == NULL)
1304 shrink_dcache_parent(filter->fo_sb->s_root);
1307 if (atomic_read(&filter->fo_vfsmnt->mnt_count) > 1)
1308 CERROR("%s: mount point %p busy, mnt_count: %d\n",
1309 obd->obd_name, filter->fo_vfsmnt,
1310 atomic_read(&filter->fo_vfsmnt->mnt_count));
1313 mntput(filter->fo_vfsmnt);
1314 //destroy_buffers(filter->fo_sb->s_dev);
1315 filter->fo_sb = NULL;
1316 fsfilt_put_ops(obd->obd_fsops);
1319 dev_clear_rdonly(2);
1324 static int filter_attach(struct obd_device *obd, obd_count len, void *data)
1326 struct lprocfs_static_vars lvars;
1329 lprocfs_init_vars(filter, &lvars);
1330 rc = lprocfs_obd_attach(obd, lvars.obd_vars);
1334 rc = lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST);
1338 /* Init obdfilter private stats here */
1339 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
1340 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
1341 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
1342 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
1344 return lproc_filter_attach_seqstat(obd);
1347 static int filter_detach(struct obd_device *dev)
1349 lprocfs_free_obd_stats(dev);
1350 return lprocfs_obd_detach(dev);
1353 /* nearly identical to mds_connect */
1354 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
1355 struct obd_uuid *cluuid)
1357 struct obd_export *exp;
1358 struct filter_export_data *fed;
1359 struct filter_client_data *fcd = NULL;
1360 struct filter_obd *filter = &obd->u.filter;
1364 if (conn == NULL || obd == NULL || cluuid == NULL)
1367 rc = class_connect(conn, obd, cluuid);
1370 exp = class_conn2export(conn);
1371 LASSERT(exp != NULL);
1373 fed = &exp->exp_filter_data;
1375 spin_lock_init(&fed->fed_lock);
1377 if (!obd->obd_replayable)
1378 GOTO(cleanup, rc = 0);
1380 OBD_ALLOC(fcd, sizeof(*fcd));
1382 CERROR("filter: out of memory for client data\n");
1383 GOTO(cleanup, rc = -ENOMEM);
1386 memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
1389 rc = filter_client_add(obd, filter, fed, -1);
1394 OBD_FREE(fcd, sizeof(*fcd));
1395 class_disconnect(exp, 0);
1397 class_export_put(exp);
1402 static int filter_precleanup(struct obd_device *obd, int flags)
1407 rc = obd_llog_finish(obd, 0);
1409 CERROR("failed to cleanup llogging subsystem\n");
1414 /* Do extra sanity checks for grant accounting. We do this at connect,
1415 * disconnect, and statfs RPC time, so it shouldn't be too bad. We can
1416 * always get rid of it or turn it off when we know accounting is good. */
1417 static void filter_grant_sanity_check(struct obd_device *obd, char *func)
1419 struct filter_export_data *fed;
1420 struct obd_export *exp;
1421 obd_size maxsize = obd->obd_osfs.os_blocks * obd->obd_osfs.os_bsize;
1422 obd_size tot_dirty = 0, tot_pending = 0, tot_granted = 0;
1423 obd_size fo_tot_dirty, fo_tot_pending, fo_tot_granted;
1425 if (list_empty(&obd->obd_exports))
1428 spin_lock(&obd->obd_osfs_lock);
1429 spin_lock(&obd->obd_dev_lock);
1430 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1431 fed = &exp->exp_filter_data;
1432 LASSERTF(fed->fed_grant + fed->fed_pending <= maxsize,
1433 "cli %s/%p %lu+%lu > "LPU64"\n",
1434 exp->exp_client_uuid.uuid, exp,
1435 fed->fed_grant, fed->fed_pending, maxsize);
1436 LASSERTF(fed->fed_dirty <= maxsize, "cli %s/%p %lu > "LPU64"\n",
1437 exp->exp_client_uuid.uuid, exp,fed->fed_dirty,maxsize);
1438 CDEBUG(D_CACHE,"%s: cli %s/%p dirty %lu pend %lu grant %lu\n",
1439 obd->obd_name, exp->exp_client_uuid.uuid, exp,
1440 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1441 tot_granted += fed->fed_grant + fed->fed_pending;
1442 tot_pending += fed->fed_pending;
1443 tot_dirty += fed->fed_dirty;
1445 fo_tot_granted = obd->u.filter.fo_tot_granted;
1446 fo_tot_pending = obd->u.filter.fo_tot_pending;
1447 fo_tot_dirty = obd->u.filter.fo_tot_dirty;
1448 spin_unlock(&obd->obd_dev_lock);
1449 spin_unlock(&obd->obd_osfs_lock);
1451 /* Do these assertions outside the spinlocks so we don't kill system */
1452 LASSERTF(tot_granted == fo_tot_granted, "%s "LPU64" != "LPU64"\n",
1453 func, tot_granted, fo_tot_granted);
1454 LASSERTF(tot_pending == fo_tot_pending, "%s "LPU64" != "LPU64"\n",
1455 func, tot_pending, fo_tot_pending);
1456 LASSERTF(tot_dirty == fo_tot_dirty, "%s "LPU64" != "LPU64"\n",
1457 func, tot_dirty, fo_tot_dirty);
1458 LASSERTF(tot_pending <= tot_granted, "%s "LPU64" > "LPU64"\n",
1459 func, tot_pending, tot_granted);
1460 LASSERTF(tot_granted <= maxsize, "%s "LPU64" > "LPU64"\n",
1461 func, tot_granted, maxsize);
1462 LASSERTF(tot_dirty <= maxsize, "%s "LPU64" > "LPU64"\n",
1463 func, tot_dirty, maxsize);
1466 /* Remove this client from the grant accounting totals. This is done at
1467 * disconnect time and also at export destroy time in case there was a race
1468 * between removing the export and an incoming BRW updating the client grant.
1469 * The client should do something similar when it invalidates its import. */
1470 static void filter_grant_discard(struct obd_export *exp)
1472 struct obd_device *obd = exp->exp_obd;
1473 struct filter_obd *filter = &obd->u.filter;
1474 struct filter_export_data *fed = &exp->exp_filter_data;
1476 spin_lock(&obd->obd_osfs_lock);
1477 CDEBUG(D_CACHE, "%s: cli %s/%p dirty %lu pend %lu grant %lu\n",
1478 obd->obd_name, exp->exp_client_uuid.uuid, exp,
1479 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1481 LASSERTF(filter->fo_tot_granted >= fed->fed_grant,
1482 "%s: tot_granted "LPU64" cli %s/%p fed_grant %lu\n",
1483 obd->obd_name, filter->fo_tot_granted,
1484 exp->exp_client_uuid.uuid, exp, fed->fed_grant);
1485 filter->fo_tot_granted -= fed->fed_grant;
1486 LASSERTF(exp->exp_obd->u.filter.fo_tot_pending >= fed->fed_pending,
1487 "%s: tot_pending "LPU64" cli %s/%p fed_pending %lu\n",
1488 obd->obd_name, filter->fo_tot_pending,
1489 exp->exp_client_uuid.uuid, exp, fed->fed_pending);
1490 LASSERTF(filter->fo_tot_dirty >= fed->fed_dirty,
1491 "%s: tot_dirty "LPU64" cli %s/%p fed_dirty %lu\n",
1492 obd->obd_name, filter->fo_tot_dirty,
1493 exp->exp_client_uuid.uuid, exp, fed->fed_dirty);
1494 filter->fo_tot_dirty -= fed->fed_dirty;
1498 spin_unlock(&obd->obd_osfs_lock);
1501 static int filter_destroy_export(struct obd_export *exp)
1505 if (exp->exp_filter_data.fed_pending)
1506 CERROR("%s: cli %s/%p has %lu pending on destroyed export\n",
1507 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
1508 exp, exp->exp_filter_data.fed_pending);
1510 target_destroy_export(exp);
1512 if (exp->exp_obd->obd_replayable)
1513 filter_client_free(exp, exp->exp_flags);
1515 filter_grant_sanity_check(exp->exp_obd, __FUNCTION__);
1520 /* also incredibly similar to mds_disconnect */
1521 static int filter_disconnect(struct obd_export *exp, int flags)
1523 struct obd_device *obd = exp->exp_obd;
1524 unsigned long irqflags;
1525 struct llog_ctxt *ctxt;
1530 class_export_get(exp);
1532 spin_lock_irqsave(&exp->exp_lock, irqflags);
1533 exp->exp_flags = flags;
1534 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
1536 filter_grant_discard(exp);
1538 /* Disconnect early so that clients can't keep using export */
1539 rc = class_disconnect(exp, flags);
1541 /* Do this twice in case a BRW arrived between the first call and
1542 * the class_export_unlink() call (bug 2663) */
1543 filter_grant_discard(exp);
1545 ldlm_cancel_locks_for_export(exp);
1547 fsfilt_sync(obd, obd->u.filter.fo_sb);
1549 /* flush any remaining cancel messages out to the target */
1550 ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
1551 llog_sync(ctxt, exp);
1553 class_export_put(exp);
1557 struct dentry *__filter_oa2dentry(struct obd_device *obd,
1558 struct obdo *oa, const char *what)
1560 struct dentry *dchild = NULL;
1563 if (oa->o_valid & OBD_MD_FLGROUP)
1566 dchild = filter_fid2dentry(obd, NULL, group, oa->o_id);
1568 if (IS_ERR(dchild)) {
1569 CERROR("%s error looking up object: "LPU64"\n", what, oa->o_id);
1573 if (dchild->d_inode == NULL) {
1574 CERROR("%s on non-existent object: "LPU64"\n", what, oa->o_id);
1576 RETURN(ERR_PTR(-ENOENT));
1582 static int filter_getattr(struct obd_export *exp, struct obdo *oa,
1583 struct lov_stripe_md *md)
1585 struct dentry *dentry = NULL;
1586 struct obd_device *obd;
1590 obd = class_exp2obd(exp);
1592 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1593 exp->exp_handle.h_cookie);
1597 dentry = filter_oa2dentry(obd, oa);
1599 RETURN(PTR_ERR(dentry));
1601 /* Limit the valid bits in the return data to what we actually use */
1602 oa->o_valid = OBD_MD_FLID;
1603 obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1609 /* this is called from filter_truncate() until we have filter_punch() */
1610 static int filter_setattr(struct obd_export *exp, struct obdo *oa,
1611 struct lov_stripe_md *md, struct obd_trans_info *oti)
1613 struct obd_run_ctxt saved;
1614 struct filter_obd *filter;
1615 struct dentry *dentry;
1617 struct ldlm_res_id res_id = { .name = { oa->o_id } };
1618 struct ldlm_resource *res;
1623 LASSERT(oti != NULL);
1625 dentry = filter_oa2dentry(exp->exp_obd, oa);
1627 RETURN(PTR_ERR(dentry));
1629 filter = &exp->exp_obd->u.filter;
1631 iattr_from_obdo(&iattr, oa, oa->o_valid);
1633 push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1636 if (iattr.ia_valid & ATTR_SIZE)
1637 down(&dentry->d_inode->i_sem);
1638 handle = fsfilt_start(exp->exp_obd, dentry->d_inode, FSFILT_OP_SETATTR,
1641 GOTO(out_unlock, rc = PTR_ERR(handle));
1643 /* XXX this could be a rwsem instead, if filter_preprw played along */
1644 if (iattr.ia_valid & ATTR_ATTR_FLAG)
1645 rc = fsfilt_iocontrol(exp->exp_obd, dentry->d_inode, NULL,
1647 (long)&iattr.ia_attr_flags);
1649 rc = fsfilt_setattr(exp->exp_obd, dentry, handle, &iattr, 1);
1650 rc = filter_finish_transno(exp, oti, rc);
1651 rc2 = fsfilt_commit(exp->exp_obd, dentry->d_inode, handle, 0);
1653 CERROR("error on commit, err = %d\n", rc2);
1658 if (iattr.ia_valid & ATTR_SIZE) {
1659 res = ldlm_resource_get(exp->exp_obd->obd_namespace, NULL,
1660 res_id, LDLM_EXTENT, 0);
1662 CERROR("!!! resource_get failed for object "LPU64" -- "
1663 "filter_setattr with no lock?\n", oa->o_id);
1665 if (res->lr_namespace->ns_lvbo &&
1666 res->lr_namespace->ns_lvbo->lvbo_update) {
1667 rc = res->lr_namespace->ns_lvbo->lvbo_update
1670 ldlm_resource_putref(res);
1674 oa->o_valid = OBD_MD_FLID;
1675 obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1678 if (iattr.ia_valid & ATTR_SIZE)
1679 up(&dentry->d_inode->i_sem);
1681 pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1687 /* XXX identical to osc_unpackmd */
1688 static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
1689 struct lov_mds_md *lmm, int lmm_bytes)
1695 if (lmm_bytes < sizeof (*lmm)) {
1696 CERROR("lov_mds_md too small: %d, need %d\n",
1697 lmm_bytes, (int)sizeof(*lmm));
1700 /* XXX LOV_MAGIC etc check? */
1702 if (lmm->lmm_object_id == cpu_to_le64(0)) {
1703 CERROR("lov_mds_md: zero lmm_object_id\n");
1708 lsm_size = lov_stripe_md_size(1);
1712 if (*lsmp != NULL && lmm == NULL) {
1713 OBD_FREE(*lsmp, lsm_size);
1718 if (*lsmp == NULL) {
1719 OBD_ALLOC(*lsmp, lsm_size);
1723 loi_init((*lsmp)->lsm_oinfo);
1727 /* XXX zero *lsmp? */
1728 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
1729 LASSERT((*lsmp)->lsm_object_id);
1732 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
1737 static void filter_destroy_precreated(struct obd_export *exp, struct obdo *oa,
1738 struct filter_obd *filter)
1740 struct obdo doa; /* XXX obdo on stack */
1745 memset(&doa, 0, sizeof(doa));
1746 if (oa->o_valid & OBD_MD_FLGROUP)
1747 doa.o_gr = oa->o_gr;
1750 doa.o_mode = S_IFREG;
1751 last = filter_last_id(filter, &doa); /* FIXME: object groups */
1752 CWARN("deleting orphan objects from "LPU64" to "LPU64"\n",
1753 oa->o_id + 1, last);
1754 for (id = oa->o_id + 1; id <= last; id++) {
1756 filter_destroy(exp, &doa, NULL, NULL);
1758 spin_lock(&filter->fo_objidlock);
1759 filter->fo_last_objids[0] = oa->o_id; /* FIXME: object groups */
1760 spin_unlock(&filter->fo_objidlock);
1764 /* returns a negative error or a nonnegative number of files to create */
1765 static int filter_should_precreate(struct obd_export *exp, struct obdo *oa,
1768 struct obd_device *obd = exp->exp_obd;
1769 struct filter_obd *filter = &obd->u.filter;
1773 diff = oa->o_id - filter_last_id(filter, oa);
1774 CDEBUG(D_INFO, "filter_last_id() = "LPU64" -> diff = %d\n",
1775 filter_last_id(filter, oa), diff);
1777 /* delete orphans request */
1778 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1779 (oa->o_flags & OBD_FL_DELORPHAN)) {
1782 if (-diff > 10000) { /* XXX make this smarter */
1783 CERROR("ignoring bogus orphan destroy request: obdid "
1784 LPU64" last_id "LPU64"\n",
1785 oa->o_id, filter_last_id(filter, oa));
1788 filter_destroy_precreated(exp, oa, filter);
1789 rc = filter_update_last_objid(obd, group, 0);
1791 CERROR("unable to write lastobjid, but orphans"
1795 /* only precreate if group == 0 and o_id is specfied */
1796 if (!(oa->o_valid & OBD_FL_DELORPHAN) &&
1797 (group != 0 || oa->o_id == 0))
1805 /* We rely on the fact that only one thread will be creating files in a given
1806 * group at a time, which is why we don't need an atomic filter_get_new_id.
1807 * Even if we had that atomic function, the following race would exist:
1809 * thread 1: gets id x from filter_next_id
1810 * thread 2: gets id (x + 1) from filter_next_id
1811 * thread 2: creates object (x + 1)
1812 * thread 1: tries to create object x, gets -ENOSPC
1814 static int filter_precreate(struct obd_device *obd, struct obdo *oa,
1815 obd_gr group, int *num)
1817 struct dentry *dchild = NULL;
1818 struct filter_obd *filter;
1819 struct dentry *dparent;
1820 int err = 0, rc = 0, i;
1822 int recreate_obj = 0;
1823 void *handle = NULL;
1826 filter = &obd->u.filter;
1828 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1829 (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1833 for (i = 0; i < *num && err == 0; i++) {
1834 int cleanup_phase = 0;
1839 last_id = filter_last_id(filter, NULL);
1840 if (next_id > last_id) {
1841 CERROR("Error: Trying to recreate obj greater"
1842 "than last id "LPD64" > "LPD64"\n",
1847 next_id = filter_last_id(filter, NULL) + 1;
1849 CDEBUG(D_INFO, "precreate objid "LPU64"\n", next_id);
1851 dparent = filter_parent_lock(obd, group, next_id);
1852 if (IS_ERR(dparent))
1853 GOTO(cleanup, rc = PTR_ERR(dparent));
1856 dchild = filter_fid2dentry(obd, dparent, group, next_id);
1858 GOTO(cleanup, rc = PTR_ERR(dchild));
1861 if (dchild->d_inode != NULL) {
1862 /* This would only happen if lastobjid was bad on disk*/
1863 /* Could also happen if recreating missing obj but
1867 CERROR("Serious error: recreating obj %*s but "
1868 "obj already exists \n",
1869 dchild->d_name.len, dchild->d_name.name);
1871 CERROR("Serious error: objid %*s already "
1872 "exists; is this filesystem corrupt?\n",
1873 dchild->d_name.len, dchild->d_name.name);
1875 GOTO(cleanup, rc = -EEXIST);
1878 handle = fsfilt_start(obd, dparent->d_inode,
1879 FSFILT_OP_CREATE_LOG, NULL);
1881 GOTO(cleanup, rc = PTR_ERR(handle));
1884 rc = ll_vfs_create(dparent->d_inode, dchild, S_IFREG, NULL);
1886 CERROR("create failed rc = %d\n", rc);
1890 if (!recreate_obj) {
1891 filter_set_last_id(filter, NULL, next_id);
1892 err = filter_update_last_objid(obd, group, 0);
1894 CERROR("unable to write lastobjid "
1895 "but file created\n");
1899 switch(cleanup_phase) {
1901 err = fsfilt_commit(obd, dparent->d_inode, handle, 0);
1903 CERROR("error on commit, err = %d\n", err);
1910 filter_parent_unlock(dparent);
1920 CDEBUG(D_INFO, "filter_precreate() created %d objects\n", i);
1924 static int filter_create(struct obd_export *exp, struct obdo *oa,
1925 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1927 struct obd_device *obd = NULL;
1928 struct obd_run_ctxt saved;
1929 struct lov_stripe_md *lsm = NULL;
1934 if (oa->o_valid & OBD_MD_FLGROUP)
1937 CDEBUG(D_INFO, "filter_create(od->o_gr="LPU64",od->o_id="LPU64")\n",
1942 rc = obd_alloc_memmd(exp, &lsm);
1949 push_ctxt(&saved, &obd->obd_ctxt, NULL);
1951 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1952 (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1953 if (oa->o_id > filter_last_id(&obd->u.filter, oa)) {
1954 CERROR("recreate objid "LPU64" > last id "LPU64"\n",
1955 oa->o_id, filter_last_id(&obd->u.filter, oa));
1959 rc = filter_precreate(obd, oa, group, &diff);
1962 diff = filter_should_precreate(exp, oa, group);
1964 oa->o_id = filter_last_id(&obd->u.filter, oa);
1965 rc = filter_precreate(obd, oa, group, &diff);
1967 oa->o_valid = OBD_MD_FLID;
1971 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1972 if (rc && ea != NULL && *ea != lsm) {
1973 obd_free_memmd(exp, &lsm);
1974 } else if (rc == 0 && ea != NULL) {
1975 /* XXX LOV STACKING: the lsm that is passed to us from
1976 * LOV does not have valid lsm_oinfo data structs, so
1977 * don't go touching that. This needs to be fixed in a
1979 lsm->lsm_object_id = oa->o_id;
1986 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
1987 struct lov_stripe_md *ea, struct obd_trans_info *oti)
1989 struct obd_device *obd;
1990 struct filter_obd *filter;
1991 struct dentry *dchild = NULL, *dparent = NULL;
1992 struct obd_run_ctxt saved;
1993 void *handle = NULL;
1994 struct llog_cookie *fcc = NULL;
1995 int rc, rc2, cleanup_phase = 0, have_prepared = 0;
1999 if (oa->o_valid & OBD_MD_FLGROUP)
2003 filter = &obd->u.filter;
2005 push_ctxt(&saved, &obd->obd_ctxt, NULL);
2008 dparent = filter_parent_lock(obd, group, oa->o_id);
2009 if (IS_ERR(dparent))
2010 GOTO(cleanup, rc = PTR_ERR(dparent));
2013 dchild = filter_fid2dentry(obd, dparent, group, oa->o_id);
2015 GOTO(cleanup, rc = -ENOENT);
2018 if (dchild->d_inode == NULL) {
2019 CDEBUG(D_INODE, "destroying non-existent object "LPU64"\n",
2021 GOTO(cleanup, rc = -ENOENT);
2024 if (!have_prepared) {
2025 /* If we're really going to destroy the object, get ready
2026 * by getting the clients to discard their cached data.
2028 * We have to drop the parent lock, because
2029 * filter_prepare_destroy will acquire a PW on the object, and
2030 * we don't want to deadlock with an incoming write to the
2031 * object, which has the extent PW and then wants to get the
2032 * parent dentry to do the lookup.
2034 * We dput the child because it's not worth the extra
2035 * complication of condition the above code to skip it on the
2036 * second time through. */
2038 filter_parent_unlock(dparent);
2040 filter_prepare_destroy(obd, oa->o_id);
2045 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK_LOG, oti);
2047 GOTO(cleanup, rc = PTR_ERR(handle));
2050 /* Our MDC connection is established by the MDS to us */
2051 if (oa->o_valid & OBD_MD_FLCOOKIE) {
2052 OBD_ALLOC(fcc, sizeof(*fcc));
2054 memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
2057 rc = filter_destroy_internal(obd, oa->o_id, dparent, dchild);
2060 switch(cleanup_phase) {
2064 fsfilt_add_journal_cb(obd, 0, oti->oti_handle,
2065 filter_cancel_cookies_cb,
2068 fsfilt_add_journal_cb(obd, 0, handle,
2069 filter_cancel_cookies_cb,
2072 rc = filter_finish_transno(exp, oti, rc);
2073 rc2 = fsfilt_commit(obd, dparent->d_inode, handle, 0);
2075 CERROR("error on commit, err = %d\n", rc2);
2082 filter_parent_unlock(dparent);
2084 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
2087 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2094 /* NB start and end are used for punch, but not truncate */
2095 static int filter_truncate(struct obd_export *exp, struct obdo *oa,
2096 struct lov_stripe_md *lsm,
2097 obd_off start, obd_off end,
2098 struct obd_trans_info *oti)
2103 if (end != OBD_OBJECT_EOF)
2104 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
2107 CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = %x, "
2108 "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
2110 error = filter_setattr(exp, oa, NULL, oti);
2114 static int filter_sync(struct obd_export *exp, struct obdo *oa,
2115 struct lov_stripe_md *lsm, obd_off start, obd_off end)
2117 struct obd_run_ctxt saved;
2118 struct filter_obd *filter;
2119 struct dentry *dentry;
2120 struct llog_ctxt *ctxt;
2124 filter = &exp->exp_obd->u.filter;
2126 /* an objid of zero is taken to mean "sync whole filesystem" */
2127 if (!oa || !(oa->o_valid & OBD_MD_FLID)) {
2128 rc = fsfilt_sync(exp->exp_obd, filter->fo_sb);
2129 /* flush any remaining cancel messages out to the target */
2130 ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_REPL_CTXT);
2131 llog_sync(ctxt, exp);
2135 dentry = filter_oa2dentry(exp->exp_obd, oa);
2137 RETURN(PTR_ERR(dentry));
2139 push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
2141 down(&dentry->d_inode->i_sem);
2142 rc = filemap_fdatasync(dentry->d_inode->i_mapping);
2144 /* just any file to grab fsync method - "file" arg unused */
2145 struct file *file = filter->fo_rcvd_filp;
2147 if (file->f_op && file->f_op->fsync)
2148 rc = file->f_op->fsync(NULL, dentry, 1);
2150 rc2 = filemap_fdatawait(dentry->d_inode->i_mapping);
2154 up(&dentry->d_inode->i_sem);
2156 oa->o_valid = OBD_MD_FLID;
2157 obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2159 pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
2165 static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2166 unsigned long max_age)
2168 struct filter_obd *filter = &obd->u.filter;
2169 int blockbits = filter->fo_sb->s_blocksize_bits;
2173 /* at least try to account for cached pages. its still racey and
2174 * might be under-reporting if clients haven't announced their
2175 * caches with brw recently */
2176 spin_lock(&obd->obd_osfs_lock);
2177 rc = fsfilt_statfs(obd, filter->fo_sb, max_age);
2178 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
2179 spin_unlock(&obd->obd_osfs_lock);
2181 CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
2182 " pending "LPU64" free "LPU64" avail "LPU64"\n",
2183 filter->fo_tot_dirty, filter->fo_tot_granted,
2184 filter->fo_tot_pending,
2185 osfs->os_bfree << blockbits, osfs->os_bavail << blockbits);
2187 filter_grant_sanity_check(obd, __FUNCTION__);
2189 osfs->os_bavail -= min(osfs->os_bavail,
2190 (filter->fo_tot_dirty + filter->fo_tot_pending +
2191 osfs->os_bsize -1) >> blockbits);
2196 static int filter_get_info(struct obd_export *exp, __u32 keylen,
2197 void *key, __u32 *vallen, void *val)
2199 struct obd_device *obd;
2202 obd = class_exp2obd(exp);
2204 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2205 exp->exp_handle.h_cookie);
2209 if (keylen == strlen("blocksize") &&
2210 memcmp(key, "blocksize", keylen) == 0) {
2211 __u32 *blocksize = val;
2212 *vallen = sizeof(*blocksize);
2213 *blocksize = obd->u.filter.fo_sb->s_blocksize;
2217 if (keylen == strlen("blocksize_bits") &&
2218 memcmp(key, "blocksize_bits", keylen) == 0) {
2219 __u32 *blocksize_bits = val;
2220 *vallen = sizeof(*blocksize_bits);
2221 *blocksize_bits = obd->u.filter.fo_sb->s_blocksize_bits;
2225 if (keylen >= strlen("last_id") && memcmp(key, "last_id", 7) == 0) {
2226 obd_id *last_id = val;
2227 /* FIXME: object groups */
2228 *last_id = filter_last_id(&obd->u.filter, 0);
2231 CDEBUG(D_IOCTL, "invalid key\n");
2235 static int filter_set_info(struct obd_export *exp, __u32 keylen,
2236 void *key, __u32 vallen, void *val)
2238 struct obd_device *obd;
2239 struct lustre_handle conn;
2240 struct llog_ctxt *ctxt;
2244 conn.cookie = exp->exp_handle.h_cookie;
2248 CDEBUG(D_IOCTL, "invalid exp %p cookie "LPX64"\n",
2253 if (keylen < strlen("mds_conn") ||
2254 memcmp(key, "mds_conn", keylen) != 0)
2257 CWARN("Received MDS connection ("LPX64")\n", conn.cookie);
2258 memcpy(&obd->u.filter.fo_mdc_conn, &conn, sizeof(conn));
2259 ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
2260 rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
2264 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
2265 int len, void *karg, void *uarg)
2267 struct obd_device *obd = exp->exp_obd;
2268 struct obd_ioctl_data *data = karg;
2272 case OBD_IOC_ABORT_RECOVERY:
2273 CERROR("aborting recovery for device %s\n", obd->obd_name);
2274 target_abort_recovery(obd);
2277 case OBD_IOC_SET_READONLY: {
2279 struct super_block *sb = obd->u.filter.fo_sb;
2280 struct inode *inode = sb->s_root->d_inode;
2281 BDEVNAME_DECLARE_STORAGE(tmp);
2282 CERROR("setting device %s read-only\n",
2283 ll_bdevname(sb, tmp));
2285 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
2287 (void)fsfilt_commit(obd, inode, handle, 1);
2289 dev_set_rdonly(ll_sbdev(obd->u.filter.fo_sb), 2);
2293 case OBD_IOC_CATLOGLIST: {
2294 rc = llog_catlog_list(obd, 1, data);
2298 case OBD_IOC_LLOG_CANCEL:
2299 case OBD_IOC_LLOG_REMOVE:
2300 case OBD_IOC_LLOG_INFO:
2301 case OBD_IOC_LLOG_PRINT: {
2302 /* FIXME to be finished */
2303 RETURN(-EOPNOTSUPP);
2305 struct llog_ctxt *ctxt = NULL;
2307 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
2308 rc = llog_ioctl(ctxt, cmd, data);
2309 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
2322 static struct llog_operations filter_unlink_repl_logops;
2323 static struct llog_operations filter_size_orig_logops = {
2324 lop_setup: llog_obd_origin_setup,
2325 lop_cleanup: llog_obd_origin_cleanup,
2326 lop_add: llog_obd_origin_add
2329 static int filter_llog_init(struct obd_device *obd, struct obd_device *tgt,
2330 int count, struct llog_catid *logid)
2332 struct llog_ctxt *ctxt;
2336 filter_unlink_repl_logops = llog_client_ops;
2337 filter_unlink_repl_logops.lop_cancel = llog_obd_repl_cancel;
2338 filter_unlink_repl_logops.lop_connect = llog_repl_connect;
2339 filter_unlink_repl_logops.lop_sync = llog_obd_repl_sync;
2341 rc = llog_setup(obd, LLOG_UNLINK_REPL_CTXT, tgt, 0, NULL,
2342 &filter_unlink_repl_logops);
2345 /* FIXME - assign unlink_cb for filter's recovery */
2346 ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
2347 ctxt->llog_proc_cb = filter_recov_log_unlink_cb;
2349 rc = llog_setup(obd, LLOG_SIZE_ORIG_CTXT, tgt, 0, NULL,
2350 &filter_size_orig_logops);
2354 static int filter_llog_finish(struct obd_device *obd, int count)
2359 rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_REPL_CTXT));
2363 rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_ORIG_CTXT));
2367 static struct dentry *filter_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2370 return filter_fid2dentry(data, NULL, gr, id);
2373 static struct lvfs_callback_ops filter_lvfs_ops = {
2374 l_fid2dentry: filter_lvfs_fid2dentry,
2377 static struct obd_ops filter_obd_ops = {
2378 o_owner: THIS_MODULE,
2379 o_attach: filter_attach,
2380 o_detach: filter_detach,
2381 o_get_info: filter_get_info,
2382 o_set_info: filter_set_info,
2383 o_setup: filter_setup,
2384 o_precleanup: filter_precleanup,
2385 o_cleanup: filter_cleanup,
2386 o_connect: filter_connect,
2387 o_disconnect: filter_disconnect,
2388 o_statfs: filter_statfs,
2389 o_getattr: filter_getattr,
2390 o_unpackmd: filter_unpackmd,
2391 o_create: filter_create,
2392 o_setattr: filter_setattr,
2393 o_destroy: filter_destroy,
2395 o_punch: filter_truncate,
2396 o_sync: filter_sync,
2397 o_preprw: filter_preprw,
2398 o_commitrw: filter_commitrw,
2399 o_destroy_export: filter_destroy_export,
2400 o_llog_init: filter_llog_init,
2401 o_llog_finish: filter_llog_finish,
2402 o_iocontrol: filter_iocontrol,
2405 static struct obd_ops filter_sanobd_ops = {
2406 o_owner: THIS_MODULE,
2407 o_attach: filter_attach,
2408 o_detach: filter_detach,
2409 o_get_info: filter_get_info,
2410 o_set_info: filter_set_info,
2411 o_setup: filter_san_setup,
2412 o_precleanup: filter_precleanup,
2413 o_cleanup: filter_cleanup,
2414 o_connect: filter_connect,
2415 o_disconnect: filter_disconnect,
2416 o_statfs: filter_statfs,
2417 o_getattr: filter_getattr,
2418 o_unpackmd: filter_unpackmd,
2419 o_create: filter_create,
2420 o_setattr: filter_setattr,
2421 o_destroy: filter_destroy,
2423 o_punch: filter_truncate,
2424 o_sync: filter_sync,
2425 o_preprw: filter_preprw,
2426 o_commitrw: filter_commitrw,
2427 o_san_preprw: filter_san_preprw,
2428 o_destroy_export: filter_destroy_export,
2429 o_llog_init: filter_llog_init,
2430 o_llog_finish: filter_llog_finish,
2431 o_iocontrol: filter_iocontrol,
2434 static int __init obdfilter_init(void)
2436 struct lprocfs_static_vars lvars;
2439 printk(KERN_INFO "Lustre: Filtering OBD driver; info@clusterfs.com\n");
2441 lprocfs_init_vars(filter, &lvars);
2443 rc = class_register_type(&filter_obd_ops, lvars.module_vars,
2444 OBD_FILTER_DEVICENAME);
2448 rc = class_register_type(&filter_sanobd_ops, lvars.module_vars,
2449 OBD_FILTER_SAN_DEVICENAME);
2451 class_unregister_type(OBD_FILTER_DEVICENAME);
2455 static void __exit obdfilter_exit(void)
2457 class_unregister_type(OBD_FILTER_SAN_DEVICENAME);
2458 class_unregister_type(OBD_FILTER_DEVICENAME);
2461 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2462 MODULE_DESCRIPTION("Lustre Filtering OBD driver");
2463 MODULE_LICENSE("GPL");
2465 module_init(obdfilter_init);
2466 module_exit(obdfilter_exit);