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, 2014, 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)
98 struct dt_object *dor = mdd->mdd_orphans;
99 dt_write_lock(env, dor, MOR_TGT_ORPHAN);
102 static inline void mdd_orphan_write_unlock(const struct lu_env *env,
103 struct mdd_device *mdd)
105 struct dt_object *dor = mdd->mdd_orphans;
106 dt_write_unlock(env, dor);
109 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
110 struct mdd_device *mdd,
111 struct mdd_object *obj,
112 __u32 op, struct thandle *th)
114 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
115 struct dt_object *dor = mdd->mdd_orphans;
116 const struct lu_fid *lf = mdo2fid(obj);
117 struct dt_key *key = orph_key_fill(env, lf, op);
120 rec->rec_type = mdd_object_type(obj);
122 return dt_insert(env, dor, (const struct dt_rec *)rec, key, th, 1);
125 static inline int mdd_orphan_delete_obj(const struct lu_env *env,
126 struct mdd_device *mdd ,
130 struct dt_object *dor = mdd->mdd_orphans;
132 return dt_delete(env, dor, key, th);
135 static inline int mdd_orphan_ref_add(const struct lu_env *env,
136 struct mdd_device *mdd,
139 struct dt_object *dor = mdd->mdd_orphans;
140 return dt_ref_add(env, dor, th);
143 static inline int mdd_orphan_ref_del(const struct lu_env *env,
144 struct mdd_device *mdd,
147 struct dt_object *dor = mdd->mdd_orphans;
148 return dt_ref_del(env, dor, th);
152 int orph_declare_index_insert(const struct lu_env *env,
153 struct mdd_object *obj,
154 umode_t mode, struct thandle *th)
156 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
157 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
161 key = orph_key_fill(env, mdo2fid(obj), ORPH_OP_UNLINK);
163 rec->rec_fid = mdo2fid(obj);
164 rec->rec_type = mode;
165 rc = dt_declare_insert(env, mdd->mdd_orphans,
166 (const struct dt_rec *)rec, key, th);
170 rc = mdo_declare_ref_add(env, obj, th);
177 rc = mdo_declare_ref_add(env, obj, th);
181 rc = dt_declare_ref_add(env, mdd->mdd_orphans, th);
185 rc = mdo_declare_index_delete(env, obj, dotdot, th);
189 rc = mdo_declare_index_insert(env, obj,
190 lu_object_fid(&mdd->mdd_orphans->do_lu),
191 S_IFDIR, dotdot, th);
196 static int orph_index_insert(const struct lu_env *env,
197 struct mdd_object *obj,
198 __u32 op, struct thandle *th)
200 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
201 struct dt_object *dor = mdd->mdd_orphans;
202 const struct lu_fid *lf_dor = lu_object_fid(&dor->do_lu);
203 struct dt_object *next = mdd_object_child(obj);
204 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
208 LASSERT(mdd_write_locked(env, obj) != 0);
209 LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
211 mdd_orphan_write_lock(env, mdd);
213 rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
217 mdo_ref_add(env, obj, th);
218 if (!S_ISDIR(mdd_object_type(obj)))
221 mdo_ref_add(env, obj, th);
222 mdd_orphan_ref_add(env, mdd, th);
224 /* try best to fixup directory, dont return errors
226 if (!dt_try_as_dir(env, next))
229 dt_delete(env, next, (const struct dt_key *)dotdot, th);
231 rec->rec_fid = lf_dor;
232 rec->rec_type = S_IFDIR;
233 dt_insert(env, next, (const struct dt_rec *)rec,
234 (const struct dt_key *)dotdot, th, 1);
238 obj->mod_flags |= ORPHAN_OBJ;
240 mdd_orphan_write_unlock(env, mdd);
245 int orph_declare_index_delete(const struct lu_env *env,
246 struct mdd_object *obj,
250 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
254 key = orph_key_fill(env, mdo2fid(obj), ORPH_OP_UNLINK);
256 rc = dt_declare_delete(env, mdd->mdd_orphans, key, th);
260 rc = mdo_declare_ref_del(env, obj, th);
264 if (S_ISDIR(mdd_object_type(obj))) {
265 rc = mdo_declare_ref_del(env, obj, th);
269 rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
275 static int orph_index_delete(const struct lu_env *env,
276 struct mdd_object *obj,
280 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
281 struct dt_object *dor = mdd->mdd_orphans;
287 LASSERT(mdd_write_locked(env, obj) != 0);
288 LASSERT(obj->mod_flags & ORPHAN_OBJ);
289 LASSERT(obj->mod_count == 0);
293 key = orph_key_fill(env, mdo2fid(obj), op);
294 mdd_orphan_write_lock(env, mdd);
296 rc = mdd_orphan_delete_obj(env, mdd, key, th);
299 key = orph_key_fill_18(env, mdo2fid(obj));
300 rc = mdd_orphan_delete_obj(env, mdd, key, th);
304 /* lov objects will be destroyed by caller */
305 mdo_ref_del(env, obj, th);
306 if (S_ISDIR(mdd_object_type(obj))) {
307 mdo_ref_del(env, obj, th);
308 mdd_orphan_ref_del(env, mdd, th);
310 obj->mod_flags &= ~ORPHAN_OBJ;
312 CERROR("could not delete object: rc = %d\n",rc);
315 mdd_orphan_write_unlock(env, mdd);
320 static int orphan_object_destroy(const struct lu_env *env,
321 struct mdd_object *obj,
324 struct thandle *th = NULL;
325 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
329 th = mdd_trans_create(env, mdd);
331 CERROR("Cannot get thandle\n");
335 rc = orph_declare_index_delete(env, obj, th);
339 rc = mdo_declare_destroy(env, obj, th);
343 rc = mdd_trans_start(env, mdd, th);
347 mdd_write_lock(env, obj, MOR_TGT_CHILD);
348 if (likely(obj->mod_count == 0)) {
349 mdd_orphan_write_lock(env, mdd);
350 rc = mdd_orphan_delete_obj(env, mdd, key, th);
352 mdo_ref_del(env, obj, th);
353 if (S_ISDIR(mdd_object_type(obj))) {
354 mdo_ref_del(env, obj, th);
355 mdd_orphan_ref_del(env, mdd, th);
357 rc = mdo_destroy(env, obj, th);
359 CERROR("could not delete object: rc = %d\n",rc);
360 mdd_orphan_write_unlock(env, mdd);
362 mdd_write_unlock(env, obj);
365 mdd_trans_stop(env, mdd, 0, th);
371 * Delete unused orphan with FID \a lf from PENDING directory
373 * \param mdd MDD device finishing recovery
374 * \param lf FID of file or directory to delete
375 * \param key cookie for this entry in index iterator
380 static int orph_key_test_and_del(const struct lu_env *env,
381 struct mdd_device *mdd,
385 struct mdd_object *mdo;
388 mdo = mdd_object_find(env, mdd, lf);
394 if (mdo->mod_count == 0) {
395 CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
396 rc = orphan_object_destroy(env, mdo, key);
397 if (rc) /* so replay-single.sh test_37 works */
398 CERROR("%s: error unlinking orphan "DFID" from "
399 "PENDING: rc = %d\n",
400 mdd2obd_dev(mdd)->obd_name, PFID(lf), rc);
402 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
403 if (likely(mdo->mod_count > 0)) {
404 CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
405 PFID(lf), mdo->mod_count);
406 mdo->mod_flags |= ORPHAN_OBJ;
408 mdd_write_unlock(env, mdo);
411 mdd_object_put(env, mdo);
416 * delete unreferenced files and directories in the PENDING directory
418 * Files that remain in PENDING after client->MDS recovery has completed
419 * have to be referenced (opened) by some client during recovery, or they
420 * will be deleted here (for clients that did not complete recovery).
422 * \param thread info about orphan cleanup thread
427 static int orph_index_iterate(const struct lu_env *env,
428 struct mdd_generic_thread *thread)
430 struct mdd_device *mdd = (struct mdd_device *)thread->mgt_data;
431 struct dt_object *dor = mdd->mdd_orphans;
432 struct lu_dirent *ent = &mdd_env_info(env)->mti_ent;
433 const struct dt_it_ops *iops;
441 iops = &dor->do_index_ops->dio_it;
442 it = iops->init(env, dor, LUDA_64BITHASH);
445 CERROR("%s: cannot clean PENDING: rc = %d\n",
446 mdd2obd_dev(mdd)->obd_name, rc);
450 rc = iops->load(env, it, 0);
454 CERROR("%s: error loading iterator to clean PENDING\n",
455 mdd2obd_dev(mdd)->obd_name);
456 /* Index contains no zero key? */
457 GOTO(out_put, rc = -EIO);
461 if (thread->mgt_abort)
464 key_sz = iops->key_size(env, it);
465 /* filter out "." and ".." entries from PENDING dir. */
469 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
471 CERROR("%s: fail to get FID for orphan it: rc = %d\n",
472 mdd2obd_dev(mdd)->obd_name, rc);
476 fid_le_to_cpu(&fid, &ent->lde_fid);
477 if (!fid_is_sane(&fid)) {
478 CERROR("%s: bad FID "DFID" cleaning PENDING\n",
479 mdd2obd_dev(mdd)->obd_name, PFID(&fid));
483 /* kill orphan object */
484 cookie = iops->store(env, it);
486 rc = orph_key_test_and_del(env, mdd, &fid,
487 (struct dt_key *)ent->lde_name);
489 /* after index delete reset iterator */
491 rc = iops->get(env, it, (const void *)"");
493 rc = iops->load(env, it, cookie);
495 rc = iops->next(env, it);
498 GOTO(out_put, rc = 0);
508 * open the PENDING directory for device \a mdd
510 * The PENDING directory persistently tracks files and directories that were
511 * unlinked from the namespace (nlink == 0) but are still held open by clients.
512 * Those inodes shouldn't be deleted if the MDS crashes, because the clients
513 * would not be able to recover and reopen those files. Instead, these inodes
514 * are linked into the PENDING directory on disk, and only deleted if all
515 * clients close them, or the MDS finishes client recovery without any client
516 * reopening them (i.e. former clients didn't join recovery).
517 * \param d mdd device being started.
520 * \retval -ve index operation error.
523 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
531 /* create PENDING dir */
533 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
534 orph_index_name, S_IFDIR | S_IRUGO |
535 S_IWUSR | S_IXUGO, &fid);
539 d = dt_locate(env, mdd->mdd_child, &fid);
542 LASSERT(lu_object_exists(&d->do_lu));
543 if (!dt_try_as_dir(env, d)) {
544 CERROR("%s: \"%s\" is not an index: rc = %d\n",
545 mdd2obd_dev(mdd)->obd_name, orph_index_name, rc);
546 lu_object_put(env, &d->do_lu);
549 mdd->mdd_orphans = d;
553 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
556 if (mdd->mdd_orphans != NULL) {
557 lu_object_put(env, &mdd->mdd_orphans->do_lu);
558 mdd->mdd_orphans = NULL;
563 static int __mdd_orphan_cleanup(void *args)
565 struct mdd_generic_thread *thread = (struct mdd_generic_thread *)args;
566 struct lu_env *env = NULL;
570 complete(&thread->mgt_started);
574 GOTO(out, rc = -ENOMEM);
576 rc = lu_env_init(env, LCT_MD_THREAD);
580 rc = orph_index_iterate(env, thread);
587 complete(&thread->mgt_finished);
592 * Iterate orphan index to cleanup orphan objects after recovery is done.
593 * \param d mdd device in recovery.
595 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
600 OBD_ALLOC(name, MTI_NAME_MAXLEN);
604 snprintf(name, MTI_NAME_MAXLEN, "orph_cleanup_%s",
605 mdd2obd_dev(d)->obd_name);
607 rc = mdd_generic_thread_start(&d->mdd_orph_cleanup_thread,
608 __mdd_orphan_cleanup, (void *)d, name);
611 CERROR("%s: start orphan cleanup thread failed:%d\n",
612 mdd2obd_dev(d)->obd_name, rc);
614 OBD_FREE(name, MTI_NAME_MAXLEN);
619 * add an orphan \a obj to the orphan index.
620 * \param obj file or directory.
621 * \param th transaction for index insert.
623 * \pre obj nlink == 0 && obj->mod_count != 0
626 * \retval -ve index operation error.
628 int __mdd_orphan_add(const struct lu_env *env,
629 struct mdd_object *obj, struct thandle *th)
631 return orph_index_insert(env, obj, ORPH_OP_UNLINK, th);
635 * delete an orphan \a obj from orphan index.
636 * \param obj file or directory.
637 * \param th transaction for index deletion and object destruction.
639 * \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
642 * \retval -ve index operation error.
644 int __mdd_orphan_del(const struct lu_env *env,
645 struct mdd_object *obj, struct thandle *th)
647 return orph_index_delete(env, obj, ORPH_OP_UNLINK, th);