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 #ifdef ENABLE_ORPHANS
143 if (body->valid & OBD_MD_FLCOOKIE) {
144 oa->o_valid |= OBD_MD_FLCOOKIE;
146 lustre_msg_buf(request->rq_repmsg, 2,
147 sizeof(struct llog_cookie) *
148 lsm->lsm_stripe_count);
149 if (oti.oti_logcookies == NULL)
150 oa->o_valid &= ~OBD_MD_FLCOOKIE;
151 body->valid &= ~OBD_MD_FLCOOKIE;
155 rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti);
158 CERROR("destroy orphan objid 0x"LPX64" on ost error "
159 "%d\n", lsm->lsm_object_id, rc);
161 obd_free_memmd(mds->mds_osc_exp, &lsm);
165 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
166 struct inode *inode, struct inode *pending_dir)
168 struct mds_obd *mds = &obd->u.mds;
169 struct mds_body *body;
171 struct ptlrpc_request *req;
172 int lengths[3] = {sizeof(struct mds_body),
174 mds->mds_max_cookiesize};
178 LASSERT(mds->mds_osc_obd != NULL);
179 OBD_ALLOC(req, sizeof(*req));
181 CERROR("request allocation out of memory\n");
182 GOTO(err_alloc_req, rc = -ENOMEM);
184 rc = lustre_pack_reply(req, 3, lengths, NULL);
186 CERROR("cannot pack request %d\n", rc);
187 GOTO(out_free_req, rc);
189 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
190 LASSERT(body != NULL);
192 mds_pack_inode2body(body, inode);
193 mds_pack_md(obd, req->rq_repmsg, 1, body, inode, 1);
195 handle = fsfilt_start(obd, pending_dir, FSFILT_OP_UNLINK_LOG, NULL);
196 if (IS_ERR(handle)) {
197 rc = PTR_ERR(handle);
198 CERROR("error fsfilt_start: %d\n", rc);
200 GOTO(out_free_msg, rc);
202 rc = vfs_unlink(pending_dir, dchild);
204 CERROR("error %d unlinking orphan %*s from PENDING directory\n",
205 rc, dchild->d_name.len, dchild->d_name.name);
207 #ifdef ENABLE_ORPHANS
208 if ((body->valid & OBD_MD_FLEASIZE)) {
209 if (mds_log_op_unlink(obd, inode, req->rq_repmsg, 1) > 0)
210 body->valid |= OBD_MD_FLCOOKIE;
214 int err = fsfilt_commit(obd, pending_dir, handle, 0);
216 CERROR("error committing orphan unlink: %d\n", err);
218 GOTO(out_free_msg, rc);
221 rc = mds_osc_destroy_orphan(mds, req);
223 OBD_FREE(req->rq_repmsg, req->rq_replen);
224 req->rq_repmsg = NULL;
226 OBD_FREE(req, sizeof(*req));
231 int mds_cleanup_orphans(struct obd_device *obd)
233 struct mds_obd *mds = &obd->u.mds;
234 struct obd_run_ctxt saved;
236 struct dentry *dchild;
237 struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
238 struct l_linux_dirent *dirent, *ptr;
239 unsigned int count = pending_dir->i_size;
240 int rc = 0, rc2 = 0, item = 0;
243 push_ctxt(&saved, &obd->obd_ctxt, NULL);
244 dget(mds->mds_pending_dir);
245 mntget(mds->mds_vfsmnt);
246 file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
247 O_RDONLY | O_LARGEFILE);
249 GOTO(err_open, rc2 = PTR_ERR(file));
251 OBD_ALLOC(dirent, count);
253 GOTO(err_alloc_dirent, rc2 = -ENOMEM);
255 rc = l_readdir(file, dirent, count);
258 GOTO(err_out, rc2 = rc);
260 for (ptr = dirent; (char *)ptr < (char *)dirent + rc;
261 (char *)ptr += ptr->d_reclen) {
262 int namlen = strlen(ptr->d_name);
264 if (((namlen == 1) && !strcmp(ptr->d_name, ".")) ||
265 ((namlen == 2) && !strcmp(ptr->d_name, "..")))
268 down(&pending_dir->i_sem);
269 dchild = lookup_one_len(ptr->d_name, mds->mds_pending_dir,
271 if (IS_ERR(dchild)) {
272 up(&pending_dir->i_sem);
273 GOTO(err_out, rc2 = PTR_ERR(dchild));
275 if (!dchild->d_inode) {
276 CDEBUG(D_ERROR, "orphan %s has been removed\n",
281 child_inode = dchild->d_inode;
282 if (mds_inode_is_orphan(child_inode) &&
283 mds_open_orphan_count(child_inode)) {
284 CWARN("orphan %s was re-opened during recovery\n",
289 rc2 = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
292 CWARN("removed orphan %s from MDS and OST\n",
296 up(&pending_dir->i_sem);
301 up(&pending_dir->i_sem);
304 OBD_FREE(dirent, count);
306 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
313 mntput(mds->mds_vfsmnt);
314 l_dput(mds->mds_pending_dir);