1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/smfs/audit_transfer.c
6 * Copyright (C) 2004 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # define EXPORT_SYMTAB
27 #define DEBUG_SUBSYSTEM S_SM
30 #include <linux/slab.h>
31 #include <linux/obd_class.h>
32 #include <linux/obd_support.h>
33 #include <linux/lustre_lib.h>
34 #include <linux/lustre_idl.h>
35 #include <linux/lustre_fsfilt.h>
36 #include <linux/lustre_smfs.h>
37 #include <linux/lustre_audit.h>
38 #include <linux/lustre_log.h>
39 #include "smfs_internal.h"
41 struct transfer_item {
42 struct llog_handle *ti_llh;
43 struct list_head ti_link;
47 #define TRANSFERD_STOP 0
48 struct transferd_ctl {
49 unsigned long tc_flags;
50 wait_queue_head_t tc_waitq;
51 struct completion tc_starting;
52 struct completion tc_stopping;
54 struct list_head tc_list;
58 static struct transferd_ctl transferd_tc;
59 static DECLARE_MUTEX(transferd_sem);
60 static int transferd_users = 0;
63 int audit_notify(struct llog_handle *llh, void * arg)
65 struct transfer_item *ti;
69 if (transferd_users == 0) {
75 if (test_bit(TRANSFERD_STOP, &transferd_tc.tc_flags)) {
76 CDEBUG(D_INFO, "transfer daemon stopped\n");
80 OBD_ALLOC(ti, sizeof(*ti));
84 INIT_LIST_HEAD(&ti->ti_link);
88 spin_lock(&transferd_tc.tc_lock);
89 list_add_tail(&ti->ti_link, &transferd_tc.tc_list);
90 spin_unlock(&transferd_tc.tc_lock);
92 wake_up(&transferd_tc.tc_waitq);
97 const char *opstr[AUDIT_MAX] = {
99 [AUDIT_CREATE] "create",
101 [AUDIT_UNLINK] "unlink",
102 [AUDIT_SYMLINK] "symlink",
103 [AUDIT_RENAME] "rename",
104 [AUDIT_SETATTR] "setattr",
105 [AUDIT_WRITE] "write",
110 [AUDIT_READLINK] "readlink",
111 [AUDIT_READDIR] "readdir",
114 #define construct_header(buf, size, rec, id_rec) \
115 snprintf(buf, size, "AUDIT:"LPX64":%u/%u:%s:%d:"DLID4":", \
116 rec->nid, rec->uid, rec->gid, opstr[rec->opcode], (__s16)rec->result,\
117 (unsigned long)id_rec->au_fid, (unsigned long)id_rec->au_mds, \
118 (unsigned long)id_rec->au_num, (unsigned long)id_rec->au_gen);
120 #define REC2ID(rec, id) { \
121 id_ino(id) = rec->au_num; \
122 id_gen(id) = rec->au_gen; \
123 id_type(id) = rec->au_type; \
124 id_fid(id) = rec->au_fid; \
125 id_group(id) = rec->au_mds; \
129 transfer_record(struct obd_device *obd, struct audit_record *rec, int type, void * data)
131 struct audit_id_record *id_rec =
132 (struct audit_id_record *)((char *)rec + sizeof(*rec));
133 struct audit_name_record *name_rec = NULL;
134 int (*audit_id2name)(struct obd_device *obd, char **name,
135 int *namelen, struct lustre_id *id) = data;
140 CDEBUG(D_INFO, "transfer %s\n", opstr[rec->opcode]);
142 memset(buf, 0, PAGE_SIZE);
143 n = construct_header(buf, PAGE_SIZE, rec, id_rec);
150 if (type != SMFS_AUDIT_NAME_REC)
166 //LASSERT(id_type(&id) & S_IFMT);
167 rc = audit_id2name(obd, &name, &namelen, &id);
169 strncat(buf, "unknown", PAGE_SIZE - n);
170 n += strlen("unknown");
171 } else if (namelen == 0) {
173 if (type != SMFS_AUDIT_NAME_REC)
176 strncat(buf, name, PAGE_SIZE - n);
178 OBD_FREE(name, namelen);
182 if (type == SMFS_AUDIT_NAME_REC) {
183 name_rec = (struct audit_name_record *)((char *)(++id_rec));
184 strncat(buf, "/", 1);
186 strncat(buf, name_rec->name, PAGE_SIZE - n);
189 CDEBUG(D_INFO, "%s\n", buf);
196 static int transfer_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
199 struct obd_device *obd = llh->lgh_ctxt->loc_obd;
200 struct audit_record *ad_rec;
201 struct llog_cookie cookie;
204 if (!(le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
205 CERROR("log is not plain\n");
208 if (rec->lrh_type != cpu_to_le32(SMFS_AUDIT_GEN_REC) &&
209 rec->lrh_type != cpu_to_le32(SMFS_AUDIT_NAME_REC)) {
210 CERROR("log record type error\n");
214 ad_rec = (struct audit_record *)((char *)rec + sizeof(*rec));
216 LASSERT(ad_rec->opcode < AUDIT_MAX);
218 cookie.lgc_lgl = llh->lgh_id;
219 cookie.lgc_subsys = LLOG_AUDIT_ORIG_CTXT;
220 cookie.lgc_index = le32_to_cpu(rec->lrh_index);
222 transfer_record(obd, ad_rec, rec->lrh_type, data);
224 llog_cancel(llh->lgh_ctxt, 1, &cookie, 0, NULL);
229 static int audit_transfer(struct transfer_item *ti)
231 struct llog_handle *llh = ti->ti_llh;
235 rc = llog_cat_process(llh, (llog_cb_t)&transfer_cb, ti->id2name);
237 CERROR("process catalog log failed: rc(%d)\n", rc);
242 static int transferd_check(struct transferd_ctl *tc)
247 if (test_bit(TRANSFERD_STOP, &tc->tc_flags))
250 spin_lock(&tc->tc_lock);
251 rc = list_empty(&tc->tc_list) ? 0 : 1;
252 spin_unlock(&tc->tc_lock);
257 static int transferd(void *arg)
259 struct transferd_ctl *tc = arg;
261 struct list_head *pos, *tmp;
262 struct transfer_item *ti = NULL;
267 /* ptlrpc_daemonize() */
269 lustre_daemonize_helper();
273 SIGNAL_MASK_LOCK(current, flags);
274 sigfillset(¤t->blocked);
276 SIGNAL_MASK_UNLOCK(current, flags);
277 THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s",
281 complete(&tc->tc_starting);
283 LASSERT(buf == NULL);
284 OBD_ALLOC(buf, PAGE_SIZE);
285 LASSERT(buf != NULL);
288 struct l_wait_info lwi = { 0 };
290 l_wait_event(tc->tc_waitq, transferd_check(tc), &lwi);
292 if (test_bit(TRANSFERD_STOP, &tc->tc_flags))
295 spin_lock(&tc->tc_lock);
296 LASSERT(!list_empty(&tc->tc_list));
297 ti = list_entry(tc->tc_list.next, struct transfer_item, ti_link);
298 list_del_init(&ti->ti_link);
299 spin_unlock(&tc->tc_lock);
302 OBD_FREE(ti, sizeof(*ti));
306 OBD_FREE(buf, PAGE_SIZE);
308 spin_lock(&tc->tc_lock);
309 list_for_each_safe(pos, tmp, &tc->tc_list) {
310 ti = list_entry(pos, struct transfer_item, ti_link);
311 list_del_init(&ti->ti_link);
312 OBD_FREE(ti, sizeof(*ti));
314 spin_unlock(&tc->tc_lock);
316 complete(&tc->tc_stopping);
320 int audit_start_transferd()
325 down(&transferd_sem);
326 if (++transferd_users != 1)
329 memset(&transferd_tc, 0, sizeof(transferd_tc));
330 init_completion(&transferd_tc.tc_starting);
331 init_completion(&transferd_tc.tc_stopping);
332 init_waitqueue_head(&transferd_tc.tc_waitq);
333 transferd_tc.tc_flags = 0;
334 INIT_LIST_HEAD(&transferd_tc.tc_list);
335 spin_lock_init(&transferd_tc.tc_lock);
337 if (kernel_thread(transferd, &transferd_tc, 0) < 0) {
339 GOTO(out, rc = -ECHILD);
342 wait_for_completion(&transferd_tc.tc_starting);
348 int audit_stop_transferd(void)
353 down(&transferd_sem);
354 if (--transferd_users > 0)
357 set_bit(TRANSFERD_STOP, &transferd_tc.tc_flags);
358 wake_up(&transferd_tc.tc_waitq);
359 wait_for_completion(&transferd_tc.tc_stopping);