Whamcloud - gitweb
Branch: HEAD
[fs/lustre-release.git] / lustre / mds / mds_audit_path.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *
5  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26 #define DEBUG_SUBSYSTEM S_MDS
27
28 #include <linux/module.h>
29 #include <linux/lustre_mds.h>
30 #include <linux/lustre_dlm.h>
31 #include <linux/lustre_fsfilt.h>
32 #include <linux/init.h>
33 #include <linux/obd_class.h>
34 #include <linux/fs.h>
35 #include <linux/namei.h>
36 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
37 # include <linux/smp_lock.h>
38 # include <linux/buffer_head.h>
39 # include <linux/workqueue.h>
40 # include <linux/mount.h>
41 #else
42 # include <linux/locks.h>
43 #endif
44 #include <linux/lustre_audit.h>
45 #include "mds_internal.h"
46
47
48 #define PP_FILE         1
49 #define PP_DIR          2
50 #define PP_SPLIT_MASTER 3
51 #define PP_SPLIT_SLAVE  4
52 #define PP_CROSS_DIR    5
53 #define PP_AUDIT_LOG    6       /* search id in audit log */
54
55 struct scan_dir_data {
56         int               rc;
57         struct lustre_id *id;
58         char             *name;
59 };
60
61 static int filldir(void *__buf, const char *name, int namlen,
62                    loff_t offset, ino_t ino, unsigned int d_type)
63 {
64         struct scan_dir_data *sd = __buf;
65         ENTRY;
66
67         if (name[0] == '.' &&
68             (namlen == 1 || (namlen == 2 && name[1] == '.'))) {
69                 /* skip special entries */
70                 RETURN(0);
71         }
72
73         LASSERT(sd != NULL);
74
75         if (ino == id_ino(sd->id)) {
76                 strncpy(sd->name, name, namlen);
77                 sd->rc = 0;
78                 RETURN(-EINTR); /* break the readdir loop */
79         }
80         RETURN(0);
81 }
82
83 int 
84 scan_name_in_parent(struct lustre_id *pid, struct lustre_id *id, char *name)
85 {
86         struct file * file;
87         char *pname;
88         struct scan_dir_data sd;
89         int len, rc = 0;
90         ENTRY;
91
92         len = strlen("__iopen__/") + 10 + 1;
93         OBD_ALLOC(pname, len);
94         if (!pname)
95                 RETURN(-ENOMEM);
96         
97         sprintf(pname, "__iopen__/0x%llx", id_ino(pid));
98
99         file = filp_open(pname, O_RDONLY, 0);
100         if (IS_ERR(file)) {
101                 CERROR("can't open directory %s: %d\n",
102                        pname, (int) PTR_ERR(file));
103                 GOTO(out, rc = PTR_ERR(file));
104         }
105         
106         sd.id = id;
107         sd.name = name;
108         sd.rc = -ENOENT;
109         vfs_readdir(file, filldir, &sd);
110
111         filp_close(file, 0);
112         rc = sd.rc;
113
114 out:
115         OBD_FREE(pname, len);
116         RETURN(rc);
117
118 }
119
120 /* id2pid - given id, get parent id or master id.
121  * @obd:   obd device
122  * @id:    child id to be parsed
123  * @pid:   parent id or master id
124  * @type:  id type
125  */
126 static int 
127 id2pid(struct obd_device *obd, struct lustre_id *id, struct lustre_id *pid, 
128        __u32 *type)
129 {
130         struct dentry *dentry = NULL;
131         struct inode *inode = NULL;
132         struct mea *mea = NULL;
133         int mea_size, rc = 0;
134         ENTRY;
135         
136         dentry = mds_id2dentry(obd, id, NULL);
137         if (IS_ERR(dentry) || !dentry->d_inode) {
138                 CERROR("can't find inode "LPU64"\n", id_ino(id));
139                 if (!IS_ERR(dentry)) l_dput(dentry);
140                 RETURN(-ENOENT);
141         }
142         inode = dentry->d_inode;
143
144         if (S_ISDIR(inode->i_mode)) {
145                 //LASSERT(S_ISDIR(id_type(id)));
146                 rc = mds_md_get_attr(obd, inode, &mea, &mea_size);
147                 if (rc)
148                         GOTO(out, rc);
149                 
150                 if (!mea) {
151                         *type = PP_DIR;
152                         goto read_pid;
153                 } else if (mea && mea->mea_count) {
154                         *type = PP_SPLIT_MASTER;
155                         goto read_pid;
156                 } else {
157                         *type = PP_SPLIT_SLAVE;
158                         *pid = mea->mea_ids[mea->mea_master];
159                 }
160                                 
161         } else {
162                 //LASSERT(!S_ISDIR(id_type(id)));
163                 *type = PP_FILE;
164 read_pid:
165                 rc = mds_read_inode_pid(obd, inode, pid);
166                 if (rc) {
167                         CERROR("can't read parent ino(%lu) rc(%d).\n",
168                                inode->i_ino, rc);
169                         GOTO(out, rc);
170                 }
171         }
172
173         /* Well, if it's dir or master split, we have to check if it's 
174          * a cross-ref dir */
175         if ((*type == PP_DIR || *type == PP_SPLIT_MASTER) &&
176              id_group(id) != id_group(pid))
177                 *type = PP_CROSS_DIR;
178 out:
179         if (mea)
180                 OBD_FREE(mea, mea_size);
181         l_dput(dentry);
182         RETURN(rc);
183 }
184
185 static int local_parse_id(struct obd_device *obd, struct parseid_pkg *pkg)
186 {
187         struct lvfs_run_ctxt saved;
188         int rc = 0;
189         ENTRY;
190
191         pkg->pp_rc = 0;
192         pkg->pp_type = 0;
193         memset(pkg->pp_name, 0, sizeof(pkg->pp_name));
194
195         LASSERT(obd->u.mds.mds_num == id_group(&pkg->pp_id1));
196
197         /* pp_id2 is present, which indicating we want to scan parent 
198          * dir(pp_id2) to find the cross-ref entry(pp_id1) */
199         if (id_fid(&pkg->pp_id2)) {
200                 /* 
201                 LASSERT(S_ISDIR(id_type(&pkg->pp_id1)));
202                 LASSERT(S_ISDIR(id_type(&pkg->pp_id2)));
203                 */
204                 pkg->pp_type = PP_DIR;
205                 goto scan;
206         }
207
208         rc = id2pid(obd, &pkg->pp_id1, &pkg->pp_id2, &pkg->pp_type);
209         if (rc)
210                 GOTO(out, rc);
211 scan:
212         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
213         
214         switch (pkg->pp_type) {
215         case PP_FILE:
216         case PP_DIR:
217         case PP_SPLIT_MASTER:
218                 rc = scan_name_in_parent(&pkg->pp_id2, &pkg->pp_id1,
219                                          pkg->pp_name);
220                 if (rc) 
221                         CERROR("scan "LPU64" in parent failed. rc=%d\n",
222                                id_ino(&pkg->pp_id1), rc);
223                 break;
224         case PP_SPLIT_SLAVE:
225         case PP_CROSS_DIR:
226                 break;
227         default:
228                 CERROR("invalid id\n");
229                 break;
230         }
231
232         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
233 out:
234         pkg->pp_rc = rc;
235         RETURN(rc);
236 }
237
238 static int 
239 local_scan_audit_log(struct obd_device *obd, struct parseid_pkg *pkg);
240
241 int mds_parse_id(struct ptlrpc_request *req)
242 {
243         struct parseid_pkg *pkg, *reppkg;
244         struct obd_device *obd = req->rq_export->exp_obd;
245         int rc = 0, size = sizeof(*reppkg);
246         ENTRY;
247
248         pkg = lustre_swab_reqbuf(req, 0, sizeof(*pkg), 
249                                  lustre_swab_parseid_pkg);
250         if (pkg == NULL)
251                 RETURN(-EPROTO);
252
253         rc = lustre_pack_reply(req, 1, &size, NULL);
254         if (rc)
255                 RETURN(rc);
256         
257         reppkg = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reppkg));
258         memcpy(reppkg, pkg, sizeof(*reppkg));
259
260         if (reppkg->pp_type == PP_AUDIT_LOG)
261                 rc = local_scan_audit_log(obd, reppkg);
262         else
263                 rc = local_parse_id(obd, reppkg);
264         
265         if (rc)
266                 CERROR("local parseid failed. (rc:%d)\n", rc);
267         RETURN(0);  /* we do need pack reply here */
268 }
269
270 static int parse_id(struct obd_device *obd, struct parseid_pkg *pkg)
271 {
272         int rc = 0;
273         int mds_num = id_group(&pkg->pp_id1);
274         ENTRY;
275         
276         LASSERT(mds_num >= 0);
277
278         if (mds_num == obd->u.mds.mds_num) {
279                 rc = local_parse_id(obd, pkg);
280         } else {
281                 struct ptlrpc_request *req;
282                 struct lmv_obd *lmv = &obd->u.mds.mds_md_obd->u.lmv;
283                 struct parseid_pkg *body;
284                 int size = sizeof(*body);
285                 struct obd_export *exp;
286                 
287                 /* make sure connection established */
288                 rc = obd_set_info(obd->u.mds.mds_md_exp, strlen("chkconnect"),
289                                   "chkconnect", 0, NULL);
290                 if (rc)
291                         RETURN(rc);
292
293                 exp = lmv->tgts[mds_num].ltd_exp;
294                 LASSERT(exp);
295
296                 req = ptlrpc_prep_req(class_exp2cliimp(exp), 
297                                       LUSTRE_MDS_VERSION, MDS_PARSE_ID, 1, 
298                                       &size, NULL);
299                 if (!req)
300                         RETURN(-ENOMEM);
301
302                 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
303                 memcpy(body, pkg, sizeof(*body));
304                 
305                 req->rq_replen = lustre_msg_size(1, &size);
306
307                 rc = ptlrpc_queue_wait(req);
308                 if (rc)
309                         GOTO(out, rc);
310
311                 body = lustre_swab_repbuf(req, 0, sizeof(*body), 
312                                           lustre_swab_parseid_pkg);
313                 if (body == NULL) {
314                         CERROR("can't unpack parseid_pkg\n");
315                         GOTO(out, rc = -EPROTO);
316                 }
317                 memcpy(pkg, body, sizeof(*pkg));
318 out:
319                 ptlrpc_req_finished(req);
320         }
321         RETURN(rc);
322 }
323
324 #define ROOT_FID        2
325 struct name_item {
326         struct list_head link;
327         char             name[NAME_MAX + 1];
328 };
329
330 int 
331 mds_id2name(struct obd_device *obd, struct lustre_id *id, 
332             struct list_head *list, struct lustre_id *lastid)
333 {
334         struct name_item *item;
335         struct parseid_pkg *pkg;
336         int rc = 0;
337         ENTRY;
338
339         OBD_ALLOC(pkg, sizeof(*pkg));
340         if (pkg == NULL)
341                 RETURN(-ENOMEM);
342
343         pkg->pp_id1 = *id;
344         while (id_fid(&pkg->pp_id1) != ROOT_FID) {
345                 
346                 rc = parse_id(obd, pkg);
347                 if (rc) {
348                         CDEBUG(D_SEC, "parse id failed. rc=%d\n", rc);
349                         *lastid = pkg->pp_id1;
350                         break;
351                 }
352
353                 switch (pkg->pp_type) {
354                 case PP_FILE:
355                 case PP_DIR:
356                 case PP_SPLIT_MASTER:
357                         OBD_ALLOC(item, sizeof(*item));
358                         if (item == NULL)
359                                 GOTO(out, rc = -ENOMEM);
360                         
361                         INIT_LIST_HEAD(&item->link);
362                         list_add(&item->link, list);
363                         memcpy(item->name, pkg->pp_name, sizeof(item->name));
364                         
365                 case PP_SPLIT_SLAVE:
366                         pkg->pp_id1 = pkg->pp_id2;
367                         memset(&pkg->pp_id2, 0, sizeof(struct lustre_id));
368                         break;
369                 case PP_CROSS_DIR:
370                         break;
371                 default:
372                         LBUG();
373                         break;
374                 }
375                 
376         }
377 out:
378         OBD_FREE(pkg, sizeof(*pkg));
379         RETURN(rc);
380 }
381
382 static int
383 scan_audit_log_cb(struct llog_handle *llh, struct llog_rec_hdr *rec, void *data)
384 {
385         struct parseid_pkg *pkg = (struct parseid_pkg *)data;
386         struct audit_record *ad_rec; 
387         struct audit_id_record *cid_rec, *pid_rec;
388         struct audit_name_record *nm_rec;
389         ENTRY;
390
391         if (!(le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
392                 CERROR("log is not plain\n");
393                 RETURN(-EINVAL);
394         }
395         if (rec->lrh_type != SMFS_AUDIT_NAME_REC &&
396             rec->lrh_type != LLOG_GEN_REC) {
397                 RETURN(0);
398         }
399
400         ad_rec = (struct audit_record *)((char *)rec + sizeof(*rec));
401
402         if (ad_rec->result || 
403             ad_rec->opcode != AUDIT_UNLINK ||
404             ad_rec->opcode != AUDIT_RENAME)
405                 RETURN(0);
406
407         cid_rec = (struct audit_id_record *)((char *)ad_rec + sizeof(*ad_rec));
408         pid_rec = cid_rec + 1;
409         nm_rec = (struct audit_name_record *)
410                 ((char *)pid_rec + sizeof(*pid_rec));
411         
412         if (cid_rec->au_num == id_ino(&pkg->pp_id1) &&
413             cid_rec->au_gen == id_gen(&pkg->pp_id1)) {
414                 /* get parent id */
415                 id_ino(&pkg->pp_id2) = pid_rec->au_num;
416                 id_gen(&pkg->pp_id2) = pid_rec->au_gen;
417                 id_type(&pkg->pp_id2) = pid_rec->au_type;
418                 id_fid(&pkg->pp_id2) = pid_rec->au_fid;
419                 id_group(&pkg->pp_id2) = pid_rec->au_mds;
420                 /* get name */
421                 memcpy(pkg->pp_name, nm_rec->name, 
422                        le32_to_cpu(nm_rec->name_len));
423
424                 RETURN(LLOG_PROC_BREAK);
425         }
426         RETURN(0);
427 }
428
429 static int
430 local_scan_audit_log(struct obd_device *obd, struct parseid_pkg *pkg)
431 {
432         struct llog_handle *llh = NULL;
433         struct llog_ctxt *ctxt = llog_get_context(&obd->obd_llogs,
434                                                   LLOG_AUDIT_ORIG_CTXT);
435         int rc = 0;
436         ENTRY;
437
438         if (ctxt)
439                 llh = ctxt->loc_handle;
440
441         if (llh == NULL)
442                 RETURN(-ENOENT);
443
444         rc = llog_cat_process(llh, (llog_cb_t)&scan_audit_log_cb, (void *)pkg);
445         if (rc != LLOG_PROC_BREAK) {
446                 CWARN("process catalog log failed: rc(%d)\n", rc);
447                 RETURN(-ENOENT);
448         }
449         RETURN(0);
450 }
451
452 static int 
453 scan_audit_log(struct obd_device *obd, struct lustre_id *cur_id, 
454                struct list_head *list, struct lustre_id *parent_id)
455 {
456         struct name_item *item = NULL;
457         int rc = 0, mds_num = id_group(cur_id);
458         struct parseid_pkg *pkg = NULL;
459         ENTRY;
460
461         OBD_ALLOC(pkg, sizeof(*pkg));
462         if (pkg == NULL)
463                 RETURN(-ENOMEM);
464
465         pkg->pp_type = PP_AUDIT_LOG;
466         pkg->pp_id1 = *cur_id;
467
468         if (obd->u.mds.mds_num == mds_num) {
469                 rc = local_scan_audit_log(obd, pkg);
470         } else {
471                 struct ptlrpc_request *req;
472                 struct lmv_obd *lmv = &obd->u.mds.mds_md_obd->u.lmv;
473                 struct parseid_pkg *body;
474                 int size = sizeof(*body);
475                 struct obd_export *exp;
476                 
477                 /* make sure connection established */
478                 rc = obd_set_info(obd->u.mds.mds_md_exp, strlen("chkconnect"),
479                                   "chkconnect", 0, NULL);
480                 if (rc)
481                         RETURN(rc);
482
483                 exp = lmv->tgts[mds_num].ltd_exp;
484                 LASSERT(exp);
485
486                 req = ptlrpc_prep_req(class_exp2cliimp(exp), 
487                                       LUSTRE_MDS_VERSION, MDS_PARSE_ID, 1, 
488                                       &size, NULL);
489                 if (!req)
490                         RETURN(-ENOMEM);
491
492                 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
493                 memcpy(body, pkg, sizeof(*body));
494                 
495                 req->rq_replen = lustre_msg_size(1, &size);
496
497                 rc = ptlrpc_queue_wait(req);
498                 if (rc)
499                         GOTO(out_req, rc);
500
501                 body = lustre_swab_repbuf(req, 0, sizeof(*body), 
502                                           lustre_swab_parseid_pkg);
503                 if (body == NULL) {
504                         CERROR("can't unpack parseid_pkg\n");
505                         GOTO(out, rc = -EPROTO);
506                 }
507                 memcpy(pkg, body, sizeof(*pkg));
508 out_req:
509                 ptlrpc_req_finished(req);
510
511         }
512
513         if (!rc) rc = pkg->pp_rc;
514         if (rc)
515                 GOTO(out, rc);
516         
517         *parent_id = pkg->pp_id2;
518
519         OBD_ALLOC(item, sizeof(*item));
520         if (item == NULL)
521                 GOTO(out, rc = -ENOMEM);
522         
523         INIT_LIST_HEAD(&item->link);
524         list_add(&item->link, list);
525         memcpy(item->name, pkg->pp_name, sizeof(item->name));
526 out:
527         OBD_FREE(pkg, sizeof(*pkg));
528         RETURN(rc);
529 }
530        
531 int 
532 mds_audit_id2name(struct obd_device *obd, char **name, int *namelen, 
533                   struct lustre_id *id)
534 {
535         int rc = 0;
536         struct list_head list, *pos, *n;
537         struct name_item *item;
538         struct lustre_id parent_id, cur_id;
539         ENTRY;
540
541         *namelen = 0;
542         INIT_LIST_HEAD(&list);
543
544         cur_id = *id;
545         if (id_fid(&cur_id) == ROOT_FID)
546                 RETURN(0);
547 next:
548         memset(&parent_id, 0, sizeof(parent_id));
549
550         rc = mds_id2name(obd, &cur_id, &list, &parent_id);
551         if (rc == -ENOENT) {
552                 /* can't reconstruct name from id, turn to audit log */
553                 LASSERT(id_fid(&parent_id));
554                 cur_id = parent_id;
555                 memset(&parent_id, 0, sizeof(parent_id));
556
557                 rc = scan_audit_log(obd, &cur_id, &list, &parent_id);
558                 if (rc) {
559                         CERROR("scan id in audit log failed. (rc:%d)\n", rc);
560                         GOTO(out, rc);
561                 }
562
563                 LASSERT(id_fid(&parent_id));
564                 cur_id = parent_id;
565                 goto next;
566
567         } else if (rc) {
568                 CERROR("reconstruct name from id failed. (rc:%d)\n", rc);
569                 GOTO(out, rc);
570         }
571         
572         list_for_each_safe (pos, n, &list) {
573                 item = list_entry(pos, struct name_item, link);
574                 *namelen += strlen(item->name) + 1;
575         }
576         OBD_ALLOC(*name, *namelen);
577         if (*name == NULL)
578                 rc = -ENOMEM;
579 out:
580         list_for_each_safe (pos, n, &list) {
581                 item = list_entry(pos, struct name_item, link);
582                 if (!rc) {
583                         strcat(*name, "/");
584                         strcat(*name, item->name);
585                 }
586                 list_del_init(&item->link);
587                 OBD_FREE(item, sizeof(*item));
588         }
589         RETURN(rc);
590 }
591 EXPORT_SYMBOL(mds_audit_id2name);