Whamcloud - gitweb
b=9300
[fs/lustre-release.git] / lustre / smfs / audit_transfer.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/smfs/audit_transfer.c
5  *
6  *  Copyright (C) 2004 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
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.
13  *
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.
18  *
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.
22  */
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26
27 #define DEBUG_SUBSYSTEM S_SM
28
29 #include <linux/fs.h>
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"
40
41 struct transfer_item {
42         struct llog_handle      *ti_llh;
43         struct list_head        ti_link;
44         void * id2name;
45 };
46
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;
53
54         struct list_head        tc_list;
55         spinlock_t              tc_lock;
56 };
57
58 static struct transferd_ctl transferd_tc;
59 static DECLARE_MUTEX(transferd_sem);
60 static int transferd_users = 0;
61 char *buf = NULL;
62
63 int audit_notify(struct llog_handle *llh, void * arg)
64 {
65         struct transfer_item *ti;
66         ENTRY;
67
68         down(&transferd_sem);
69         if (transferd_users == 0) {
70                 up(&transferd_sem);
71                 RETURN(0);
72         }
73         up(&transferd_sem);
74
75         if (test_bit(TRANSFERD_STOP, &transferd_tc.tc_flags)) {
76                 CDEBUG(D_INFO, "transfer daemon stopped\n");
77                 RETURN(0);
78         }
79
80         OBD_ALLOC(ti, sizeof(*ti));
81         if (ti == NULL)
82                 RETURN(-ENOMEM);
83         
84         INIT_LIST_HEAD(&ti->ti_link);
85         ti->ti_llh = llh;
86         ti->id2name = arg;
87
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);
91
92         wake_up(&transferd_tc.tc_waitq);
93
94         RETURN(0);
95 }
96                                                                                                                                                
97 const char *opstr[AUDIT_MAX] = {
98         [AUDIT_UNKNOWN]  "unknown",
99         [AUDIT_CREATE]   "create",
100         [AUDIT_LINK]     "link",
101         [AUDIT_UNLINK]   "unlink",
102         [AUDIT_RENAME]   "rename",
103         [AUDIT_SETATTR]  "setattr",
104         [AUDIT_WRITE]    "write",
105         [AUDIT_READ]     "read",
106         [AUDIT_OPEN]     "open",
107         [AUDIT_STAT]     "stat",
108         [AUDIT_MMAP]     "mmap",
109         [AUDIT_READLINK] "readlink",
110         [AUDIT_READDIR]  "readdir",
111 };
112
113 #define construct_header(buf, size, rec, id_rec)                        \
114         snprintf(buf, size, "AUDIT:"LPX64":%u/%u:%s:%d:"DLID4":",       \
115         rec->nid, rec->uid, rec->gid, opstr[rec->opcode], (__s16)rec->result,\
116         (unsigned long)id_rec->au_fid, (unsigned long)id_rec->au_mds, \
117         (unsigned long)id_rec->au_num, (unsigned long)id_rec->au_gen);
118
119 #define REC2ID(rec, id) {                                       \
120         id_ino(id) = rec->au_num;                               \
121         id_gen(id) = rec->au_gen;                               \
122         id_type(id) = rec->au_type;                             \
123         id_fid(id) = rec->au_fid;                               \
124         id_group(id) = rec->au_mds;                             \
125 }
126
127 static int 
128 transfer_record(struct obd_device *obd, struct audit_record *rec, int type, void * data)
129 {
130         struct audit_id_record *id_rec = 
131                 (struct audit_id_record *)(rec + 1);
132         struct audit_name_record *name_rec = NULL;
133         int (*audit_id2name)(struct obd_device *obd, char **name, 
134                      int *namelen, struct lustre_id *id) = data;
135
136         int n, rc = 0;
137         ENTRY;
138
139         CDEBUG(D_INFO, "transfer %s\n", opstr[rec->opcode]);
140
141         memset(buf, 0, PAGE_SIZE);
142         n = construct_header(buf, PAGE_SIZE, rec, id_rec);
143         if (n < 0)
144                 RETURN(n);
145         /* let's use remaining space */
146         n = PAGE_SIZE - n;
147         
148         switch (rec->opcode)
149         {
150                 case AUDIT_UNLINK:
151                         if (type != SMFS_AUDIT_NAME_REC)
152                                 break;
153                 case AUDIT_LINK:
154                 case AUDIT_RENAME:
155                         id_rec++;
156                 default:
157                         break;
158         }
159         
160         if (audit_id2name) {
161                 char *name = NULL;
162                 struct lustre_id id;
163                 int namelen = 0;
164         
165                 REC2ID(id_rec, &id);
166                 
167                 //LASSERT(id_type(&id) & S_IFMT);
168                 rc = audit_id2name(obd, &name, &namelen, &id);
169                 if (rc < 0) {
170                         strncat(buf, "unknown", n);
171                         n -= strlen("unknown");
172                 } else if (namelen == 0) {
173                         //root itself
174                         if (type != SMFS_AUDIT_NAME_REC)
175                                 strcat(buf, "/");
176                 } else {
177                         strncat(buf, name, n);
178                         n -= namelen;
179                         OBD_FREE(name, namelen);
180                 } 
181         }
182
183         if (type == SMFS_AUDIT_NAME_REC && n > 0) {
184                 name_rec = (struct audit_name_record *)(++id_rec);
185                 strcat(buf, "/");
186                 n -= 1;
187                 /* get minimum size to copy name with exact len */
188                 if (n > name_rec->name_len)
189                         n = name_rec->name_len; 
190                 strncat(buf, name_rec->name, n);
191         }
192         
193         CDEBUG(D_INFO, "%s\n", buf);
194
195         printk("%s\n", buf);
196
197         RETURN(0);
198 }
199
200 static int transfer_cb(struct llog_handle *llh, struct llog_rec_hdr *rec,
201                        void *data)
202 {
203         struct obd_device *obd = llh->lgh_ctxt->loc_obd;
204         struct audit_record *ad_rec;
205         struct llog_cookie cookie;
206         ENTRY;
207         
208         if (!(le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
209                 CERROR("log is not plain\n");
210                 RETURN(-EINVAL);
211         }
212         if (rec->lrh_type != cpu_to_le32(SMFS_AUDIT_GEN_REC) &&
213             rec->lrh_type != cpu_to_le32(SMFS_AUDIT_NAME_REC)) {
214                 CERROR("log record type error\n");
215                 RETURN(-EINVAL);
216         }
217
218         ad_rec = (struct audit_record *)((char *)rec + sizeof(*rec));
219         
220         LASSERT(ad_rec->opcode < AUDIT_MAX);
221
222         cookie.lgc_lgl = llh->lgh_id;
223         cookie.lgc_subsys = LLOG_AUDIT_ORIG_CTXT;
224         cookie.lgc_index = le32_to_cpu(rec->lrh_index);
225
226         transfer_record(obd, ad_rec, rec->lrh_type, data);
227         
228         llog_cancel(llh->lgh_ctxt, 1, &cookie, 0, NULL);
229
230         RETURN(0);
231 }
232
233 static int audit_transfer(struct transfer_item *ti)
234 {
235         struct llog_handle *llh = ti->ti_llh;
236         int rc = 0;
237         ENTRY;
238
239         rc = llog_cat_process(llh, (llog_cb_t)&transfer_cb, ti->id2name);
240         if (rc)
241                 CERROR("process catalog log failed: rc(%d)\n", rc);
242
243         RETURN(0);
244 }
245
246 static int transferd_check(struct transferd_ctl *tc)
247 {
248         int rc = 0;
249         ENTRY;
250         
251         if (test_bit(TRANSFERD_STOP, &tc->tc_flags))
252                 RETURN(1);
253         
254         spin_lock(&tc->tc_lock);
255         rc = list_empty(&tc->tc_list) ? 0 : 1;
256         spin_unlock(&tc->tc_lock);
257         
258         RETURN(rc);
259 }
260                 
261 static int transferd(void *arg)
262 {
263         struct transferd_ctl *tc = arg;
264         unsigned long flags;
265         struct list_head *pos, *tmp;
266         struct transfer_item *ti = NULL;
267         ENTRY;
268
269         lock_kernel();
270         
271         /* ptlrpc_daemonize() */
272         exit_mm(current);
273         lustre_daemonize_helper();
274         exit_files(current);
275         reparent_to_init();
276         
277         SIGNAL_MASK_LOCK(current, flags);
278         sigfillset(&current->blocked);
279         RECALC_SIGPENDING;
280         SIGNAL_MASK_UNLOCK(current, flags);
281         THREAD_NAME(current->comm, sizeof(current->comm) - 1, "%s", 
282                     "audit_transferd");
283         unlock_kernel();
284
285         complete(&tc->tc_starting);
286
287         LASSERT(buf == NULL);
288         OBD_ALLOC(buf, PAGE_SIZE);
289         LASSERT(buf != NULL);
290
291         while (1) {
292                 struct l_wait_info lwi = { 0 };
293                 
294                 l_wait_event(tc->tc_waitq, transferd_check(tc), &lwi);
295                 
296                 if (test_bit(TRANSFERD_STOP, &tc->tc_flags))
297                         break;
298                 
299                 spin_lock(&tc->tc_lock);
300                 LASSERT(!list_empty(&tc->tc_list));
301                 ti = list_entry(tc->tc_list.next, struct transfer_item, ti_link);
302                 list_del_init(&ti->ti_link);
303                 spin_unlock(&tc->tc_lock);
304                 
305                 audit_transfer(ti);
306                 OBD_FREE(ti, sizeof(*ti));
307
308         }
309
310         OBD_FREE(buf, PAGE_SIZE);
311
312         spin_lock(&tc->tc_lock);
313         list_for_each_safe(pos, tmp, &tc->tc_list) {
314                 ti = list_entry(pos, struct transfer_item, ti_link);
315                 list_del_init(&ti->ti_link);
316                 OBD_FREE(ti, sizeof(*ti));
317         }
318         spin_unlock(&tc->tc_lock);
319
320         complete(&tc->tc_stopping);
321         RETURN(0);
322 }
323
324 int audit_start_transferd()
325 {
326         int rc = 0;
327         ENTRY;
328         
329         down(&transferd_sem);
330         if (++transferd_users != 1)
331                 GOTO(out, rc = 0);
332
333         memset(&transferd_tc, 0, sizeof(transferd_tc));
334         init_completion(&transferd_tc.tc_starting);
335         init_completion(&transferd_tc.tc_stopping);
336         init_waitqueue_head(&transferd_tc.tc_waitq);
337         transferd_tc.tc_flags = 0;
338         INIT_LIST_HEAD(&transferd_tc.tc_list);
339         spin_lock_init(&transferd_tc.tc_lock);
340         
341         if (kernel_thread(transferd, &transferd_tc, 0) < 0) {
342                 transferd_users--;
343                 GOTO(out, rc = -ECHILD);
344         }
345
346         wait_for_completion(&transferd_tc.tc_starting);
347 out:
348         up(&transferd_sem);
349         RETURN(rc);
350 }
351
352 int audit_stop_transferd(void)
353 {
354         int rc = 0;
355         ENTRY;
356
357         down(&transferd_sem);
358         if (--transferd_users > 0)
359                 GOTO(out, rc = 0);
360
361         set_bit(TRANSFERD_STOP, &transferd_tc.tc_flags);
362         wake_up(&transferd_tc.tc_waitq);
363         wait_for_completion(&transferd_tc.tc_stopping);
364 out:
365         up(&transferd_sem);
366         RETURN(rc);
367 }