4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_orphans.c
38 * Orphan handling code
40 * Author: Mike Pershin <tappro@clusterfs.com>
41 * Pravin B Shelar <pravin.shelar@sun.com>
44 #define DEBUG_SUBSYSTEM S_MDS
47 #include <obd_class.h>
48 #include <lustre_ver.h>
49 #include <obd_support.h>
50 #include <lustre_fid.h>
51 #include "mdd_internal.h"
53 const char orph_index_name[] = "PENDING";
54 static const char dotdot[] = "..";
61 #define ORPHAN_FILE_NAME_FORMAT "%016llx:%08x:%08x:%2x"
62 #define ORPHAN_FILE_NAME_FORMAT_18 "%llx:%08x"
64 static struct dt_key* orph_key_fill(const struct lu_env *env,
65 const struct lu_fid *lf, __u32 op)
67 char *key = mdd_env_info(env)->mti_key;
71 rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT,
72 (long long unsigned int)fid_seq(lf),
73 fid_oid(lf), fid_ver(lf), op);
75 return (struct dt_key*) key;
80 static struct dt_key* orph_key_fill_18(const struct lu_env *env,
81 const struct lu_fid *lf)
83 char *key = mdd_env_info(env)->mti_key;
87 rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT_18,
88 (unsigned long long)fid_seq(lf), fid_oid(lf));
90 return (struct dt_key*) key;
95 static inline void mdd_orphan_write_lock(const struct lu_env *env,
96 struct mdd_device *mdd)
99 struct dt_object *dor = mdd->mdd_orphans;
100 dor->do_ops->do_write_lock(env, dor, MOR_TGT_ORPHAN);
103 static inline void mdd_orphan_write_unlock(const struct lu_env *env,
104 struct mdd_device *mdd)
107 struct dt_object *dor = mdd->mdd_orphans;
108 dor->do_ops->do_write_unlock(env, dor);
111 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
112 struct mdd_device *mdd,
113 struct mdd_object *obj,
114 __u32 op, struct thandle *th)
116 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
117 struct dt_object *dor = mdd->mdd_orphans;
118 const struct lu_fid *lf = mdo2fid(obj);
119 struct dt_key *key = orph_key_fill(env, lf, op);
122 rec->rec_type = mdd_object_type(obj);
124 return dor->do_index_ops->dio_insert(env, dor,
125 (const struct dt_rec *)rec,
126 key, th, BYPASS_CAPA, 1);
129 static inline int mdd_orphan_delete_obj(const struct lu_env *env,
130 struct mdd_device *mdd ,
134 struct dt_object *dor = mdd->mdd_orphans;
136 return dor->do_index_ops->dio_delete(env, dor,
141 static inline void mdd_orphan_ref_add(const struct lu_env *env,
142 struct mdd_device *mdd,
145 struct dt_object *dor = mdd->mdd_orphans;
146 dor->do_ops->do_ref_add(env, dor, th);
149 static inline void mdd_orphan_ref_del(const struct lu_env *env,
150 struct mdd_device *mdd,
153 struct dt_object *dor = mdd->mdd_orphans;
154 dor->do_ops->do_ref_del(env, dor, th);
158 int orph_declare_index_insert(const struct lu_env *env,
159 struct mdd_object *obj,
160 umode_t mode, struct thandle *th)
162 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
163 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
167 key = orph_key_fill(env, mdo2fid(obj), ORPH_OP_UNLINK);
169 rec->rec_fid = mdo2fid(obj);
170 rec->rec_type = mode;
171 rc = dt_declare_insert(env, mdd->mdd_orphans,
172 (const struct dt_rec *)rec, key, th);
176 rc = mdo_declare_ref_add(env, obj, th);
183 rc = mdo_declare_ref_add(env, obj, th);
187 rc = dt_declare_ref_add(env, mdd->mdd_orphans, th);
191 rc = mdo_declare_index_delete(env, obj, dotdot, th);
195 rc = mdo_declare_index_insert(env, obj,
196 lu_object_fid(&mdd->mdd_orphans->do_lu),
197 S_IFDIR, dotdot, th);
202 static int orph_index_insert(const struct lu_env *env,
203 struct mdd_object *obj,
204 __u32 op, struct thandle *th)
206 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
207 struct dt_object *dor = mdd->mdd_orphans;
208 const struct lu_fid *lf_dor = lu_object_fid(&dor->do_lu);
209 struct dt_object *next = mdd_object_child(obj);
210 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
214 LASSERT(mdd_write_locked(env, obj) != 0);
215 LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
217 mdd_orphan_write_lock(env, mdd);
219 rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
223 mdo_ref_add(env, obj, th);
224 if (!S_ISDIR(mdd_object_type(obj)))
227 mdo_ref_add(env, obj, th);
228 mdd_orphan_ref_add(env, mdd, th);
230 /* try best to fixup directory, dont return errors
232 if (!dt_try_as_dir(env, next))
234 next->do_index_ops->dio_delete(env, next,
235 (const struct dt_key *)dotdot,
238 rec->rec_fid = lf_dor;
239 rec->rec_type = S_IFDIR;
240 next->do_index_ops->dio_insert(env, next, (const struct dt_rec *)rec,
241 (const struct dt_key *)dotdot,
246 obj->mod_flags |= ORPHAN_OBJ;
248 mdd_orphan_write_unlock(env, mdd);
253 int orph_declare_index_delete(const struct lu_env *env,
254 struct mdd_object *obj,
258 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
262 key = orph_key_fill(env, mdo2fid(obj), ORPH_OP_UNLINK);
264 rc = dt_declare_delete(env, mdd->mdd_orphans, key, th);
268 rc = mdo_declare_ref_del(env, obj, th);
272 if (S_ISDIR(mdd_object_type(obj))) {
273 rc = mdo_declare_ref_del(env, obj, th);
277 rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
283 static int orph_index_delete(const struct lu_env *env,
284 struct mdd_object *obj,
288 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
289 struct dt_object *dor = mdd->mdd_orphans;
295 LASSERT(mdd_write_locked(env, obj) != 0);
296 LASSERT(obj->mod_flags & ORPHAN_OBJ);
297 LASSERT(obj->mod_count == 0);
301 key = orph_key_fill(env, mdo2fid(obj), op);
302 mdd_orphan_write_lock(env, mdd);
304 rc = mdd_orphan_delete_obj(env, mdd, key, th);
307 key = orph_key_fill_18(env, mdo2fid(obj));
308 rc = mdd_orphan_delete_obj(env, mdd, key, th);
312 /* lov objects will be destroyed by caller */
313 mdo_ref_del(env, obj, th);
314 if (S_ISDIR(mdd_object_type(obj))) {
315 mdo_ref_del(env, obj, th);
316 mdd_orphan_ref_del(env, mdd, th);
318 obj->mod_flags &= ~ORPHAN_OBJ;
320 CERROR("could not delete object: rc = %d\n",rc);
323 mdd_orphan_write_unlock(env, mdd);
328 static int orphan_object_destroy(const struct lu_env *env,
329 struct mdd_object *obj,
332 struct thandle *th = NULL;
333 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
337 th = mdd_trans_create(env, mdd);
339 CERROR("Cannot get thandle\n");
343 rc = orph_declare_index_delete(env, obj, th);
347 rc = mdo_declare_destroy(env, obj, th);
351 rc = mdd_trans_start(env, mdd, th);
355 mdd_write_lock(env, obj, MOR_TGT_CHILD);
356 if (likely(obj->mod_count == 0)) {
357 mdd_orphan_write_lock(env, mdd);
358 rc = mdd_orphan_delete_obj(env, mdd, key, th);
360 mdo_ref_del(env, obj, th);
361 if (S_ISDIR(mdd_object_type(obj))) {
362 mdo_ref_del(env, obj, th);
363 mdd_orphan_ref_del(env, mdd, th);
365 rc = mdo_destroy(env, obj, th);
367 CERROR("could not delete object: rc = %d\n",rc);
368 mdd_orphan_write_unlock(env, mdd);
370 mdd_write_unlock(env, obj);
373 mdd_trans_stop(env, mdd, 0, th);
379 * Delete unused orphan with FID \a lf from PENDING directory
381 * \param mdd MDD device finishing recovery
382 * \param lf FID of file or directory to delete
383 * \param key cookie for this entry in index iterator
388 static int orph_key_test_and_del(const struct lu_env *env,
389 struct mdd_device *mdd,
393 struct mdd_object *mdo;
396 mdo = mdd_object_find(env, mdd, lf);
402 if (mdo->mod_count == 0) {
403 CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
404 rc = orphan_object_destroy(env, mdo, key);
405 if (rc) /* so replay-single.sh test_37 works */
406 CERROR("%s: error unlinking orphan "DFID" from "
407 "PENDING: rc = %d\n",
408 mdd2obd_dev(mdd)->obd_name, PFID(lf), rc);
410 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
411 if (likely(mdo->mod_count > 0)) {
412 CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
413 PFID(lf), mdo->mod_count);
414 mdo->mod_flags |= ORPHAN_OBJ;
416 mdd_write_unlock(env, mdo);
419 mdd_object_put(env, mdo);
424 * delete unreferenced files and directories in the PENDING directory
426 * Files that remain in PENDING after client->MDS recovery has completed
427 * have to be referenced (opened) by some client during recovery, or they
428 * will be deleted here (for clients that did not complete recovery).
430 * \param thread info about orphan cleanup thread
435 static int orph_index_iterate(const struct lu_env *env,
436 struct mdd_generic_thread *thread)
438 struct mdd_device *mdd = (struct mdd_device *)thread->mgt_data;
439 struct dt_object *dor = mdd->mdd_orphans;
440 struct lu_dirent *ent = &mdd_env_info(env)->mti_ent;
441 const struct dt_it_ops *iops;
449 iops = &dor->do_index_ops->dio_it;
450 it = iops->init(env, dor, LUDA_64BITHASH, BYPASS_CAPA);
453 CERROR("%s: cannot clean PENDING: rc = %d\n",
454 mdd2obd_dev(mdd)->obd_name, rc);
458 rc = iops->load(env, it, 0);
462 CERROR("%s: error loading iterator to clean PENDING\n",
463 mdd2obd_dev(mdd)->obd_name);
464 /* Index contains no zero key? */
465 GOTO(out_put, rc = -EIO);
469 if (thread->mgt_abort)
472 key_sz = iops->key_size(env, it);
473 /* filter out "." and ".." entries from PENDING dir. */
477 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
479 CERROR("%s: fail to get FID for orphan it: rc = %d\n",
480 mdd2obd_dev(mdd)->obd_name, rc);
484 fid_le_to_cpu(&fid, &ent->lde_fid);
485 if (!fid_is_sane(&fid)) {
486 CERROR("%s: bad FID "DFID" cleaning PENDING\n",
487 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
491 /* kill orphan object */
492 cookie = iops->store(env, it);
494 rc = orph_key_test_and_del(env, mdd, &fid,
495 (struct dt_key *)ent->lde_name);
497 /* after index delete reset iterator */
499 rc = iops->get(env, it, (const void *)"");
501 rc = iops->load(env, it, cookie);
503 rc = iops->next(env, it);
506 GOTO(out_put, rc = 0);
516 * open the PENDING directory for device \a mdd
518 * The PENDING directory persistently tracks files and directories that were
519 * unlinked from the namespace (nlink == 0) but are still held open by clients.
520 * Those inodes shouldn't be deleted if the MDS crashes, because the clients
521 * would not be able to recover and reopen those files. Instead, these inodes
522 * are linked into the PENDING directory on disk, and only deleted if all
523 * clients close them, or the MDS finishes client recovery without any client
524 * reopening them (i.e. former clients didn't join recovery).
525 * \param d mdd device being started.
528 * \retval -ve index operation error.
531 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
539 /* create PENDING dir */
541 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
542 orph_index_name, S_IFDIR | S_IRUGO |
543 S_IWUSR | S_IXUGO, &fid);
547 d = dt_locate(env, mdd->mdd_child, &fid);
550 LASSERT(lu_object_exists(&d->do_lu));
551 if (!dt_try_as_dir(env, d)) {
552 CERROR("%s: \"%s\" is not an index: rc = %d\n",
553 mdd2obd_dev(mdd)->obd_name, orph_index_name, rc);
554 lu_object_put(env, &d->do_lu);
557 mdd->mdd_orphans = d;
561 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
564 if (mdd->mdd_orphans != NULL) {
565 lu_object_put(env, &mdd->mdd_orphans->do_lu);
566 mdd->mdd_orphans = NULL;
571 static int __mdd_orphan_cleanup(void *args)
573 struct mdd_generic_thread *thread = (struct mdd_generic_thread *)args;
574 struct lu_env *env = NULL;
578 complete(&thread->mgt_started);
582 GOTO(out, rc = -ENOMEM);
584 rc = lu_env_init(env, LCT_MD_THREAD);
588 rc = orph_index_iterate(env, thread);
595 complete(&thread->mgt_finished);
600 * Iterate orphan index to cleanup orphan objects after recovery is done.
601 * \param d mdd device in recovery.
603 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
608 OBD_ALLOC(name, MTI_NAME_MAXLEN);
612 snprintf(name, MTI_NAME_MAXLEN, "orph_cleanup_%s",
613 mdd2obd_dev(d)->obd_name);
615 rc = mdd_generic_thread_start(&d->mdd_orph_cleanup_thread,
616 __mdd_orphan_cleanup, (void *)d, name);
619 CERROR("%s: start orphan cleanup thread failed:%d\n",
620 mdd2obd_dev(d)->obd_name, rc);
622 OBD_FREE(name, MTI_NAME_MAXLEN);
627 * add an orphan \a obj to the orphan index.
628 * \param obj file or directory.
629 * \param th transaction for index insert.
631 * \pre obj nlink == 0 && obj->mod_count != 0
634 * \retval -ve index operation error.
636 int __mdd_orphan_add(const struct lu_env *env,
637 struct mdd_object *obj, struct thandle *th)
639 return orph_index_insert(env, obj, ORPH_OP_UNLINK, th);
643 * delete an orphan \a obj from orphan index.
644 * \param obj file or directory.
645 * \param th transaction for index deletion and object destruction.
647 * \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
650 * \retval -ve index operation error.
652 int __mdd_orphan_del(const struct lu_env *env,
653 struct mdd_object *obj, struct thandle *th)
655 return orph_index_delete(env, obj, ORPH_OP_UNLINK, th);