1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mds/mds_unlink_open.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Andreas Dilger <adilger@clusterfs.com>
40 * Author: Phil Schwan <phil@clusterfs.com>
43 /* code for handling open unlinked files */
45 #define DEBUG_SUBSYSTEM S_MDS
47 #ifndef AUTOCONF_INCLUDED
48 #include <linux/config.h>
50 #include <linux/module.h>
51 #include <linux/version.h>
53 #include <libcfs/list.h>
54 #include <obd_class.h>
55 #include <lustre_fsfilt.h>
56 #include <lustre_mds.h>
59 #include "mds_internal.h"
61 int mds_osc_destroy_orphan(struct obd_device *obd,
63 struct lov_mds_md *lmm,
65 struct llog_cookie *logcookies,
68 struct mds_obd *mds = &obd->u.mds;
69 struct lov_stripe_md *lsm = NULL;
70 struct obd_trans_info oti = { 0 };
78 rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
80 CERROR("Error unpack md %p\n", lmm);
83 LASSERT(rc >= sizeof(*lsm));
87 rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
89 GOTO(out_free_memmd, rc);
93 GOTO(out_free_memmd, rc = -ENOMEM);
94 oa->o_id = lsm->lsm_object_id;
95 oa->o_mode = mode & S_IFMT;
96 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
98 if (log_unlink && logcookies) {
99 oa->o_valid |= OBD_MD_FLCOOKIE;
100 oti.oti_logcookies = logcookies;
102 rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti, obd->obd_self_export);
105 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
106 "%d\n", lsm->lsm_object_id, rc);
108 obd_free_memmd(mds->mds_osc_exp, &lsm);
112 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
113 struct inode *inode, struct inode *pending_dir)
115 struct mds_obd *mds = &obd->u.mds;
116 struct lov_mds_md *lmm = NULL;
117 struct llog_cookie *logcookies = NULL;
118 int lmm_size, log_unlink = 0, cookie_size = 0;
124 LASSERT(mds->mds_osc_obd != NULL);
126 /* We don't need to do any of these other things for orhpan dirs,
127 * especially not mds_get_md (may get a default LOV EA, bug 4554) */
128 mode = inode->i_mode;
130 rc = ll_vfs_rmdir(pending_dir, dchild, mds->mds_vfsmnt);
132 CERROR("error %d unlinking dir %*s from PENDING\n",
133 rc, dchild->d_name.len, dchild->d_name.name);
137 lmm_size = mds->mds_max_mdsize;
138 OBD_ALLOC(lmm, lmm_size);
142 rc = mds_get_md(obd, inode, lmm, &lmm_size, 1, 0, 0);
144 GOTO(out_free_lmm, rc);
146 handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
147 le32_to_cpu(lmm->lmm_stripe_count));
148 if (IS_ERR(handle)) {
149 rc = PTR_ERR(handle);
150 CERROR("error fsfilt_start: %d\n", rc);
152 GOTO(out_free_lmm, rc);
155 rc = ll_vfs_unlink(pending_dir, dchild, mds->mds_vfsmnt);
157 CERROR("error %d unlinking orphan %.*s from PENDING\n",
158 rc, dchild->d_name.len, dchild->d_name.name);
159 } else if (lmm_size) {
160 cookie_size = mds_get_cookie_size(obd, lmm);
161 OBD_ALLOC(logcookies, cookie_size);
162 if (logcookies == NULL)
164 else if (mds_log_op_unlink(obd, lmm,lmm_size,logcookies,
169 err = fsfilt_commit(obd, pending_dir, handle, 0);
171 CERROR("error committing orphan unlink: %d\n", err);
175 rc = mds_osc_destroy_orphan(obd, mode, lmm, lmm_size,
176 logcookies, log_unlink);
179 if (logcookies != NULL)
180 OBD_FREE(logcookies, cookie_size);
182 OBD_FREE(lmm, mds->mds_max_mdsize);
186 static __u64 mds_orphans_max_version(struct obd_device *obd)
188 struct obd_export *exp;
189 __u32 epoch = lr_epoch(obd->u.mds.mds_last_transno);
190 spin_lock(&obd->obd_dev_lock);
191 list_for_each_entry(exp, &obd->obd_delayed_exports, exp_obd_chain) {
192 struct lu_export_data *led = &exp->exp_target_data;
193 epoch = min(epoch, le32_to_cpu(led->led_lcd->lcd_first_epoch));
195 spin_unlock(&obd->obd_dev_lock);
196 return (__u64)epoch << LR_EPOCH_BITS;
199 /* Delete inodes which were previously open-unlinked but were not reopened
200 * during MDS recovery for whatever reason (e.g. client also failed, recovery
202 int mds_cleanup_pending(struct obd_device *obd)
204 struct mds_obd *mds = &obd->u.mds;
205 struct lvfs_run_ctxt saved;
207 struct dentry *dchild, *dentry;
208 struct vfsmount *mnt;
209 struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
210 struct l_linux_dirent *dirent, *n;
211 struct list_head dentry_list;
212 char d_name[LL_FID_NAMELEN];
215 int i = 0, rc = 0, item = 0, namlen;
218 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
219 /* dentry and mnt ref dropped in dentry_open() on error, or
220 * in filp_close() if dentry_open() succeeds */
221 dentry = dget(mds->mds_pending_dir);
223 GOTO(err_pop, rc = PTR_ERR(dentry));
224 mnt = mntget(mds->mds_vfsmnt);
226 GOTO(err_mntget, rc = PTR_ERR(mnt));
228 file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
229 O_RDONLY | O_LARGEFILE);
231 GOTO(err_pop, rc = PTR_ERR(file));
233 CFS_INIT_LIST_HEAD(&dentry_list);
234 rc = l_readdir(file, &dentry_list);
239 /** Get maximum version for orphans to delete. All other orphans may be
240 * needed for delayed clients */
241 max_version = mds_orphans_max_version(obd);
243 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
247 list_del(&dirent->lld_list);
249 namlen = strlen(dirent->lld_name);
250 LASSERT(sizeof(d_name) >= namlen + 1);
251 strcpy(d_name, dirent->lld_name);
252 inum = dirent->lld_ino;
253 OBD_FREE_PTR(dirent);
255 CDEBUG(D_INODE, "entry %d of PENDING DIR: %s\n", i, d_name);
257 if (((namlen == 1) && !strcmp(d_name, ".")) ||
258 ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
261 LOCK_INODE_MUTEX(pending_dir);
262 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
263 if (IS_ERR(dchild)) {
264 UNLOCK_INODE_MUTEX(pending_dir);
265 GOTO(err_out, rc = PTR_ERR(dchild));
267 if (!dchild->d_inode) {
268 CWARN("%s: orphan %s has already been removed\n",
269 obd->obd_name, d_name);
273 if (is_bad_inode(dchild->d_inode)) {
274 CERROR("%s: bad orphan inode found %lu/%u\n",
275 obd->obd_name, dchild->d_inode->i_ino,
276 dchild->d_inode->i_generation);
277 GOTO(next, rc = -ENOENT);
280 child_inode = dchild->d_inode;
281 MDS_DOWN_READ_ORPHAN_SEM(child_inode);
282 if (mds_inode_is_orphan(child_inode) &&
283 mds_orphan_open_count(child_inode)) {
284 MDS_UP_READ_ORPHAN_SEM(child_inode);
285 CWARN("%s: orphan %s re-opened during recovery\n",
286 obd->obd_name, d_name);
289 /** Keep orphans for possible use by delayed exports. Remove
290 * orphans with version lower than minimal one of all exports */
291 version = fsfilt_get_version(obd, child_inode);
292 if ((__s64)version != -EOPNOTSUPP &&
293 version >= max_version) {
294 MDS_UP_READ_ORPHAN_SEM(child_inode);
296 "%s: orphan %s is needed for delayed exports\n",
297 obd->obd_name, d_name);
300 MDS_UP_READ_ORPHAN_SEM(child_inode);
302 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
303 CDEBUG(D_INODE, "%s: removed orphan %s: rc %d\n",
304 obd->obd_name, d_name, rc);
311 UNLOCK_INODE_MUTEX(pending_dir);
315 list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
316 list_del(&dirent->lld_list);
317 OBD_FREE(dirent, sizeof(*dirent));
320 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
322 CWARN("%s: removed %d pending open-unlinked files\n",
323 obd->obd_name, item);
327 l_dput(mds->mds_pending_dir);
332 * Determine there is no orphan with the same inode number. That may happens
333 * since unlink replay don't delete inode but keep orphan for delayed clients.
334 * Therefore replays like 'create, unlink, create' will fail due to inode can't
337 int mds_check_stale_orphan(struct obd_device *obd, struct ll_fid *fid)
339 struct mds_obd *mds = &obd->u.mds;
341 struct dentry *result;
342 struct inode *inode, *pending_dir = mds->mds_pending_dir->d_inode;
343 int fidlen = 0, rc = 0;
345 /* no need in checks*/
346 if (fid->id == 0 || obd->obd_recovering == 0)
349 /** open by fid like mds_fid2dentry does */
350 snprintf(fidname, sizeof(fidname), "0x%lx", (unsigned long)(fid->id));
351 fidlen = strlen(fidname);
352 result = mds_lookup(obd, fidname, mds->mds_fid_de, fidlen);
355 inode = result->d_inode;
359 LOCK_INODE_MUTEX(pending_dir);
360 MDS_DOWN_READ_ORPHAN_SEM(inode);
361 if (mds_inode_is_orphan(inode)) {
362 struct dentry *orphan;
364 /* The exactly same inode can't be orphan */
365 LASSERT(inode->i_generation != fid->generation);
367 if (mds_orphan_open_count(inode) > 0) {
368 CERROR("Orphan "LPU64"/%u is in use!\n",
369 fid->id, fid->generation);
370 GOTO(unlock_child, rc = -EFAULT);
373 /** Found orphan in pending dir and delete it */
374 fidlen = ll_fid2str(fidname, fid->id, inode->i_generation);
375 orphan = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
376 if (IS_ERR(orphan)) {
377 rc = PTR_ERR(orphan);
378 CERROR("error looking up %s in PENDING: rc = %d\n",
380 GOTO(unlock_child, rc);
382 if (orphan->d_inode != inode) {
384 CWARN("%s: Found wrong orphan %s %p/%p\n",
385 obd->obd_name, fidname, orphan->d_inode, inode);
386 GOTO(unlock_child, rc = -EFAULT);
388 MDS_UP_READ_ORPHAN_SEM(inode);
390 rc = mds_unlink_orphan(obd, orphan, inode, pending_dir);
391 CDEBUG(D_INODE, "%s: removed orphan %s: rc %d\n",
392 obd->obd_name, fidname, rc);
397 MDS_UP_READ_ORPHAN_SEM(inode);
399 UNLOCK_INODE_MUTEX(pending_dir);