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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdd/mdd_orphans.c
34 * Orphan handling code
36 * Author: Mike Pershin <tappro@clusterfs.com>
37 * Pravin B Shelar <pravin.shelar@sun.com>
40 #define DEBUG_SUBSYSTEM S_MDS
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_fid.h>
46 #include "mdd_internal.h"
48 static const char mdd_orphan_index_name[] = MDT_ORPHAN_DIR;
49 static const char dotdot[] = "..";
55 /* obsolete after 2.11, needed for upgrades from older 2.x versions */
56 #define ORPHAN_FILE_NAME_FORMAT_20 "%016llx:%08x:%08x:%2x"
58 static struct dt_key *mdd_orphan_key_fill(const struct lu_env *env,
59 const struct lu_fid *lf)
61 char *key = mdd_env_info(env)->mti_key;
64 if (!(MTI_KEEP_KEY & mdd_env_info(env)->mti_flags))
65 snprintf(key, sizeof(mdd_env_info(env)->mti_key),
66 DFID_NOBRACE, PFID(lf));
68 return (struct dt_key *)key;
71 /* compatibility with orphan files created in versions before 2.11 */
72 static struct dt_key *mdd_orphan_key_fill_20(const struct lu_env *env,
73 const struct lu_fid *lf)
75 char *key = mdd_env_info(env)->mti_key;
78 if (!(MTI_KEEP_KEY & mdd_env_info(env)->mti_flags))
79 snprintf(key, sizeof(mdd_env_info(env)->mti_key),
80 ORPHAN_FILE_NAME_FORMAT_20,
81 fid_seq(lf), fid_oid(lf), fid_ver(lf),
84 return (struct dt_key *)key;
87 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
88 struct mdd_device *mdd,
89 struct mdd_object *obj,
92 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
93 struct dt_object *dor = mdd->mdd_orphans;
94 const struct lu_fid *lf = mdo2fid(obj);
95 struct dt_key *key = mdd_orphan_key_fill(env, lf);
98 rec->rec_type = mdd_object_type(obj);
100 return dt_insert(env, dor, (const struct dt_rec *)rec, key, th);
103 int mdd_orphan_declare_insert(const struct lu_env *env, struct mdd_object *obj,
104 umode_t mode, struct thandle *th)
106 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
107 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
111 key = mdd_orphan_key_fill(env, mdo2fid(obj));
113 rec->rec_fid = mdo2fid(obj);
114 rec->rec_type = mode;
115 rc = dt_declare_insert(env, mdd->mdd_orphans,
116 (const struct dt_rec *)rec, key, th);
120 rc = mdo_declare_ref_add(env, obj, th);
127 rc = mdo_declare_ref_add(env, obj, th);
131 rc = dt_declare_ref_add(env, mdd->mdd_orphans, th);
135 rc = mdo_declare_index_delete(env, obj, dotdot, th);
139 rc = mdo_declare_index_insert(env, obj,
140 lu_object_fid(&mdd->mdd_orphans->do_lu),
141 S_IFDIR, dotdot, th);
147 * add an orphan \a obj to the orphan index.
148 * \param obj file or directory.
149 * \param th transaction for index insert.
151 * \pre obj nlink == 0 && obj->mod_count != 0
154 * \retval -ve index operation error.
156 int mdd_orphan_insert(const struct lu_env *env, struct mdd_object *obj,
159 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
160 struct dt_object *dor = mdd->mdd_orphans;
161 const struct lu_fid *lf_dor = lu_object_fid(&dor->do_lu);
162 struct dt_object *next = mdd_object_child(obj);
163 struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
167 LASSERT(mdd_write_locked(env, obj) != 0);
168 LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
170 dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
172 rc = mdd_orphan_insert_obj(env, mdd, obj, th);
176 mdo_ref_add(env, obj, th);
177 if (!S_ISDIR(mdd_object_type(obj)))
180 mdo_ref_add(env, obj, th);
181 dt_ref_add(env, mdd->mdd_orphans, th);
183 /* try best to fixup directory, do not return errors from here */
184 if (!dt_try_as_dir(env, next))
187 dt_delete(env, next, (const struct dt_key *)dotdot, th);
189 rec->rec_fid = lf_dor;
190 rec->rec_type = S_IFDIR;
191 dt_insert(env, next, (const struct dt_rec *)rec,
192 (const struct dt_key *)dotdot, th);
196 obj->mod_flags |= ORPHAN_OBJ;
198 dt_write_unlock(env, mdd->mdd_orphans);
203 int mdd_orphan_declare_delete(const struct lu_env *env, struct mdd_object *obj,
206 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
210 key = mdd_orphan_key_fill(env, mdo2fid(obj));
212 rc = dt_declare_delete(env, mdd->mdd_orphans, key, th);
216 if (!mdd_object_exists(obj))
219 rc = mdo_declare_ref_del(env, obj, th);
223 if (S_ISDIR(mdd_object_type(obj))) {
224 rc = mdo_declare_ref_del(env, obj, th);
228 rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
235 * delete an orphan \a obj from orphan index.
236 * \param obj file or directory.
237 * \param th transaction for index deletion and object destruction.
239 * \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
242 * \retval -ve index operation error.
244 int mdd_orphan_delete(const struct lu_env *env, struct mdd_object *obj,
247 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
248 struct dt_object *dor = mdd->mdd_orphans;
254 LASSERT(mdd_write_locked(env, obj) != 0);
255 LASSERT(obj->mod_flags & ORPHAN_OBJ);
256 LASSERT(obj->mod_count == 0);
260 key = mdd_orphan_key_fill(env, mdo2fid(obj));
261 dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
263 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ORPHAN_DELETE))
266 rc = dt_delete(env, mdd->mdd_orphans, key, th);
268 key = mdd_orphan_key_fill_20(env, mdo2fid(obj));
269 rc = dt_delete(env, mdd->mdd_orphans, key, th);
274 /* lov objects will be destroyed by caller */
275 mdo_ref_del(env, obj, th);
276 if (S_ISDIR(mdd_object_type(obj))) {
277 mdo_ref_del(env, obj, th);
278 dt_ref_del(env, mdd->mdd_orphans, th);
280 obj->mod_flags &= ~ORPHAN_OBJ;
282 CERROR("%s: could not delete orphan object "DFID": rc = %d\n",
283 mdd2obd_dev(mdd)->obd_name, PFID(mdo2fid(obj)), rc);
286 dt_write_unlock(env, mdd->mdd_orphans);
291 static int mdd_orphan_destroy(const struct lu_env *env, struct mdd_object *obj,
294 struct thandle *th = NULL;
295 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
296 bool orphan_exists = true;
300 th = mdd_trans_create(env, mdd);
303 if (rc != -EINPROGRESS)
304 CERROR("%s: cannot get orphan thandle: rc = %d\n",
305 mdd2obd_dev(mdd)->obd_name, rc);
309 mdd_write_lock(env, obj, MOR_TGT_CHILD);
310 rc = mdd_orphan_declare_delete(env, obj, th);
311 if (rc == -ENOENT || lu_object_is_dying(obj->mod_obj.mo_lu.lo_header))
312 orphan_exists = false;
317 rc = mdo_declare_destroy(env, obj, th);
322 rc = mdd_trans_start(env, mdd, th);
326 if (likely(obj->mod_count == 0)) {
327 dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
328 rc = dt_delete(env, mdd->mdd_orphans, key, th);
329 /* We should remove object even dt_delete failed */
331 mdo_ref_del(env, obj, th);
332 if (S_ISDIR(mdd_object_type(obj))) {
333 mdo_ref_del(env, obj, th);
334 dt_ref_del(env, mdd->mdd_orphans, th);
336 rc1 = mdo_destroy(env, obj, th);
338 dt_write_unlock(env, mdd->mdd_orphans);
341 mdd_write_unlock(env, obj);
342 mdd_trans_stop(env, mdd, 0, th);
344 RETURN(rc ? rc : rc1);
348 * Delete unused orphan with FID \a lf from PENDING directory
350 * \param mdd MDD device finishing recovery
351 * \param lf FID of file or directory to delete
352 * \param key cookie for this entry in index iterator
357 static int mdd_orphan_key_test_and_delete(const struct lu_env *env,
358 struct mdd_device *mdd,
359 struct lu_fid *lf, struct dt_key *key)
361 struct mdd_object *mdo;
364 mdo = mdd_object_find(env, mdd, lf);
370 if (mdo->mod_count == 0) {
371 CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
372 rc = mdd_orphan_destroy(env, mdo, key);
373 if (rc) /* so replay-single.sh test_37 works */
374 CERROR("%s: error unlinking orphan "DFID": rc = %d\n",
375 mdd2obd_dev(mdd)->obd_name, PFID(lf), rc);
377 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
378 if (likely(mdo->mod_count > 0)) {
379 CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
380 PFID(lf), mdo->mod_count);
381 mdo->mod_flags |= ORPHAN_OBJ;
383 mdd_write_unlock(env, mdo);
386 mdd_object_put(env, mdo);
391 * delete unreferenced files and directories in the PENDING directory
393 * Files that remain in PENDING after client->MDS recovery has completed
394 * have to be referenced (opened) by some client during recovery, or they
395 * will be deleted here (for clients that did not complete recovery).
397 * \param thread info about orphan cleanup thread
402 static int mdd_orphan_index_iterate(const struct lu_env *env,
403 struct mdd_generic_thread *thread)
405 struct mdd_device *mdd = (struct mdd_device *)thread->mgt_data;
406 struct dt_object *dor = mdd->mdd_orphans;
407 struct lu_dirent *ent = &mdd_env_info(env)->mti_ent;
408 const struct dt_it_ops *iops;
415 iops = &dor->do_index_ops->dio_it;
416 it = iops->init(env, dor, LUDA_64BITHASH);
419 CERROR("%s: cannot clean '%s': rc = %d\n",
420 mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name, rc);
424 rc = iops->load(env, it, 0);
428 CERROR("%s: error loading iterator to clean '%s'\n",
429 mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name);
430 /* Index contains no zero key? */
431 GOTO(out_put, rc = -EIO);
434 mdd_env_info(env)->mti_flags |= MTI_KEEP_KEY;
436 if (thread->mgt_abort)
439 key_sz = iops->key_size(env, it);
440 /* filter out "." and ".." entries from PENDING dir. */
444 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
446 CERROR("%s: fail to get FID for orphan it: rc = %d\n",
447 mdd2obd_dev(mdd)->obd_name, rc);
451 fid_le_to_cpu(&fid, &ent->lde_fid);
452 if (!fid_is_sane(&fid)) {
453 CERROR("%s: bad FID "DFID" cleaning '%s'\n",
454 mdd2obd_dev(mdd)->obd_name, PFID(&fid),
455 mdd_orphan_index_name);
459 /* kill orphan object */
461 rc = mdd_orphan_key_test_and_delete(env, mdd, &fid,
462 (struct dt_key *)ent->lde_name);
463 /* after index delete reset iterator */
465 rc = iops->get(env, it, (const void *)"");
467 rc = iops->next(env, it);
470 GOTO(out_put, rc = 0);
480 * open the PENDING directory for device \a mdd
482 * The PENDING directory persistently tracks files and directories that were
483 * unlinked from the namespace (nlink == 0) but are still held open by clients.
484 * Those inodes shouldn't be deleted if the MDS crashes, because the clients
485 * would not be able to recover and reopen those files. Instead, these inodes
486 * are linked into the PENDING directory on disk, and only deleted if all
487 * clients close them, or the MDS finishes client recovery without any client
488 * reopening them (i.e. former clients didn't join recovery).
489 * \param d mdd device being started.
492 * \retval -ve index operation error.
495 int mdd_orphan_index_init(const struct lu_env *env, struct mdd_device *mdd)
503 /* create PENDING dir */
505 rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
506 mdd_orphan_index_name, S_IFDIR | S_IRUGO |
507 S_IWUSR | S_IXUGO, &fid);
511 d = dt_locate(env, mdd->mdd_child, &fid);
514 LASSERT(lu_object_exists(&d->do_lu));
515 if (!dt_try_as_dir(env, d)) {
516 CERROR("%s: orphan dir '%s' is not an index: rc = %d\n",
517 mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name, rc);
518 dt_object_put(env, d);
521 mdd->mdd_orphans = d;
525 void mdd_orphan_index_fini(const struct lu_env *env, struct mdd_device *mdd)
528 if (mdd->mdd_orphans != NULL) {
529 dt_object_put(env, mdd->mdd_orphans);
530 mdd->mdd_orphans = NULL;
535 static int mdd_orphan_cleanup_thread(void *args)
537 struct mdd_generic_thread *thread = (struct mdd_generic_thread *)args;
538 struct lu_env *env = NULL;
542 complete(&thread->mgt_started);
546 GOTO(out, rc = -ENOMEM);
548 rc = lu_env_init(env, LCT_MD_THREAD);
552 rc = mdd_orphan_index_iterate(env, thread);
559 complete(&thread->mgt_finished);
564 * Iterate orphan index to cleanup orphan objects after recovery is done.
565 * \param d mdd device in recovery.
567 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
572 OBD_ALLOC(name, MTI_NAME_MAXLEN);
576 snprintf(name, MTI_NAME_MAXLEN, "orph_%s", mdd2obd_dev(d)->obd_name);
578 rc = mdd_generic_thread_start(&d->mdd_orphan_cleanup_thread,
579 mdd_orphan_cleanup_thread, d, name);
582 CERROR("%s: start orphan cleanup thread failed: rc = %d\n",
583 mdd2obd_dev(d)->obd_name, rc);
585 OBD_FREE(name, MTI_NAME_MAXLEN);