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