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 <portals/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"
44 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
45 * we instead link the inode into the PENDING directory until it is
46 * finally released. We can't simply call mds_reint_rename() or some
47 * part thereof, because we don't have the inode to check for link
48 * count/open status until after it is locked.
50 * For lock ordering, we always get the PENDING, then pending_child lock
51 * last to avoid deadlocks.
54 int mds_open_unlink_rename(struct mds_update_record *rec,
55 struct obd_device *obd, struct dentry *dparent,
56 struct dentry *dchild, void **handle)
58 struct mds_obd *mds = &obd->u.mds;
59 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
60 struct dentry *pending_child;
61 char fidname[LL_FID_NAMELEN];
65 LASSERT(!mds_inode_is_orphan(dchild->d_inode));
67 down(&pending_dir->i_sem);
68 fidlen = ll_fid2str(fidname, dchild->d_inode->i_ino,
69 dchild->d_inode->i_generation);
71 CWARN("pending destroy of %dx open file %s = %s\n",
72 mds_open_orphan_count(dchild->d_inode),
73 rec->ur_name, fidname);
75 pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
76 if (IS_ERR(pending_child))
77 GOTO(out_lock, rc = PTR_ERR(pending_child));
79 if (pending_child->d_inode != NULL) {
80 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
81 LASSERT(pending_child->d_inode == dchild->d_inode);
82 GOTO(out_dput, rc = 0);
85 *handle = fsfilt_start(obd, pending_dir, FSFILT_OP_RENAME, NULL);
87 GOTO(out_dput, rc = PTR_ERR(*handle));
90 rc = vfs_rename(dparent->d_inode, dchild, pending_dir, pending_child);
93 CERROR("error renaming orphan %lu/%s to PENDING: rc = %d\n",
94 dparent->d_inode->i_ino, rec->ur_name, rc);
96 mds_inode_set_orphan(dchild->d_inode);
100 up(&pending_dir->i_sem);
104 static int mds_osc_destroy_orphan(struct mds_obd *mds,
105 struct ptlrpc_request *request)
107 struct mds_body *body;
108 struct lov_mds_md *lmm = NULL;
109 struct lov_stripe_md *lsm = NULL;
110 struct obd_trans_info oti = { 0 };
115 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
116 if (!(body->valid & OBD_MD_FLEASIZE))
118 if (body->eadatasize == 0) {
119 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
120 RETURN(rc = -EPROTO);
123 lmm = lustre_msg_buf(request->rq_repmsg, 1, body->eadatasize);
124 LASSERT(lmm != NULL);
126 rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, body->eadatasize);
128 CERROR("Error unpack md %p\n", lmm);
131 LASSERT(rc >= sizeof(*lsm));
137 GOTO(out_free_memmd, rc = -ENOMEM);
138 oa->o_id = lsm->lsm_object_id;
139 oa->o_mode = body->mode & S_IFMT;
140 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
142 if (body->valid & OBD_MD_FLCOOKIE) {
143 oa->o_valid |= OBD_MD_FLCOOKIE;
145 lustre_msg_buf(request->rq_repmsg, 2,
146 sizeof(struct llog_cookie) *
147 lsm->lsm_stripe_count);
148 if (oti.oti_logcookies == NULL)
149 oa->o_valid &= ~OBD_MD_FLCOOKIE;
150 body->valid &= ~OBD_MD_FLCOOKIE;
153 rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti);
156 CERROR("destroy orphan objid 0x"LPX64" on ost error "
157 "%d\n", lsm->lsm_object_id, rc);
159 obd_free_memmd(mds->mds_osc_exp, &lsm);
163 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
164 struct inode *inode, struct inode *pending_dir)
166 struct mds_obd *mds = &obd->u.mds;
167 struct mds_body *body;
169 struct ptlrpc_request *req;
170 int lengths[3] = {sizeof(struct mds_body),
172 mds->mds_max_cookiesize};
176 LASSERT(mds->mds_osc_obd != NULL);
177 OBD_ALLOC(req, sizeof(*req));
179 CERROR("request allocation out of memory\n");
180 GOTO(err_alloc_req, rc = -ENOMEM);
182 rc = lustre_pack_reply(req, 3, lengths, NULL);
184 CERROR("cannot pack request %d\n", rc);
185 GOTO(out_free_req, rc);
187 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
188 LASSERT(body != NULL);
190 mds_pack_inode2body(body, inode);
191 mds_pack_md(obd, req->rq_repmsg, 1, body, inode, 1);
193 handle = fsfilt_start(obd, pending_dir, FSFILT_OP_UNLINK_LOG, NULL);
194 if (IS_ERR(handle)) {
195 rc = PTR_ERR(handle);
196 CERROR("error fsfilt_start: %d\n", rc);
198 GOTO(out_free_msg, rc);
201 if (S_ISDIR(inode->i_mode)) {
202 rc = vfs_rmdir(pending_dir, dchild);
204 rc = vfs_unlink(pending_dir, dchild);
207 CERROR("error %d unlinking orphan %*s from PENDING directory\n",
208 rc, dchild->d_name.len, dchild->d_name.name);
210 if ((body->valid & OBD_MD_FLEASIZE)) {
211 if (mds_log_op_unlink(obd, inode, req->rq_repmsg, 1) > 0)
212 body->valid |= OBD_MD_FLCOOKIE;
216 int err = fsfilt_commit(obd, pending_dir, handle, 0);
218 CERROR("error committing orphan unlink: %d\n", err);
220 GOTO(out_free_msg, rc);
223 rc = mds_osc_destroy_orphan(mds, req);
225 OBD_FREE(req->rq_repmsg, req->rq_replen);
226 req->rq_repmsg = NULL;
228 OBD_FREE(req, sizeof(*req));
233 int mds_cleanup_orphans(struct obd_device *obd)
235 struct mds_obd *mds = &obd->u.mds;
236 struct obd_run_ctxt saved;
238 struct dentry *dchild;
239 struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
240 struct l_linux_dirent *dirent, *ptr;
241 unsigned int count = pending_dir->i_size;
242 int rc = 0, rc2 = 0, item = 0;
245 push_ctxt(&saved, &obd->obd_ctxt, NULL);
246 dget(mds->mds_pending_dir);
247 mntget(mds->mds_vfsmnt);
248 file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
249 O_RDONLY | O_LARGEFILE);
251 GOTO(err_open, rc2 = PTR_ERR(file));
253 OBD_ALLOC(dirent, count);
255 GOTO(err_alloc_dirent, rc2 = -ENOMEM);
257 rc = l_readdir(file, dirent, count);
260 GOTO(err_out, rc2 = rc);
262 for (ptr = dirent; (char *)ptr < (char *)dirent + rc;
263 (char *)ptr += ptr->d_reclen) {
264 int namlen = strlen(ptr->d_name);
266 if (((namlen == 1) && !strcmp(ptr->d_name, ".")) ||
267 ((namlen == 2) && !strcmp(ptr->d_name, "..")))
270 down(&pending_dir->i_sem);
271 dchild = lookup_one_len(ptr->d_name, mds->mds_pending_dir,
273 if (IS_ERR(dchild)) {
274 up(&pending_dir->i_sem);
275 GOTO(err_out, rc2 = PTR_ERR(dchild));
277 if (!dchild->d_inode) {
278 CDEBUG(D_ERROR, "orphan %s has been removed\n",
283 child_inode = dchild->d_inode;
284 if (mds_inode_is_orphan(child_inode) &&
285 mds_open_orphan_count(child_inode)) {
286 CWARN("orphan %s was re-opened during recovery\n",
291 rc2 = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
294 CWARN("removed orphan %s from MDS and OST\n",
298 up(&pending_dir->i_sem);
303 up(&pending_dir->i_sem);
306 OBD_FREE(dirent, count);
308 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
315 mntput(mds->mds_vfsmnt);
316 l_dput(mds->mds_pending_dir);