1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/mds/mds_orphan.c
6 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
7 * Author: Peter Braam <braam@clusterfs.com>
8 * Author: Andreas Dilger <adilger@clusterfs.com>
9 * Author: Phil Schwan <phil@clusterfs.com>
11 * This file is part of Lustre, http://www.lustre.org.
13 * Lustre is free software; you can redistribute it and/or
14 * modify it under the terms of version 2 of the GNU General Public
15 * License as published by the Free Software Foundation.
17 * Lustre is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Lustre; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /* code for handling open unlinked files */
29 #define DEBUG_SUBSYSTEM S_MDS
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
35 #include <libcfs/list.h>
36 #include <linux/obd_class.h>
37 #include <linux/lustre_fsfilt.h>
38 #include <linux/lustre_commit_confd.h>
39 #include <linux/lvfs.h>
41 #include "mds_internal.h"
43 static int mds_osc_destroy_orphan(struct mds_obd *mds,
45 struct lov_mds_md *lmm,
47 struct llog_cookie *logcookies,
50 struct lov_stripe_md *lsm = NULL;
51 struct obd_trans_info oti = { 0 };
59 rc = obd_unpackmd(mds->mds_dt_exp, &lsm, lmm, lmm_size);
61 CERROR("Error unpack md %p\n", lmm);
64 LASSERT(rc >= sizeof(*lsm));
70 GOTO(out_free_memmd, rc = -ENOMEM);
71 oa->o_id = lsm->lsm_object_id;
72 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
73 oa->o_mode = inode->i_mode & S_IFMT;
74 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
76 if (log_unlink && logcookies) {
77 oa->o_valid |= OBD_MD_FLCOOKIE;
78 oti.oti_logcookies = logcookies;
81 rc = obd_destroy(mds->mds_dt_exp, oa, lsm, &oti);
84 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
85 "%d\n", lsm->lsm_object_id, rc);
87 obd_free_memmd(mds->mds_dt_exp, &lsm);
91 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
92 struct inode *inode, struct inode *pending_dir)
94 struct mds_obd *mds = &obd->u.mds;
95 struct lov_mds_md *lmm = NULL;
96 struct llog_cookie *logcookies = NULL;
97 int lmm_size, log_unlink = 0;
102 LASSERT(mds->mds_dt_obd != NULL);
103 LASSERT(obd->obd_recovering == 0);
105 /* We don't need to do any of these other things for orhpan dirs,
106 * especially not mds_get_md (may get a default LOV EA, bug 4554) */
107 if (S_ISDIR(inode->i_mode)) {
108 rc = vfs_rmdir(pending_dir, dchild);
110 CERROR("error %d unlinking dir %*s from PENDING\n",
111 rc, dchild->d_name.len, dchild->d_name.name);
115 lmm_size = mds->mds_max_mdsize;
116 OBD_ALLOC(lmm, lmm_size);
120 rc = mds_get_md(obd, inode, lmm, &lmm_size, 1, 0);
122 GOTO(out_free_lmm, rc);
124 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
125 le32_to_cpu(lmm->lmm_stripe_count));
126 if (IS_ERR(handle)) {
127 rc = PTR_ERR(handle);
128 CERROR("error fsfilt_start: %d\n", rc);
130 GOTO(out_free_lmm, rc);
133 rc = vfs_unlink(pending_dir, dchild);
135 CERROR("error %d unlinking orphan %.*s from PENDING\n",
136 rc, dchild->d_name.len, dchild->d_name.name);
137 } else if (lmm_size) {
138 OBD_ALLOC(logcookies, mds->mds_max_cookiesize);
139 if (logcookies == NULL)
141 else if (mds_log_op_unlink(obd, inode, lmm,lmm_size,logcookies,
142 mds->mds_max_cookiesize, NULL) > 0)
145 err = fsfilt_commit(obd, mds->mds_sb, pending_dir, handle, 0);
147 CERROR("error committing orphan unlink: %d\n", err);
151 rc = mds_osc_destroy_orphan(mds, inode, lmm, lmm_size,
152 logcookies, log_unlink);
155 if (logcookies != NULL)
156 OBD_FREE(logcookies, mds->mds_max_cookiesize);
158 OBD_FREE(lmm, mds->mds_max_mdsize);
162 int mds_cleanup_orphans(struct obd_device *obd)
164 struct mds_obd *mds = &obd->u.mds;
165 struct lvfs_run_ctxt saved;
167 struct dentry *dchild, *dentry;
168 struct vfsmount *mnt;
169 struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
170 struct l_linux_dirent *dirent, *n;
171 struct list_head dentry_list;
172 char d_name[LL_ID_NAMELEN];
175 int rc = 0, item = 0, namlen;
178 LASSERT(obd->obd_recovering == 0);
179 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
180 dentry = dget(mds->mds_pending_dir);
182 GOTO(err_pop, rc = PTR_ERR(dentry));
183 mnt = mntget(mds->mds_vfsmnt);
185 GOTO(err_mntget, rc = PTR_ERR(mnt));
187 file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
188 O_RDONLY | O_LARGEFILE);
190 GOTO(err_pop, rc = PTR_ERR(file));
192 INIT_LIST_HEAD(&dentry_list);
193 rc = l_readdir(file, &dentry_list);
198 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
200 list_del(&dirent->lld_list);
202 namlen = strlen(dirent->lld_name);
203 LASSERT(sizeof(d_name) >= namlen + 1);
204 strcpy(d_name, dirent->lld_name);
205 inum = dirent->lld_ino;
206 OBD_FREE(dirent, sizeof(*dirent));
208 CDEBUG(D_INODE, "entry "LPU64" of PENDING DIR: %s\n",
211 if (((namlen == 1) && !strcmp(d_name, ".")) ||
212 ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
215 down(&pending_dir->i_sem);
216 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
217 if (IS_ERR(dchild)) {
218 up(&pending_dir->i_sem);
219 GOTO(err_out, rc = PTR_ERR(dchild));
221 if (!dchild->d_inode) {
222 CERROR("orphan %s has been removed\n", d_name);
226 if (is_bad_inode(dchild->d_inode)) {
227 CERROR("bad orphan inode found %lu/%u\n",
228 dchild->d_inode->i_ino,
229 dchild->d_inode->i_generation);
230 GOTO(next, rc = -ENOENT);
233 child_inode = dchild->d_inode;
234 DOWN_READ_I_ALLOC_SEM(child_inode);
235 if (mds_inode_is_orphan(child_inode) &&
236 mds_orphan_open_count(child_inode)) {
237 UP_READ_I_ALLOC_SEM(child_inode);
238 CWARN("orphan %s re-opened during recovery\n", d_name);
241 UP_READ_I_ALLOC_SEM(child_inode);
242 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
245 CWARN("removed orphan %s from MDS and OST\n", d_name);
247 CDEBUG(D_INODE, "removed orphan %s from MDS/OST failed,"
248 " rc = %d\n", d_name, rc);
253 up(&pending_dir->i_sem);
256 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
257 list_del(&dirent->lld_list);
258 OBD_FREE(dirent, sizeof(*dirent));
261 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
267 l_dput(mds->mds_pending_dir);