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_gr = lsm->lsm_object_gr;
82 oa->o_mode = mode & S_IFMT;
83 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
85 if (log_unlink && logcookies) {
86 oa->o_valid |= OBD_MD_FLCOOKIE;
87 oti.oti_logcookies = logcookies;
89 rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti, obd->obd_self_export,
93 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
94 "%d\n", lsm->lsm_object_id, rc);
96 obd_free_memmd(mds->mds_osc_exp, &lsm);
100 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
101 struct inode *inode, struct inode *pending_dir)
103 struct mds_obd *mds = &obd->u.mds;
104 struct lov_mds_md *lmm = NULL;
105 struct llog_cookie *logcookies = NULL;
106 int lmm_size, log_unlink = 0, cookie_size = 0;
112 LASSERT(mds->mds_osc_obd != NULL);
114 /* We don't need to do any of these other things for orhpan dirs,
115 * especially not mds_get_md (may get a default LOV EA, bug 4554) */
116 mode = inode->i_mode;
118 rc = vfs_rmdir(pending_dir, dchild);
120 CERROR("error %d unlinking dir %*s from PENDING\n",
121 rc, dchild->d_name.len, dchild->d_name.name);
125 lmm_size = mds->mds_max_mdsize;
126 OBD_ALLOC(lmm, lmm_size);
130 rc = mds_get_md(obd, inode, lmm, &lmm_size, 1);
132 GOTO(out_free_lmm, rc);
134 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
135 le32_to_cpu(lmm->lmm_stripe_count));
136 if (IS_ERR(handle)) {
137 rc = PTR_ERR(handle);
138 CERROR("error fsfilt_start: %d\n", rc);
140 GOTO(out_free_lmm, rc);
143 rc = vfs_unlink(pending_dir, dchild);
145 CERROR("error %d unlinking orphan %.*s from PENDING\n",
146 rc, dchild->d_name.len, dchild->d_name.name);
147 } else if (lmm_size) {
148 cookie_size = mds_get_cookie_size(obd, lmm);
149 OBD_ALLOC(logcookies, cookie_size);
150 if (logcookies == NULL)
152 else if (mds_log_op_unlink(obd, lmm,lmm_size,logcookies,
157 err = fsfilt_commit(obd, pending_dir, handle, 0);
159 CERROR("error committing orphan unlink: %d\n", err);
163 rc = mds_osc_destroy_orphan(obd, mode, lmm, lmm_size,
164 logcookies, log_unlink);
167 if (logcookies != NULL)
168 OBD_FREE(logcookies, cookie_size);
170 OBD_FREE(lmm, mds->mds_max_mdsize);
174 /* Delete inodes which were previously open-unlinked but were not reopened
175 * during MDS recovery for whatever reason (e.g. client also failed, recovery
177 int mds_cleanup_pending(struct obd_device *obd)
179 struct mds_obd *mds = &obd->u.mds;
180 struct lvfs_run_ctxt saved;
182 struct dentry *dchild, *dentry;
183 struct vfsmount *mnt;
184 struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
185 struct l_linux_dirent *dirent, *n;
186 struct list_head dentry_list;
187 char d_name[LL_FID_NAMELEN];
189 int i = 0, rc = 0, item = 0, namlen;
192 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
193 /* dentry and mnt ref dropped in dentry_open() on error, or
194 * in filp_close() if dentry_open() succeeds */
195 dentry = dget(mds->mds_pending_dir);
197 GOTO(err_pop, rc = PTR_ERR(dentry));
198 mnt = mntget(mds->mds_vfsmnt);
200 GOTO(err_mntget, rc = PTR_ERR(mnt));
202 file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
203 O_RDONLY | O_LARGEFILE);
205 GOTO(err_pop, rc = PTR_ERR(file));
207 INIT_LIST_HEAD(&dentry_list);
208 rc = l_readdir(file, &dentry_list);
213 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
215 list_del(&dirent->lld_list);
217 namlen = strlen(dirent->lld_name);
218 LASSERT(sizeof(d_name) >= namlen + 1);
219 strcpy(d_name, dirent->lld_name);
220 inum = dirent->lld_ino;
221 OBD_FREE(dirent, sizeof(*dirent));
223 CDEBUG(D_INODE, "entry %d of PENDING DIR: %s\n", i, d_name);
225 if (((namlen == 1) && !strcmp(d_name, ".")) ||
226 ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
229 LOCK_INODE_MUTEX(pending_dir);
230 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
231 if (IS_ERR(dchild)) {
232 UNLOCK_INODE_MUTEX(pending_dir);
233 GOTO(err_out, rc = PTR_ERR(dchild));
235 if (!dchild->d_inode) {
236 CWARN("%s: orphan %s has already been removed\n",
237 obd->obd_name, d_name);
241 if (is_bad_inode(dchild->d_inode)) {
242 CERROR("%s: bad orphan inode found %lu/%u\n",
243 obd->obd_name, dchild->d_inode->i_ino,
244 dchild->d_inode->i_generation);
245 GOTO(next, rc = -ENOENT);
248 child_inode = dchild->d_inode;
249 MDS_DOWN_READ_ORPHAN_SEM(child_inode);
250 if (mds_inode_is_orphan(child_inode) &&
251 mds_orphan_open_count(child_inode)) {
252 MDS_UP_READ_ORPHAN_SEM(child_inode);
253 CWARN("%s: orphan %s re-opened during recovery\n",
254 obd->obd_name, d_name);
257 MDS_UP_READ_ORPHAN_SEM(child_inode);
259 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
262 CDEBUG(D_HA, "%s: removed orphan %s\n",
263 obd->obd_name, d_name);
265 CDEBUG(D_INODE, "%s: removed orphan %s failed,"
266 " rc = %d\n", obd->obd_name, d_name, rc);
271 UNLOCK_INODE_MUTEX(pending_dir);
275 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
276 list_del(&dirent->lld_list);
277 OBD_FREE(dirent, sizeof(*dirent));
280 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
282 CWARN("%s: removed %d pending open-unlinked files\n",
283 obd->obd_name, item);
287 l_dput(mds->mds_pending_dir);