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 the Lustre file system, http://www.lustre.org
12 * Lustre is a trademark of Cluster File Systems, Inc.
14 * You may have signed or agreed to another license before downloading
15 * this software. If so, you are bound by the terms and conditions
16 * of that agreement, and the following does not apply to you. See the
17 * LICENSE file included with this distribution for more information.
19 * If you did not agree to a different license, then this copy of Lustre
20 * is open source software; you can redistribute it and/or modify it
21 * under the terms of version 2 of the GNU General Public License as
22 * published by the Free Software Foundation.
24 * In either case, Lustre is distributed in the hope that it will be
25 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * license text for more details.
30 /* code for handling open unlinked files */
32 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/version.h>
38 #include <libcfs/list.h>
39 #include <obd_class.h>
40 #include <lustre_fsfilt.h>
41 #include <lustre_mds.h>
42 #include <lustre_commit_confd.h>
45 #include "mds_internal.h"
47 static int mds_osc_destroy_orphan(struct obd_device *obd,
49 struct lov_mds_md *lmm,
51 struct llog_cookie *logcookies,
54 struct mds_obd *mds = &obd->u.mds;
55 struct lov_stripe_md *lsm = NULL;
56 struct obd_trans_info oti = { 0 };
64 rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
66 CERROR("Error unpack md %p\n", lmm);
69 LASSERT(rc >= sizeof(*lsm));
73 rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
75 GOTO(out_free_memmd, rc);
79 GOTO(out_free_memmd, rc = -ENOMEM);
80 oa->o_id = lsm->lsm_object_id;
81 oa->o_mode = inode->i_mode & S_IFMT;
82 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
84 if (log_unlink && logcookies) {
85 oa->o_valid |= OBD_MD_FLCOOKIE;
86 oti.oti_logcookies = logcookies;
88 rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti, obd->obd_self_export);
91 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
92 "%d\n", lsm->lsm_object_id, rc);
94 obd_free_memmd(mds->mds_osc_exp, &lsm);
98 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
99 struct inode *inode, struct inode *pending_dir)
101 struct mds_obd *mds = &obd->u.mds;
102 struct lov_mds_md *lmm = NULL;
103 struct llog_cookie *logcookies = NULL;
104 int lmm_size, log_unlink = 0, cookie_size = 0;
109 LASSERT(mds->mds_osc_obd != NULL);
111 /* We don't need to do any of these other things for orhpan dirs,
112 * especially not mds_get_md (may get a default LOV EA, bug 4554) */
113 if (S_ISDIR(inode->i_mode)) {
114 rc = vfs_rmdir(pending_dir, dchild);
116 CERROR("error %d unlinking dir %*s from PENDING\n",
117 rc, dchild->d_name.len, dchild->d_name.name);
121 lmm_size = mds->mds_max_mdsize;
122 OBD_ALLOC(lmm, lmm_size);
126 rc = mds_get_md(obd, inode, lmm, &lmm_size, 1);
128 GOTO(out_free_lmm, rc);
130 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
131 le32_to_cpu(lmm->lmm_stripe_count));
132 if (IS_ERR(handle)) {
133 rc = PTR_ERR(handle);
134 CERROR("error fsfilt_start: %d\n", rc);
136 GOTO(out_free_lmm, rc);
139 rc = vfs_unlink(pending_dir, dchild);
141 CERROR("error %d unlinking orphan %.*s from PENDING\n",
142 rc, dchild->d_name.len, dchild->d_name.name);
143 } else if (lmm_size) {
144 cookie_size = mds_get_cookie_size(obd, lmm);
145 OBD_ALLOC(logcookies, cookie_size);
146 if (logcookies == NULL)
148 else if (mds_log_op_unlink(obd, inode, lmm,lmm_size,logcookies,
153 err = fsfilt_commit(obd, pending_dir, handle, 0);
155 CERROR("error committing orphan unlink: %d\n", err);
159 rc = mds_osc_destroy_orphan(obd, inode, lmm, lmm_size,
160 logcookies, log_unlink);
163 if (logcookies != NULL)
164 OBD_FREE(logcookies, cookie_size);
166 OBD_FREE(lmm, mds->mds_max_mdsize);
170 /* Delete inodes which were previously open-unlinked but were not reopened
171 * during MDS recovery for whatever reason (e.g. client also failed, recovery
173 int mds_cleanup_pending(struct obd_device *obd)
175 struct mds_obd *mds = &obd->u.mds;
176 struct lvfs_run_ctxt saved;
178 struct dentry *dchild, *dentry;
179 struct vfsmount *mnt;
180 struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
181 struct l_linux_dirent *dirent, *n;
182 struct list_head dentry_list;
183 char d_name[LL_FID_NAMELEN];
185 int i = 0, rc = 0, item = 0, namlen;
188 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
189 /* dentry and mnt ref dropped in dentry_open() on error, or
190 * in filp_close() if dentry_open() succeeds */
191 dentry = dget(mds->mds_pending_dir);
193 GOTO(err_pop, rc = PTR_ERR(dentry));
194 mnt = mntget(mds->mds_vfsmnt);
196 GOTO(err_mntget, rc = PTR_ERR(mnt));
198 file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
199 O_RDONLY | O_LARGEFILE);
201 GOTO(err_pop, rc = PTR_ERR(file));
203 INIT_LIST_HEAD(&dentry_list);
204 rc = l_readdir(file, &dentry_list);
209 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
211 list_del(&dirent->lld_list);
213 namlen = strlen(dirent->lld_name);
214 LASSERT(sizeof(d_name) >= namlen + 1);
215 strcpy(d_name, dirent->lld_name);
216 inum = dirent->lld_ino;
217 OBD_FREE(dirent, sizeof(*dirent));
219 CDEBUG(D_INODE, "entry %d of PENDING DIR: %s\n", i, d_name);
221 if (((namlen == 1) && !strcmp(d_name, ".")) ||
222 ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
225 LOCK_INODE_MUTEX(pending_dir);
226 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
227 if (IS_ERR(dchild)) {
228 UNLOCK_INODE_MUTEX(pending_dir);
229 GOTO(err_out, rc = PTR_ERR(dchild));
231 if (!dchild->d_inode) {
232 CWARN("%s: orphan %s has already been removed\n",
233 obd->obd_name, d_name);
237 if (is_bad_inode(dchild->d_inode)) {
238 CERROR("%s: bad orphan inode found %lu/%u\n",
239 obd->obd_name, dchild->d_inode->i_ino,
240 dchild->d_inode->i_generation);
241 GOTO(next, rc = -ENOENT);
244 child_inode = dchild->d_inode;
245 MDS_DOWN_READ_ORPHAN_SEM(child_inode);
246 if (mds_inode_is_orphan(child_inode) &&
247 mds_orphan_open_count(child_inode)) {
248 MDS_UP_READ_ORPHAN_SEM(child_inode);
249 CWARN("%s: orphan %s re-opened during recovery\n",
250 obd->obd_name, d_name);
253 MDS_UP_READ_ORPHAN_SEM(child_inode);
255 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
258 CDEBUG(D_HA, "%s: removed orphan %s\n",
259 obd->obd_name, d_name);
261 CDEBUG(D_INODE, "%s: removed orphan %s failed,"
262 " rc = %d\n", obd->obd_name, d_name, rc);
267 UNLOCK_INODE_MUTEX(pending_dir);
271 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
272 list_del(&dirent->lld_list);
273 OBD_FREE(dirent, sizeof(*dirent));
276 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
278 CWARN("%s: removed %d pending open-unlinked files\n",
279 obd->obd_name, item);
283 l_dput(mds->mds_pending_dir);