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