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 {
62 static int filldir(void *__buf, const char *name, int namlen,
63 loff_t offset, ino_t ino, unsigned int d_type)
65 struct scan_dir_data *sd = __buf;
69 (namlen == 1 || (namlen == 2 && name[1] == '.'))) {
70 /* skip special entries */
76 /* skip non-cross_ref entries if we need cross-ref */
77 if (sd->cross_ref && d_type & 128)
80 if (ino == sd->i_num) {
81 strncpy(sd->name, name, namlen);
83 RETURN(-EINTR); /* break the readdir loop */
88 static int scan_name_in_parent(struct lustre_id *pid, struct lustre_id *id,
93 struct scan_dir_data sd;
97 len = strlen("__iopen__/") + 10 + 1;
98 OBD_ALLOC(pname, len);
102 sprintf(pname, "__iopen__/0x%llx", id_ino(pid));
104 file = filp_open(pname, O_RDONLY, 0);
106 CERROR("can't open directory %s: %d\n",
107 pname, (int) PTR_ERR(file));
108 GOTO(out, rc = PTR_ERR(file));
111 sd.i_num = id_ino(id);
115 vfs_readdir(file, filldir, &sd);
121 OBD_FREE(pname, len);
126 /* id2pid - given id, get parent id or master id.
128 * @id: child id to be parsed
129 * @pid: parent id or master id
133 id2pid(struct obd_device *obd, struct lustre_id *id, struct lustre_id *pid,
136 struct dentry *dentry = NULL;
137 struct inode *inode = NULL;
138 struct mea *mea = NULL;
139 int mea_size, rc = 0;
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);
148 inode = dentry->d_inode;
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);
159 } else if (mea && mea->mea_count) {
160 *type = PP_SPLIT_MASTER;
163 *type = PP_SPLIT_SLAVE;
164 *pid = mea->mea_ids[mea->mea_master];
168 //LASSERT(!S_ISDIR(id_type(id)));
171 rc = mds_read_inode_pid(obd, inode, pid);
173 CERROR("can't read parent ino(%lu) rc(%d).\n",
179 /* Well, if it's dir or master split, we have to check if it's
181 if ((*type == PP_DIR || *type == PP_SPLIT_MASTER) &&
182 id_group(id) != id_group(pid))
183 *type = PP_CROSS_DIR;
186 OBD_FREE(mea, mea_size);
191 static int local_parse_id(struct obd_device *obd, struct parseid_pkg *pkg)
193 struct lvfs_run_ctxt saved;
194 int rc = 0, cross_ref = 0;
199 memset(pkg->pp_name, 0, sizeof(pkg->pp_name));
201 //LASSERT(obd->u.mds.mds_num == id_group(&pkg->pp_id1));
203 /* pp_id2 is present, which indicating we want to scan parent
204 * dir(pp_id2) to find the cross-ref entry(pp_id1) */
205 if (id_fid(&pkg->pp_id2)) {
206 pkg->pp_type = PP_DIR;
209 rc = id2pid(obd, &pkg->pp_id1, &pkg->pp_id2, &pkg->pp_type);
214 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
216 switch (pkg->pp_type) {
219 case PP_SPLIT_MASTER:
220 rc = scan_name_in_parent(&pkg->pp_id2, &pkg->pp_id1,
221 pkg->pp_name, cross_ref);
223 CERROR("scan "LPU64" in parent failed. rc=%d\n",
224 id_ino(&pkg->pp_id1), rc);
230 CERROR("invalid id\n");
234 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
241 local_scan_audit_log(struct obd_device *obd, struct parseid_pkg *pkg);
243 int mds_parse_id(struct ptlrpc_request *req)
245 struct parseid_pkg *pkg, *reppkg;
246 struct obd_device *obd = req->rq_export->exp_obd;
247 int rc = 0, size = sizeof(*reppkg);
250 pkg = lustre_swab_reqbuf(req, 0, sizeof(*pkg),
251 lustre_swab_parseid_pkg);
255 rc = lustre_pack_reply(req, 1, &size, NULL);
259 reppkg = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reppkg));
260 memcpy(reppkg, pkg, sizeof(*reppkg));
262 if (reppkg->pp_type == PP_AUDIT_LOG)
263 rc = local_scan_audit_log(obd, reppkg);
265 rc = local_parse_id(obd, reppkg);
268 CERROR("local parseid failed. (rc:%d)\n", rc);
269 RETURN(0); /* we do need pack reply here */
272 static int parse_id(struct obd_device *obd, struct parseid_pkg *pkg)
275 int mds_num = id_group(&pkg->pp_id1);
278 LASSERT(mds_num >= 0);
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);
284 if (mds_num == obd->u.mds.mds_num) {
285 rc = local_parse_id(obd, pkg);
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;
293 /* make sure connection established */
294 rc = obd_set_info(obd->u.mds.mds_md_exp, strlen("chkconnect"),
295 "chkconnect", 0, NULL);
299 exp = lmv->tgts[mds_num].ltd_exp;
302 req = ptlrpc_prep_req(class_exp2cliimp(exp),
303 LUSTRE_MDS_VERSION, MDS_PARSE_ID, 1,
308 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
309 memcpy(body, pkg, sizeof(*body));
311 req->rq_replen = lustre_msg_size(1, &size);
313 rc = ptlrpc_queue_wait(req);
317 body = lustre_swab_repbuf(req, 0, sizeof(*body),
318 lustre_swab_parseid_pkg);
320 CERROR("can't unpack parseid_pkg\n");
321 GOTO(out, rc = -EPROTO);
323 memcpy(pkg, body, sizeof(*pkg));
325 ptlrpc_req_finished(req);
332 struct list_head link;
333 char name[NAME_MAX + 1];
337 mds_id2name(struct obd_device *obd, struct lustre_id *id,
338 struct list_head *list, struct lustre_id *lastid)
340 struct name_item *item;
341 struct parseid_pkg *pkg;
345 OBD_ALLOC(pkg, sizeof(*pkg));
350 while (id_fid(&pkg->pp_id1) != ROOT_FID) {
352 rc = parse_id(obd, pkg);
354 CDEBUG(D_SEC, "parse id failed. rc=%d\n", rc);
355 *lastid = pkg->pp_id1;
359 switch (pkg->pp_type) {
362 case PP_SPLIT_MASTER:
363 OBD_ALLOC(item, sizeof(*item));
365 GOTO(out, rc = -ENOMEM);
367 INIT_LIST_HEAD(&item->link);
368 list_add(&item->link, list);
369 memcpy(item->name, pkg->pp_name, sizeof(item->name));
372 pkg->pp_id1 = pkg->pp_id2;
373 memset(&pkg->pp_id2, 0, sizeof(struct lustre_id));
377 CERROR("Wrong id = %i\n", pkg->pp_type);
383 OBD_FREE(pkg, sizeof(*pkg));
388 scan_audit_log_cb(struct llog_handle *llh, struct llog_rec_hdr *rec, void *data)
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;
396 if (!(le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
397 CERROR("log is not plain\n");
400 if (rec->lrh_type != SMFS_AUDIT_NAME_REC &&
401 rec->lrh_type != LLOG_GEN_REC) {
405 ad_rec = (struct audit_record *)(rec + 1);
407 if (ad_rec->result ||
408 ad_rec->opcode != AUDIT_UNLINK ||
409 ad_rec->opcode != AUDIT_RENAME)
412 cid_rec = (struct audit_id_record *)(ad_rec + 1);
413 pid_rec = cid_rec + 1;
414 nm_rec = (struct audit_name_record *)(pid_rec + 1);
416 if (cid_rec->au_num == id_ino(&pkg->pp_id1) &&
417 cid_rec->au_gen == id_gen(&pkg->pp_id1)) {
419 id_ino(&pkg->pp_id2) = pid_rec->au_num;
420 id_gen(&pkg->pp_id2) = pid_rec->au_gen;
421 id_type(&pkg->pp_id2) = pid_rec->au_type;
422 id_fid(&pkg->pp_id2) = pid_rec->au_fid;
423 id_group(&pkg->pp_id2) = pid_rec->au_mds;
425 memcpy(pkg->pp_name, nm_rec->name,
426 le32_to_cpu(nm_rec->name_len));
428 RETURN(LLOG_PROC_BREAK);
434 local_scan_audit_log(struct obd_device *obd, struct parseid_pkg *pkg)
436 struct llog_handle *llh = NULL;
437 struct llog_ctxt *ctxt = llog_get_context(&obd->obd_llogs,
438 LLOG_AUDIT_ORIG_CTXT);
443 llh = ctxt->loc_handle;
448 rc = llog_cat_process(llh, (llog_cb_t)&scan_audit_log_cb, (void *)pkg);
449 if (rc != LLOG_PROC_BREAK) {
450 CWARN("process catalog log failed: rc(%d)\n", rc);
457 scan_audit_log(struct obd_device *obd, struct lustre_id *cur_id,
458 struct list_head *list, struct lustre_id *parent_id)
460 struct name_item *item = NULL;
461 int rc = 0, mds_num = id_group(cur_id);
462 struct parseid_pkg *pkg = NULL;
465 OBD_ALLOC(pkg, sizeof(*pkg));
469 pkg->pp_type = PP_AUDIT_LOG;
470 pkg->pp_id1 = *cur_id;
472 if (obd->u.mds.mds_num == mds_num) {
473 rc = local_scan_audit_log(obd, pkg);
475 struct ptlrpc_request *req;
476 struct lmv_obd *lmv = &obd->u.mds.mds_md_obd->u.lmv;
477 struct parseid_pkg *body;
478 int size = sizeof(*body);
479 struct obd_export *exp;
481 /* make sure connection established */
482 rc = obd_set_info(obd->u.mds.mds_md_exp, strlen("chkconnect"),
483 "chkconnect", 0, NULL);
487 exp = lmv->tgts[mds_num].ltd_exp;
490 req = ptlrpc_prep_req(class_exp2cliimp(exp),
491 LUSTRE_MDS_VERSION, MDS_PARSE_ID, 1,
496 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
497 memcpy(body, pkg, sizeof(*body));
499 req->rq_replen = lustre_msg_size(1, &size);
501 rc = ptlrpc_queue_wait(req);
505 body = lustre_swab_repbuf(req, 0, sizeof(*body),
506 lustre_swab_parseid_pkg);
508 CERROR("can't unpack parseid_pkg\n");
509 GOTO(out, rc = -EPROTO);
511 memcpy(pkg, body, sizeof(*pkg));
513 ptlrpc_req_finished(req);
517 if (!rc) rc = pkg->pp_rc;
521 *parent_id = pkg->pp_id2;
523 OBD_ALLOC(item, sizeof(*item));
525 GOTO(out, rc = -ENOMEM);
527 INIT_LIST_HEAD(&item->link);
528 list_add(&item->link, list);
529 memcpy(item->name, pkg->pp_name, sizeof(item->name));
531 OBD_FREE(pkg, sizeof(*pkg));
536 mds_audit_id2name(struct obd_device *obd, char **name, int *namelen,
537 struct lustre_id *id)
540 struct list_head list, *pos, *n;
541 struct name_item *item;
542 struct lustre_id parent_id, cur_id;
546 INIT_LIST_HEAD(&list);
549 if (id_fid(&cur_id) == ROOT_FID)
552 memset(&parent_id, 0, sizeof(parent_id));
553 rc = mds_id2name(obd, &cur_id, &list, &parent_id);
555 /* can't reconstruct name from id, turn to audit log */
556 LASSERT(id_fid(&parent_id));
558 memset(&parent_id, 0, sizeof(parent_id));
560 rc = scan_audit_log(obd, &cur_id, &list, &parent_id);
562 CERROR("scan id in audit log failed. (rc:%d)\n", rc);
566 LASSERT(id_fid(&parent_id));
571 CERROR("reconstruct name from id failed. (rc:%d)\n", rc);
575 list_for_each_safe (pos, n, &list) {
576 item = list_entry(pos, struct name_item, link);
577 *namelen += strlen(item->name) + 1;
579 OBD_ALLOC(*name, *namelen);
583 list_for_each_safe (pos, n, &list) {
584 item = list_entry(pos, struct name_item, link);
587 strcat(*name, item->name);
589 list_del_init(&item->link);
590 OBD_FREE(item, sizeof(*item));
594 EXPORT_SYMBOL(mds_audit_id2name);