1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
7 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
24 # define EXPORT_SYMTAB
26 #define DEBUG_SUBSYSTEM S_MDS
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>
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>
42 # include <linux/locks.h>
44 #include <linux/lustre_audit.h>
45 #include "mds_internal.h"
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 */
55 struct scan_dir_data {
61 static int filldir(void *__buf, const char *name, int namlen,
62 loff_t offset, ino_t ino, unsigned int d_type)
64 struct scan_dir_data *sd = __buf;
68 (namlen == 1 || (namlen == 2 && name[1] == '.'))) {
69 /* skip special entries */
75 if (ino == id_ino(sd->id)) {
76 strncpy(sd->name, name, namlen);
78 RETURN(-EINTR); /* break the readdir loop */
84 scan_name_in_parent(struct lustre_id *pid, struct lustre_id *id, char *name)
88 struct scan_dir_data sd;
92 len = strlen("__iopen__/") + 10 + 1;
93 OBD_ALLOC(pname, len);
97 sprintf(pname, "__iopen__/0x%llx", id_ino(pid));
99 file = filp_open(pname, O_RDONLY, 0);
101 CERROR("can't open directory %s: %d\n",
102 pname, (int) PTR_ERR(file));
103 GOTO(out, rc = PTR_ERR(file));
109 vfs_readdir(file, filldir, &sd);
115 OBD_FREE(pname, len);
120 /* id2pid - given id, get parent id or master id.
122 * @id: child id to be parsed
123 * @pid: parent id or master id
127 id2pid(struct obd_device *obd, struct lustre_id *id, struct lustre_id *pid,
130 struct dentry *dentry = NULL;
131 struct inode *inode = NULL;
132 struct mea *mea = NULL;
133 int mea_size, rc = 0;
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);
142 inode = dentry->d_inode;
144 if (S_ISDIR(id_type(id))) {
145 LASSERT(S_ISDIR(inode->i_mode));
146 rc = mds_md_get_attr(obd, inode, &mea, &mea_size);
153 } else if (mea && mea->mea_count) {
154 *type = PP_SPLIT_MASTER;
157 *type = PP_SPLIT_SLAVE;
158 *pid = mea->mea_ids[mea->mea_master];
162 LASSERT(!S_ISDIR(inode->i_mode));
165 rc = mds_read_inode_pid(obd, inode, pid);
167 CERROR("can't read parent ino(%lu) rc(%d).\n",
173 /* Well, if it's dir or master split, we have to check if it's
175 if ((*type == PP_DIR || *type == PP_SPLIT_MASTER) &&
176 id_group(id) != id_group(pid))
177 *type = PP_CROSS_DIR;
180 OBD_FREE(mea, mea_size);
185 static int local_parse_id(struct obd_device *obd, struct parseid_pkg *pkg)
187 struct lvfs_run_ctxt saved;
193 memset(pkg->pp_name, 0, sizeof(pkg->pp_name));
195 LASSERT(obd->u.mds.mds_num == id_group(&pkg->pp_id1));
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 LASSERT(S_ISDIR(id_type(&pkg->pp_id1)));
201 LASSERT(S_ISDIR(id_type(&pkg->pp_id2)));
203 pkg->pp_type = PP_DIR;
207 rc = id2pid(obd, &pkg->pp_id1, &pkg->pp_id2, &pkg->pp_type);
211 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
213 switch (pkg->pp_type) {
216 case PP_SPLIT_MASTER:
217 rc = scan_name_in_parent(&pkg->pp_id2, &pkg->pp_id1,
220 CERROR("scan "LPU64" in parent failed. rc=%d\n",
221 id_ino(&pkg->pp_id1), rc);
227 CERROR("invalid id\n");
231 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
238 local_scan_audit_log(struct obd_device *obd, struct parseid_pkg *pkg);
240 int mds_parse_id(struct ptlrpc_request *req)
242 struct parseid_pkg *pkg, *reppkg;
243 struct obd_device *obd = req->rq_export->exp_obd;
244 int rc = 0, size = sizeof(*reppkg);
247 pkg = lustre_swab_reqbuf(req, 0, sizeof(*pkg),
248 lustre_swab_parseid_pkg);
252 rc = lustre_pack_reply(req, 1, &size, NULL);
256 reppkg = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reppkg));
257 memcpy(reppkg, pkg, sizeof(*reppkg));
259 if (reppkg->pp_type == PP_AUDIT_LOG)
260 rc = local_scan_audit_log(obd, reppkg);
262 rc = local_parse_id(obd, reppkg);
265 CERROR("local parseid failed. (rc:%d)\n", rc);
266 RETURN(0); /* we do need pack reply here */
269 static int parse_id(struct obd_device *obd, struct parseid_pkg *pkg)
272 int mds_num = id_group(&pkg->pp_id1);
275 LASSERT(mds_num >= 0);
277 if (mds_num == obd->u.mds.mds_num) {
278 rc = local_parse_id(obd, pkg);
280 struct ptlrpc_request *req;
281 struct lmv_obd *lmv = &obd->u.mds.mds_md_obd->u.lmv;
282 struct parseid_pkg *body;
283 int size = sizeof(*body);
284 struct obd_export *exp = lmv->tgts[mds_num].ltd_exp;
286 req = ptlrpc_prep_req(class_exp2cliimp(exp),
287 LUSTRE_MDS_VERSION, MDS_PARSE_ID, 1,
292 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
293 memcpy(body, pkg, sizeof(*body));
295 req->rq_replen = lustre_msg_size(1, &size);
297 rc = ptlrpc_queue_wait(req);
301 body = lustre_swab_repbuf(req, 0, sizeof(*body),
302 lustre_swab_parseid_pkg);
304 CERROR("can't unpack parseid_pkg\n");
305 GOTO(out, rc = -EPROTO);
307 memcpy(pkg, body, sizeof(*pkg));
309 ptlrpc_req_finished(req);
315 struct list_head link;
316 char name[NAME_MAX + 1];
320 mds_id2name(struct obd_device *obd, struct lustre_id *id,
321 struct lustre_id *rootid, struct list_head *list,
322 struct lustre_id *lastid)
324 struct name_item *item;
325 struct parseid_pkg *pkg;
329 OBD_ALLOC(pkg, sizeof(*pkg));
334 while (!id_equal(&pkg->pp_id1, rootid)) {
336 rc = parse_id(obd, pkg);
338 CDEBUG(D_SEC, "parse id failed. rc=%d\n", rc);
339 *lastid = pkg->pp_id1;
343 switch (pkg->pp_type) {
346 case PP_SPLIT_MASTER:
347 OBD_ALLOC(item, sizeof(*item));
349 GOTO(out, rc = -ENOMEM);
351 INIT_LIST_HEAD(&item->link);
352 list_add(&item->link, list);
353 memcpy(item->name, pkg->pp_name, sizeof(item->name));
356 pkg->pp_id1 = pkg->pp_id2;
357 memset(&pkg->pp_id2, 0, sizeof(struct lustre_id));
368 OBD_FREE(pkg, sizeof(*pkg));
373 scan_audit_log_cb(struct llog_handle *llh, struct llog_rec_hdr *rec, void *data)
375 struct parseid_pkg *pkg = (struct parseid_pkg *)data;
376 struct audit_record *ad_rec;
377 struct audit_id_record *cid_rec, *pid_rec;
378 struct audit_name_record *nm_rec;
381 if (!(le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
382 CERROR("log is not plain\n");
385 if (rec->lrh_type != SMFS_AUDIT_NAME_REC &&
386 rec->lrh_type != LLOG_GEN_REC) {
390 ad_rec = (struct audit_record *)((char *)rec + sizeof(*rec));
392 if (ad_rec->result ||
393 ad_rec->opcode != AUDIT_UNLINK ||
394 ad_rec->opcode != AUDIT_RENAME)
397 cid_rec = (struct audit_id_record *)((char *)ad_rec + sizeof(*ad_rec));
398 pid_rec = cid_rec + 1;
399 nm_rec = (struct audit_name_record *)
400 ((char *)pid_rec + sizeof(*pid_rec));
402 if (cid_rec->au_num == id_ino(&pkg->pp_id1) &&
403 cid_rec->au_gen == id_gen(&pkg->pp_id1)) {
405 id_ino(&pkg->pp_id2) = pid_rec->au_num;
406 id_gen(&pkg->pp_id2) = pid_rec->au_gen;
407 id_type(&pkg->pp_id2) = pid_rec->au_type;
408 id_fid(&pkg->pp_id2) = pid_rec->au_fid;
409 id_group(&pkg->pp_id2) = pid_rec->au_mds;
411 memcpy(pkg->pp_name, nm_rec->name,
412 le32_to_cpu(nm_rec->name_len));
414 RETURN(LLOG_PROC_BREAK);
420 local_scan_audit_log(struct obd_device *obd, struct parseid_pkg *pkg)
422 struct llog_handle *llh = NULL;
423 struct llog_ctxt *ctxt = llog_get_context(&obd->obd_llogs,
424 LLOG_AUDIT_ORIG_CTXT);
429 llh = ctxt->loc_handle;
434 rc = llog_cat_process(llh, (llog_cb_t)&scan_audit_log_cb, (void *)pkg);
435 if (rc != LLOG_PROC_BREAK) {
436 CWARN("process catalog log failed: rc(%d)\n", rc);
443 scan_audit_log(struct obd_device *obd, struct lustre_id *cur_id,
444 struct list_head *list, struct lustre_id *parent_id)
446 struct name_item *item = NULL;
447 int rc = 0, mds_num = id_group(cur_id);
448 struct parseid_pkg *pkg = NULL;
451 OBD_ALLOC(pkg, sizeof(*pkg));
455 pkg->pp_type = PP_AUDIT_LOG;
456 pkg->pp_id1 = *cur_id;
458 if (obd->u.mds.mds_num == mds_num) {
459 rc = local_scan_audit_log(obd, pkg);
461 struct ptlrpc_request *req;
462 struct lmv_obd *lmv = &obd->u.mds.mds_md_obd->u.lmv;
463 struct parseid_pkg *body;
464 int size = sizeof(*body);
465 struct obd_export *exp = lmv->tgts[mds_num].ltd_exp;
467 req = ptlrpc_prep_req(class_exp2cliimp(exp),
468 LUSTRE_MDS_VERSION, MDS_PARSE_ID, 1,
473 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
474 memcpy(body, pkg, sizeof(*body));
476 req->rq_replen = lustre_msg_size(1, &size);
478 rc = ptlrpc_queue_wait(req);
482 body = lustre_swab_repbuf(req, 0, sizeof(*body),
483 lustre_swab_parseid_pkg);
485 CERROR("can't unpack parseid_pkg\n");
486 GOTO(out, rc = -EPROTO);
488 memcpy(pkg, body, sizeof(*pkg));
490 ptlrpc_req_finished(req);
494 if (!rc) rc = pkg->pp_rc;
498 *parent_id = pkg->pp_id2;
500 OBD_ALLOC(item, sizeof(*item));
502 GOTO(out, rc = -ENOMEM);
504 INIT_LIST_HEAD(&item->link);
505 list_add(&item->link, list);
506 memcpy(item->name, pkg->pp_name, sizeof(item->name));
508 OBD_FREE(pkg, sizeof(*pkg));
513 mds_audit_id2name(struct obd_device *obd, char **name, int *namelen,
514 struct lustre_id *id)
517 struct list_head list, *pos, *n;
518 struct name_item *item;
519 struct lustre_id parent_id, cur_id, rootid;
523 INIT_LIST_HEAD(&list);
525 if (obd->u.mds.mds_num) {
526 int valsize = sizeof(rootid);
527 rc = obd_get_info(obd->u.mds.mds_md_exp, strlen("rootid"),
528 "rootid", &valsize, &rootid);
530 CERROR("cann't get rootid!\n");
534 rootid = obd->u.mds.mds_rootid;
538 if (id_equal(&cur_id, &rootid))
541 memset(&parent_id, 0, sizeof(parent_id));
543 rc = mds_id2name(obd, &cur_id, &rootid, &list, &parent_id);
545 /* can't reconstruct name from id, turn to audit log */
546 LASSERT(id_fid(&parent_id));
548 memset(&parent_id, 0, sizeof(parent_id));
550 rc = scan_audit_log(obd, &cur_id, &list, &parent_id);
552 CERROR("scan id in audit log failed. (rc:%d)\n", rc);
556 LASSERT(id_fid(&parent_id));
561 CERROR("reconstruct name from id failed. (rc:%d)\n", rc);
565 list_for_each_safe (pos, n, &list) {
566 item = list_entry(pos, struct name_item, link);
567 *namelen += strlen(item->name) + 1;
569 OBD_ALLOC(*name, *namelen);
573 list_for_each_safe (pos, n, &list) {
574 item = list_entry(pos, struct name_item, link);
577 strcat(*name, item->name);
579 list_del_init(&item->link);
580 OBD_FREE(item, sizeof(*item));
584 EXPORT_SYMBOL(mds_audit_id2name);