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);
105 fcd->fcd_mount_count = filter->fo_fsd->fsd_mount_count;
107 /* could get xid from oti, if it's ever needed */
108 fcd->fcd_last_xid = 0;
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);
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);
127 void f_dput(struct dentry *dentry)
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);
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)
144 unsigned long *bitmap = filter->fo_last_rcvd_slots;
145 int new_client = (cl_idx == -1);
148 LASSERT(bitmap != NULL);
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))
154 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
155 * there's no need for extra complication here
158 cl_idx = find_first_zero_bit(bitmap, FILTER_LR_MAX_CLIENTS);
160 if (cl_idx >= FILTER_LR_MAX_CLIENTS) {
161 CERROR("no client slots - fix FILTER_LR_MAX_CLIENTS\n");
164 if (test_and_set_bit(cl_idx, bitmap)) {
165 CERROR("FILTER client %d: found bit is set in bitmap\n",
167 cl_idx = find_next_zero_bit(bitmap,
168 FILTER_LR_MAX_CLIENTS,
173 if (test_and_set_bit(cl_idx, bitmap)) {
174 CERROR("FILTER client %d: bit already set in bitmap!\n",
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);
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);
188 struct obd_run_ctxt saved;
189 loff_t off = fed->fed_lr_off;
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));
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);
205 err = fsfilt_write_record(obd, filter->fo_rcvd_filp,
207 sizeof(*fed->fed_fcd),
210 filter->fo_rcvd_filp->f_dentry->d_inode,
213 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
216 CERROR("error writing %s client idx %u: rc %d\n",
217 LAST_RCVD, fed->fed_lr_idx, err);
224 static int filter_client_free(struct obd_export *exp, int flags)
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;
235 if (fed->fed_fcd == NULL)
238 if (flags & OBD_OPT_FAILOVER)
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)
245 LASSERT(filter->fo_last_rcvd_slots != NULL);
247 off = fed->fed_lr_off;
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);
252 if (!test_and_clear_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
253 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
258 memset(&zero_fcd, 0, sizeof zero_fcd);
259 push_ctxt(&saved, &obd->obd_ctxt, NULL);
260 rc = fsfilt_write_record(obd, filter->fo_rcvd_filp, &zero_fcd,
261 sizeof(zero_fcd), &off, 1);
262 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
264 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
265 "zeroing disconnecting client %s at idx %u (%llu) in %s rc %d\n",
266 fed->fed_fcd->fcd_uuid, fed->fed_lr_idx, fed->fed_lr_off,
270 OBD_FREE(fed->fed_fcd, sizeof(*fed->fed_fcd));
275 static int filter_free_server_data(struct filter_obd *filter)
277 OBD_FREE(filter->fo_fsd, sizeof(*filter->fo_fsd));
278 filter->fo_fsd = NULL;
279 OBD_FREE(filter->fo_last_rcvd_slots,
280 FILTER_LR_MAX_CLIENT_WORDS * sizeof(unsigned long));
281 filter->fo_last_rcvd_slots = NULL;
285 /* assumes caller is already in kernel ctxt */
286 int filter_update_server_data(struct obd_device *obd, struct file *filp,
287 struct filter_server_data *fsd, int force_sync)
293 CDEBUG(D_INODE, "server uuid : %s\n", fsd->fsd_uuid);
294 CDEBUG(D_INODE, "server last_rcvd : "LPU64"\n",
295 le64_to_cpu(fsd->fsd_last_transno));
296 CDEBUG(D_INODE, "server last_mount: "LPU64"\n",
297 le64_to_cpu(fsd->fsd_mount_count));
299 rc = fsfilt_write_record(obd, filp, fsd, sizeof(*fsd), &off,force_sync);
301 CERROR("error writing filter_server_data: rc = %d\n", rc);
306 int filter_update_last_objid(struct obd_device *obd, obd_gr group,
309 struct filter_obd *filter = &obd->u.filter;
315 CDEBUG(D_INODE, "server last_objid for group "LPU64": "LPU64"\n",
316 group, filter->fo_last_objids[group]);
318 tmp = cpu_to_le64(filter->fo_last_objids[group]);
319 rc = fsfilt_write_record(obd, filter->fo_last_objid_files[group],
320 &tmp, sizeof(tmp), &off, force_sync);
322 CERROR("error writing group "LPU64" last objid: rc = %d\n",
327 /* assumes caller has already in kernel ctxt */
328 static int filter_init_server_data(struct obd_device *obd, struct file * filp)
330 struct filter_obd *filter = &obd->u.filter;
331 struct filter_server_data *fsd;
332 struct filter_client_data *fcd = NULL;
333 struct inode *inode = filp->f_dentry->d_inode;
334 unsigned long last_rcvd_size = inode->i_size;
340 /* ensure padding in the struct is the correct size */
341 LASSERT (offsetof(struct filter_server_data, fsd_padding) +
342 sizeof(fsd->fsd_padding) == FILTER_LR_SERVER_SIZE);
343 LASSERT (offsetof(struct filter_client_data, fcd_padding) +
344 sizeof(fcd->fcd_padding) == FILTER_LR_CLIENT_SIZE);
346 OBD_ALLOC(fsd, sizeof(*fsd));
349 filter->fo_fsd = fsd;
351 OBD_ALLOC(filter->fo_last_rcvd_slots,
352 FILTER_LR_MAX_CLIENT_WORDS * sizeof(unsigned long));
353 if (filter->fo_last_rcvd_slots == NULL) {
354 OBD_FREE(fsd, sizeof(*fsd));
358 if (last_rcvd_size == 0) {
359 CWARN("%s: initializing new %s\n", obd->obd_name, LAST_RCVD);
361 memcpy(fsd->fsd_uuid, obd->obd_uuid.uuid,sizeof(fsd->fsd_uuid));
362 fsd->fsd_last_transno = 0;
363 mount_count = fsd->fsd_mount_count = 0;
364 fsd->fsd_server_size = cpu_to_le32(FILTER_LR_SERVER_SIZE);
365 fsd->fsd_client_start = cpu_to_le32(FILTER_LR_CLIENT_START);
366 fsd->fsd_client_size = cpu_to_le16(FILTER_LR_CLIENT_SIZE);
367 fsd->fsd_subdir_count = cpu_to_le16(FILTER_SUBDIR_COUNT);
368 filter->fo_subdir_count = FILTER_SUBDIR_COUNT;
370 rc = fsfilt_read_record(obd, filp, fsd, sizeof(*fsd), &off);
372 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
376 if (strcmp(fsd->fsd_uuid, obd->obd_uuid.uuid) != 0) {
377 CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
378 obd->obd_uuid.uuid, fsd->fsd_uuid);
379 GOTO(err_fsd, rc = -EINVAL);
381 mount_count = le64_to_cpu(fsd->fsd_mount_count);
382 filter->fo_subdir_count = le16_to_cpu(fsd->fsd_subdir_count);
385 if (fsd->fsd_feature_incompat & ~le32_to_cpu(FILTER_INCOMPAT_SUPP)) {
386 CERROR("unsupported feature %x\n",
387 le32_to_cpu(fsd->fsd_feature_incompat) &
388 ~FILTER_INCOMPAT_SUPP);
389 GOTO(err_fsd, rc = -EINVAL);
391 if (fsd->fsd_feature_rocompat & ~le32_to_cpu(FILTER_ROCOMPAT_SUPP)) {
392 CERROR("read-only feature %x\n",
393 le32_to_cpu(fsd->fsd_feature_rocompat) &
394 ~FILTER_ROCOMPAT_SUPP);
395 /* Do something like remount filesystem read-only */
396 GOTO(err_fsd, rc = -EINVAL);
399 CDEBUG(D_INODE, "%s: server last_rcvd : "LPU64"\n",
400 obd->obd_name, le64_to_cpu(fsd->fsd_last_transno));
401 CDEBUG(D_INODE, "%s: server last_mount: "LPU64"\n",
402 obd->obd_name, mount_count);
403 CDEBUG(D_INODE, "%s: server data size: %u\n",
404 obd->obd_name, le32_to_cpu(fsd->fsd_server_size));
405 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
406 obd->obd_name, le32_to_cpu(fsd->fsd_client_start));
407 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
408 obd->obd_name, le32_to_cpu(fsd->fsd_client_size));
409 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
410 obd->obd_name, le16_to_cpu(fsd->fsd_subdir_count));
411 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
412 last_rcvd_size <= FILTER_LR_CLIENT_START ? 0 :
413 (last_rcvd_size-FILTER_LR_CLIENT_START) /FILTER_LR_CLIENT_SIZE);
415 if (!obd->obd_replayable) {
416 CWARN("%s: recovery support OFF\n", obd->obd_name);
420 for (cl_idx = 0, off = le32_to_cpu(fsd->fsd_client_start);
421 off < last_rcvd_size; cl_idx++) {
426 OBD_ALLOC(fcd, sizeof(*fcd));
428 GOTO(err_client, rc = -ENOMEM);
431 /* Don't assume off is incremented properly by
432 * fsfilt_read_record(), in case sizeof(*fcd)
433 * isn't the same as fsd->fsd_client_size. */
434 off = le32_to_cpu(fsd->fsd_client_start) +
435 cl_idx * le16_to_cpu(fsd->fsd_client_size);
436 rc = fsfilt_read_record(obd, filp, fcd, sizeof(*fcd), &off);
438 CERROR("error reading FILT %s idx %d off %llu: rc %d\n",
439 LAST_RCVD, cl_idx, off, rc);
440 break; /* read error shouldn't cause startup to fail */
443 if (fcd->fcd_uuid[0] == '\0') {
444 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
449 last_rcvd = le64_to_cpu(fcd->fcd_last_rcvd);
451 /* These exports are cleaned up by filter_disconnect(), so they
452 * need to be set up like real exports as filter_connect() does.
454 mount_age = mount_count - le64_to_cpu(fcd->fcd_mount_count);
455 if (mount_age < FILTER_MOUNT_RECOV) {
456 struct obd_export *exp = class_new_export(obd);
457 struct filter_export_data *fed;
458 CERROR("RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
459 " srv lr: "LPU64" mnt: "LPU64" last mount: "
460 LPU64"\n", fcd->fcd_uuid, cl_idx,
461 last_rcvd, le64_to_cpu(fsd->fsd_last_transno),
462 le64_to_cpu(fcd->fcd_mount_count), mount_count);
464 GOTO(err_client, rc = -ENOMEM);
466 memcpy(&exp->exp_client_uuid.uuid, fcd->fcd_uuid,
467 sizeof exp->exp_client_uuid.uuid);
468 fed = &exp->exp_filter_data;
470 filter_client_add(obd, filter, fed, cl_idx);
471 /* create helper if export init gets more complex */
472 spin_lock_init(&fed->fed_lock);
475 obd->obd_recoverable_clients++;
476 class_export_put(exp);
478 CDEBUG(D_INFO, "discarded client %d UUID '%s' count "
479 LPU64"\n", cl_idx, fcd->fcd_uuid,
480 le64_to_cpu(fcd->fcd_mount_count));
483 CDEBUG(D_OTHER, "client at idx %d has last_rcvd = "LPU64"\n",
486 if (last_rcvd > le64_to_cpu(filter->fo_fsd->fsd_last_transno))
487 filter->fo_fsd->fsd_last_transno=cpu_to_le64(last_rcvd);
491 obd->obd_last_committed = le64_to_cpu(filter->fo_fsd->fsd_last_transno);
493 if (obd->obd_recoverable_clients) {
494 CERROR("RECOVERY: %d recoverable clients, last_rcvd "
495 LPU64"\n", obd->obd_recoverable_clients,
496 le64_to_cpu(filter->fo_fsd->fsd_last_transno));
497 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
498 obd->obd_recovering = 1;
502 OBD_FREE(fcd, sizeof(*fcd));
505 fsd->fsd_mount_count = cpu_to_le64(mount_count + 1);
507 /* save it, so mount count and last_transno is current */
508 rc = filter_update_server_data(obd, filp, filter->fo_fsd, 1);
513 class_disconnect_exports(obd, 0);
515 filter_free_server_data(filter);
519 static int filter_cleanup_groups(struct obd_device *obd)
521 struct filter_obd *filter = &obd->u.filter;
525 if (filter->fo_dentry_O_groups != NULL &&
526 filter->fo_last_objids != NULL &&
527 filter->fo_last_objid_files != NULL) {
528 for (i = 0; i < FILTER_GROUPS; i++) {
529 struct dentry *dentry = filter->fo_dentry_O_groups[i];
530 struct file *filp = filter->fo_last_objid_files[i];
531 if (dentry != NULL) {
533 filter->fo_dentry_O_groups[i] = NULL;
537 filter->fo_last_objid_files[i] = NULL;
541 if (filter->fo_dentry_O_sub != NULL && filter->fo_subdir_count) {
542 for (i = 0; i < filter->fo_subdir_count; i++) {
543 struct dentry *dentry = filter->fo_dentry_O_sub[i];
544 if (dentry != NULL) {
546 filter->fo_dentry_O_sub[i] = NULL;
549 OBD_FREE(filter->fo_dentry_O_sub,
550 filter->fo_subdir_count *
551 sizeof(*filter->fo_dentry_O_sub));
553 if (filter->fo_dentry_O_groups != NULL)
554 OBD_FREE(filter->fo_dentry_O_groups,
555 FILTER_GROUPS * sizeof(struct dentry *));
556 if (filter->fo_last_objids != NULL)
557 OBD_FREE(filter->fo_last_objids,
558 FILTER_GROUPS * sizeof(__u64));
559 if (filter->fo_last_objid_files != NULL)
560 OBD_FREE(filter->fo_last_objid_files,
561 FILTER_GROUPS * sizeof(struct file *));
565 /* FIXME: object groups */
566 static int filter_prep_groups(struct obd_device *obd)
568 struct filter_obd *filter = &obd->u.filter;
569 struct dentry *dentry, *O_dentry;
571 int i, rc = 0, cleanup_phase = 0;
574 O_dentry = simple_mkdir(current->fs->pwd, "O", 0700);
575 CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
576 if (IS_ERR(O_dentry)) {
577 rc = PTR_ERR(O_dentry);
578 CERROR("cannot open/create O: rc = %d\n", rc);
581 filter->fo_dentry_O = O_dentry;
582 cleanup_phase = 1; /* O_dentry */
584 /* Lookup "R" to tell if we're on an old OST FS and need to convert
585 * from O/R/<dir>/<objid> to O/0/<dir>/<objid>. This can be removed
586 * some time post 1.0 when all old-style OSTs have converted along
587 * with the init_objid hack. */
588 dentry = ll_lookup_one_len("R", O_dentry, 1);
590 GOTO(cleanup, rc = PTR_ERR(dentry));
591 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
592 struct dentry *O0_dentry = lookup_one_len("0", O_dentry, 1);
595 CWARN("converting OST to new object layout\n");
596 if (IS_ERR(O0_dentry)) {
597 rc = PTR_ERR(O0_dentry);
598 CERROR("error looking up O/0: rc %d\n", rc);
602 if (O0_dentry->d_inode) {
603 CERROR("Both O/R and O/0 exist. Fix manually.\n");
604 GOTO(cleanup_O0, rc = -EEXIST);
607 down(&O_dentry->d_inode->i_sem);
608 rc = vfs_rename(O_dentry->d_inode, dentry,
609 O_dentry->d_inode, O0_dentry);
610 up(&O_dentry->d_inode->i_sem);
613 CERROR("error renaming O/R to O/0: rc %d\n", rc);
614 GOTO(cleanup_O0, rc);
616 filter->fo_fsd->fsd_feature_incompat |=
617 cpu_to_le32(FILTER_INCOMPAT_GROUPS);
618 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
620 GOTO(cleanup_O0, rc);
632 OBD_ALLOC(filter->fo_last_objids, FILTER_GROUPS * sizeof(__u64));
633 if (filter->fo_last_objids == NULL)
634 GOTO(cleanup, rc = -ENOMEM);
635 cleanup_phase = 2; /* groups */
637 OBD_ALLOC(filter->fo_dentry_O_groups, FILTER_GROUPS * sizeof(dentry));
638 if (filter->fo_dentry_O_groups == NULL)
639 GOTO(cleanup, rc = -ENOMEM);
640 OBD_ALLOC(filter->fo_last_objid_files, FILTER_GROUPS * sizeof(filp));
641 if (filter->fo_last_objid_files == NULL)
642 GOTO(cleanup, rc = -ENOMEM);
644 for (i = 0; i < FILTER_GROUPS; i++) {
648 sprintf(name, "%d", i);
649 dentry = simple_mkdir(O_dentry, name, 0700);
650 CDEBUG(D_INODE, "got/created O/%s: %p\n", name, dentry);
651 if (IS_ERR(dentry)) {
652 rc = PTR_ERR(dentry);
653 CERROR("cannot lookup/create O/%s: rc = %d\n",
657 filter->fo_dentry_O_groups[i] = dentry;
659 sprintf(name, "O/%d/LAST_ID", i);
660 filp = filp_open(name, O_CREAT | O_RDWR, 0700);
661 if (IS_ERR(dentry)) {
662 rc = PTR_ERR(dentry);
663 CERROR("cannot create %s: rc = %d\n", name, rc);
666 filter->fo_last_objid_files[i] = filp;
668 if (filp->f_dentry->d_inode->i_size == 0) {
669 if (i == 0 && filter->fo_fsd->fsd_unused != 0) {
670 /* OST conversion, remove sometime post 1.0 */
671 filter->fo_last_objids[i] =
672 le64_to_cpu(filter->fo_fsd->fsd_unused);
673 CWARN("saving old objid "LPU64" to LAST_ID\n",
674 filter->fo_last_objids[i]);
675 rc = filter_update_last_objid(obd, 0, 1);
679 filter->fo_last_objids[i] = FILTER_INIT_OBJID;
684 rc = fsfilt_read_record(obd, filp, &filter->fo_last_objids[i],
685 sizeof(__u64), &off);
687 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
691 filter->fo_last_objids[i] =
692 le64_to_cpu(filter->fo_last_objids[i]);
693 CDEBUG(D_INODE, "%s: server last_objid group %d: "LPU64"\n",
694 obd->obd_name, i, filter->fo_last_objids[i]);
697 if (filter->fo_subdir_count) {
698 O_dentry = filter->fo_dentry_O_groups[0];
699 OBD_ALLOC(filter->fo_dentry_O_sub,
700 filter->fo_subdir_count * sizeof(dentry));
701 if (filter->fo_dentry_O_sub == NULL)
702 GOTO(cleanup, rc = -ENOMEM);
704 for (i = 0; i < filter->fo_subdir_count; i++) {
706 snprintf(dir, sizeof(dir), "d%u", i);
708 dentry = simple_mkdir(O_dentry, dir, 0700);
709 CDEBUG(D_INODE, "got/created O/0/%s: %p\n", dir,dentry);
710 if (IS_ERR(dentry)) {
711 rc = PTR_ERR(dentry);
712 CERROR("can't lookup/create O/0/%s: rc = %d\n",
716 filter->fo_dentry_O_sub[i] = dentry;
722 switch (cleanup_phase) {
724 filter_cleanup_groups(obd);
726 f_dput(filter->fo_dentry_O);
727 filter->fo_dentry_O = NULL;
734 /* setup the object store with correct subdirectories */
735 static int filter_prep(struct obd_device *obd)
737 struct obd_run_ctxt saved;
738 struct filter_obd *filter = &obd->u.filter;
744 push_ctxt(&saved, &obd->obd_ctxt, NULL);
745 file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
746 if (!file || IS_ERR(file)) {
748 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
753 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
754 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
755 file->f_dentry->d_inode->i_mode);
756 GOTO(err_filp, rc = -ENOENT);
759 /* steal operations */
760 inode = file->f_dentry->d_inode;
761 filter->fo_fop = file->f_op;
762 filter->fo_iop = inode->i_op;
763 filter->fo_aops = inode->i_mapping->a_ops;
765 rc = filter_init_server_data(obd, file);
767 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
770 filter->fo_rcvd_filp = file;
772 rc = filter_prep_groups(obd);
774 GOTO(err_server_data, rc);
777 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
782 //class_disconnect_exports(obd, 0);
783 filter_free_server_data(filter);
785 if (filp_close(file, 0))
786 CERROR("can't close %s after error\n", LAST_RCVD);
787 filter->fo_rcvd_filp = NULL;
791 /* cleanup the filter: write last used object id to status file */
792 static void filter_post(struct obd_device *obd)
794 struct obd_run_ctxt saved;
795 struct filter_obd *filter = &obd->u.filter;
798 /* XXX: filter_update_lastobjid used to call fsync_dev. It might be
799 * best to start a transaction with h_sync, because we removed this
802 push_ctxt(&saved, &obd->obd_ctxt, NULL);
803 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
806 CERROR("error writing server data: rc = %d\n", rc);
808 for (i = 0; i < FILTER_GROUPS; i++) {
809 rc = filter_update_last_objid(obd, i, (i == FILTER_GROUPS - 1));
811 CERROR("error writing group %d lastobjid: rc = %d\n",
815 filp_close(filter->fo_rcvd_filp, 0);
816 filter->fo_rcvd_filp = NULL;
818 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
820 filter_cleanup_groups(obd);
821 f_dput(filter->fo_dentry_O);
822 filter_free_server_data(filter);
823 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
826 static void filter_set_last_id(struct filter_obd *filter, struct obdo *oa,
830 LASSERT(filter->fo_fsd != NULL);
833 LASSERT(oa->o_gr <= FILTER_GROUPS);
837 spin_lock(&filter->fo_objidlock);
838 filter->fo_last_objids[group] = id;
839 spin_unlock(&filter->fo_objidlock);
842 __u64 filter_last_id(struct filter_obd *filter, struct obdo *oa)
846 LASSERT(filter->fo_fsd != NULL);
849 LASSERT(oa->o_gr <= FILTER_GROUPS);
853 /* FIXME: object groups */
854 spin_lock(&filter->fo_objidlock);
855 id = filter->fo_last_objids[group];
856 spin_unlock(&filter->fo_objidlock);
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)
869 if (flag == LDLM_CB_CANCELING) {
870 /* Don't need to do anything here. */
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
881 if (lock->l_blocking_ast != filter_blocking_ast) {
882 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
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);
891 struct lustre_handle lockh;
894 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
895 ldlm_lock2handle(lock, &lockh);
896 rc = ldlm_cli_cancel(&lockh);
898 CERROR("ldlm_cli_cancel: %d\n", rc);
900 LDLM_DEBUG(lock, "Lock still has references, will be "
906 static int filter_lock_dentry(struct obd_device *obd, struct dentry *de,
907 ldlm_mode_t lock_mode,struct lustre_handle *lockh)
909 struct ldlm_res_id res_id = { .name = {0} };
913 res_id.name[0] = de->d_inode->i_ino;
914 res_id.name[1] = de->d_inode->i_generation;
915 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
916 res_id, LDLM_PLAIN, NULL, 0, lock_mode,
917 &flags, ldlm_completion_ast,
918 filter_blocking_ast, NULL, lockh);
920 RETURN(rc == ELDLM_OK ? 0 : -EIO); /* XXX translate ldlm code */
923 /* We never dget the object parent, so DON'T dput it either */
924 static void filter_parent_unlock(struct dentry *dparent,
925 struct lustre_handle *lockh,
926 ldlm_mode_t lock_mode)
928 ldlm_lock_decref(lockh, lock_mode);
931 /* We never dget the object parent, so DON'T dput it either */
932 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
934 struct filter_obd *filter = &obd->u.filter;
935 LASSERT(group < FILTER_GROUPS); /* FIXME: object groups */
937 if (group > 0 || filter->fo_subdir_count == 0)
938 return filter->fo_dentry_O_groups[group];
940 return filter->fo_dentry_O_sub[objid & (filter->fo_subdir_count - 1)];
943 /* We never dget the object parent, so DON'T dput it either */
944 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
945 obd_id objid, ldlm_mode_t lock_mode,
946 struct lustre_handle *lockh)
948 unsigned long now = jiffies;
949 struct dentry *de = filter_parent(obd, group, objid);
955 rc = filter_lock_dentry(obd, de, lock_mode, lockh);
956 if (time_after(jiffies, now + 15 * HZ))
957 CERROR("slow parent lock %lus\n", (jiffies - now) / HZ);
958 return rc ? ERR_PTR(rc) : de;
961 /* How to get files, dentries, inodes from object id's.
963 * If dir_dentry is passed, the caller has already locked the parent
964 * appropriately for this operation (normally a write lock). If
965 * dir_dentry is NULL, we do a read lock while we do the lookup to
966 * avoid races with create/destroy and such changing the directory
967 * internal to the filesystem code. */
968 struct dentry *filter_fid2dentry(struct obd_device *obd,
969 struct dentry *dir_dentry,
970 obd_gr group, obd_id id)
972 struct lustre_handle lockh;
973 struct dentry *dparent = dir_dentry;
974 struct dentry *dchild;
980 CERROR("fatal: invalid object id 0\n");
981 RETURN(ERR_PTR(-ESTALE));
984 len = sprintf(name, LPU64, id);
985 if (dir_dentry == NULL) {
986 dparent = filter_parent_lock(obd, group, id, LCK_PR, &lockh);
990 CDEBUG(D_INODE, "looking up object O/%*s/%s\n",
991 dparent->d_name.len, dparent->d_name.name, name);
992 dchild = ll_lookup_one_len(name, dparent, len);
993 if (dir_dentry == NULL)
994 filter_parent_unlock(dparent, &lockh, LCK_PR);
995 if (IS_ERR(dchild)) {
996 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
1000 CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
1001 name, dchild, atomic_read(&dchild->d_count));
1003 LASSERT(atomic_read(&dchild->d_count) > 0);
1008 static int filter_prepare_destroy(struct obd_device *obd, obd_id objid)
1010 struct lustre_handle lockh;
1011 int flags = LDLM_AST_DISCARD_DATA, rc;
1012 struct ldlm_res_id res_id = { .name = { objid } };
1013 struct ldlm_extent extent = { 0, OBD_OBJECT_EOF };
1016 /* Tell the clients that the object is gone now and that they should
1017 * throw away any cached pages. If we're the OST at stripe 0 in the
1018 * file then this enqueue will communicate the DISCARD to all the
1019 * clients. This assumes that we always destroy all the objects for
1020 * a file at a time, as is currently the case. If we're not the
1021 * OST at stripe 0 then we'll harmlessly get a very lonely lock in
1022 * the local DLM and immediately drop it. */
1023 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
1024 res_id, LDLM_EXTENT, &extent,
1025 sizeof(extent), LCK_PW, &flags,
1026 ldlm_completion_ast, filter_blocking_ast,
1029 /* We only care about the side-effects, just drop the lock. */
1031 ldlm_lock_decref(&lockh, LCK_PW);
1036 /* Caller must hold LCK_PW on parent and push us into kernel context.
1037 * Caller is also required to ensure that dchild->d_inode exists. */
1038 static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
1039 struct dentry *dparent,
1040 struct dentry *dchild)
1042 struct inode *inode = dchild->d_inode;
1046 if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
1047 CERROR("destroying objid %*s nlink = %lu, count = %d\n",
1048 dchild->d_name.len, dchild->d_name.name,
1049 (unsigned long)inode->i_nlink,
1050 atomic_read(&inode->i_count));
1053 rc = vfs_unlink(dparent->d_inode, dchild);
1056 CERROR("error unlinking objid %*s: rc %d\n",
1057 dchild->d_name.len, dchild->d_name.name, rc);
1062 /* mount the file system (secretly) */
1063 int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
1066 struct lustre_cfg* lcfg = buf;
1067 struct filter_obd *filter = &obd->u.filter;
1068 struct vfsmount *mnt;
1072 dev_clear_rdonly(2);
1074 if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
1077 obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
1078 if (IS_ERR(obd->obd_fsops))
1079 RETURN(PTR_ERR(obd->obd_fsops));
1081 mnt = do_kern_mount(lcfg->lcfg_inlbuf2, MS_NOATIME | MS_NODIRATIME,
1082 lcfg->lcfg_inlbuf1, option);
1087 if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3) {
1088 if (*lcfg->lcfg_inlbuf3 == 'f') {
1089 obd->obd_replayable = 1;
1090 obd_sync_filter = 1;
1091 CERROR("%s: recovery enabled\n", obd->obd_name);
1093 if (*lcfg->lcfg_inlbuf3 != 'n') {
1094 CERROR("unrecognised flag '%c'\n",
1095 *lcfg->lcfg_inlbuf3);
1097 // XXX Robert? Why do we get errors here
1098 // GOTO(err_mntput, rc = -EINVAL);
1102 filter->fo_vfsmnt = mnt;
1103 filter->fo_sb = mnt->mnt_sb;
1104 filter->fo_fstype = mnt->mnt_sb->s_type->name;
1105 CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
1107 OBD_SET_CTXT_MAGIC(&obd->obd_ctxt);
1108 obd->obd_ctxt.pwdmnt = mnt;
1109 obd->obd_ctxt.pwd = mnt->mnt_root;
1110 obd->obd_ctxt.fs = get_ds();
1111 obd->obd_ctxt.cb_ops = filter_lvfs_ops;
1113 rc = filter_prep(obd);
1115 GOTO(err_mntput, rc);
1117 spin_lock_init(&filter->fo_translock);
1118 spin_lock_init(&filter->fo_objidlock);
1119 INIT_LIST_HEAD(&filter->fo_export_list);
1120 sema_init(&filter->fo_alloc_lock, 1);
1122 obd->obd_namespace = ldlm_namespace_new("filter-tgt",
1123 LDLM_NAMESPACE_SERVER);
1124 if (obd->obd_namespace == NULL)
1125 GOTO(err_post, rc = -ENOMEM);
1127 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1128 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1140 fsfilt_put_ops(obd->obd_fsops);
1144 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
1146 struct lustre_cfg* lcfg = buf;
1147 const char *str = NULL;
1148 char *option = NULL;
1152 if (!strcmp(lcfg->lcfg_inlbuf2, "ext3")) {
1153 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1154 /* bug 1577: implement async-delete for 2.5 */
1155 str = "errors=remount-ro,asyncdel";
1157 str = "errors=remount-ro";
1159 n = strlen(str) + 1;
1160 OBD_ALLOC(option, n);
1163 strcpy(option, str);
1166 rc = filter_common_setup(obd, len, buf, option);
1168 OBD_FREE(option, n);
1172 static int filter_postsetup(struct obd_device *obd)
1177 // XXX add a storage location for the logid for size changes
1178 #ifdef ENABLE_ORPHANS
1179 rc = llog_cat_initialize(obd, 1);
1181 CERROR("failed to setup llogging subsystems\n");
1186 static int filter_cleanup(struct obd_device *obd, int flags)
1188 struct filter_obd *filter = &obd->u.filter;
1191 if (flags & OBD_OPT_FAILOVER)
1192 CERROR("%s: shutting down for failover; client state will"
1193 " be preserved.\n", obd->obd_name);
1195 if (!list_empty(&obd->obd_exports)) {
1196 CERROR("%s: still has clients!\n", obd->obd_name);
1197 class_disconnect_exports(obd, flags);
1198 if (!list_empty(&obd->obd_exports)) {
1199 CERROR("still has exports after forced cleanup?\n");
1204 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
1206 if (filter->fo_sb == NULL)
1211 shrink_dcache_parent(filter->fo_sb->s_root);
1214 if (atomic_read(&filter->fo_vfsmnt->mnt_count) > 1)
1215 CERROR("%s: mount point %p busy, mnt_count: %d\n",
1216 obd->obd_name, filter->fo_vfsmnt,
1217 atomic_read(&filter->fo_vfsmnt->mnt_count));
1220 mntput(filter->fo_vfsmnt);
1221 //destroy_buffers(filter->fo_sb->s_dev);
1222 filter->fo_sb = NULL;
1223 fsfilt_put_ops(obd->obd_fsops);
1226 dev_clear_rdonly(2);
1231 static int filter_attach(struct obd_device *obd, obd_count len, void *data)
1233 struct lprocfs_static_vars lvars;
1236 lprocfs_init_vars(filter, &lvars);
1237 rc = lprocfs_obd_attach(obd, lvars.obd_vars);
1241 rc = lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST);
1245 /* Init obdfilter private stats here */
1246 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
1247 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
1248 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
1249 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
1253 static int filter_detach(struct obd_device *dev)
1255 lprocfs_free_obd_stats(dev);
1256 return lprocfs_obd_detach(dev);
1259 /* nearly identical to mds_connect */
1260 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
1261 struct obd_uuid *cluuid)
1263 struct obd_export *exp;
1264 struct filter_export_data *fed;
1265 struct filter_client_data *fcd = NULL;
1266 struct filter_obd *filter = &obd->u.filter;
1270 if (conn == NULL || obd == NULL || cluuid == NULL)
1273 rc = class_connect(conn, obd, cluuid);
1276 exp = class_conn2export(conn);
1277 LASSERT(exp != NULL);
1279 fed = &exp->exp_filter_data;
1281 spin_lock_init(&fed->fed_lock);
1283 if (!obd->obd_replayable)
1284 GOTO(cleanup, rc = 0);
1286 OBD_ALLOC(fcd, sizeof(*fcd));
1288 CERROR("filter: out of memory for client data\n");
1289 GOTO(cleanup, rc = -ENOMEM);
1292 memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
1294 fcd->fcd_mount_count = cpu_to_le64(filter->fo_fsd->fsd_mount_count);
1296 rc = filter_client_add(obd, filter, fed, -1);
1301 OBD_FREE(fcd, sizeof(*fcd));
1302 class_disconnect(exp, 0);
1304 class_export_put(exp);
1309 static int filter_precleanup(struct obd_device *obd, int flags)
1314 #ifdef ENABLE_ORPHANS
1315 rc = obd_llog_finish(obd, 0);
1317 CERROR("failed to cleanup llogging subsystem\n");
1323 static int filter_destroy_export(struct obd_export *exp)
1327 target_destroy_export(exp);
1329 if (exp->exp_obd->obd_replayable)
1330 filter_client_free(exp, exp->exp_flags);
1334 /* also incredibly similar to mds_disconnect */
1335 static int filter_disconnect(struct obd_export *exp, int flags)
1337 unsigned long irqflags;
1338 struct llog_ctxt *ctxt;
1343 ldlm_cancel_locks_for_export(exp);
1345 spin_lock_irqsave(&exp->exp_lock, irqflags);
1346 exp->exp_flags = flags;
1347 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
1349 fsfilt_sync(exp->exp_obd, exp->exp_obd->u.filter.fo_sb);
1350 /* XXX cleanup preallocated inodes */
1352 /* flush any remaining cancel messages out to the target */
1353 ctxt = llog_get_context(exp->exp_obd, LLOG_UNLINK_REPL_CTXT);
1354 llog_sync(ctxt, exp);
1356 rc = class_disconnect(exp, flags);
1360 struct dentry *__filter_oa2dentry(struct obd_device *obd,
1361 struct obdo *oa, const char *what)
1363 struct dentry *dchild = NULL;
1366 if (oa->o_valid & OBD_MD_FLGROUP)
1369 dchild = filter_fid2dentry(obd, NULL, group, oa->o_id);
1371 if (IS_ERR(dchild)) {
1372 CERROR("%s error looking up object: "LPU64"\n", what, oa->o_id);
1376 if (dchild->d_inode == NULL) {
1377 CERROR("%s on non-existent object: "LPU64"\n", what, oa->o_id);
1379 RETURN(ERR_PTR(-ENOENT));
1385 static int filter_getattr(struct obd_export *exp, struct obdo *oa,
1386 struct lov_stripe_md *md)
1388 struct dentry *dentry = NULL;
1389 struct obd_device *obd;
1393 obd = class_exp2obd(exp);
1395 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1396 exp->exp_handle.h_cookie);
1400 dentry = filter_oa2dentry(obd, oa);
1402 RETURN(PTR_ERR(dentry));
1404 /* Limit the valid bits in the return data to what we actually use */
1405 oa->o_valid = OBD_MD_FLID;
1406 obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1412 /* this is called from filter_truncate() until we have filter_punch() */
1413 static int filter_setattr(struct obd_export *exp, struct obdo *oa,
1414 struct lov_stripe_md *md, struct obd_trans_info *oti)
1416 struct obd_run_ctxt saved;
1417 struct filter_obd *filter;
1418 struct dentry *dentry;
1424 LASSERT(oti != NULL);
1426 dentry = filter_oa2dentry(exp->exp_obd, oa);
1428 RETURN(PTR_ERR(dentry));
1430 filter = &exp->exp_obd->u.filter;
1432 iattr_from_obdo(&iattr, oa, oa->o_valid);
1434 push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1437 if (iattr.ia_valid & ATTR_SIZE)
1438 down(&dentry->d_inode->i_sem);
1439 handle = fsfilt_start(exp->exp_obd, dentry->d_inode, FSFILT_OP_SETATTR,
1442 GOTO(out_unlock, rc = PTR_ERR(handle));
1444 /* XXX this could be a rwsem instead, if filter_preprw played along */
1445 if (iattr.ia_valid & ATTR_ATTR_FLAG)
1446 rc = fsfilt_iocontrol(exp->exp_obd, dentry->d_inode, NULL,
1448 (long)&iattr.ia_attr_flags);
1450 rc = fsfilt_setattr(exp->exp_obd, dentry, handle, &iattr, 1);
1451 rc = filter_finish_transno(exp, oti, rc);
1452 rc2 = fsfilt_commit(exp->exp_obd, dentry->d_inode, handle, 0);
1454 CERROR("error on commit, err = %d\n", rc2);
1459 oa->o_valid = OBD_MD_FLID;
1460 obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1463 if (iattr.ia_valid & ATTR_SIZE)
1464 up(&dentry->d_inode->i_sem);
1466 pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1472 /* XXX identical to osc_unpackmd */
1473 static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
1474 struct lov_mds_md *lmm, int lmm_bytes)
1480 if (lmm_bytes < sizeof (*lmm)) {
1481 CERROR("lov_mds_md too small: %d, need %d\n",
1482 lmm_bytes, (int)sizeof(*lmm));
1485 /* XXX LOV_MAGIC etc check? */
1487 if (lmm->lmm_object_id == cpu_to_le64(0)) {
1488 CERROR("lov_mds_md: zero lmm_object_id\n");
1493 lsm_size = lov_stripe_md_size(1);
1497 if (*lsmp != NULL && lmm == NULL) {
1498 OBD_FREE(*lsmp, lsm_size);
1503 if (*lsmp == NULL) {
1504 OBD_ALLOC(*lsmp, lsm_size);
1508 loi_init((*lsmp)->lsm_oinfo);
1512 /* XXX zero *lsmp? */
1513 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
1514 LASSERT((*lsmp)->lsm_object_id);
1517 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
1522 static void filter_destroy_precreated(struct obd_export *exp, struct obdo *oa,
1523 struct filter_obd *filter)
1525 struct obdo doa; /* XXX obdo on stack */
1530 memset(&doa, 0, sizeof(doa));
1531 if (oa->o_valid & OBD_MD_FLGROUP)
1532 doa.o_gr = oa->o_gr;
1535 doa.o_mode = S_IFREG;
1536 last = filter_last_id(filter, &doa); /* FIXME: object groups */
1537 CWARN("deleting orphan objects from "LPU64" to "LPU64"\n",
1538 oa->o_id + 1, last);
1539 for (id = oa->o_id + 1; id <= last; id++) {
1541 filter_destroy(exp, &doa, NULL, NULL);
1543 spin_lock(&filter->fo_objidlock);
1544 filter->fo_last_objids[0] = oa->o_id; /* FIXME: object groups */
1545 spin_unlock(&filter->fo_objidlock);
1549 /* returns a negative error or a nonnegative number of files to create */
1550 static int filter_should_precreate(struct obd_export *exp, struct obdo *oa,
1553 struct obd_device *obd = exp->exp_obd;
1554 struct filter_obd *filter = &obd->u.filter;
1558 diff = oa->o_id - filter_last_id(filter, oa);
1559 CDEBUG(D_INFO, "filter_last_id() = "LPU64" -> diff = %d\n",
1560 filter_last_id(filter, oa), diff);
1562 /* delete orphans request */
1563 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1564 (oa->o_flags & OBD_FL_DELORPHAN)) {
1568 filter_destroy_precreated(exp, oa, filter);
1569 rc = filter_update_last_objid(obd, group, 0);
1571 CERROR("unable to write lastobjid, but orphans"
1575 /* only precreate if group == 0 and o_id is specfied */
1576 if (!(oa->o_valid & OBD_FL_DELORPHAN) &&
1577 (group != 0 || oa->o_id == 0))
1586 /* We rely on the fact that only one thread will be creating files in a given
1587 * group at a time, which is why we don't need an atomic filter_get_new_id.
1588 * Even if we had that atomic function, the following race would exist:
1590 * thread 1: gets id x from filter_next_id
1591 * thread 2: gets id (x + 1) from filter_next_id
1592 * thread 2: creates object (x + 1)
1593 * thread 1: tries to create object x, gets -ENOSPC
1595 static int filter_precreate(struct obd_device *obd, struct obdo *oa,
1596 obd_gr group, int *num)
1598 struct lustre_handle parent_lockh;
1599 struct dentry *dchild = NULL;
1600 struct filter_obd *filter;
1601 struct dentry *dparent;
1602 int err = 0, rc = 0, i;
1607 filter = &obd->u.filter;
1609 for (i = 0; i < *num && err == 0; i++) {
1610 int cleanup_phase = 0;
1612 next_id = filter_last_id(filter, oa) + 1;
1613 CDEBUG(D_INFO, "precreate objid "LPU64"\n", next_id);
1615 dparent = filter_parent_lock(obd, group, next_id, LCK_PW,
1617 if (IS_ERR(dparent))
1618 GOTO(cleanup, PTR_ERR(dparent));
1621 dchild = filter_fid2dentry(obd, dparent, group, next_id);
1623 GOTO(cleanup, rc = PTR_ERR(dchild));
1626 if (dchild->d_inode != NULL) {
1627 /* This would only happen if lastobjid was bad on disk*/
1628 CERROR("Serious error: objid %*s already exists; is "
1629 "this filesystem corrupt?\n",
1630 dchild->d_name.len, dchild->d_name.name);
1631 GOTO(cleanup, rc = -EEXIST);
1634 handle = fsfilt_start(obd, dparent->d_inode,
1635 FSFILT_OP_CREATE_LOG, NULL);
1637 GOTO(cleanup, rc = PTR_ERR(handle));
1640 rc = ll_vfs_create(dparent->d_inode, dchild, S_IFREG, NULL);
1642 CERROR("create failed rc = %d\n", rc);
1646 filter_set_last_id(filter, oa, next_id);
1647 err = filter_update_last_objid(obd, group, 0);
1649 CERROR("unable to write lastobjid but file created\n");
1652 switch(cleanup_phase) {
1654 err = fsfilt_commit(obd, dparent->d_inode, handle, 0);
1656 CERROR("error on commit, err = %d\n", err);
1663 filter_parent_unlock(dparent, &parent_lockh, LCK_PW);
1672 CDEBUG(D_INFO, "filter_precreate() created %d objects\n", i);
1676 static int filter_create(struct obd_export *exp, struct obdo *oa,
1677 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1679 struct obd_device *obd = NULL;
1680 struct obd_run_ctxt saved;
1681 struct lov_stripe_md *lsm = NULL;
1686 if (oa->o_valid & OBD_MD_FLGROUP)
1689 CDEBUG(D_INFO, "filter_create(od->o_gr="LPU64",od->o_id="LPU64")\n",
1694 rc = obd_alloc_memmd(exp, &lsm);
1701 push_ctxt(&saved, &obd->obd_ctxt, NULL);
1703 diff = filter_should_precreate(exp, oa, group);
1705 oa->o_id = filter_last_id(&obd->u.filter, oa);
1706 rc = filter_precreate(obd, oa, group, &diff);
1708 oa->o_valid = OBD_MD_FLID;
1711 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1712 if (rc && ea != NULL && *ea != lsm) {
1713 obd_free_memmd(exp, &lsm);
1714 } else if (rc == 0 && ea != NULL) {
1715 /* XXX LOV STACKING: the lsm that is passed to us from
1716 * LOV does not have valid lsm_oinfo data structs, so
1717 * don't go touching that. This needs to be fixed in a
1719 lsm->lsm_object_id = oa->o_id;
1726 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
1727 struct lov_stripe_md *ea, struct obd_trans_info *oti)
1729 struct obd_device *obd;
1730 struct filter_obd *filter;
1731 struct dentry *dchild = NULL, *dparent = NULL;
1732 struct obd_run_ctxt saved;
1733 void *handle = NULL;
1734 struct lustre_handle parent_lockh;
1735 struct llog_cookie *fcc = NULL;
1736 int rc, rc2, cleanup_phase = 0, have_prepared = 0;
1740 if (oa->o_valid & OBD_MD_FLGROUP)
1744 filter = &obd->u.filter;
1746 push_ctxt(&saved, &obd->obd_ctxt, NULL);
1749 dparent = filter_parent_lock(obd, group, oa->o_id, LCK_PW,
1751 if (IS_ERR(dparent))
1752 GOTO(cleanup, rc = PTR_ERR(dparent));
1755 dchild = filter_fid2dentry(obd, dparent, group, oa->o_id);
1757 GOTO(cleanup, rc = -ENOENT);
1760 if (dchild->d_inode == NULL) {
1761 CERROR("destroying non-existent object "LPU64"\n", oa->o_id);
1762 GOTO(cleanup, rc = -ENOENT);
1765 if (!have_prepared) {
1766 /* If we're really going to destroy the object, get ready
1767 * by getting the clients to discard their cached data.
1769 * We have to drop the parent lock, because
1770 * filter_prepare_destroy will acquire a PW on the object, and
1771 * we don't want to deadlock with an incoming write to the
1772 * object, which has the extent PW and then wants to get the
1773 * parent dentry to do the lookup.
1775 * We dput the child because it's not worth the extra
1776 * complication of condition the above code to skip it on the
1777 * second time through. */
1779 filter_parent_unlock(dparent, &parent_lockh, LCK_PW);
1781 filter_prepare_destroy(obd, oa->o_id);
1786 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK_LOG, oti);
1788 GOTO(cleanup, rc = PTR_ERR(handle));
1791 /* Our MDC connection is established by the MDS to us */
1792 if (oa->o_valid & OBD_MD_FLCOOKIE) {
1793 OBD_ALLOC(fcc, sizeof(*fcc));
1795 memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
1798 rc = filter_destroy_internal(obd, oa->o_id, dparent, dchild);
1801 switch(cleanup_phase) {
1804 fsfilt_add_journal_cb(obd, 0, oti->oti_handle,
1805 filter_cancel_cookies_cb, fcc);
1806 rc = filter_finish_transno(exp, oti, rc);
1807 rc2 = fsfilt_commit(obd, dparent->d_inode, handle, 0);
1809 CERROR("error on commit, err = %d\n", rc2);
1816 if (rc || oti == NULL) {
1817 filter_parent_unlock(dparent, &parent_lockh, LCK_PW);
1819 memcpy(&oti->oti_ack_locks[0].lock, &parent_lockh,
1820 sizeof(parent_lockh));
1821 oti->oti_ack_locks[0].mode = LCK_PW;
1824 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1827 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1834 /* NB start and end are used for punch, but not truncate */
1835 static int filter_truncate(struct obd_export *exp, struct obdo *oa,
1836 struct lov_stripe_md *lsm,
1837 obd_off start, obd_off end,
1838 struct obd_trans_info *oti)
1843 if (end != OBD_OBJECT_EOF)
1844 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
1847 CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = %x, "
1848 "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
1850 error = filter_setattr(exp, oa, NULL, oti);
1854 static int filter_sync(struct obd_export *exp, struct obdo *oa,
1855 struct lov_stripe_md *lsm, obd_off start, obd_off end)
1857 struct obd_run_ctxt saved;
1858 struct filter_obd *filter;
1859 struct dentry *dentry;
1863 filter = &exp->exp_obd->u.filter;
1865 /* an objid of zero is taken to mean "sync whole filesystem" */
1866 if (!oa || !(oa->o_valid & OBD_MD_FLID)) {
1867 rc = fsfilt_sync(exp->exp_obd, filter->fo_sb);
1871 dentry = filter_oa2dentry(exp->exp_obd, oa);
1873 RETURN(PTR_ERR(dentry));
1875 push_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1877 down(&dentry->d_inode->i_sem);
1878 rc = filemap_fdatasync(dentry->d_inode->i_mapping);
1880 /* just any file to grab fsync method - "file" arg unused */
1881 struct file *file = filter->fo_rcvd_filp;
1883 if (file->f_op && file->f_op->fsync)
1884 rc = file->f_op->fsync(NULL, dentry, 1);
1886 rc2 = filemap_fdatawait(dentry->d_inode->i_mapping);
1890 up(&dentry->d_inode->i_sem);
1892 oa->o_valid = OBD_MD_FLID;
1893 obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
1895 pop_ctxt(&saved, &exp->exp_obd->obd_ctxt, NULL);
1901 static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1902 unsigned long max_age)
1905 RETURN(fsfilt_statfs(obd, obd->u.filter.fo_sb, osfs));
1908 static int filter_get_info(struct obd_export *exp, __u32 keylen,
1909 void *key, __u32 *vallen, void *val)
1911 struct obd_device *obd;
1914 obd = class_exp2obd(exp);
1916 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1917 exp->exp_handle.h_cookie);
1921 if (keylen == strlen("blocksize") &&
1922 memcmp(key, "blocksize", keylen) == 0) {
1923 __u32 *blocksize = val;
1924 *vallen = sizeof(*blocksize);
1925 *blocksize = obd->u.filter.fo_sb->s_blocksize;
1929 if (keylen == strlen("blocksize_bits") &&
1930 memcmp(key, "blocksize_bits", keylen) == 0) {
1931 __u32 *blocksize_bits = val;
1932 *vallen = sizeof(*blocksize_bits);
1933 *blocksize_bits = obd->u.filter.fo_sb->s_blocksize_bits;
1937 if (keylen >= strlen("last_id") && memcmp(key, "last_id", 7) == 0) {
1938 obd_id *last_id = val;
1939 /* FIXME: object groups */
1940 *last_id = filter_last_id(&obd->u.filter, 0);
1943 CDEBUG(D_IOCTL, "invalid key\n");
1947 static int filter_set_info(struct obd_export *exp, __u32 keylen,
1948 void *key, __u32 vallen, void *val)
1950 struct obd_device *obd;
1951 struct lustre_handle conn;
1952 #ifdef ENABLE_ORPHANS
1953 struct llog_ctxt *ctxt;
1958 conn.cookie = exp->exp_handle.h_cookie;
1962 CDEBUG(D_IOCTL, "invalid exp %p cookie "LPX64"\n",
1967 if (keylen < strlen("mds_conn") ||
1968 memcmp(key, "mds_conn", keylen) != 0)
1971 CWARN("Received MDS connection ("LPX64")\n", conn.cookie);
1972 memcpy(&obd->u.filter.fo_mdc_conn, &conn, sizeof(conn));
1973 #ifdef ENABLE_ORPHANS
1974 ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
1975 rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
1980 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
1981 int len, void *karg, void *uarg)
1983 struct obd_device *obd = exp->exp_obd;
1984 struct obd_ioctl_data *data = karg;
1988 case OBD_IOC_ABORT_RECOVERY:
1989 CERROR("aborting recovery for device %s\n", obd->obd_name);
1990 target_abort_recovery(obd);
1993 case OBD_IOC_SET_READONLY: {
1995 struct super_block *sb = obd->u.filter.fo_sb;
1996 struct inode *inode = sb->s_root->d_inode;
1997 BDEVNAME_DECLARE_STORAGE(tmp);
1998 CERROR("setting device %s read-only\n",
1999 ll_bdevname(sb, tmp));
2001 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
2003 (void)fsfilt_commit(obd, inode, handle, 1);
2005 dev_set_rdonly(ll_sbdev(obd->u.filter.fo_sb), 2);
2009 case OBD_IOC_CATLOGLIST: {
2010 rc = llog_catlog_list(obd, 1, data);
2014 case OBD_IOC_LLOG_CANCEL:
2015 case OBD_IOC_LLOG_REMOVE:
2016 case OBD_IOC_LLOG_INFO:
2017 case OBD_IOC_LLOG_PRINT: {
2018 /* FIXME to be finished */
2019 RETURN(-EOPNOTSUPP);
2021 struct llog_ctxt *ctxt = NULL;
2023 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
2024 rc = llog_ioctl(ctxt, cmd, data);
2025 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
2038 static struct llog_operations filter_unlink_repl_logops;
2039 static struct llog_operations filter_size_orig_logops = {
2040 lop_setup: llog_obd_origin_setup,
2041 lop_cleanup: llog_obd_origin_cleanup,
2042 lop_add: llog_obd_origin_add
2045 static int filter_llog_init(struct obd_device *obd, struct obd_device *tgt,
2046 int count, struct llog_logid *logid)
2048 struct llog_ctxt *ctxt;
2052 filter_unlink_repl_logops = llog_client_ops;
2053 filter_unlink_repl_logops.lop_cancel = llog_obd_repl_cancel;
2054 filter_unlink_repl_logops.lop_connect = llog_repl_connect;
2055 filter_unlink_repl_logops.lop_sync = llog_obd_repl_sync;
2057 rc = llog_setup(obd, LLOG_UNLINK_REPL_CTXT, tgt, 0, NULL,
2058 &filter_unlink_repl_logops);
2061 /* FIXME - assign unlink_cb for filter's recovery */
2062 ctxt = llog_get_context(obd, LLOG_UNLINK_REPL_CTXT);
2063 ctxt->llog_proc_cb = filter_recov_log_unlink_cb;
2065 rc = llog_setup(obd, LLOG_SIZE_ORIG_CTXT, tgt, 0, NULL,
2066 &filter_size_orig_logops);
2070 static int filter_llog_finish(struct obd_device *obd, int count)
2075 rc = llog_cleanup(llog_get_context(obd, LLOG_UNLINK_REPL_CTXT));
2079 rc = llog_cleanup(llog_get_context(obd, LLOG_SIZE_ORIG_CTXT));
2083 static struct dentry *filter_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2086 return filter_fid2dentry(data, NULL, gr, id);
2089 static struct lvfs_callback_ops filter_lvfs_ops = {
2090 l_fid2dentry: filter_lvfs_fid2dentry,
2093 static struct obd_ops filter_obd_ops = {
2094 o_owner: THIS_MODULE,
2095 o_attach: filter_attach,
2096 o_detach: filter_detach,
2097 o_get_info: filter_get_info,
2098 o_set_info: filter_set_info,
2099 o_setup: filter_setup,
2100 o_postsetup: filter_postsetup,
2101 o_precleanup: filter_precleanup,
2102 o_cleanup: filter_cleanup,
2103 o_connect: filter_connect,
2104 o_disconnect: filter_disconnect,
2105 o_statfs: filter_statfs,
2106 o_getattr: filter_getattr,
2107 o_unpackmd: filter_unpackmd,
2108 o_create: filter_create,
2109 o_setattr: filter_setattr,
2110 o_destroy: filter_destroy,
2112 o_punch: filter_truncate,
2113 o_sync: filter_sync,
2114 o_preprw: filter_preprw,
2115 o_commitrw: filter_commitrw,
2116 o_destroy_export: filter_destroy_export,
2117 o_llog_init: filter_llog_init,
2118 o_llog_finish: filter_llog_finish,
2119 o_iocontrol: filter_iocontrol,
2122 static struct obd_ops filter_sanobd_ops = {
2123 o_owner: THIS_MODULE,
2124 o_attach: filter_attach,
2125 o_detach: filter_detach,
2126 o_get_info: filter_get_info,
2127 o_set_info: filter_set_info,
2128 o_setup: filter_san_setup,
2129 o_precleanup: filter_precleanup,
2130 o_cleanup: filter_cleanup,
2131 o_connect: filter_connect,
2132 o_disconnect: filter_disconnect,
2133 o_statfs: filter_statfs,
2134 o_getattr: filter_getattr,
2135 o_unpackmd: filter_unpackmd,
2136 o_create: filter_create,
2137 o_setattr: filter_setattr,
2138 o_destroy: filter_destroy,
2140 o_punch: filter_truncate,
2141 o_sync: filter_sync,
2142 o_preprw: filter_preprw,
2143 o_commitrw: filter_commitrw,
2144 o_san_preprw: filter_san_preprw,
2145 o_destroy_export: filter_destroy_export,
2146 o_llog_init: filter_llog_init,
2147 o_llog_finish: filter_llog_finish,
2148 o_iocontrol: filter_iocontrol,
2151 static int __init obdfilter_init(void)
2153 struct lprocfs_static_vars lvars;
2156 printk(KERN_INFO "Lustre: Filtering OBD driver; info@clusterfs.com\n");
2158 lprocfs_init_vars(filter, &lvars);
2160 rc = class_register_type(&filter_obd_ops, lvars.module_vars,
2161 OBD_FILTER_DEVICENAME);
2165 rc = class_register_type(&filter_sanobd_ops, lvars.module_vars,
2166 OBD_FILTER_SAN_DEVICENAME);
2168 class_unregister_type(OBD_FILTER_DEVICENAME);
2172 static void __exit obdfilter_exit(void)
2174 class_unregister_type(OBD_FILTER_SAN_DEVICENAME);
2175 class_unregister_type(OBD_FILTER_DEVICENAME);
2178 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2179 MODULE_DESCRIPTION("Lustre Filtering OBD driver");
2180 MODULE_LICENSE("GPL");
2182 module_init(obdfilter_init);
2183 module_exit(obdfilter_exit);