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 #ifndef AUTOCONF_INCLUDED
35 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/version.h>
40 #include <libcfs/list.h>
41 #include <obd_class.h>
42 #include <lustre_fsfilt.h>
43 #include <lustre_mds.h>
44 #include <lustre_commit_confd.h>
47 #include "mds_internal.h"
49 int mds_osc_destroy_orphan(struct obd_device *obd,
51 struct lov_mds_md *lmm,
53 struct llog_cookie *logcookies,
56 struct mds_obd *mds = &obd->u.mds;
57 struct lov_stripe_md *lsm = NULL;
58 struct obd_trans_info oti = { 0 };
66 rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
68 CERROR("Error unpack md %p\n", lmm);
71 LASSERT(rc >= sizeof(*lsm));
75 rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
77 GOTO(out_free_memmd, rc);
81 GOTO(out_free_memmd, rc = -ENOMEM);
82 oa->o_id = lsm->lsm_object_id;
83 oa->o_gr = lsm->lsm_object_gr;
84 oa->o_mode = mode & S_IFMT;
85 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
87 if (log_unlink && logcookies) {
88 oa->o_valid |= OBD_MD_FLCOOKIE;
89 oti.oti_logcookies = logcookies;
91 rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti, obd->obd_self_export);
94 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
95 "%d\n", lsm->lsm_object_id, rc);
97 obd_free_memmd(mds->mds_osc_exp, &lsm);
101 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
102 struct inode *inode, struct inode *pending_dir)
104 struct mds_obd *mds = &obd->u.mds;
105 struct lov_mds_md *lmm = NULL;
106 struct llog_cookie *logcookies = NULL;
107 int lmm_size, log_unlink = 0, cookie_size = 0;
113 LASSERT(mds->mds_osc_obd != NULL);
115 /* We don't need to do any of these other things for orhpan dirs,
116 * especially not mds_get_md (may get a default LOV EA, bug 4554) */
117 mode = inode->i_mode;
119 rc = vfs_rmdir(pending_dir, dchild);
121 CERROR("error %d unlinking dir %*s from PENDING\n",
122 rc, dchild->d_name.len, dchild->d_name.name);
126 lmm_size = mds->mds_max_mdsize;
127 OBD_ALLOC(lmm, lmm_size);
131 rc = mds_get_md(obd, inode, lmm, &lmm_size, 1);
133 GOTO(out_free_lmm, rc);
135 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
136 le32_to_cpu(lmm->lmm_stripe_count));
137 if (IS_ERR(handle)) {
138 rc = PTR_ERR(handle);
139 CERROR("error fsfilt_start: %d\n", rc);
141 GOTO(out_free_lmm, rc);
144 rc = vfs_unlink(pending_dir, dchild);
146 CERROR("error %d unlinking orphan %.*s from PENDING\n",
147 rc, dchild->d_name.len, dchild->d_name.name);
148 } else if (lmm_size) {
149 cookie_size = mds_get_cookie_size(obd, lmm);
150 OBD_ALLOC(logcookies, cookie_size);
151 if (logcookies == NULL)
153 else if (mds_log_op_unlink(obd, lmm,lmm_size,logcookies,
158 err = fsfilt_commit(obd, pending_dir, handle, 0);
160 CERROR("error committing orphan unlink: %d\n", err);
164 rc = mds_osc_destroy_orphan(obd, mode, lmm, lmm_size,
165 logcookies, log_unlink);
168 if (logcookies != NULL)
169 OBD_FREE(logcookies, cookie_size);
171 OBD_FREE(lmm, mds->mds_max_mdsize);
175 /* Delete inodes which were previously open-unlinked but were not reopened
176 * during MDS recovery for whatever reason (e.g. client also failed, recovery
178 int mds_cleanup_pending(struct obd_device *obd)
180 struct mds_obd *mds = &obd->u.mds;
181 struct lvfs_run_ctxt saved;
183 struct dentry *dchild, *dentry;
184 struct vfsmount *mnt;
185 struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
186 struct l_linux_dirent *dirent, *n;
187 struct list_head dentry_list;
188 char d_name[LL_FID_NAMELEN];
190 int i = 0, rc = 0, item = 0, namlen;
193 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
194 /* dentry and mnt ref dropped in dentry_open() on error, or
195 * in filp_close() if dentry_open() succeeds */
196 dentry = dget(mds->mds_pending_dir);
198 GOTO(err_pop, rc = PTR_ERR(dentry));
199 mnt = mntget(mds->mds_vfsmnt);
201 GOTO(err_mntget, rc = PTR_ERR(mnt));
203 file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
204 O_RDONLY | O_LARGEFILE);
206 GOTO(err_pop, rc = PTR_ERR(file));
208 INIT_LIST_HEAD(&dentry_list);
209 rc = l_readdir(file, &dentry_list);
214 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
216 list_del(&dirent->lld_list);
218 namlen = strlen(dirent->lld_name);
219 LASSERT(sizeof(d_name) >= namlen + 1);
220 strcpy(d_name, dirent->lld_name);
221 inum = dirent->lld_ino;
222 OBD_FREE(dirent, sizeof(*dirent));
224 CDEBUG(D_INODE, "entry %d of PENDING DIR: %s\n", i, d_name);
226 if (((namlen == 1) && !strcmp(d_name, ".")) ||
227 ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
230 LOCK_INODE_MUTEX(pending_dir);
231 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
232 if (IS_ERR(dchild)) {
233 UNLOCK_INODE_MUTEX(pending_dir);
234 GOTO(err_out, rc = PTR_ERR(dchild));
236 if (!dchild->d_inode) {
237 CWARN("%s: orphan %s has already been removed\n",
238 obd->obd_name, d_name);
242 if (is_bad_inode(dchild->d_inode)) {
243 CERROR("%s: bad orphan inode found %lu/%u\n",
244 obd->obd_name, dchild->d_inode->i_ino,
245 dchild->d_inode->i_generation);
246 GOTO(next, rc = -ENOENT);
249 child_inode = dchild->d_inode;
250 MDS_DOWN_READ_ORPHAN_SEM(child_inode);
251 if (mds_inode_is_orphan(child_inode) &&
252 mds_orphan_open_count(child_inode)) {
253 MDS_UP_READ_ORPHAN_SEM(child_inode);
254 CWARN("%s: orphan %s re-opened during recovery\n",
255 obd->obd_name, d_name);
258 MDS_UP_READ_ORPHAN_SEM(child_inode);
260 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
261 CDEBUG(D_INODE, "%s: removed orphan %s: rc %d\n",
262 obd->obd_name, d_name, rc);
269 UNLOCK_INODE_MUTEX(pending_dir);
273 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
274 list_del(&dirent->lld_list);
275 OBD_FREE(dirent, sizeof(*dirent));
278 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
280 CWARN("%s: removed %d pending open-unlinked files\n",
281 obd->obd_name, item);
285 l_dput(mds->mds_pending_dir);