Whamcloud - gitweb
b=7214
[fs/lustre-release.git] / lustre / smfs / audit_ost.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/smfs/audit_mds.c
5  *  Lustre filesystem audit part for MDS
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27
28 #define DEBUG_SUBSYSTEM S_SM
29
30 #include <linux/kmod.h>
31 #include <linux/init.h>
32 #include <linux/fs.h>
33 #include <linux/slab.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_log.h>
38 #include <linux/lustre_fsfilt.h>
39 #include <linux/lustre_smfs.h>
40 #include <linux/lustre_audit.h>
41 #include "smfs_internal.h"
42
43 static int audit_ost_get_id(struct inode * inode, struct lustre_id * id) 
44 {
45         struct fsfilt_operations *fsfilt = S2SMI(inode->i_sb)->sm_fsfilt;
46         
47         ENTRY;
48         if(fsfilt->fs_get_md(inode, id, sizeof(*id), EA_SID) <= 0)
49                 RETURN(-ENODATA);
50         RETURN(0);        
51 }
52
53 static int audit_ost_create_rec(struct inode * parent, void * arg,
54                                 struct audit_priv * priv, char * buffer,
55                                 __u32 * type)
56 {
57         struct hook_msg * msg = arg;
58         struct audit_record * rec = (void*)buffer;
59         char * pbuf = buffer + sizeof(*rec); 
60         struct inode * inode = msg->dentry->d_inode;
61         struct lustre_id id;
62         int len = sizeof(*rec);
63
64         if (audit_ost_get_id(inode, &id) < 0) 
65                 CERROR("Cannot get lustre id from object EA\n");
66
67         rec->opcode = AUDIT_CREATE;
68         len += audit_rec_from_id(&pbuf, &id);
69         *type = SMFS_AUDIT_GEN_REC;
70         return len;
71 }
72
73 static int audit_ost_unlink_rec(struct inode * parent, void * arg,
74                                 struct audit_priv * priv, char * buffer,
75                                 __u32 *type)
76 {
77         struct hook_unlink_msg * msg = arg;
78         struct inode * inode = msg->dentry->d_inode;
79         struct audit_record * rec = (void*)buffer;
80         char * pbuf = buffer + sizeof(*rec);
81         int len = sizeof(*rec);
82         struct lustre_id id;
83         
84         if (audit_ost_get_id(inode, &id) < 0) 
85                 CERROR("Cannot get lustre id from object EA\n");
86
87         rec->opcode = AUDIT_UNLINK;
88         len += audit_rec_from_id(&pbuf, &id);
89         //len += audit_fill_id_rec(&pbuf, parent);
90         *type = SMFS_AUDIT_GEN_REC;
91         
92         return len;        
93 }
94
95 int static audit_ost_setattr_rec(struct inode * inode, void * arg, 
96                                  struct audit_priv * priv, char * buffer,
97                                  __u32 *type)
98 {
99         //struct hook_attr_msg * msg = arg;
100         struct audit_record * rec = (void*)buffer;
101         char * pbuf = buffer + sizeof(*rec);
102         int len = sizeof(*rec);
103         struct lustre_id id;
104         
105         if (audit_ost_get_id(inode, &id) < 0) 
106                 CERROR("Cannot get lustre id from object EA\n");
107
108         rec->opcode = AUDIT_SETATTR;
109         len += audit_rec_from_id(&pbuf, &id);
110         *type = SMFS_AUDIT_GEN_REC;
111                 
112         return len;
113 }
114
115 int static audit_ost_rw_rec(struct inode * inode, void * arg, 
116                             struct audit_priv * priv, char * buffer,
117                             __u32 * type)
118 {
119         struct hook_rw_msg * msg = arg;
120         struct audit_record * rec = (void*)buffer;
121         char * pbuf = buffer + sizeof(*rec); 
122         int len = sizeof(*rec);
123         struct lustre_id id;
124
125         if (audit_ost_get_id(inode, &id) < 0) 
126                 CERROR("Cannot get lustre id from object EA\n");
127
128         rec->opcode = msg->write ? AUDIT_WRITE : AUDIT_READ;
129         len += audit_rec_from_id(&pbuf, &id);
130         *type = SMFS_AUDIT_GEN_REC;
131         
132         return len;
133 }
134
135 static audit_get_op audit_ost_record[HOOK_MAX] = {
136         [HOOK_SI_READ]      audit_ost_rw_rec,
137         [HOOK_SI_WRITE]     audit_ost_rw_rec,
138         [HOOK_READ]         audit_ost_rw_rec,
139         [HOOK_WRITE]        audit_ost_rw_rec,
140         [HOOK_CREATE]       NULL, /* audit_ost_create_rec, */
141         [HOOK_UNLINK]       audit_ost_unlink_rec,
142         [HOOK_SETATTR]      audit_ost_setattr_rec,
143         [HOOK_F_SETATTR]    audit_ost_setattr_rec
144 };
145
146 int audit_ost_setup(struct obd_device * obd, struct super_block *sb,
147                     struct audit_priv *priv) 
148 {
149         int rc;
150         struct smfs_super_info * smb = S2SMI(sb);
151         struct llog_ctxt **ctxt = &priv->audit_ctxt;
152         
153         //this will do OBD_ALLOC() for ctxt
154         rc = llog_catalog_setup(ctxt, AUDIT_OST_NAME, smb->smsi_exp,
155                                 smb->smsi_ctxt, smb->sm_fsfilt,
156                                 smb->smsi_logs_dir, smb->smsi_objects_dir);
157         
158         /* export audit llog ctxt */
159         if (*ctxt) {
160                 (*ctxt)->loc_idx = LLOG_AUDIT_ORIG_CTXT;
161                 (*ctxt)->loc_obd = obd;
162                 (*ctxt)->loc_llogs = &obd->obd_llogs;
163                 (*ctxt)->loc_llogs->llog_ctxt[LLOG_AUDIT_ORIG_CTXT] = *ctxt;
164         }
165         
166         priv->audit_get_record = audit_ost_record;
167         return 0;
168 }