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/mdd/mdd_orphans.c
38 * Orphan handling code
40 * Author: Mike Pershin <tappro@clusterfs.com>
41 * Pravin B Shelar <pravin.shelar@sun.com>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
50 #include <obd_class.h>
51 #include <lustre_ver.h>
52 #include <obd_support.h>
53 #include <lustre_fid.h>
54 #include "mdd_internal.h"
56 const char orph_index_name[] = "PENDING";
63 #define ORPHAN_FILE_NAME_FORMAT "%016llx:%08x:%08x:%2x"
64 #define ORPHAN_FILE_NAME_FORMAT_18 "%llx:%08x"
66 static struct dt_key* orph_key_fill(const struct lu_env *env,
67 const struct lu_fid *lf, __u32 op)
69 char *key = mdd_env_info(env)->mti_orph_key;
73 rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT, fid_seq(lf),
74 fid_oid(lf), fid_ver(lf), op);
76 return (struct dt_key*) key;
81 static struct dt_key* orph_key_fill_18(const struct lu_env *env,
82 const struct lu_fid *lf)
84 char *key = mdd_env_info(env)->mti_orph_key;
88 rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT_18, fid_seq(lf),
91 return (struct dt_key*) key;
96 static int orphan_key_to_fid(char *key, struct lu_fid *lf)
101 rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT, &lf->f_seq, &lf->f_oid,
107 rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT_18,
108 &lf->f_seq, &lf->f_oid);
114 CERROR("can not parse orphan file name %s\n",key);
118 static inline void mdd_orphan_write_lock(const struct lu_env *env,
119 struct mdd_device *mdd)
122 struct dt_object *dor = mdd->mdd_orphans;
123 dor->do_ops->do_write_lock(env, dor, MOR_TGT_ORPHAN);
126 static inline void mdd_orphan_write_unlock(const struct lu_env *env,
127 struct mdd_device *mdd)
130 struct dt_object *dor = mdd->mdd_orphans;
131 dor->do_ops->do_write_unlock(env, dor);
134 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
135 struct mdd_device *mdd,
136 struct mdd_object *obj,
140 struct dt_object *dor = mdd->mdd_orphans;
141 const struct lu_fid *lf = mdo2fid(obj);
142 struct dt_key *key = orph_key_fill(env, lf, op);
145 return dor->do_index_ops->dio_insert(env, dor,
146 __mdd_fid_rec(env, lf),
151 static inline int mdd_orphan_delete_obj(const struct lu_env *env,
152 struct mdd_device *mdd ,
156 struct dt_object *dor = mdd->mdd_orphans;
158 return dor->do_index_ops->dio_delete(env, dor,
163 static inline void mdd_orphan_ref_add(const struct lu_env *env,
164 struct mdd_device *mdd,
167 struct dt_object *dor = mdd->mdd_orphans;
168 dor->do_ops->do_ref_add(env, dor, th);
171 static inline void mdd_orphan_ref_del(const struct lu_env *env,
172 struct mdd_device *mdd,
175 struct dt_object *dor = mdd->mdd_orphans;
176 dor->do_ops->do_ref_del(env, dor, th);
180 static int orph_index_insert(const struct lu_env *env,
181 struct mdd_object *obj,
185 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
186 struct dt_object *dor = mdd->mdd_orphans;
187 const struct lu_fid *lf_dor = lu_object_fid(&dor->do_lu);
188 struct dt_object *next = mdd_object_child(obj);
189 const struct dt_key *dotdot = (const struct dt_key *) "..";
193 mdd_orphan_write_lock(env, mdd);
195 rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
199 mdo_ref_add(env, obj, th);
200 if (!S_ISDIR(mdd_object_type(obj)))
203 mdo_ref_add(env, obj, th);
204 mdd_orphan_ref_add(env, mdd, th);
206 /* try best to fixup directory, dont return errors
208 if (!dt_try_as_dir(env, next))
210 next->do_index_ops->dio_delete(env, next,
211 dotdot, th, BYPASS_CAPA);
213 next->do_index_ops->dio_insert(env, next,
214 __mdd_fid_rec(env, lf_dor),
215 dotdot, th, BYPASS_CAPA, 1);
218 mdd_orphan_write_unlock(env, mdd);
224 * destroy osd object on mdd and associated ost objects.
226 * \param obj orphan object
227 * \param mdd used for sending llog msg to osts
232 static int orphan_object_kill(const struct lu_env *env,
233 struct mdd_object *obj,
234 struct mdd_device *mdd,
237 struct lu_attr *la = &mdd_env_info(env)->mti_la;
240 /* No need to lock this object as its recovery phase, and
241 * no other thread can access it. But we need to lock it
242 * as its precondition for osd api we using. */
244 mdd_write_lock(env, obj, MOR_TGT_CHILD);
245 mdo_ref_del(env, obj, th);
246 if (S_ISDIR(mdd_object_type(obj))) {
247 mdo_ref_del(env, obj, th);
248 mdd_orphan_ref_del(env, mdd, th);
249 mdd_write_unlock(env, obj);
251 /* regular file , cleanup linked ost objects */
252 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
253 mdd_write_unlock(env, obj);
257 mdd_lov_destroy(env, mdd, obj, la);
262 static int orph_index_delete(const struct lu_env *env,
263 struct mdd_object *obj,
267 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
268 struct dt_object *dor = mdd->mdd_orphans;
276 key = orph_key_fill(env, mdo2fid(obj), op);
277 mdd_orphan_write_lock(env, mdd);
279 rc = mdd_orphan_delete_obj(env, mdd, key, th);
282 key = orph_key_fill_18(env, mdo2fid(obj));
283 rc = mdd_orphan_delete_obj(env, mdd, key, th);
287 /* lov objects will be destroyed by caller */
288 mdo_ref_del(env, obj, th);
289 if (S_ISDIR(mdd_object_type(obj))) {
290 mdo_ref_del(env, obj, th);
291 mdd_orphan_ref_del(env, mdd, th);
294 CERROR("could not delete object: rc = %d\n",rc);
296 obj->mod_flags &= ~ORPHAN_OBJ;
297 mdd_orphan_write_unlock(env, mdd);
302 static int orphan_object_destroy(const struct lu_env *env,
303 struct mdd_object *obj,
306 struct thandle *th = NULL;
307 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
311 mdd_txn_param_build(env, mdd, MDD_TXN_UNLINK_OP);
312 th = mdd_trans_start(env, mdd);
314 CERROR("Cannot get thandle\n");
318 mdd_orphan_write_lock(env, mdd);
319 rc = mdd_orphan_delete_obj(env, mdd, key, th);
321 orphan_object_kill(env, obj, mdd, th);
323 CERROR("could not delete object: rc = %d\n",rc);
325 mdd_orphan_write_unlock(env, mdd);
326 mdd_trans_stop(env, mdd, 0, th);
331 static int orph_key_test_and_del(const struct lu_env *env,
332 struct mdd_device *mdd,
336 struct mdd_object *mdo;
339 mdo = mdd_object_find(env, mdd, lf);
345 if (mdo->mod_count == 0) {
346 CWARN("Found orphan!\n");
347 rc = orphan_object_destroy(env, mdo, key);
349 mdo->mod_flags |= ORPHAN_OBJ;
352 mdd_object_put(env, mdo);
356 static int orph_index_iterate(const struct lu_env *env,
357 struct mdd_device *mdd)
359 struct dt_object *dor = mdd->mdd_orphans;
360 char *mti_key = mdd_env_info(env)->mti_orph_key;
361 const struct dt_it_ops *iops;
371 /* In recovery phase, do not need for any lock here */
373 iops = &dor->do_index_ops->dio_it;
374 it = iops->init(env, dor, BYPASS_CAPA);
376 result = iops->get(env, it, (const void *)"");
381 key = (void *)iops->key(env, it);
384 key_sz = iops->key_size(env, it);
386 /* filter out "." and ".." entries from
391 memcpy(mti_key, key, key_sz);
394 if (orphan_key_to_fid(mti_key, &fid))
396 if (!fid_is_sane(&fid))
399 /* kill orphan object */
400 cookie = iops->store(env, it);
402 rc = orph_key_test_and_del(env, mdd, &fid,
403 (struct dt_key *)mti_key);
405 /* after index delete reset iterator */
407 result = iops->get(env, it,
410 result = iops->load(env, it, cookie);
412 result = iops->next(env, it);
413 } while (result == 0);
415 } else if (result == 0)
416 /* Index contains no zero key? */
426 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
433 d = dt_store_open(env, mdd->mdd_child, "", orph_index_name, &fid);
435 mdd->mdd_orphans = d;
436 if (!dt_try_as_dir(env, d)) {
438 CERROR("\"%s\" is not an index! : rc = %d\n",
439 orph_index_name, rc);
442 CERROR("cannot find \"%s\" obj %d\n",
443 orph_index_name, (int)PTR_ERR(d));
450 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
453 if (mdd->mdd_orphans != NULL) {
454 lu_object_put(env, &mdd->mdd_orphans->do_lu);
455 mdd->mdd_orphans = NULL;
461 * Iterate orphan index to cleanup orphan objects in case of recovery.
462 * \param d mdd device in recovery.
466 int __mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
468 return orph_index_iterate(env, d);
472 * delete an orphan \a obj from orphan index.
473 * \param obj file or directory.
474 * \param th transaction for index insert.
476 * \pre obj nlink == 0 && obj->mod_count != 0
479 * \retva -ve index operation error.
482 int __mdd_orphan_add(const struct lu_env *env,
483 struct mdd_object *obj, struct thandle *th)
485 return orph_index_insert(env, obj, ORPH_OP_UNLINK, th);
489 * delete an orphan \a obj from orphan index.
490 * \param obj file or directory.
491 * \param th transaction for index deletion and object destruction.
493 * \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
496 * \retva -ve index operation error.
499 int __mdd_orphan_del(const struct lu_env *env,
500 struct mdd_object *obj, struct thandle *th)
502 return orph_index_delete(env, obj, ORPH_OP_UNLINK, th);