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 #include <linux/jbd.h>
45 #include <linux/ext3_fs.h>
46 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
47 # include <linux/mount.h>
48 # include <linux/buffer_head.h>
49 # include <linux/bio.h>
52 #include <linux/obd_class.h>
53 #include <linux/obd_lov.h>
54 #include <linux/obd_ost.h>
55 #include <linux/lustre_dlm.h>
56 #include <linux/lustre_fsfilt.h>
57 #include <linux/lprocfs_status.h>
58 #include <linux/lustre_log.h>
59 #include <linux/lustre_commit_confd.h>
60 #include <libcfs/list.h>
62 #include <linux/lustre_smfs.h>
63 #include <linux/lustre_sec.h>
64 #include "filter_internal.h"
66 /* Group 0 is no longer a legal group, to catch uninitialized IDs */
67 #define FILTER_MIN_GROUPS 3
69 static struct lvfs_callback_ops filter_lvfs_ops;
71 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
72 struct lov_stripe_md *ea, struct obd_trans_info *);
73 struct obd_llogs *filter_grab_llog_for_group(struct obd_device *,
74 int, struct obd_export *);
76 static void filter_commit_cb(struct obd_device *obd, __u64 transno,
77 void *cb_data, int error)
79 obd_transno_commit_cb(obd, transno, error);
82 /* Assumes caller has already pushed us into the kernel context. */
83 int filter_finish_transno(struct obd_export *exp, struct obd_trans_info *oti,
86 struct filter_obd *filter = &exp->exp_obd->u.filter;
87 struct filter_export_data *fed = &exp->exp_filter_data;
88 struct filter_client_data *fcd = fed->fed_fcd;
91 int err, log_pri = D_HA;
93 /* Propagate error code. */
97 if (!exp->exp_obd->obd_replayable || oti == NULL)
100 /* we don't allocate new transnos for replayed requests */
101 if (oti->oti_transno == 0) {
102 spin_lock(&filter->fo_translock);
103 last_rcvd = le64_to_cpu(filter->fo_fsd->fsd_last_transno) + 1;
104 filter->fo_fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
105 spin_unlock(&filter->fo_translock);
106 oti->oti_transno = last_rcvd;
108 spin_lock(&filter->fo_translock);
109 last_rcvd = oti->oti_transno;
110 if (last_rcvd > le64_to_cpu(filter->fo_fsd->fsd_last_transno))
111 filter->fo_fsd->fsd_last_transno =
112 cpu_to_le64(last_rcvd);
113 spin_unlock(&filter->fo_translock);
116 fcd->fcd_last_rcvd = cpu_to_le64(last_rcvd);
118 /* could get xid from oti, if it's ever needed */
119 fcd->fcd_last_xid = 0;
121 off = fed->fed_lr_off;
123 fsfilt_add_journal_cb(exp->exp_obd, filter->fo_sb, last_rcvd,
124 oti->oti_handle, filter_commit_cb, NULL);
126 err = fsfilt_write_record(exp->exp_obd, filter->fo_rcvd_filp, fcd,
127 sizeof(*fcd), &off, 0);
134 CDEBUG(log_pri, "wrote trans "LPU64" for client %s at #%d: err = %d\n",
135 last_rcvd, fcd->fcd_uuid, fed->fed_lr_idx, err);
140 void f_dput(struct dentry *dentry)
142 /* Can't go inside filter_ddelete because it can block */
143 CDEBUG(D_INODE, "putting %s: %p, count = %d\n",
144 dentry->d_name.name, dentry, atomic_read(&dentry->d_count) - 1);
145 LASSERT(atomic_read(&dentry->d_count) > 0);
150 /* Add client data to the FILTER. We use a bitmap to locate a free space
151 * in the last_rcvd file if cl_idx is -1 (i.e. a new client).
152 * Otherwise, we have just read the data from the last_rcvd file and
153 * we know its offset. */
154 static int filter_client_add(struct obd_device *obd, struct filter_obd *filter,
155 struct filter_export_data *fed, int cl_idx)
157 unsigned long *bitmap = filter->fo_last_rcvd_slots;
158 int new_client = (cl_idx == -1);
161 LASSERT(bitmap != NULL);
163 /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
164 if (!strcmp((char *)fed->fed_fcd->fcd_uuid, (char *)obd->obd_uuid.uuid))
167 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
168 * there's no need for extra complication here
171 cl_idx = find_first_zero_bit(bitmap, FILTER_LR_MAX_CLIENTS);
173 if (cl_idx >= FILTER_LR_MAX_CLIENTS) {
174 CERROR("no client slots - fix FILTER_LR_MAX_CLIENTS\n");
177 if (test_and_set_bit(cl_idx, bitmap)) {
178 CERROR("FILTER client %d: found bit is set in bitmap\n",
180 cl_idx = find_next_zero_bit(bitmap,
181 FILTER_LR_MAX_CLIENTS,
186 if (test_and_set_bit(cl_idx, bitmap)) {
187 CERROR("FILTER client %d: bit already set in bitmap!\n",
193 fed->fed_lr_idx = cl_idx;
194 fed->fed_lr_off = le32_to_cpu(filter->fo_fsd->fsd_client_start) +
195 cl_idx * le16_to_cpu(filter->fo_fsd->fsd_client_size);
197 CDEBUG(D_INFO, "client at index %d (%llu) with UUID '%s' added\n",
198 fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
201 struct lvfs_run_ctxt saved;
202 loff_t off = fed->fed_lr_off;
206 CDEBUG(D_INFO, "writing client fcd at idx %u (%llu) (len %u)\n",
207 fed->fed_lr_idx,off,(unsigned int)sizeof(*fed->fed_fcd));
209 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
210 /* Transaction needed to fix bug 1403 */
211 handle = fsfilt_start(obd,
212 filter->fo_rcvd_filp->f_dentry->d_inode,
213 FSFILT_OP_SETATTR, NULL);
214 if (IS_ERR(handle)) {
215 err = PTR_ERR(handle);
216 CERROR("unable to start transaction: rc %d\n", err);
218 err = fsfilt_write_record(obd, filter->fo_rcvd_filp,
220 sizeof(*fed->fed_fcd),
222 fsfilt_commit(obd, filter->fo_sb,
223 filter->fo_rcvd_filp->f_dentry->d_inode,
226 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
229 CERROR("error writing %s client idx %u: rc %d\n",
230 LAST_RCVD, fed->fed_lr_idx, err);
237 static int filter_client_free(struct obd_export *exp, int flags)
239 struct filter_export_data *fed = &exp->exp_filter_data;
240 struct filter_obd *filter = &exp->exp_obd->u.filter;
241 struct obd_device *obd = exp->exp_obd;
242 struct filter_client_data zero_fcd;
243 struct lvfs_run_ctxt saved;
248 if (fed->fed_fcd == NULL)
251 if (flags & OBD_OPT_FAILOVER)
254 /* XXX if fcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
255 if (!strcmp((char *)fed->fed_fcd->fcd_uuid, (char *)obd->obd_uuid.uuid))
258 LASSERT(filter->fo_last_rcvd_slots != NULL);
260 off = fed->fed_lr_off;
262 CDEBUG(D_INFO, "freeing client at idx %u (%lld) with UUID '%s'\n",
263 fed->fed_lr_idx, fed->fed_lr_off, fed->fed_fcd->fcd_uuid);
265 /* Clear the bit _after_ zeroing out the client so we don't
266 race with filter_client_add and zero out new clients.*/
267 if (!test_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
268 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
273 memset(&zero_fcd, 0, sizeof zero_fcd);
274 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
275 rc = fsfilt_write_record(obd, filter->fo_rcvd_filp, &zero_fcd,
276 sizeof(zero_fcd), &off, 1);
278 /* update server's transno */
279 filter_update_server_data(obd, filter->fo_rcvd_filp,
281 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
283 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
284 "zeroing disconnecting client %s at idx %u (%llu) in %s rc %d\n",
285 fed->fed_fcd->fcd_uuid, fed->fed_lr_idx, fed->fed_lr_off,
288 if (!test_and_clear_bit(fed->fed_lr_idx, filter->fo_last_rcvd_slots)) {
289 CERROR("FILTER client %u: bit already clear in bitmap!!\n",
295 OBD_FREE(fed->fed_fcd, sizeof(*fed->fed_fcd));
300 static int filter_free_server_data(struct filter_obd *filter)
302 OBD_FREE(filter->fo_fsd, sizeof(*filter->fo_fsd));
303 filter->fo_fsd = NULL;
304 OBD_FREE(filter->fo_last_rcvd_slots, FILTER_LR_MAX_CLIENTS/8);
305 filter->fo_last_rcvd_slots = NULL;
309 /* assumes caller is already in kernel ctxt */
310 int filter_update_server_data(struct obd_device *obd, struct file *filp,
311 struct filter_server_data *fsd, int force_sync)
317 CDEBUG(D_INODE, "server uuid : %s\n", fsd->fsd_uuid);
318 CDEBUG(D_INODE, "server last_rcvd : "LPU64"\n",
319 le64_to_cpu(fsd->fsd_last_transno));
320 CDEBUG(D_INODE, "server last_mount: "LPU64"\n",
321 le64_to_cpu(fsd->fsd_mount_count));
323 rc = fsfilt_write_record(obd, filp, fsd, sizeof(*fsd), &off,force_sync);
325 CERROR("error writing filter_server_data: rc = %d\n", rc);
330 int filter_update_last_objid(struct obd_device *obd, obd_gr group,
333 struct filter_obd *filter = &obd->u.filter;
339 if (filter->fo_last_objid_files[group] == NULL) {
340 CERROR("Object group "LPU64" not fully setup; not updating "
341 "last_objid\n", group);
345 CDEBUG(D_INODE, "server last_objid for group "LPU64": "LPU64"\n",
346 group, filter->fo_last_objids[group]);
348 tmp = cpu_to_le64(filter->fo_last_objids[group]);
349 rc = fsfilt_write_record(obd, filter->fo_last_objid_files[group],
350 &tmp, sizeof(tmp), &off, force_sync);
352 CERROR("error writing group "LPU64" last objid: rc = %d\n",
357 /* assumes caller has already in kernel ctxt */
358 static int filter_init_server_data(struct obd_device *obd, struct file * filp)
360 struct filter_obd *filter = &obd->u.filter;
361 struct filter_server_data *fsd;
362 struct filter_client_data *fcd = NULL;
363 struct inode *inode = filp->f_dentry->d_inode;
364 unsigned long last_rcvd_size = inode->i_size;
370 /* ensure padding in the struct is the correct size */
371 LASSERT (offsetof(struct filter_server_data, fsd_padding) +
372 sizeof(fsd->fsd_padding) == FILTER_LR_SERVER_SIZE);
373 LASSERT (offsetof(struct filter_client_data, fcd_padding) +
374 sizeof(fcd->fcd_padding) == FILTER_LR_CLIENT_SIZE);
376 OBD_ALLOC(fsd, sizeof(*fsd));
379 filter->fo_fsd = fsd;
381 OBD_ALLOC(filter->fo_last_rcvd_slots, FILTER_LR_MAX_CLIENTS/8);
382 if (filter->fo_last_rcvd_slots == NULL) {
383 OBD_FREE(fsd, sizeof(*fsd));
387 if (last_rcvd_size == 0) {
388 CWARN("%s: initializing new %s\n", obd->obd_name, LAST_RCVD);
390 memcpy(fsd->fsd_uuid, obd->obd_uuid.uuid,sizeof(fsd->fsd_uuid));
391 fsd->fsd_last_transno = 0;
392 mount_count = fsd->fsd_mount_count = 0;
393 fsd->fsd_server_size = cpu_to_le32(FILTER_LR_SERVER_SIZE);
394 fsd->fsd_client_start = cpu_to_le32(FILTER_LR_CLIENT_START);
395 fsd->fsd_client_size = cpu_to_le16(FILTER_LR_CLIENT_SIZE);
396 fsd->fsd_subdir_count = cpu_to_le16(FILTER_SUBDIR_COUNT);
397 filter->fo_subdir_count = FILTER_SUBDIR_COUNT;
399 rc = fsfilt_read_record(obd, filp, fsd, sizeof(*fsd), &off);
401 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
405 if (strcmp((char *)fsd->fsd_uuid, (char *)obd->obd_uuid.uuid)) {
406 CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
407 obd->obd_uuid.uuid, fsd->fsd_uuid);
408 GOTO(err_fsd, rc = -EINVAL);
410 mount_count = le64_to_cpu(fsd->fsd_mount_count);
411 filter->fo_subdir_count = le16_to_cpu(fsd->fsd_subdir_count);
414 if (fsd->fsd_feature_incompat & ~cpu_to_le32(FILTER_INCOMPAT_SUPP)) {
415 CERROR("unsupported feature %x\n",
416 le32_to_cpu(fsd->fsd_feature_incompat) &
417 ~FILTER_INCOMPAT_SUPP);
418 GOTO(err_fsd, rc = -EINVAL);
420 if (fsd->fsd_feature_rocompat & ~cpu_to_le32(FILTER_ROCOMPAT_SUPP)) {
421 CERROR("read-only feature %x\n",
422 le32_to_cpu(fsd->fsd_feature_rocompat) &
423 ~FILTER_ROCOMPAT_SUPP);
424 /* Do something like remount filesystem read-only */
425 GOTO(err_fsd, rc = -EINVAL);
428 CDEBUG(D_INODE, "%s: server last_rcvd : "LPU64"\n",
429 obd->obd_name, le64_to_cpu(fsd->fsd_last_transno));
430 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
431 obd->obd_name, mount_count + 1);
432 CDEBUG(D_INODE, "%s: server data size: %u\n",
433 obd->obd_name, le32_to_cpu(fsd->fsd_server_size));
434 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
435 obd->obd_name, le32_to_cpu(fsd->fsd_client_start));
436 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
437 obd->obd_name, le32_to_cpu(fsd->fsd_client_size));
438 CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
439 obd->obd_name, le16_to_cpu(fsd->fsd_subdir_count));
440 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
441 last_rcvd_size <= le32_to_cpu(fsd->fsd_client_start) ? 0 :
442 (last_rcvd_size - le32_to_cpu(fsd->fsd_client_start)) /
443 le16_to_cpu(fsd->fsd_client_size));
445 if (!obd->obd_replayable) {
446 CWARN("%s: recovery support OFF\n", obd->obd_name);
450 for (cl_idx = 0, off = le32_to_cpu(fsd->fsd_client_start);
451 off < last_rcvd_size; cl_idx++) {
453 struct obd_export *exp;
454 struct filter_export_data *fed;
457 OBD_ALLOC(fcd, sizeof(*fcd));
459 GOTO(err_client, rc = -ENOMEM);
462 /* Don't assume off is incremented properly by
463 * fsfilt_read_record(), in case sizeof(*fcd)
464 * isn't the same as fsd->fsd_client_size. */
465 off = le32_to_cpu(fsd->fsd_client_start) +
466 cl_idx * le16_to_cpu(fsd->fsd_client_size);
467 rc = fsfilt_read_record(obd, filp, fcd, sizeof(*fcd), &off);
469 CERROR("error reading FILT %s idx %d off %llu: rc %d\n",
470 LAST_RCVD, cl_idx, off, rc);
471 break; /* read error shouldn't cause startup to fail */
474 if (fcd->fcd_uuid[0] == '\0') {
475 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
480 last_rcvd = le64_to_cpu(fcd->fcd_last_rcvd);
482 /* These exports are cleaned up by filter_disconnect(), so they
483 * need to be set up like real exports as filter_connect() does.
485 exp = class_new_export(obd);
486 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64" "
487 "srv lr: "LPU64" fcd_group %d \n", fcd->fcd_uuid, cl_idx,
488 last_rcvd, le64_to_cpu(fsd->fsd_last_transno),
489 le32_to_cpu(fcd->fcd_group));
491 GOTO(err_client, rc = -ENOMEM);
493 memcpy(&exp->exp_client_uuid.uuid, fcd->fcd_uuid,
494 sizeof exp->exp_client_uuid.uuid);
495 fed = &exp->exp_filter_data;
497 fed->fed_group = le32_to_cpu(fcd->fcd_group);
498 filter_client_add(obd, filter, fed, cl_idx);
499 /* create helper if export init gets more complex */
500 spin_lock_init(&fed->fed_lock);
503 exp->exp_connected = 0;
504 exp->exp_req_replay_needed = 1;
505 exp->exp_lock_replay_needed = 1;
506 atomic_inc(&obd->obd_req_replay_clients);
507 atomic_inc(&obd->obd_lock_replay_clients);
508 obd->obd_recoverable_clients++;
509 obd->obd_max_recoverable_clients++;
510 class_export_put(exp);
512 CDEBUG(D_OTHER, "client at idx %d has last_rcvd = "LPU64"\n",
515 if (last_rcvd > le64_to_cpu(fsd->fsd_last_transno))
516 fsd->fsd_last_transno = cpu_to_le64(last_rcvd);
520 obd->obd_last_committed = le64_to_cpu(fsd->fsd_last_transno);
522 if (obd->obd_recoverable_clients) {
523 CWARN("RECOVERY: service %s, %d recoverable clients, "
524 "last_transno "LPU64"\n", obd->obd_name,
525 obd->obd_recoverable_clients,
526 le64_to_cpu(fsd->fsd_last_transno));
527 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
528 target_start_recovery_thread(obd, ost_handle);
532 OBD_FREE(fcd, sizeof(*fcd));
535 filter->fo_mount_count = mount_count + 1;
536 fsd->fsd_mount_count = cpu_to_le64(filter->fo_mount_count);
538 /* save it, so mount count and last_transno is current */
539 rc = filter_update_server_data(obd, filp, filter->fo_fsd, 1);
541 GOTO(err_client, rc);
545 class_disconnect_exports(obd, 0);
547 filter_free_server_data(filter);
551 static int filter_cleanup_groups(struct obd_device *obd)
553 struct filter_obd *filter = &obd->u.filter;
554 struct dentry *dentry;
558 for (i = 0; i < filter->fo_group_count; i++) {
559 if (filter->fo_subdirs != NULL) {
560 for (k = 0; k < filter->fo_subdir_count; k++) {
561 dentry = filter->fo_subdirs[i].dentry[k];
565 filter->fo_subdirs[i].dentry[k] = NULL;
568 if (filter->fo_last_objid_files[i] != NULL) {
569 filp_close(filter->fo_last_objid_files[i], 0);
570 filter->fo_last_objid_files[i] = NULL;
572 if (filter->fo_groups[i] != NULL) {
573 dput(filter->fo_groups[i]);
574 filter->fo_groups[i] = NULL;
577 if (filter->fo_subdirs != NULL)
578 OBD_FREE(filter->fo_subdirs,
579 filter->fo_group_count * sizeof(*filter->fo_subdirs));
580 if (filter->fo_groups != NULL)
581 OBD_FREE(filter->fo_groups,
582 filter->fo_group_count * sizeof(*filter->fo_groups));
583 if (filter->fo_last_objids != NULL)
584 OBD_FREE(filter->fo_last_objids,
585 filter->fo_group_count * sizeof(__u64));
586 if (filter->fo_last_objid_files != NULL)
587 OBD_FREE(filter->fo_last_objid_files,
588 filter->fo_group_count * sizeof(struct file *));
589 f_dput(filter->fo_dentry_O);
593 static int filter_update_last_group(struct obd_device *obd, int group)
595 struct filter_obd *filter = &obd->u.filter;
596 struct file *filp = NULL;
597 int last_group = 0, rc;
601 if (group <= filter->fo_committed_group)
604 filp = filp_open("LAST_GROUP", O_RDWR, 0700);
608 CERROR("cannot open LAST_GROUP: rc = %d\n", rc);
612 rc = fsfilt_read_record(obd, filp, &last_group, sizeof(__u32), &off);
614 CDEBUG(D_INODE, "error reading LAST_GROUP: rc %d\n",rc);
617 LASSERT(off == 0 || last_group >= FILTER_MIN_GROUPS);
618 CDEBUG(D_INODE, "%s: previous %d, new %d\n",
619 obd->obd_name, last_group, group);
623 /* must be sync: bXXXX */
624 rc = fsfilt_write_record(obd, filp, &last_group, sizeof(__u32), &off, 1);
626 CDEBUG(D_INODE, "error updating LAST_GROUP: rc %d\n", rc);
630 filter->fo_committed_group = group;
637 static int filter_read_group_internal(struct obd_device *obd, int group,
640 struct filter_obd *filter = &obd->u.filter;
641 __u64 *new_objids = NULL;
642 struct filter_subdirs *new_subdirs = NULL, *tmp_subdirs = NULL;
643 struct dentry **new_groups = NULL;
644 struct file **new_files = NULL;
645 struct dentry *dentry;
647 int old_count = filter->fo_group_count, rc, stage = 0, i;
652 snprintf(name, 24, "%d", group);
656 dentry = ll_lookup_one_len(name, filter->fo_dentry_O,
658 if (IS_ERR(dentry)) {
659 CERROR("Cannot lookup expected object group %d: %ld\n",
660 group, PTR_ERR(dentry));
661 RETURN(PTR_ERR(dentry));
664 dentry = simple_mkdir(filter->fo_dentry_O, name, 0700, 1);
665 if (IS_ERR(dentry)) {
666 CERROR("cannot lookup/create O/%s: rc = %ld\n", name,
668 RETURN(PTR_ERR(dentry));
673 snprintf(name, 24, "O/%d/LAST_ID", group);
675 filp = filp_open(name, O_CREAT | O_RDWR, 0700);
677 CERROR("cannot create %s: rc = %ld\n", name, PTR_ERR(filp));
678 GOTO(cleanup, rc = PTR_ERR(filp));
682 rc = fsfilt_read_record(obd, filp, &last_objid, sizeof(__u64), &off);
684 CDEBUG(D_INODE, "error reading %s: rc %d\n", name, rc);
688 if (filter->fo_subdir_count) {
689 OBD_ALLOC(tmp_subdirs, sizeof(*tmp_subdirs));
690 if (tmp_subdirs == NULL)
691 GOTO(cleanup, rc = -ENOMEM);
694 for (i = 0; i < filter->fo_subdir_count; i++) {
696 snprintf(dir, sizeof(dir), "d%u", i);
698 tmp_subdirs->dentry[i] =
699 simple_mkdir(dentry, dir, 0700, 1);
700 if (IS_ERR(tmp_subdirs->dentry[i])) {
701 rc = PTR_ERR(tmp_subdirs->dentry[i]);
702 CERROR("can't lookup/create O/%d/%s: rc = %d\n",
707 /* turn on smfs plugins if any */
708 fsfilt_set_fs_flags(obd, tmp_subdirs->dentry[i]->d_inode,
711 CDEBUG(D_INODE, "got/created O/%d/%s: %p\n", group, dir,
712 tmp_subdirs->dentry[i]);
716 /* 'group' is an index; we need an array of length 'group + 1' */
717 if (group + 1 > old_count) {
719 OBD_ALLOC(new_objids, len * sizeof(*new_objids));
720 OBD_ALLOC(new_subdirs, len * sizeof(*new_subdirs));
721 OBD_ALLOC(new_groups, len * sizeof(*new_groups));
722 OBD_ALLOC(new_files, len * sizeof(*new_files));
724 if (new_objids == NULL || new_subdirs == NULL ||
725 new_groups == NULL || new_files == NULL)
726 GOTO(cleanup, rc = -ENOMEM);
728 memcpy(new_objids, filter->fo_last_objids,
729 old_count * sizeof(*new_objids));
730 memcpy(new_subdirs, filter->fo_subdirs,
731 old_count * sizeof(*new_subdirs));
732 memcpy(new_groups, filter->fo_groups,
733 old_count * sizeof(*new_groups));
734 memcpy(new_files, filter->fo_last_objid_files,
735 old_count * sizeof(*new_files));
738 OBD_FREE(filter->fo_last_objids,
739 old_count * sizeof(*new_objids));
740 OBD_FREE(filter->fo_subdirs,
741 old_count * sizeof(*new_subdirs));
742 OBD_FREE(filter->fo_groups,
743 old_count * sizeof(*new_groups));
744 OBD_FREE(filter->fo_last_objid_files,
745 old_count * sizeof(*new_files));
747 filter->fo_last_objids = new_objids;
748 filter->fo_subdirs = new_subdirs;
749 filter->fo_groups = new_groups;
750 filter->fo_last_objid_files = new_files;
751 filter->fo_group_count = len;
754 filter->fo_groups[group] = dentry;
755 filter->fo_last_objid_files[group] = filp;
756 if (filter->fo_subdir_count) {
757 filter->fo_subdirs[group] = *tmp_subdirs;
758 OBD_FREE(tmp_subdirs, sizeof(*tmp_subdirs));
761 filter_update_last_group(obd, group);
763 if (filp->f_dentry->d_inode->i_size == 0) {
764 filter->fo_last_objids[group] = FILTER_INIT_OBJID;
768 filter->fo_last_objids[group] = le64_to_cpu(last_objid);
769 CDEBUG(D_INODE, "%s: server last_objid group %d: "LPU64"\n",
770 obd->obd_name, group, last_objid);
775 if (new_objids != NULL)
776 OBD_FREE(new_objids, group * sizeof(*new_objids));
777 if (new_subdirs != NULL)
778 OBD_FREE(new_subdirs, group * sizeof(*new_subdirs));
779 if (new_groups != NULL)
780 OBD_FREE(new_groups, group * sizeof(*new_groups));
781 if (new_files != NULL)
782 OBD_FREE(new_files, group * sizeof(*new_files));
784 if (filter->fo_subdir_count) {
785 for (i = 0; i < filter->fo_subdir_count; i++) {
786 if (tmp_subdirs->dentry[i] != NULL)
787 dput(tmp_subdirs->dentry[i]);
789 OBD_FREE(tmp_subdirs, sizeof(*tmp_subdirs));
799 static int filter_read_groups(struct obd_device *obd, int last_group,
802 struct filter_obd *filter = &obd->u.filter;
803 int old_count, group, rc = 0;
805 down(&filter->fo_init_lock);
806 old_count = filter->fo_group_count;
807 for (group = old_count; group <= last_group; group++) {
809 continue; /* no group zero */
811 rc = filter_read_group_internal(obd, group, create);
815 up(&filter->fo_init_lock);
819 static int filter_prep_groups(struct obd_device *obd)
821 struct filter_obd *filter = &obd->u.filter;
822 struct dentry *dentry, *O_dentry;
823 int rc = 0, cleanup_phase = 0;
824 struct file *filp = NULL;
829 O_dentry = simple_mkdir(current->fs->pwd, "O", 0700, 1);
830 CDEBUG(D_INODE, "got/created O: %p\n", O_dentry);
831 if (IS_ERR(O_dentry)) {
832 rc = PTR_ERR(O_dentry);
833 CERROR("cannot open/create O: rc = %d\n", rc);
836 filter->fo_dentry_O = O_dentry;
837 cleanup_phase = 1; /* O_dentry */
839 /* Lookup "R" to tell if we're on an old OST FS and need to convert
840 * from O/R/<dir>/<objid> to O/0/<dir>/<objid>. This can be removed
841 * some time post 1.0 when all old-style OSTs have converted along
842 * with the init_objid hack. */
843 dentry = ll_lookup_one_len("R", O_dentry, 1);
845 GOTO(cleanup, rc = PTR_ERR(dentry));
846 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
847 struct dentry *O0_dentry = lookup_one_len("0", O_dentry, 1);
850 CWARN("converting OST to new object layout\n");
851 if (IS_ERR(O0_dentry)) {
852 rc = PTR_ERR(O0_dentry);
853 CERROR("error looking up O/0: rc %d\n", rc);
857 if (O0_dentry->d_inode) {
858 CERROR("Both O/R and O/0 exist. Fix manually.\n");
859 GOTO(cleanup_O0, rc = -EEXIST);
862 down(&O_dentry->d_inode->i_sem);
863 rc = vfs_rename(O_dentry->d_inode, dentry,
864 O_dentry->d_inode, O0_dentry);
865 up(&O_dentry->d_inode->i_sem);
868 CERROR("error renaming O/R to O/0: rc %d\n", rc);
869 GOTO(cleanup_O0, rc);
871 filter->fo_fsd->fsd_feature_incompat |=
872 cpu_to_le32(FILTER_INCOMPAT_GROUPS);
873 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
875 GOTO(cleanup_O0, rc);
887 cleanup_phase = 2; /* groups */
889 /* we have to initialize all groups before first connections from
890 * clients because they may send create/destroy for any group -bzzz */
891 filp = filp_open("LAST_GROUP", O_CREAT | O_RDWR, 0700);
893 CERROR("cannot create LAST_GROUP: rc = %ld\n", PTR_ERR(filp));
894 GOTO(cleanup, rc = PTR_ERR(filp));
896 cleanup_phase = 3; /* filp */
898 rc = fsfilt_read_record(obd, filp, &last_group, sizeof(__u32), &off);
900 CDEBUG(D_INODE, "error reading LAST_GROUP: rc %d\n", rc);
904 last_group = FILTER_MIN_GROUPS;
906 LASSERT(last_group >= FILTER_MIN_GROUPS);
909 CWARN("%s: initialize groups [%d,%d]\n", obd->obd_name,
910 FILTER_MIN_GROUPS, last_group);
911 filter->fo_committed_group = last_group;
912 rc = filter_read_groups(obd, last_group, 1);
920 switch (cleanup_phase) {
924 filter_cleanup_groups(obd);
926 f_dput(filter->fo_dentry_O);
927 filter->fo_dentry_O = NULL;
935 /* setup the object store with correct subdirectories */
936 static int filter_prep(struct obd_device *obd)
938 struct lvfs_run_ctxt saved;
939 struct filter_obd *filter = &obd->u.filter;
945 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
946 file = filp_open(LAST_RCVD, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
947 if (!file || IS_ERR(file)) {
949 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
954 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
955 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
956 file->f_dentry->d_inode->i_mode);
957 GOTO(err_filp, rc = -ENOENT);
960 /* steal operations */
961 inode = file->f_dentry->d_inode;
962 filter->fo_fop = file->f_op;
963 filter->fo_iop = inode->i_op;
964 filter->fo_aops = inode->i_mapping->a_ops;
966 rc = filter_init_server_data(obd, file);
968 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
971 filter->fo_rcvd_filp = file;
973 rc = filter_prep_groups(obd);
975 GOTO(err_server_data, rc);
978 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
983 //class_disconnect_exports(obd, 0);
984 filter_free_server_data(filter);
986 if (filp_close(file, 0))
987 CERROR("can't close %s after error\n", LAST_RCVD);
988 filter->fo_rcvd_filp = NULL;
992 /* cleanup the filter: write last used object id to status file */
993 static void filter_post(struct obd_device *obd)
995 struct lvfs_run_ctxt saved;
996 struct filter_obd *filter = &obd->u.filter;
999 /* XXX: filter_update_lastobjid used to call fsync_dev. It might be
1000 * best to start a transaction with h_sync, because we removed this
1003 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1004 rc = filter_update_server_data(obd, filter->fo_rcvd_filp,
1007 CERROR("error writing server data: rc = %d\n", rc);
1009 for (i = 1; i < filter->fo_group_count; i++) {
1010 rc = filter_update_last_objid(obd, i,
1011 (i == filter->fo_group_count - 1));
1013 CERROR("error writing group %d lastobjid: rc = %d\n",
1017 rc = filp_close(filter->fo_rcvd_filp, 0);
1018 filter->fo_rcvd_filp = NULL;
1020 CERROR("error closing %s: rc = %d\n", LAST_RCVD, rc);
1022 filter_cleanup_groups(obd);
1023 filter_free_server_data(filter);
1024 filter_free_capa_keys(filter);
1025 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1028 static void filter_set_last_id(struct filter_obd *filter, int group, obd_id id)
1030 LASSERT(filter->fo_fsd != NULL);
1032 LASSERT(group < filter->fo_group_count);
1034 spin_lock(&filter->fo_objidlock);
1035 filter->fo_last_objids[group] = id;
1036 spin_unlock(&filter->fo_objidlock);
1039 __u64 filter_last_id(struct filter_obd *filter, int group)
1042 LASSERT(filter->fo_fsd != NULL);
1044 LASSERT(group < filter->fo_group_count);
1046 spin_lock(&filter->fo_objidlock);
1047 id = filter->fo_last_objids[group];
1048 spin_unlock(&filter->fo_objidlock);
1053 static void filter_save_last_id(struct filter_obd *filter, int group, obd_id id)
1056 LASSERT(group < filter->fo_group_count);
1058 spin_lock(&filter->fo_lastidlock);
1059 if (id > filter_last_id(filter, group))
1060 filter_set_last_id(filter, group, id);
1061 spin_unlock(&filter->fo_lastidlock);
1064 /* direct cut-n-paste of mds_blocking_ast() */
1065 static int filter_blocking_ast(struct ldlm_lock *lock,
1066 struct ldlm_lock_desc *desc,
1067 void *data, int flag)
1072 if (flag == LDLM_CB_CANCELING) {
1073 /* Don't need to do anything here. */
1077 /* XXX layering violation! -phil */
1078 lock_res_and_lock(lock);
1079 /* Get this: if filter_blocking_ast is racing with ldlm_intent_policy,
1080 * such that filter_blocking_ast is called just before l_i_p takes the
1081 * ns_lock, then by the time we get the lock, we might not be the
1082 * correct blocking function anymore. So check, and return early, if
1084 if (lock->l_blocking_ast != filter_blocking_ast) {
1085 unlock_res_and_lock(lock);
1089 lock->l_flags |= LDLM_FL_CBPENDING;
1090 do_ast = (!lock->l_readers && !lock->l_writers);
1091 unlock_res_and_lock(lock);
1094 struct lustre_handle lockh;
1097 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
1098 ldlm_lock2handle(lock, &lockh);
1099 rc = ldlm_cli_cancel(&lockh);
1101 CERROR("ldlm_cli_cancel: %d\n", rc);
1103 LDLM_DEBUG(lock, "Lock still has references, will be "
1109 extern void *lock_dir(struct inode *dir, struct qstr *name);
1110 extern void unlock_dir(struct inode *dir, void *lock);
1112 static void *filter_lock_dentry(struct obd_device *obd,
1113 struct dentry *dparent,
1121 len = sprintf(name, LPU64, id);
1122 qstr_assign(&qstr, (char *)name, len);
1123 return lock_dir(dparent->d_inode, &qstr);
1125 down(&dparent->d_inode->i_sem);
1130 /* We never dget the object parent, so DON'T dput it either */
1131 static void filter_parent_unlock(struct dentry *dparent, void *lock)
1134 LASSERT(lock != NULL);
1135 unlock_dir(dparent->d_inode, lock);
1137 up(&dparent->d_inode->i_sem);
1141 /* We never dget the object parent, so DON'T dput it either */
1142 struct dentry *filter_parent(struct obd_device *obd, obd_gr group, obd_id objid)
1144 struct filter_obd *filter = &obd->u.filter;
1145 LASSERTF(group < filter->fo_group_count,
1146 "group %d < filter->fo_group_count %d failed\n",
1147 (int)group, (int)filter->fo_group_count);
1148 LASSERTF(group > 0, "group %d > 0 failed\n", (int)group);
1150 if (filter->fo_subdir_count == 0)
1151 return filter->fo_groups[group];
1153 return filter->fo_subdirs[group].dentry[objid & (filter->fo_subdir_count - 1)];
1156 /* We never dget the object parent, so DON'T dput it either */
1157 struct dentry *filter_parent_lock(struct obd_device *obd, obd_gr group,
1158 obd_id objid, void **lock)
1160 unsigned long now = jiffies;
1161 struct dentry *dparent = filter_parent(obd, group, objid);
1163 if (IS_ERR(dparent))
1167 LASSERT(dparent->d_inode);
1169 *lock = filter_lock_dentry(obd, dparent, objid);
1170 fsfilt_check_slow(now, obd_timeout, "parent lock");
1174 /* How to get files, dentries, inodes from object id's.
1176 * If dir_dentry is passed, the caller has already locked the parent
1177 * appropriately for this operation (normally a write lock). If
1178 * dir_dentry is NULL, we do a read lock while we do the lookup to
1179 * avoid races with create/destroy and such changing the directory
1180 * internal to the filesystem code. */
1181 struct dentry *filter_id2dentry(struct obd_device *obd,
1182 struct dentry *dir_dentry,
1183 obd_gr group, obd_id id)
1185 struct dentry *dparent = dir_dentry;
1186 struct dentry *dchild;
1193 CERROR("fatal: invalid object id 0\n");
1194 RETURN(ERR_PTR(-ESTALE));
1197 len = sprintf(name, LPU64, id);
1198 if (dir_dentry == NULL) {
1199 dparent = filter_parent_lock(obd, group, id, &lock);
1200 if (IS_ERR(dparent)) {
1201 CERROR("%s: error getting object "LPU64":"LPU64
1202 " parent: rc %ld\n", obd->obd_name,
1203 id, group, PTR_ERR(dparent));
1207 CDEBUG(D_INODE, "looking up object O/%.*s/%s\n",
1208 dparent->d_name.len, dparent->d_name.name, name);
1209 dchild = /*ll_*/lookup_one_len(name, dparent, len);
1210 if (dir_dentry == NULL)
1211 filter_parent_unlock(dparent, lock);
1212 if (IS_ERR(dchild)) {
1213 CERROR("%s: child lookup error %ld\n", obd->obd_name,
1218 if (dchild->d_inode != NULL && is_bad_inode(dchild->d_inode)) {
1219 CERROR("%s: got bad inode "LPU64"\n", obd->obd_name, id);
1221 RETURN(ERR_PTR(-ENOENT));
1224 CDEBUG(D_INODE, "got child objid %s: %p, count = %d\n",
1225 name, dchild, atomic_read(&dchild->d_count));
1227 LASSERT(atomic_read(&dchild->d_count) > 0);
1232 static int filter_prepare_destroy(struct obd_device *obd, obd_id objid,
1235 struct lustre_handle lockh;
1236 int flags = LDLM_AST_DISCARD_DATA, rc;
1237 struct ldlm_res_id res_id = { .name = { objid, 0, group, 0 } };
1238 ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
1241 /* Tell the clients that the object is gone now and that they should
1242 * throw away any cached pages. */
1243 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
1244 LDLM_EXTENT, &policy, LCK_PW,
1245 &flags, filter_blocking_ast, ldlm_completion_ast,
1246 NULL, NULL, NULL, 0, NULL, &lockh);
1248 /* We only care about the side-effects, just drop the lock. */
1250 ldlm_lock_decref(&lockh, LCK_PW);
1255 /* Caller must hold LCK_PW on parent and push us into kernel context.
1256 * Caller is also required to ensure that dchild->d_inode exists. */
1257 static int filter_destroy_internal(struct obd_device *obd, obd_id objid,
1258 struct dentry *dparent,
1259 struct dentry *dchild)
1261 struct inode *inode = dchild->d_inode;
1265 if (inode->i_nlink != 1 || atomic_read(&inode->i_count) != 1) {
1266 CERROR("destroying objid %.*s nlink = %lu, count = %d\n",
1267 dchild->d_name.len, dchild->d_name.name,
1268 (unsigned long)inode->i_nlink,
1269 atomic_read(&inode->i_count));
1272 rc = vfs_unlink(dparent->d_inode, dchild);
1275 CERROR("error unlinking objid %.*s: rc %d\n",
1276 dchild->d_name.len, dchild->d_name.name, rc);
1281 static int filter_intent_policy(struct ldlm_namespace *ns,
1282 struct ldlm_lock **lockp, void *req_cookie,
1283 ldlm_mode_t mode, int flags, void *data)
1285 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1286 struct ptlrpc_request *req = req_cookie;
1287 struct ldlm_lock *lock = *lockp, *l = NULL;
1288 struct ldlm_resource *res = lock->l_resource;
1289 ldlm_processing_policy policy;
1290 struct ost_lvb *res_lvb, *reply_lvb;
1291 struct ldlm_reply *rep;
1292 struct list_head *tmp;
1294 int tmpflags = 0, rc, repsize[2] = {sizeof(*rep), sizeof(*reply_lvb)};
1297 policy = ldlm_get_processing_policy(res);
1298 LASSERT(policy != NULL);
1299 LASSERT(req != NULL);
1301 rc = lustre_pack_reply(req, 2, repsize, NULL);
1303 RETURN(req->rq_status = rc);
1305 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
1306 LASSERT(rep != NULL);
1308 reply_lvb = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*reply_lvb));
1309 LASSERT(reply_lvb != NULL);
1311 //fixup_handle_for_resent_req(req, lock, &lockh);
1313 /* If we grant any lock at all, it will be a whole-file read lock.
1314 * Call the extent policy function to see if our request can be
1315 * granted, or is blocked. */
1316 lock->l_policy_data.l_extent.start = 0;
1317 lock->l_policy_data.l_extent.end = OBD_OBJECT_EOF;
1318 lock->l_req_mode = LCK_PR;
1320 lock_res_and_lock(lock);
1321 res = lock->l_resource;
1322 rc = policy(lock, &tmpflags, 0, &err, &rpc_list);
1324 /* FIXME: we should change the policy function slightly, to not make
1325 * this list at all, since we just turn around and free it */
1326 while (!list_empty(&rpc_list)) {
1327 struct ldlm_lock *wlock =
1328 list_entry(rpc_list.next, struct ldlm_lock, l_cp_ast);
1329 LASSERT((lock->l_flags & LDLM_FL_AST_SENT) == 0);
1330 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1331 lock->l_flags &= ~LDLM_FL_CP_REQD;
1332 list_del_init(&wlock->l_cp_ast);
1333 LDLM_LOCK_PUT(wlock);
1336 if (rc == LDLM_ITER_CONTINUE) {
1337 /* The lock met with no resistance; we're finished. */
1338 unlock_res_and_lock(lock);
1339 RETURN(ELDLM_LOCK_REPLACED);
1342 /* Do not grant any lock, but instead send GL callbacks. The extent
1343 * policy nicely created a list of all PW locks for us. We will choose
1344 * the highest of those which are larger than the size in the LVB, if
1345 * any, and perform a glimpse callback. */
1346 res_lvb = res->lr_lvb_data;
1347 LASSERT(res_lvb != NULL);
1348 *reply_lvb = *res_lvb;
1350 list_for_each(tmp, &res->lr_granted) {
1351 struct ldlm_lock *tmplock =
1352 list_entry(tmp, struct ldlm_lock, l_res_link);
1354 if (tmplock->l_granted_mode == LCK_PR)
1357 if (tmplock->l_policy_data.l_extent.end <= reply_lvb->lvb_size)
1361 l = LDLM_LOCK_GET(tmplock);
1365 if (l->l_policy_data.l_extent.start >
1366 tmplock->l_policy_data.l_extent.start)
1370 l = LDLM_LOCK_GET(tmplock);
1374 /* There were no PW locks beyond the size in the LVB; finished. */
1376 RETURN(ELDLM_LOCK_ABORTED);
1378 if (l->l_glimpse_ast == NULL) {
1379 /* We are racing with unlink(); just return -ENOENT */
1380 CDEBUG(D_OTHER, "race with unlink: "LPX64"/"LPX64"\n",
1381 res->lr_name.name[0], res->lr_name.name[2]);
1382 rep->lock_policy_res1 = -ENOENT;
1386 LASSERTF(l->l_glimpse_ast != NULL, "l == %p", l);
1388 rc = l->l_glimpse_ast(l, NULL); /* this will update the LVB */
1389 if (rc != 0 && res->lr_namespace->ns_lvbo &&
1390 res->lr_namespace->ns_lvbo->lvbo_update) {
1391 res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
1395 *reply_lvb = *res_lvb;
1400 RETURN(ELDLM_LOCK_ABORTED);
1403 static int filter_post_fs_cleanup(struct obd_device *obd)
1407 rc = fsfilt_post_cleanup(obd);
1413 static int filter_group_set_fs_flags(struct obd_device *obd, int group)
1415 struct filter_obd *filter = &obd->u.filter;
1419 /* zero group is not longer valid. */
1422 for (i = 0; i < filter->fo_subdir_count; i++) {
1423 struct dentry *dentry;
1424 dentry = (filter->fo_subdirs + group)->dentry[i];
1425 rc = fsfilt_set_fs_flags(obd, dentry->d_inode,
1426 SM_DO_REC | SM_DO_COW);
1434 static int filter_post_fs_setup(struct obd_device *obd)
1436 struct filter_obd *filter = &obd->u.filter;
1440 rc = fsfilt_post_setup(obd, filter->fo_dentry_O);
1445 /* mount the file system (secretly) */
1446 int filter_common_setup(struct obd_device *obd, obd_count len, void *buf,
1449 struct lustre_cfg *lcfg = buf;
1450 struct filter_obd *filter = &obd->u.filter;
1451 struct lvfs_obd_ctxt *lvfs_ctxt = NULL;
1452 struct vfsmount *mnt;
1453 struct crypto_tfm *tfm;
1459 if ((LUSTRE_CFG_BUFLEN(lcfg, 1)) < 1 ||
1460 (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1))
1463 obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
1464 if (IS_ERR(obd->obd_fsops))
1465 RETURN(PTR_ERR(obd->obd_fsops));
1467 rc = lvfs_mount_fs(lustre_cfg_string(lcfg, 1),
1468 lustre_cfg_string(lcfg, 2),
1469 option, MS_NOATIME | MS_NODIRATIME, &lvfs_ctxt);
1471 CERROR("lvfs_mount_fs failed: rc = %d\n", rc);
1476 mnt = lvfs_ctxt->loc_mnt;
1477 filter->fo_lvfs_ctxt = lvfs_ctxt;
1479 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && lustre_cfg_buf(lcfg, 3)) {
1480 str = lustre_cfg_string(lcfg, 3);
1482 obd->obd_replayable = 1;
1483 obd_sync_filter = 1;
1484 CWARN("%s: recovery enabled\n", obd->obd_name);
1487 CERROR("unrecognised flag '%c'\n",
1490 // XXX Robert? Why do we get errors here
1491 // GOTO(err_mntput, rc = -EINVAL);
1495 filter->fo_vfsmnt = mnt;
1496 filter->fo_sb = mnt->mnt_sb;
1497 filter->fo_fstype = mnt->mnt_sb->s_type->name;
1498 CDEBUG(D_SUPER, "%s: mnt = %p\n", filter->fo_fstype, mnt);
1500 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1501 obd->obd_lvfs_ctxt.pwdmnt = mnt;
1502 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1503 obd->obd_lvfs_ctxt.fs = get_ds();
1504 obd->obd_lvfs_ctxt.cb_ops = filter_lvfs_ops;
1506 ll_clear_rdonly(ll_sbdev(filter->fo_sb));
1508 rc = fsfilt_setup(obd, mnt->mnt_sb);
1510 GOTO(err_mntput, rc);
1512 sema_init(&filter->fo_init_lock, 1);
1513 filter->fo_committed_group = 0;
1515 tfm = crypto_alloc_tfm(CAPA_HMAC_ALG, 0);
1517 GOTO(err_mntput, rc = -ENOSYS);
1519 filter->fo_capa_hmac = tfm;
1520 INIT_LIST_HEAD(&filter->fo_capa_keys);
1521 spin_lock_init(&filter->fo_capa_lock);
1523 rc = filter_prep(obd);
1527 filter->fo_destroys_in_progress = 0;
1528 for (i = 0; i < 32; i++)
1529 sema_init(&filter->fo_create_locks[i], 1);
1531 spin_lock_init(&filter->fo_translock);
1532 spin_lock_init(&filter->fo_objidlock);
1533 spin_lock_init(&filter->fo_lastidlock);
1534 INIT_LIST_HEAD(&filter->fo_export_list);
1535 sema_init(&filter->fo_alloc_lock, 1);
1536 spin_lock_init(&filter->fo_r_pages.oh_lock);
1537 spin_lock_init(&filter->fo_w_pages.oh_lock);
1538 spin_lock_init(&filter->fo_r_discont_pages.oh_lock);
1539 spin_lock_init(&filter->fo_w_discont_pages.oh_lock);
1540 spin_lock_init(&filter->fo_r_discont_blocks.oh_lock);
1541 spin_lock_init(&filter->fo_w_discont_blocks.oh_lock);
1542 filter->fo_readcache_max_filesize = FILTER_MAX_CACHE_SIZE;
1544 INIT_LIST_HEAD(&filter->fo_llog_list);
1545 spin_lock_init(&filter->fo_llog_list_lock);
1547 spin_lock_init(&filter->fo_denylist_lock);
1548 INIT_LIST_HEAD(&filter->fo_denylist);
1550 sprintf(ns_name, "filter-%s", obd->obd_uuid.uuid);
1551 obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
1553 if (obd->obd_namespace == NULL)
1554 GOTO(err_post, rc = -ENOMEM);
1555 obd->obd_namespace->ns_lvbp = obd;
1556 obd->obd_namespace->ns_lvbo = &filter_lvbo;
1557 ldlm_register_intent(obd->obd_namespace, filter_intent_policy);
1559 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1560 "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1562 rc = obd_llog_cat_initialize(obd, &obd->obd_llogs, 1, CATLIST);
1564 CERROR("failed to setup llogging subsystems\n");
1572 crypto_free_tfm(filter->fo_capa_hmac);
1575 lvfs_umount_fs(filter->fo_lvfs_ctxt);
1579 fsfilt_put_ops(obd->obd_fsops);
1583 static int filter_attach(struct obd_device *obd, obd_count len, void *data)
1585 struct lprocfs_static_vars lvars;
1588 lprocfs_init_vars(filter, &lvars);
1589 rc = lprocfs_obd_attach(obd, lvars.obd_vars);
1593 rc = lprocfs_alloc_obd_stats(obd, LPROC_FILTER_LAST);
1597 /* Init obdfilter private stats here */
1598 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_READ_BYTES,
1599 LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
1600 lprocfs_counter_init(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
1601 LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
1603 return lproc_filter_attach_seqstat(obd);
1606 static int filter_detach(struct obd_device *dev)
1608 lprocfs_free_obd_stats(dev);
1609 return lprocfs_obd_detach(dev);
1612 static int filter_setup(struct obd_device *obd, obd_count len, void *buf)
1614 struct lustre_cfg *lcfg = buf;
1619 /* 2.6.9 selinux wants a full option page for do_kern_mount (bug6471) */
1620 page = get_zeroed_page(GFP_KERNEL);
1624 memcpy((void *)page, lustre_cfg_buf(lcfg, 4),
1625 LUSTRE_CFG_BUFLEN(lcfg, 4));
1627 /* all mount options including errors=remount-ro and asyncdel are passed
1628 * using 4th lcfg param. And it is good, finally we have got rid of
1629 * hardcoded fs types in the code. */
1630 rc = filter_common_setup(obd, len, buf, (void *)page);
1635 rc = filter_post_fs_setup(obd);
1639 static int filter_cleanup(struct obd_device *obd, int flags)
1641 struct filter_obd *filter = &obd->u.filter;
1642 ll_sbdev_type save_dev;
1645 if (flags & OBD_OPT_FAILOVER)
1646 CERROR("%s: shutting down for failover; client state will"
1647 " be preserved.\n", obd->obd_name);
1649 if (!list_empty(&obd->obd_exports)) {
1650 CERROR("%s: still has clients!\n", obd->obd_name);
1651 class_disconnect_exports(obd, flags);
1652 if (!list_empty(&obd->obd_exports)) {
1653 CERROR("still has exports after forced cleanup?\n");
1658 target_cleanup_recovery(obd);
1660 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
1662 if (filter->fo_sb == NULL)
1665 save_dev = ll_sbdev(filter->fo_sb);
1666 filter_post_fs_cleanup(obd);
1669 shrink_dcache_parent(filter->fo_sb->s_root);
1672 spin_lock(&filter->fo_denylist_lock);
1673 while (!list_empty(&filter->fo_denylist)) {
1674 deny_sec_t *p_deny_sec = list_entry(filter->fo_denylist.next,
1676 list_del(&p_deny_sec->list);
1677 OBD_FREE(p_deny_sec, sizeof(*p_deny_sec));
1679 spin_unlock(&filter->fo_denylist_lock);
1682 lvfs_umount_fs(filter->fo_lvfs_ctxt);
1683 //destroy_buffers(filter->fo_sb->s_dev);
1684 filter->fo_sb = NULL;
1685 fsfilt_put_ops(obd->obd_fsops);
1688 ll_clear_rdonly(save_dev);
1693 static int filter_process_config(struct obd_device *obd, obd_count len, void *buf)
1695 struct lustre_cfg *lcfg = buf;
1696 struct filter_obd *filter = &obd->u.filter;
1700 switch(lcfg->lcfg_command) {
1701 case LCFG_SET_SECURITY: {
1702 if ((LUSTRE_CFG_BUFLEN(lcfg, 1) == 0) ||
1703 (LUSTRE_CFG_BUFLEN(lcfg, 2) == 0))
1704 GOTO(out, rc = -EINVAL);
1706 if (!strcmp(lustre_cfg_string(lcfg, 1), "deny_sec")){
1707 spin_lock(&filter->fo_denylist_lock);
1708 rc = add_deny_security(lustre_cfg_string(lcfg, 2),
1709 &filter->fo_denylist);
1710 spin_unlock(&filter->fo_denylist_lock);
1712 CERROR("Unrecognized key\n");
1718 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1719 GOTO(out, rc = -EINVAL);
1727 static int filter_connect_post(struct obd_export *exp, unsigned initial,
1728 unsigned long connect_flags)
1730 struct obd_device *obd = exp->exp_obd;
1731 struct filter_export_data *fed;
1732 char str[PTL_NALFMT_SIZE];
1733 struct obd_llogs *llog;
1734 struct llog_ctxt *ctxt;
1738 fed = &exp->exp_filter_data;
1739 if (fed->fed_group < FILTER_MIN_GROUPS)
1742 /* initialize llogs for connections from MDS */
1743 llog = filter_grab_llog_for_group(obd, fed->fed_group, exp);
1744 LASSERT(llog != NULL);
1746 ctxt = llog_get_context(llog, LLOG_UNLINK_REPL_CTXT);
1747 LASSERT(ctxt != NULL);
1749 rc = llog_receptor_accept(ctxt, exp->exp_imp_reverse);
1751 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
1752 exp->exp_connection->c_peer.peer_id.nid, str);
1754 CDEBUG(D_OTHER, "%s: init llog ctxt for export "LPX64"/%s, group %d\n",
1755 obd->obd_name, exp->exp_connection->c_peer.peer_id.nid,
1756 str, fed->fed_group);
1761 /* nearly identical to mds_connect */
1762 static int filter_connect(struct lustre_handle *conn, struct obd_device *obd,
1763 struct obd_uuid *cluuid,
1764 struct obd_connect_data *data,
1765 unsigned long connect_flags)
1767 struct obd_export *exp;
1768 struct filter_export_data *fed;
1769 struct filter_client_data *fcd = NULL;
1770 struct filter_obd *filter = &obd->u.filter;
1771 struct lvfs_run_ctxt saved;
1775 if (conn == NULL || obd == NULL || cluuid == NULL)
1778 rc = class_connect(conn, obd, cluuid);
1781 exp = class_conn2export(conn);
1782 LASSERT(exp != NULL);
1784 fed = &exp->exp_filter_data;
1786 spin_lock_init(&fed->fed_lock);
1788 /* connection from MDS */
1789 group = connect_flags;
1790 if (obd->obd_replayable) {
1791 OBD_ALLOC(fcd, sizeof(*fcd));
1793 CERROR("filter: out of memory for client data\n");
1794 GOTO(cleanup, rc = -ENOMEM);
1797 memcpy(fcd->fcd_uuid, cluuid, sizeof(fcd->fcd_uuid));
1799 fed->fed_fcd->fcd_group = group;
1800 rc = filter_client_add(obd, filter, fed, -1);
1804 CWARN("%s: Received MDS connection ("LPX64"); group %d\n",
1805 obd->obd_name, exp->exp_handle.h_cookie, group);
1810 if (fed->fed_group != 0 && fed->fed_group != group) {
1811 char str[PTL_NALFMT_SIZE];
1812 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
1813 exp->exp_connection->c_peer.peer_id.nid, str);
1814 CERROR("!!! This export (nid "LPX64"/%s) used object group %d "
1815 "earlier; now it's trying to use group %d! This could "
1816 "be a bug in the MDS. Tell CFS.\n",
1817 exp->exp_connection->c_peer.peer_id.nid, str,
1818 fed->fed_group, group);
1819 GOTO(cleanup, rc = -EPROTO);
1821 fed->fed_group = group;
1823 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1824 rc = filter_read_groups(obd, group, 1);
1825 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1827 CERROR("can't read group %u\n", group);
1831 rc = filter_group_set_fs_flags(obd, group);
1833 CERROR("can't set kml flags %u\n", group);
1840 OBD_FREE(fcd, sizeof(*fcd));
1841 class_disconnect(exp, 0);
1843 class_export_put(exp);
1848 static int filter_precleanup(struct obd_device *obd, int flags)
1850 struct filter_group_llog *log;
1851 struct filter_obd *filter;
1855 filter = &obd->u.filter;
1857 spin_lock(&filter->fo_llog_list_lock);
1858 while (!list_empty(&filter->fo_llog_list)) {
1859 log = list_entry(filter->fo_llog_list.next,
1860 struct filter_group_llog, list);
1861 list_del(&log->list);
1862 spin_unlock(&filter->fo_llog_list_lock);
1864 rc = obd_llog_finish(obd, log->llogs, 0);
1866 CERROR("failed to cleanup llogging subsystem for %u\n",
1868 OBD_FREE(log->llogs, sizeof(*(log->llogs)));
1869 OBD_FREE(log, sizeof(*log));
1870 spin_lock(&filter->fo_llog_list_lock);
1872 spin_unlock(&filter->fo_llog_list_lock);
1874 rc = obd_llog_finish(obd, &obd->obd_llogs, 0);
1876 CERROR("failed to cleanup llogging subsystem\n");
1881 /* Do extra sanity checks for grant accounting. We do this at connect,
1882 * disconnect, and statfs RPC time, so it shouldn't be too bad. We can
1883 * always get rid of it or turn it off when we know accounting is good. */
1884 static void filter_grant_sanity_check(struct obd_device *obd, const char *func)
1886 struct filter_export_data *fed;
1887 struct obd_export *exp;
1888 obd_size maxsize = obd->obd_osfs.os_blocks * obd->obd_osfs.os_bsize;
1889 obd_size tot_dirty = 0, tot_pending = 0, tot_granted = 0;
1890 obd_size fo_tot_dirty, fo_tot_pending, fo_tot_granted;
1891 int level = D_CACHE;
1893 if (list_empty(&obd->obd_exports))
1896 spin_lock(&obd->obd_osfs_lock);
1897 spin_lock(&obd->obd_dev_lock);
1898 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1899 fed = &exp->exp_filter_data;
1900 if (fed->fed_grant < 0 || fed->fed_pending < 0 ||
1903 if (maxsize > 0) { /* we may not have done a statfs yet */
1904 LASSERTF(fed->fed_grant + fed->fed_pending <= maxsize,
1905 "cli %s/%p %ld+%ld > "LPU64"\n",
1906 exp->exp_client_uuid.uuid, exp,
1907 fed->fed_grant, fed->fed_pending, maxsize);
1908 LASSERTF(fed->fed_dirty <= maxsize,
1909 "cli %s/%p %ld > "LPU64"\n",
1910 exp->exp_client_uuid.uuid, exp,
1911 fed->fed_dirty, maxsize);
1913 CDEBUG(level, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
1914 obd->obd_name, exp->exp_client_uuid.uuid, exp,
1915 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1916 tot_granted += fed->fed_grant + fed->fed_pending;
1917 tot_pending += fed->fed_pending;
1918 tot_dirty += fed->fed_dirty;
1920 fo_tot_granted = obd->u.filter.fo_tot_granted;
1921 fo_tot_pending = obd->u.filter.fo_tot_pending;
1922 fo_tot_dirty = obd->u.filter.fo_tot_dirty;
1923 spin_unlock(&obd->obd_dev_lock);
1924 spin_unlock(&obd->obd_osfs_lock);
1926 /* Do these assertions outside the spinlocks so we don't kill system */
1927 if (tot_granted != fo_tot_granted)
1928 CERROR("%s: tot_granted "LPU64" != fo_tot_granted "LPU64"\n",
1929 func, tot_granted, fo_tot_granted);
1930 if (tot_pending != fo_tot_pending)
1931 CERROR("%s: tot_pending "LPU64" != fo_tot_pending "LPU64"\n",
1932 func, tot_pending, fo_tot_pending);
1933 if (tot_dirty != fo_tot_dirty)
1934 CERROR("%s: tot_dirty "LPU64" != fo_tot_dirty "LPU64"\n",
1935 func, tot_dirty, fo_tot_dirty);
1936 if (tot_pending > tot_granted)
1937 CERROR("%s: tot_pending "LPU64" > tot_granted "LPU64"\n",
1938 func, tot_pending, tot_granted);
1939 if (tot_granted > maxsize)
1940 CERROR("%s: tot_granted "LPU64" > maxsize "LPU64"\n",
1941 func, tot_granted, maxsize);
1942 if (tot_dirty > maxsize)
1943 CERROR("%s: tot_dirty "LPU64" > maxsize "LPU64"\n",
1944 func, tot_dirty, maxsize);
1947 /* Remove this client from the grant accounting totals. We also remove
1948 * the export from the obd device under the osfs and dev locks to ensure
1949 * that the filter_grant_sanity_check() calculations are always valid.
1950 * The client should do something similar when it invalidates its import. */
1951 static void filter_grant_discard(struct obd_export *exp)
1953 struct obd_device *obd = exp->exp_obd;
1954 struct filter_obd *filter = &obd->u.filter;
1955 struct filter_export_data *fed = &exp->exp_filter_data;
1956 int level = D_CACHE;
1958 spin_lock(&obd->obd_osfs_lock);
1959 spin_lock(&exp->exp_obd->obd_dev_lock);
1960 list_del_init(&exp->exp_obd_chain);
1961 spin_unlock(&exp->exp_obd->obd_dev_lock);
1963 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0)
1965 CDEBUG(level, "%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
1966 obd->obd_name, exp->exp_client_uuid.uuid, exp,
1967 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
1969 LASSERTF(filter->fo_tot_granted >= fed->fed_grant,
1970 "%s: tot_granted "LPU64" cli %s/%p fed_grant %ld\n",
1971 obd->obd_name, filter->fo_tot_granted,
1972 exp->exp_client_uuid.uuid, exp, fed->fed_grant);
1973 filter->fo_tot_granted -= fed->fed_grant;
1974 LASSERTF(filter->fo_tot_pending >= fed->fed_pending,
1975 "%s: tot_pending "LPU64" cli %s/%p fed_pending %ld\n",
1976 obd->obd_name, filter->fo_tot_pending,
1977 exp->exp_client_uuid.uuid, exp, fed->fed_pending);
1978 LASSERTF(filter->fo_tot_dirty >= fed->fed_dirty,
1979 "%s: tot_dirty "LPU64" cli %s/%p fed_dirty %ld\n",
1980 obd->obd_name, filter->fo_tot_dirty,
1981 exp->exp_client_uuid.uuid, exp, fed->fed_dirty);
1982 filter->fo_tot_dirty -= fed->fed_dirty;
1986 spin_unlock(&obd->obd_osfs_lock);
1989 static int filter_destroy_export(struct obd_export *exp)
1993 if (exp->exp_filter_data.fed_pending)
1994 CERROR("%s: cli %s/%p has %lu pending on destroyed export\n",
1995 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
1996 exp, exp->exp_filter_data.fed_pending);
1998 target_destroy_export(exp);
2000 if (exp->exp_obd->obd_replayable)
2001 filter_client_free(exp, exp->exp_flags);
2003 filter_grant_discard(exp);
2004 if (!(exp->exp_flags & OBD_OPT_FORCE))
2005 filter_grant_sanity_check(exp->exp_obd, __FUNCTION__);
2010 static void filter_sync_llogs(struct obd_device *obd, struct obd_export *dexp)
2012 struct filter_group_llog *fglog, *nlog;
2013 struct filter_obd *filter;
2014 int worked = 0, group;
2015 struct llog_ctxt *ctxt;
2018 filter = &obd->u.filter;
2020 /* we can't sync log holding spinlock. also, we do not want to get
2021 * into livelock. so we do following: loop over MDS's exports in
2022 * group order and skip already synced llogs -bzzz */
2024 /* look for group with min. number, but > worked */
2027 spin_lock(&filter->fo_llog_list_lock);
2028 list_for_each_entry(nlog, &filter->fo_llog_list, list) {
2030 if (nlog->group <= worked) {
2031 /* this group is already synced */
2035 if (group < nlog->group) {
2036 /* we have group with smaller number to sync */
2040 /* store current minimal group */
2042 group = nlog->group;
2044 spin_unlock(&filter->fo_llog_list_lock);
2049 worked = fglog->group;
2050 if (fglog->exp && (dexp == fglog->exp || dexp == NULL)) {
2051 ctxt = llog_get_context(fglog->llogs,
2052 LLOG_UNLINK_REPL_CTXT);
2053 LASSERT(ctxt != NULL);
2054 llog_sync(ctxt, fglog->exp);
2056 } while (fglog != NULL);
2059 /* also incredibly similar to mds_disconnect */
2060 static int filter_disconnect(struct obd_export *exp, unsigned long flags)
2062 struct obd_device *obd = exp->exp_obd;
2063 unsigned long irqflags;
2068 class_export_get(exp);
2070 spin_lock_irqsave(&exp->exp_lock, irqflags);
2071 exp->exp_flags = flags;
2072 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
2074 if (!(flags & OBD_OPT_FORCE))
2075 filter_grant_sanity_check(obd, __FUNCTION__);
2076 filter_grant_discard(exp);
2078 /* Disconnect early so that clients can't keep using export */
2079 rc = class_disconnect(exp, flags);
2081 ldlm_cancel_locks_for_export(exp);
2083 fsfilt_sync(obd, obd->u.filter.fo_sb);
2085 /* flush any remaining cancel messages out to the target */
2086 filter_sync_llogs(obd, exp);
2087 class_export_put(exp);
2091 struct dentry *__filter_oa2dentry(struct obd_device *obd,
2092 struct obdo *oa, const char *what)
2094 struct dentry *dchild = NULL;
2098 if (oa->o_valid & OBD_MD_FLGROUP)
2101 dchild = filter_id2dentry(obd, NULL, group, oa->o_id);
2103 if (IS_ERR(dchild)) {
2104 CERROR("%s error looking up object: "LPU64"\n",
2109 if (dchild->d_inode == NULL) {
2110 CDEBUG(D_INFO, "%s: %s on non-existent object: "
2111 LPU64"\n", obd->obd_name, what, oa->o_id);
2113 RETURN(ERR_PTR(-ENOENT));
2119 static int filter_getattr(struct obd_export *exp, struct obdo *oa,
2120 struct lov_stripe_md *md)
2122 struct dentry *dentry = NULL;
2123 struct obd_device *obd;
2127 obd = class_exp2obd(exp);
2129 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2130 exp->exp_handle.h_cookie);
2134 dentry = filter_oa2dentry(obd, oa);
2136 RETURN(PTR_ERR(dentry));
2138 /* Limit the valid bits in the return data to what we actually use */
2139 oa->o_valid = OBD_MD_FLID;
2140 obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2146 int filter_setattr_internal(struct obd_export *exp, struct dentry *dentry,
2147 struct obdo *oa, struct obd_trans_info *oti)
2149 struct filter_obd *filter;
2155 LASSERT(dentry != NULL);
2156 LASSERT(!IS_ERR(dentry));
2157 LASSERT(dentry->d_inode != NULL);
2159 filter = &exp->exp_obd->u.filter;
2160 iattr_from_obdo(&iattr, oa, oa->o_valid);
2162 if (iattr.ia_valid & ATTR_SIZE)
2163 down(&dentry->d_inode->i_sem);
2164 handle = fsfilt_start(exp->exp_obd, dentry->d_inode,
2165 FSFILT_OP_SETATTR, oti);
2167 GOTO(out_unlock, rc = PTR_ERR(handle));
2169 /* XXX this could be a rwsem instead, if filter_preprw played along */
2170 if (iattr.ia_valid & ATTR_ATTR_FLAG)
2171 rc = fsfilt_iocontrol(exp->exp_obd, dentry->d_inode,
2172 NULL, EXT3_IOC_SETFLAGS,
2173 (long)&iattr.ia_attr_flags);
2175 rc = fsfilt_setattr(exp->exp_obd, dentry, handle,
2178 rc = filter_finish_transno(exp, oti, rc);
2180 err = fsfilt_commit(exp->exp_obd, filter->fo_sb,
2181 dentry->d_inode, handle,
2184 CERROR("error on commit, err = %d\n", err);
2190 if (iattr.ia_valid & ATTR_SIZE)
2191 up(&dentry->d_inode->i_sem);
2195 /* this is called from filter_truncate() until we have filter_punch() */
2196 int filter_setattr(struct obd_export *exp, struct obdo *oa,
2197 struct lov_stripe_md *md, struct obd_trans_info *oti)
2199 struct ldlm_res_id res_id = { .name = { oa->o_id, 0, oa->o_gr, 0 } };
2200 struct ldlm_valblock_ops *ns_lvbo;
2201 struct lvfs_run_ctxt saved;
2202 struct filter_obd *filter;
2203 struct ldlm_resource *res;
2204 struct dentry *dentry;
2210 LASSERT(oti != NULL);
2212 filter = &exp->exp_obd->u.filter;
2213 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2214 /* pass nid through */
2215 current->user->nid = oti->oti_nid;
2217 /* make sure that object is allocated. */
2218 dentry = filter_crow_object(exp->exp_obd, oa);
2220 GOTO(out_pop, rc = PTR_ERR(dentry));
2224 /* setting objects attributes (including owner/group) */
2225 rc = filter_setattr_internal(exp, dentry, oa, oti);
2227 GOTO(out_unlock, rc);
2229 res = ldlm_resource_get(exp->exp_obd->obd_namespace, NULL,
2230 res_id, LDLM_EXTENT, 0);
2233 ns_lvbo = res->lr_namespace->ns_lvbo;
2234 if (ns_lvbo && ns_lvbo->lvbo_update)
2235 rc = ns_lvbo->lvbo_update(res, NULL, 0, 0);
2236 ldlm_resource_putref(res);
2239 oa->o_valid = OBD_MD_FLID;
2240 obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2247 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2251 /* XXX identical to osc_unpackmd */
2252 static int filter_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2253 struct lov_mds_md *lmm, int lmm_bytes)
2259 if (lmm_bytes < sizeof (*lmm)) {
2260 CERROR("lov_mds_md too small: %d, need %d\n",
2261 lmm_bytes, (int)sizeof(*lmm));
2264 /* XXX LOV_MAGIC etc check? */
2266 if (lmm->lmm_object_id == cpu_to_le64(0)) {
2267 CERROR("lov_mds_md: zero lmm_object_id\n");
2272 lsm_size = lov_stripe_md_size(1);
2276 if (*lsmp != NULL && lmm == NULL) {
2277 OBD_FREE(*lsmp, lsm_size);
2282 if (*lsmp == NULL) {
2283 OBD_ALLOC(*lsmp, lsm_size);
2287 loi_init((*lsmp)->lsm_oinfo);
2291 /* XXX zero *lsmp? */
2292 (*lsmp)->lsm_object_id = le64_to_cpu (lmm->lmm_object_id);
2293 LASSERT((*lsmp)->lsm_object_id);
2296 (*lsmp)->lsm_maxbytes = LUSTRE_STRIPE_MAXBYTES;
2301 static int filter_statfs(struct obd_device *obd, struct obd_statfs *osfs,
2302 unsigned long max_age)
2304 struct filter_obd *filter = &obd->u.filter;
2305 int blockbits = filter->fo_sb->s_blocksize_bits;
2309 /* at least try to account for cached pages. its still racey and
2310 * might be under-reporting if clients haven't announced their
2311 * caches with brw recently */
2312 spin_lock(&obd->obd_osfs_lock);
2313 rc = fsfilt_statfs(obd, filter->fo_sb, max_age);
2314 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
2315 spin_unlock(&obd->obd_osfs_lock);
2317 CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
2318 " pending "LPU64" free "LPU64" avail "LPU64"\n",
2319 filter->fo_tot_dirty, filter->fo_tot_granted,
2320 filter->fo_tot_pending,
2321 osfs->os_bfree << blockbits, osfs->os_bavail << blockbits);
2323 filter_grant_sanity_check(obd, __FUNCTION__);
2325 osfs->os_bavail -= min(osfs->os_bavail,
2326 (filter->fo_tot_dirty + filter->fo_tot_pending +
2327 osfs->os_bsize -1) >> blockbits);
2332 int filter_create_object(struct obd_device *obd, struct obdo *oa)
2334 struct dentry *dparent = NULL;
2335 struct dentry *dchild = NULL;
2336 struct lvfs_ucred uc = {0,};
2337 struct lvfs_run_ctxt saved;
2338 struct filter_obd *filter;
2339 int cleanup_phase = 0;
2340 int err = 0, rc = 0;
2341 void *handle = NULL;
2345 filter = &obd->u.filter;
2347 down(&filter->fo_create_locks[oa->o_gr]);
2349 if (test_bit(oa->o_gr, &filter->fo_destroys_in_progress)) {
2350 CWARN("%s: create aborted by destroy\n",
2352 GOTO(out, rc = -EALREADY);
2355 CDEBUG(D_INFO, "create objid "LPU64"\n", oa->o_id);
2357 dparent = filter_parent_lock(obd, oa->o_gr, oa->o_id, &lock);
2358 if (IS_ERR(dparent))
2359 GOTO(cleanup, rc = PTR_ERR(dparent));
2362 dchild = filter_id2dentry(obd, dparent, oa->o_gr, oa->o_id);
2364 GOTO(cleanup, rc = PTR_ERR(dchild));
2367 if (dchild->d_inode != NULL)
2368 GOTO(cleanup, rc = 0);
2370 handle = fsfilt_start_log(obd, dparent->d_inode,
2371 FSFILT_OP_CREATE, NULL, 1);
2373 GOTO(cleanup, rc = PTR_ERR(handle));
2376 uc.luc_uid = oa->o_uid;
2377 uc.luc_gid = oa->o_gid;
2378 uc.luc_fsuid = oa->o_uid;
2379 uc.luc_fsgid = oa->o_gid;
2380 uc.luc_cap = current->cap_effective;
2381 cap_raise(uc.luc_cap, CAP_SYS_RESOURCE);
2383 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2384 rc = ll_vfs_create(dparent->d_inode, dchild, S_IFREG, NULL);
2385 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2388 CERROR("create failed rc = %d\n", rc);
2392 /* nobody else is touching this newly created object */
2393 LASSERT(dchild->d_inode);
2394 if (oa->o_valid & OBD_MD_FLIFID) {
2395 rc = fsfilt_set_md(obd, dchild->d_inode, handle, obdo_id(oa),
2396 sizeof(struct lustre_id), EA_SID);
2398 CERROR("store fid in object failed! rc:%d\n", rc);
2402 CWARN("create object without lustre id!\n");
2403 portals_debug_dumpstack(NULL);
2407 fsfilt_set_fs_flags(obd, dparent->d_inode, SM_DO_REC);
2409 /* save last created object id */
2410 filter_save_last_id(filter, oa->o_gr, oa->o_id);
2412 rc = filter_update_last_objid(obd, oa->o_gr, 0);
2414 CERROR("unable to write lastobjid, but "
2415 "orphans were deleted, err = %d\n",
2420 switch(cleanup_phase) {
2422 err = fsfilt_commit(obd, filter->fo_sb,
2423 dparent->d_inode, handle, 0);
2425 CERROR("error on commit, err = %d\n", err);
2432 filter_parent_unlock(dparent, lock);
2441 up(&filter->fo_create_locks[oa->o_gr]);
2446 filter_crow_object(struct obd_device *obd, struct obdo *oa)
2448 struct dentry *dentry;
2454 /* check if object is already allocated */
2455 dentry = filter_id2dentry(obd, NULL, oa->o_gr, oa->o_id);
2459 if (dentry->d_inode)
2463 CDEBUG(D_INODE, "OSS object "LPU64"/"LPU64
2464 " does not exists - allocate it now\n",
2465 oa->o_id, oa->o_gr);
2467 uid = oa->o_valid & OBD_MD_FLUID ? oa->o_uid : 0;
2468 gid = oa->o_valid & OBD_MD_FLGID ? oa->o_gid : 0;
2470 rc = filter_create_object(obd, oa);
2472 CERROR("cannot create OSS object "LPU64"/"LPU64
2473 ", err = %d\n", oa->o_id, oa->o_gr, rc);
2474 RETURN(ERR_PTR(rc));
2477 /* lookup for just created object and return it to caller */
2478 dentry = filter_id2dentry(obd, NULL, oa->o_gr, oa->o_id);
2482 if (dentry->d_inode == NULL) {
2484 dentry = ERR_PTR(-ENOENT);
2485 CERROR("cannot find just created OSS object "
2486 LPU64"/"LPU64" err = %d\n", oa->o_id,
2487 oa->o_gr, (int)PTR_ERR(dentry));
2494 filter_clear_orphans(struct obd_export *exp, struct obdo *oa)
2496 struct obd_device *obd = NULL;
2497 struct filter_obd *filter;
2498 struct obdo *doa = NULL;
2499 int rc = 0, orphans;
2504 LASSERT(oa->o_gr != 0);
2505 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2508 filter = &obd->u.filter;
2510 last = filter_last_id(filter, oa->o_gr);
2511 orphans = last - oa->o_id;
2520 doa->o_gr = oa->o_gr;
2521 doa->o_mode = S_IFREG;
2522 doa->o_valid = oa->o_valid & (OBD_MD_FLGROUP | OBD_MD_FLID);
2524 set_bit(doa->o_gr, &filter->fo_destroys_in_progress);
2525 down(&filter->fo_create_locks[doa->o_gr]);
2526 if (!test_bit(doa->o_gr, &filter->fo_destroys_in_progress)) {
2527 CERROR("%s:["LPU64"] destroy_in_progress already cleared\n",
2528 exp->exp_obd->obd_name, doa->o_gr);
2529 up(&filter->fo_create_locks[doa->o_gr]);
2530 GOTO(out_free_doa, 0);
2533 CDEBUG(D_ERROR, "%s:["LPU64"] deleting orphan objects from "LPU64" to "
2534 LPU64"\n", exp->exp_obd->obd_name, doa->o_gr,
2535 oa->o_id + 1, last);
2537 for (id = oa->o_id + 1; id <= last; id++) {
2539 filter_destroy(exp, doa, NULL, NULL);
2542 CDEBUG(D_ERROR, "%s:["LPU64"] after destroy: set last_objids = "
2543 LPU64"\n", exp->exp_obd->obd_name, doa->o_gr, last + 1);
2545 /* return next free id to be used as a new start of sequence -bzzz */
2546 oa->o_id = last + 1;
2548 filter_set_last_id(filter, oa->o_gr, oa->o_id);
2549 clear_bit(doa->o_gr, &filter->fo_destroys_in_progress);
2550 up(&filter->fo_create_locks[oa->o_gr]);
2559 * by now this function is only needed as entry point for deleting orphanes on
2560 * OSS as objects are created on first write attempt. --umka
2563 filter_create(struct obd_export *exp, struct obdo *oa, void *acl,
2564 int acl_size, struct lov_stripe_md **ea,
2565 struct obd_trans_info *oti)
2567 struct filter_export_data *fed;
2568 struct obd_device *obd = NULL;
2569 int group = oa->o_gr, rc = 0;
2570 struct lvfs_run_ctxt saved;
2571 struct filter_obd *filter;
2572 char str[PTL_NALFMT_SIZE];
2575 LASSERT(acl == NULL && acl_size == 0);
2577 if (!(oa->o_valid & OBD_MD_FLGROUP) || group == 0) {
2578 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
2579 exp->exp_connection->c_peer.peer_id.nid, str);
2580 CERROR("!!! nid "LPX64"/%s sent invalid object group %d\n",
2581 exp->exp_connection->c_peer.peer_id.nid, str, group);
2586 fed = &exp->exp_filter_data;
2587 filter = &obd->u.filter;
2589 if (fed->fed_group != group) {
2590 portals_nid2str(exp->exp_connection->c_peer.peer_ni->pni_number,
2591 exp->exp_connection->c_peer.peer_id.nid, str);
2592 CERROR("!!! this export (nid "LPX64"/%s) used object group %d "
2593 "earlier; now it's trying to use group %d! This could "
2594 "be a bug in the MDS. Tell CFS.\n",
2595 exp->exp_connection->c_peer.peer_id.nid, str,
2596 fed->fed_group, group);
2600 CDEBUG(D_INFO, "filter_create(od->o_gr=%d,od->o_id="LPU64")\n",
2604 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2606 LASSERT((oa->o_valid & OBD_MD_FLFLAGS) &&
2607 (oa->o_flags == OBD_FL_DELORPHAN));
2609 rc = filter_clear_orphans(exp, oa);
2611 CERROR("cannot clear orphanes starting from "
2612 LPU64", err = %d\n", oa->o_id, rc);
2614 rc = filter_update_last_objid(obd, group, 0);
2616 CERROR("unable to write lastobjid, but "
2617 "orphans were deleted, err = %d\n",
2621 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2626 static int filter_destroy(struct obd_export *exp, struct obdo *oa,
2627 struct lov_stripe_md *ea, struct obd_trans_info *oti)
2629 int rc, rc2, cleanup_phase = 0, have_prepared = 0;
2630 struct dentry *dchild = NULL, *dparent = NULL;
2631 struct llog_cookie *fcc = NULL;
2632 struct lvfs_run_ctxt saved;
2633 struct filter_obd *filter;
2634 struct obd_device *obd;
2635 void *handle = NULL;
2640 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2643 filter = &obd->u.filter;
2645 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2648 dparent = filter_parent_lock(obd, oa->o_gr, oa->o_id, &lock);
2649 if (IS_ERR(dparent))
2650 GOTO(cleanup, rc = PTR_ERR(dparent));
2653 dchild = filter_id2dentry(obd, dparent, oa->o_gr, oa->o_id);
2655 GOTO(cleanup, rc = PTR_ERR(dchild));
2658 if (dchild->d_inode == NULL) {
2659 CDEBUG(D_INODE, "destroying non-existent object "LPU64"\n",
2662 GOTO(cleanup, rc = -ENOENT);
2665 if (!have_prepared) {
2666 /* If we're really going to destroy the object, get ready
2667 * by getting the clients to discard their cached data.
2669 * We have to drop the parent lock, because
2670 * filter_prepare_destroy will acquire a PW on the object, and
2671 * we don't want to deadlock with an incoming write to the
2672 * object, which has the extent PW and then wants to get the
2673 * parent dentry to do the lookup.
2675 * We dput the child because it's not worth the extra
2676 * complication of condition the above code to skip it on the
2677 * second time through. */
2679 filter_parent_unlock(dparent, lock);
2681 filter_prepare_destroy(obd, oa->o_id, oa->o_gr);
2686 /* Our MDC connection is established by the MDS to us */
2687 if (oa->o_valid & OBD_MD_FLCOOKIE) {
2688 OBD_ALLOC(fcc, sizeof(*fcc));
2690 memcpy(fcc, obdo_logcookie(oa), sizeof(*fcc));
2693 /* we're gonna truncate it first in order to avoid possible
2696 * open trasaction open transaction
2697 * down(i_zombie) down(i_zombie)
2698 * restart transaction
2699 * (see BUG 4180) -bzzz
2701 down(&dchild->d_inode->i_sem);
2702 handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_SETATTR,NULL,1);
2703 if (IS_ERR(handle)) {
2704 up(&dchild->d_inode->i_sem);
2705 GOTO(cleanup, rc = PTR_ERR(handle));
2708 iattr.ia_valid = ATTR_SIZE;
2710 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 1);
2712 rc2 = fsfilt_commit(obd, filter->fo_sb, dparent->d_inode, handle, 0);
2713 up(&dchild->d_inode->i_sem);
2717 GOTO(cleanup, rc = rc2);
2719 handle = fsfilt_start_log(obd, dparent->d_inode,FSFILT_OP_UNLINK,oti,1);
2721 GOTO(cleanup, rc = PTR_ERR(handle));
2725 rc = filter_destroy_internal(obd, oa->o_id, dparent, dchild);
2728 switch(cleanup_phase) {
2731 fsfilt_add_journal_cb(obd, filter->fo_sb, 0,
2732 oti ? oti->oti_handle : handle,
2733 filter_cancel_cookies_cb, fcc);
2735 rc = filter_finish_transno(exp, oti, rc);
2736 rc2 = fsfilt_commit(obd, filter->fo_sb, dparent->d_inode,
2737 handle, exp->exp_sync);
2739 CERROR("error on commit, err = %d\n", rc2);
2746 filter_parent_unlock(dparent, lock);
2748 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2751 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2758 /* NB start and end are used for punch, but not truncate */
2759 static int filter_truncate(struct obd_export *exp, struct obdo *oa,
2760 struct lov_stripe_md *lsm, obd_off start,
2761 obd_off end, struct obd_trans_info *oti,
2762 struct lustre_capa *capa)
2767 if (end != OBD_OBJECT_EOF)
2768 CERROR("PUNCH not supported, only truncate: end = "LPX64"\n",
2771 CDEBUG(D_INODE, "calling truncate for object "LPU64", valid = "LPU64", "
2772 "o_size = "LPD64"\n", oa->o_id, oa->o_valid, start);
2773 error = filter_verify_capa(OBD_BRW_WRITE, exp, capa);
2778 error = filter_setattr(exp, oa, NULL, oti);
2782 static int filter_sync(struct obd_export *exp, struct obdo *oa,
2783 struct lov_stripe_md *lsm, obd_off start, obd_off end)
2785 struct obd_device *obd = exp->exp_obd;
2786 struct lvfs_run_ctxt saved;
2787 struct filter_obd *filter;
2788 struct dentry *dentry;
2792 filter = &obd->u.filter;
2794 /* an objid of zero is taken to mean "sync whole filesystem" */
2795 if (!oa || !(oa->o_valid & OBD_MD_FLID)) {
2796 rc = fsfilt_sync(obd, filter->fo_sb);
2797 /* flush any remaining cancel messages out to the target */
2798 filter_sync_llogs(obd, NULL);
2802 dentry = filter_oa2dentry(obd, oa);
2804 RETURN(PTR_ERR(dentry));
2806 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2808 down(&dentry->d_inode->i_sem);
2809 rc = filemap_fdatawrite(dentry->d_inode->i_mapping);
2811 /* just any file to grab fsync method - "file" arg unused */
2812 struct file *file = filter->fo_rcvd_filp;
2814 if (file->f_op && file->f_op->fsync)
2815 rc = file->f_op->fsync(NULL, dentry, 1);
2817 rc2 = filemap_fdatawait(dentry->d_inode->i_mapping);
2821 up(&dentry->d_inode->i_sem);
2823 oa->o_valid = OBD_MD_FLID;
2824 obdo_from_inode(oa, dentry->d_inode, FILTER_VALID_FLAGS);
2826 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
2832 static int filter_set_info(struct obd_export *exp, __u32 keylen,
2833 void *key, __u32 vallen, void *val)
2835 struct obd_device *obd;
2839 obd = class_exp2obd(exp);
2841 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2842 exp->exp_handle.h_cookie);
2846 if (keylen == 8 && memcmp(key, "auditlog", 8) == 0) {
2847 rc = fsfilt_set_info(obd, obd->u.filter.fo_sb, NULL,
2848 8, "auditlog", vallen, val);
2850 } else if (keylen == 5 && strcmp(key, "audit") == 0) {
2851 /* set audit for whole FS on OSS */
2852 struct audit_attr_msg * msg = val;
2854 rc = fsfilt_set_info(obd, obd->u.filter.fo_sb, NULL,
2855 5, "audit", sizeof(msg->attr), &msg->attr);
2857 } else if (keylen == 9 && strcmp(key, "audit_obj") == 0) {
2858 struct obdo * oa = val;
2859 struct dentry * dentry;
2860 __u64 mask = oa->o_fid;
2862 dentry = filter_crow_object(obd, oa);
2864 RETURN(PTR_ERR(dentry));
2866 rc = fsfilt_set_info(obd, obd->u.filter.fo_sb, dentry->d_inode,
2867 5, "audit", sizeof(mask), &mask);
2872 } else if (keylen == 8 && memcmp(key, "capa_key", 8) == 0) {
2873 struct lustre_capa_key *lkey = val;
2874 rc = filter_update_capa_key(obd, lkey);
2879 CDEBUG(D_IOCTL, "invalid key '%s'\n", (char *)key);
2884 static int filter_get_info(struct obd_export *exp, __u32 keylen,
2885 void *key, __u32 *vallen, void *val)
2887 struct filter_export_data *fed = &exp->exp_filter_data;
2888 struct obd_device *obd;
2891 obd = class_exp2obd(exp);
2893 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2894 exp->exp_handle.h_cookie);
2898 if (keylen == strlen("blocksize") &&
2899 memcmp(key, "blocksize", keylen) == 0) {
2900 __u32 *blocksize = val;
2901 *vallen = sizeof(*blocksize);
2902 *blocksize = obd->u.filter.fo_sb->s_blocksize;
2906 if (keylen == strlen("blocksize_bits") &&
2907 memcmp(key, "blocksize_bits", keylen) == 0) {
2908 __u32 *blocksize_bits = val;
2909 *vallen = sizeof(*blocksize_bits);
2910 *blocksize_bits = obd->u.filter.fo_sb->s_blocksize_bits;
2914 if (keylen >= strlen("last_id") && memcmp(key, "last_id", 7) == 0) {
2915 obd_id *last_id = val;
2916 *last_id = filter_last_id(&obd->u.filter, fed->fed_group);
2919 if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
2920 /*Get log_context handle*/
2921 unsigned long *llh_handle = val;
2922 *vallen = sizeof(unsigned long);
2923 *llh_handle = (unsigned long)llog_get_context(&obd->obd_llogs,
2924 LLOG_REINT_ORIG_CTXT);
2927 if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
2928 unsigned long *sb = val;
2929 *vallen = sizeof(unsigned long);
2930 *sb = (unsigned long)obd->u.filter.fo_sb;
2934 CDEBUG(D_IOCTL, "invalid key\n");
2938 struct obd_llogs *filter_grab_llog_for_group(struct obd_device *obd, int group,
2939 struct obd_export *export)
2941 struct filter_group_llog *fglog, *nlog;
2942 char name[32] = "CATLIST";
2943 struct filter_obd *filter;
2944 struct llog_ctxt *ctxt;
2945 struct list_head *cur;
2948 filter = &obd->u.filter;
2950 spin_lock(&filter->fo_llog_list_lock);
2951 list_for_each(cur, &filter->fo_llog_list) {
2952 fglog = list_entry(cur, struct filter_group_llog, list);
2953 if (fglog->group == group) {
2954 if (!(fglog->exp == NULL || fglog->exp == export || export == NULL))
2955 CWARN("%s: export for group %d changes: 0x%p -> 0x%p\n",
2956 obd->obd_name, group, fglog->exp, export);
2957 spin_unlock(&filter->fo_llog_list_lock);
2961 spin_unlock(&filter->fo_llog_list_lock);
2966 OBD_ALLOC(fglog, sizeof(*fglog));
2969 fglog->group = group;
2971 OBD_ALLOC(fglog->llogs, sizeof(struct obd_llogs));
2972 if (fglog->llogs == NULL) {
2973 OBD_FREE(fglog, sizeof(*fglog));
2977 spin_lock(&filter->fo_llog_list_lock);
2978 list_for_each(cur, &filter->fo_llog_list) {
2979 nlog = list_entry(cur, struct filter_group_llog, list);
2980 LASSERT(nlog->group != group);
2982 list_add(&fglog->list, &filter->fo_llog_list);
2983 spin_unlock(&filter->fo_llog_list_lock);
2985 rc = obd_llog_cat_initialize(obd, fglog->llogs, 1, name);
2987 OBD_FREE(fglog->llogs, sizeof(*(fglog->llogs)));
2988 OBD_FREE(fglog, sizeof(*fglog));
2994 fglog->exp = export;
2995 ctxt = llog_get_context(fglog->llogs, LLOG_UNLINK_REPL_CTXT);
2996 LASSERT(ctxt != NULL);
2998 llog_receptor_accept(ctxt, export->exp_imp_reverse);
3000 CDEBUG(D_OTHER, "%s: new llog 0x%p for group %u\n",
3001 obd->obd_name, fglog->llogs, group);
3003 RETURN(fglog->llogs);
3006 int filter_iocontrol(unsigned int cmd, struct obd_export *exp,
3007 int len, void *karg, void *uarg)
3009 struct obd_device *obd = exp->exp_obd;
3010 struct obd_ioctl_data *data = karg;
3014 case OBD_IOC_ABORT_RECOVERY:
3015 target_stop_recovery_thread(obd);
3018 case OBD_IOC_SET_READONLY: {
3020 struct super_block *sb = obd->u.filter.fo_sb;
3021 struct inode *inode = sb->s_root->d_inode;
3022 BDEVNAME_DECLARE_STORAGE(tmp);
3023 CERROR("setting device %s read-only\n",
3024 ll_bdevname(sb, tmp));
3026 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
3028 (void)fsfilt_commit(obd, sb, inode, handle, 1);
3030 ll_set_rdonly(ll_sbdev(obd->u.filter.fo_sb));
3034 case OBD_IOC_CATLOGLIST: {
3035 rc = llog_catalog_list(obd, 1, data);
3038 case OBD_IOC_LLOG_CANCEL:
3039 case OBD_IOC_LLOG_REMOVE:
3040 case OBD_IOC_LLOG_INFO:
3041 case OBD_IOC_LLOG_PRINT: {
3042 /* FIXME to be finished */
3043 RETURN(-EOPNOTSUPP);
3045 struct llog_ctxt *ctxt = NULL;
3047 push_ctxt(&saved, &ctxt->loc_ctxt, NULL);
3048 rc = llog_ioctl(ctxt, cmd, data);
3049 pop_ctxt(&saved, &ctxt->loc_ctxt, NULL);
3062 static struct llog_operations filter_unlink_repl_logops;
3063 static struct llog_operations filter_size_orig_logops = {
3064 lop_setup: llog_obd_origin_setup,
3065 lop_cleanup: llog_catalog_cleanup,
3066 lop_add: llog_catalog_add,
3069 static int filter_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
3070 struct obd_device *tgt, int count,
3071 struct llog_catid *catid)
3073 struct llog_ctxt *ctxt;
3077 filter_unlink_repl_logops = llog_client_ops;
3078 filter_unlink_repl_logops.lop_cancel = llog_obd_repl_cancel;
3079 filter_unlink_repl_logops.lop_connect = llog_repl_connect;
3080 filter_unlink_repl_logops.lop_sync = llog_obd_repl_sync;
3082 rc = obd_llog_setup(obd, llogs, LLOG_UNLINK_REPL_CTXT, tgt, 0, NULL,
3083 &filter_unlink_repl_logops);
3086 /* FIXME - assign unlink_cb for filter's recovery */
3087 ctxt = llog_get_context(llogs, LLOG_UNLINK_REPL_CTXT);
3088 ctxt->llog_proc_cb = filter_recov_log_unlink_cb;
3090 filter_size_orig_logops = llog_lvfs_ops;
3092 filter_size_orig_logops.lop_setup = llog_obd_origin_setup;
3093 filter_size_orig_logops.lop_cleanup = llog_catalog_cleanup;
3094 filter_size_orig_logops.lop_add = llog_catalog_add;
3096 rc = obd_llog_setup(obd, llogs, LLOG_SIZE_ORIG_CTXT, tgt, 1,
3097 &catid->lci_logid, &filter_size_orig_logops);
3101 static int filter_llog_finish(struct obd_device *obd,
3102 struct obd_llogs *llogs, int count)
3107 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_UNLINK_REPL_CTXT));
3111 rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_SIZE_ORIG_CTXT));
3115 static int filter_llog_connect(struct obd_export *exp,
3116 struct llogd_conn_body *body)
3118 struct obd_device *obd = exp->exp_obd;
3119 struct llog_ctxt *ctxt;
3120 struct obd_llogs *llog;
3124 CDEBUG(D_OTHER, "handle connect for %s: %u/%u/%u\n", obd->obd_name,
3125 (unsigned) body->lgdc_logid.lgl_ogr,
3126 (unsigned) body->lgdc_logid.lgl_oid,
3127 (unsigned) body->lgdc_logid.lgl_ogen);
3128 llog = filter_grab_llog_for_group(obd, body->lgdc_logid.lgl_ogr, exp);
3129 LASSERT(llog != NULL);
3130 ctxt = llog_get_context(llog, body->lgdc_ctxt_idx);
3131 rc = llog_connect(ctxt, 1, &body->lgdc_logid,
3132 &body->lgdc_gen, NULL);
3134 CERROR("failed to connect\n");
3139 static struct dentry *filter_lvfs_id2dentry(__u64 id, __u32 gen,
3140 __u64 gr, void *data)
3142 return filter_id2dentry(data, NULL, gr, id);
3145 static struct lvfs_callback_ops filter_lvfs_ops = {
3146 l_id2dentry: filter_lvfs_id2dentry,
3149 static struct obd_ops filter_obd_ops = {
3150 .o_owner = THIS_MODULE,
3151 .o_attach = filter_attach,
3152 .o_detach = filter_detach,
3153 .o_set_info = filter_set_info,
3154 .o_get_info = filter_get_info,
3155 .o_setup = filter_setup,
3156 .o_precleanup = filter_precleanup,
3157 .o_cleanup = filter_cleanup,
3158 .o_process_config = filter_process_config,
3159 .o_connect = filter_connect,
3160 .o_connect_post = filter_connect_post,
3161 .o_disconnect = filter_disconnect,
3162 .o_statfs = filter_statfs,
3163 .o_getattr = filter_getattr,
3164 .o_unpackmd = filter_unpackmd,
3165 .o_create = filter_create,
3166 .o_setattr = filter_setattr,
3167 .o_destroy = filter_destroy,
3168 .o_brw = filter_brw,
3169 .o_punch = filter_truncate,
3170 .o_sync = filter_sync,
3171 .o_preprw = filter_preprw,
3172 .o_commitrw = filter_commitrw,
3173 .o_do_cow = filter_do_cow,
3174 .o_write_extents = filter_write_extents,
3175 .o_destroy_export = filter_destroy_export,
3176 .o_llog_init = filter_llog_init,
3177 .o_llog_finish = filter_llog_finish,
3178 .o_llog_connect = filter_llog_connect,
3179 .o_iocontrol = filter_iocontrol,
3182 static struct obd_ops filter_sanobd_ops = {
3183 .o_owner = THIS_MODULE,
3184 .o_attach = filter_attach,
3185 .o_detach = filter_detach,
3186 .o_get_info = filter_get_info,
3187 .o_setup = filter_san_setup,
3188 .o_precleanup = filter_precleanup,
3189 .o_cleanup = filter_cleanup,
3190 .o_connect = filter_connect,
3191 .o_connect_post = filter_connect_post,
3192 .o_disconnect = filter_disconnect,
3193 .o_statfs = filter_statfs,
3194 .o_getattr = filter_getattr,
3195 .o_unpackmd = filter_unpackmd,
3196 .o_create = filter_create,
3197 .o_setattr = filter_setattr,
3198 .o_destroy = filter_destroy,
3199 .o_brw = filter_brw,
3200 .o_punch = filter_truncate,
3201 .o_sync = filter_sync,
3202 .o_preprw = filter_preprw,
3203 .o_commitrw = filter_commitrw,
3204 .o_do_cow = filter_do_cow,
3205 .o_write_extents = filter_write_extents,
3206 .o_san_preprw = filter_san_preprw,
3207 .o_destroy_export = filter_destroy_export,
3208 .o_llog_init = filter_llog_init,
3209 .o_llog_finish = filter_llog_finish,
3210 .o_llog_connect = filter_llog_connect,
3211 .o_iocontrol = filter_iocontrol,
3214 static int __init obdfilter_init(void)
3216 struct lprocfs_static_vars lvars;
3219 printk(KERN_INFO "Lustre: Filtering OBD driver; info@clusterfs.com\n");
3221 lprocfs_init_vars(filter, &lvars);
3223 size = OBDFILTER_CREATED_SCRATCHPAD_ENTRIES *
3224 sizeof(*obdfilter_created_scratchpad);
3226 OBD_ALLOC(obdfilter_created_scratchpad, size);
3227 if (obdfilter_created_scratchpad == NULL) {
3228 CERROR ("Can't allocate scratchpad\n");
3232 rc = class_register_type(&filter_obd_ops, NULL, lvars.module_vars,
3233 OBD_FILTER_DEVICENAME);
3235 OBD_FREE(obdfilter_created_scratchpad, size);
3239 rc = class_register_type(&filter_sanobd_ops, NULL, lvars.module_vars,
3240 OBD_FILTER_SAN_DEVICENAME);
3242 class_unregister_type(OBD_FILTER_DEVICENAME);
3243 OBD_FREE(obdfilter_created_scratchpad, size);
3248 static void __exit obdfilter_exit(void)
3250 class_unregister_type(OBD_FILTER_SAN_DEVICENAME);
3251 class_unregister_type(OBD_FILTER_DEVICENAME);
3252 OBD_FREE(obdfilter_created_scratchpad,
3253 OBDFILTER_CREATED_SCRATCHPAD_ENTRIES *
3254 sizeof(*obdfilter_created_scratchpad));
3257 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
3258 MODULE_DESCRIPTION("Lustre Filtering OBD driver");
3259 MODULE_LICENSE("GPL");
3261 module_init(obdfilter_init);
3262 module_exit(obdfilter_exit);