Whamcloud - gitweb
2902df5c3103f34816221228f1c061f14bfbe19c
[fs/lustre-release.git] / lustre / mdd / mdd_orphans.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/mdd/mdd_orphans.c
40  *
41  * Orphan handling code
42  *
43  * Author: Mike Pershin <tappro@clusterfs.com>
44  *         Pravin B Shelar <pravin.shelar@sun.com>
45  */
46
47 #ifndef EXPORT_SYMTAB
48 # define EXPORT_SYMTAB
49 #endif
50 #define DEBUG_SUBSYSTEM S_MDS
51
52 #include <obd.h>
53 #include <obd_class.h>
54 #include <lustre_ver.h>
55 #include <obd_support.h>
56 #include <lustre_fid.h>
57 #include "mdd_internal.h"
58
59 const char orph_index_name[] = "PENDING";
60
61 enum {
62         ORPH_OP_UNLINK,
63         ORPH_OP_TRUNCATE
64 };
65
66 #define ORPHAN_FILE_NAME_FORMAT         "%016llx:%08x:%08x:%2x"
67 #define ORPHAN_FILE_NAME_FORMAT_18      "%llx:%08x"
68
69 static struct dt_key* orph_key_fill(const struct lu_env *env,
70                                     const struct lu_fid *lf, __u32 op)
71 {
72         char *key = mdd_env_info(env)->mti_orph_key;
73         int rc;
74
75         LASSERT(key);
76         rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT,
77                       (long long unsigned int)fid_seq(lf),
78                       fid_oid(lf), fid_ver(lf), op);
79         if (rc > 0)
80                 return (struct dt_key*) key;
81         else
82                 return ERR_PTR(rc);
83 }
84
85 static struct dt_key* orph_key_fill_18(const struct lu_env *env,
86                                        const struct lu_fid *lf)
87 {
88         char *key = mdd_env_info(env)->mti_orph_key;
89         int rc;
90
91         LASSERT(key);
92         rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT_18,
93                       (unsigned long long)fid_seq(lf), fid_oid(lf));
94         if (rc > 0)
95                 return (struct dt_key*) key;
96         else
97                 return ERR_PTR(rc);
98 }
99
100 static int orphan_key_to_fid(char *key, struct lu_fid *lf)
101 {
102         int rc = 0;
103         unsigned int op;
104
105         rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT,
106                     (long long unsigned int *)&lf->f_seq, &lf->f_oid,
107                     &lf->f_ver, &op);
108         if (rc == 4)
109                 return 0;
110
111         /* build igif */
112         rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT_18,
113                     (long long unsigned int *)&lf->f_seq, &lf->f_oid);
114         if (rc == 2) {
115                 lf->f_ver = 0;
116                 return 0;
117         }
118
119         CERROR("can not parse orphan file name %s\n",key);
120         return -EINVAL;
121 }
122
123 static inline void mdd_orphan_write_lock(const struct lu_env *env,
124                                     struct mdd_device *mdd)
125 {
126
127         struct dt_object        *dor    = mdd->mdd_orphans;
128         dor->do_ops->do_write_lock(env, dor, MOR_TGT_ORPHAN);
129 }
130
131 static inline void mdd_orphan_write_unlock(const struct lu_env *env,
132                                            struct mdd_device *mdd)
133 {
134
135         struct dt_object        *dor    = mdd->mdd_orphans;
136         dor->do_ops->do_write_unlock(env, dor);
137 }
138
139 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
140                                         struct mdd_device *mdd,
141                                         struct mdd_object *obj,
142                                         __u32 op,
143                                         struct thandle *th)
144 {
145         struct dt_object        *dor    = mdd->mdd_orphans;
146         const struct lu_fid     *lf     = mdo2fid(obj);
147         struct dt_key           *key    = orph_key_fill(env, lf, op);
148         ENTRY;
149
150         return  dor->do_index_ops->dio_insert(env, dor,
151                                               (struct dt_rec *)lf,
152                                               key, th,
153                                               BYPASS_CAPA, 1);
154 }
155
156 static inline int mdd_orphan_delete_obj(const struct lu_env *env,
157                                         struct mdd_device  *mdd ,
158                                         struct dt_key *key,
159                                         struct thandle *th)
160 {
161         struct dt_object        *dor    = mdd->mdd_orphans;
162
163         return  dor->do_index_ops->dio_delete(env, dor,
164                                               key, th,
165                                               BYPASS_CAPA);
166 }
167
168 static inline void mdd_orphan_ref_add(const struct lu_env *env,
169                                  struct mdd_device *mdd,
170                                  struct thandle *th)
171 {
172         struct dt_object        *dor    = mdd->mdd_orphans;
173         dor->do_ops->do_ref_add(env, dor, th);
174 }
175
176 static inline void mdd_orphan_ref_del(const struct lu_env *env,
177                                  struct mdd_device *mdd,
178                                  struct thandle *th)
179 {
180         struct dt_object        *dor    = mdd->mdd_orphans;
181         dor->do_ops->do_ref_del(env, dor, th);
182 }
183
184
185 static int orph_index_insert(const struct lu_env *env,
186                              struct mdd_object *obj,
187                              __u32 op,
188                              struct thandle *th)
189 {
190         struct mdd_device       *mdd    = mdo2mdd(&obj->mod_obj);
191         struct dt_object        *dor    = mdd->mdd_orphans;
192         const struct lu_fid     *lf_dor = lu_object_fid(&dor->do_lu);
193         struct dt_object        *next   = mdd_object_child(obj);
194         const struct dt_key     *dotdot = (const struct dt_key *) "..";
195         int rc;
196         ENTRY;
197
198         LASSERT(mdd_write_locked(env, obj) != 0);
199         LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
200         LASSERT(obj->mod_count > 0);
201
202         mdd_orphan_write_lock(env, mdd);
203
204         rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
205         if (rc)
206                 GOTO(out, rc);
207
208         mdo_ref_add(env, obj, th);
209         if (!S_ISDIR(mdd_object_type(obj)))
210                 goto out;
211
212         mdo_ref_add(env, obj, th);
213         mdd_orphan_ref_add(env, mdd, th);
214
215         /* try best to fixup directory, dont return errors
216          * from here */
217         if (!dt_try_as_dir(env, next))
218                 goto out;
219         next->do_index_ops->dio_delete(env, next,
220                                        dotdot, th, BYPASS_CAPA);
221
222         next->do_index_ops->dio_insert(env, next,
223                                        (struct dt_rec *) lf_dor,
224                                        dotdot, th, BYPASS_CAPA, 1);
225
226 out:
227         if (rc == 0)
228                 obj->mod_flags |= ORPHAN_OBJ;
229
230         mdd_orphan_write_unlock(env, mdd);
231
232         RETURN(rc);
233 }
234
235 /**
236  * destroy osd object on mdd and associated ost objects.
237  *
238  * \param obj orphan object
239  * \param mdd used for sending llog msg to osts
240  *
241  * \retval  0   success
242  * \retval -ve  error
243  */
244 static int orphan_object_kill(const struct lu_env *env,
245                               struct mdd_object *obj,
246                               struct mdd_device *mdd,
247                               struct thandle *th)
248 {
249         struct lu_attr *la = &mdd_env_info(env)->mti_la;
250         int rc = 0;
251         ENTRY;
252
253         /* No need to lock this object as its recovery phase, and
254          * no other thread can access it. But we need to lock it
255          * as its precondition for osd api we using. */
256
257         mdo_ref_del(env, obj, th);
258         if (S_ISDIR(mdd_object_type(obj))) {
259                 mdo_ref_del(env, obj, th);
260                 mdd_orphan_ref_del(env, mdd, th);
261         } else {
262                 /* regular file , cleanup linked ost objects */
263                 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
264                 if (rc == 0)
265                         rc = mdd_lov_destroy(env, mdd, obj, la);
266         }
267         RETURN(rc);
268 }
269
270 static int orph_index_delete(const struct lu_env *env,
271                              struct mdd_object *obj,
272                              __u32 op,
273                              struct thandle *th)
274 {
275         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
276         struct dt_object *dor = mdd->mdd_orphans;
277         struct dt_key *key;
278         int rc;
279
280         ENTRY;
281
282         LASSERT(mdd_write_locked(env, obj) != 0);
283         LASSERT(obj->mod_flags & ORPHAN_OBJ);
284         LASSERT(obj->mod_count == 0);
285
286         LASSERT(dor);
287
288         key = orph_key_fill(env, mdo2fid(obj), op);
289         mdd_orphan_write_lock(env, mdd);
290
291         rc = mdd_orphan_delete_obj(env, mdd, key, th);
292
293         if (rc == -ENOENT) {
294                 key = orph_key_fill_18(env, mdo2fid(obj));
295                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
296         }
297
298         if (!rc) {
299                 /* lov objects will be destroyed by caller */
300                 mdo_ref_del(env, obj, th);
301                 if (S_ISDIR(mdd_object_type(obj))) {
302                         mdo_ref_del(env, obj, th);
303                         mdd_orphan_ref_del(env, mdd, th);
304                 }
305                 obj->mod_flags &= ~ORPHAN_OBJ;
306         } else {
307                 CERROR("could not delete object: rc = %d\n",rc);
308         }
309
310         mdd_orphan_write_unlock(env, mdd);
311         RETURN(rc);
312 }
313
314
315 static int orphan_object_destroy(const struct lu_env *env,
316                                  struct mdd_object *obj,
317                                  struct dt_key *key)
318 {
319         struct thandle *th = NULL;
320         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
321         struct md_attr *ma = &mdd_env_info(env)->mti_ma;
322         int rc = 0;
323         ENTRY;
324
325         /* init ma */
326         ma->ma_lmm_size = mdd_lov_mdsize(env, mdd);
327         ma->ma_lmm = mdd_max_lmm_get(env, mdd);
328         ma->ma_cookie_size = mdd_lov_cookiesize(env, mdd);
329         ma->ma_cookie = mdd_max_cookie_get(env, mdd);
330         ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
331         ma->ma_valid = 0;
332
333         mdd_log_txn_param_build(env, &obj->mod_obj, ma, MDD_TXN_UNLINK_OP, 0);
334         th = mdd_trans_start(env, mdd);
335         if (IS_ERR(th)) {
336                 CERROR("Cannot get thandle\n");
337                 RETURN(-ENOMEM);
338         }
339
340         mdd_write_lock(env, obj, MOR_TGT_CHILD);
341         if (likely(obj->mod_count == 0)) {
342                 mdd_orphan_write_lock(env, mdd);
343                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
344                 if (!rc)
345                         orphan_object_kill(env, obj, mdd, th);
346                 else
347                         CERROR("could not delete object: rc = %d\n",rc);
348                 mdd_orphan_write_unlock(env, mdd);
349         }
350         mdd_write_unlock(env, obj);
351         mdd_trans_stop(env, mdd, 0, th);
352
353         RETURN(rc);
354 }
355
356 static int orph_key_test_and_del(const struct lu_env *env,
357                                  struct mdd_device *mdd,
358                                  struct lu_fid *lf,
359                                  struct dt_key *key)
360 {
361         struct mdd_object *mdo;
362         int rc;
363
364         mdo = mdd_object_find(env, mdd, lf);
365
366         if (IS_ERR(mdo))
367                 return PTR_ERR(mdo);
368
369         rc = -EBUSY;
370         if (mdo->mod_count == 0) {
371                 CWARN("Found orphan! Delete it\n");
372                 rc = orphan_object_destroy(env, mdo, key);
373         } else {
374                 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
375                 if (likely(mdo->mod_count > 0)) {
376                         CDEBUG(D_HA, "Found orphan, open count = %d\n",
377                                mdo->mod_count);
378                         mdo->mod_flags |= ORPHAN_OBJ;
379                 }
380                 mdd_write_unlock(env, mdo);
381         }
382
383         mdd_object_put(env, mdo);
384         return rc;
385 }
386
387 static int orph_index_iterate(const struct lu_env *env,
388                               struct mdd_device *mdd)
389 {
390         struct dt_object *dor = mdd->mdd_orphans;
391         char             *mti_key = mdd_env_info(env)->mti_orph_key;
392         const struct dt_it_ops *iops;
393         struct dt_it     *it;
394         char             *key;
395         struct lu_fid     fid;
396         int               result = 0;
397         int               key_sz = 0;
398         int               rc;
399         __u64             cookie;
400         ENTRY;
401
402         /* In recovery phase, do not need for any lock here */
403
404         iops = &dor->do_index_ops->dio_it;
405         it = iops->init(env, dor, LUDA_64BITHASH, BYPASS_CAPA);
406         if (!IS_ERR(it)) {
407                 result = iops->load(env, it, 0);
408                 if (result > 0) {
409                         /* main cycle */
410                         do {
411
412                                 key = (void *)iops->key(env, it);
413                                 if (IS_ERR(key)) {
414                                         CERROR("key failed when clean pending.\n");
415                                         goto next;
416                                 }
417                                 key_sz = iops->key_size(env, it);
418
419                                 /* filter out "." and ".." entries from
420                                  * PENDING dir. */
421                                 if (key_sz < 8)
422                                         goto next;
423
424                                 memcpy(mti_key, key, key_sz);
425                                 mti_key[key_sz] = 0;
426
427                                 if (orphan_key_to_fid(mti_key, &fid))
428                                         goto next;
429                                 if (!fid_is_sane(&fid)) {
430                                         CERROR("fid is not sane when clean pending.\n");
431                                         goto next;
432                                 }
433
434                                 /* kill orphan object */
435                                 cookie =  iops->store(env, it);
436                                 iops->put(env, it);
437                                 rc = orph_key_test_and_del(env, mdd, &fid,
438                                                 (struct dt_key *)mti_key);
439
440                                 /* after index delete reset iterator */
441                                 if (!rc)
442                                         result = iops->get(env, it,
443                                                            (const void *)"");
444                                 else
445                                         result = iops->load(env, it, cookie);
446 next:
447                                 result = iops->next(env, it);
448                         } while (result == 0);
449                         result = 0;
450                 } else if (result == 0) {
451                         CERROR("Input/Output for clean pending.\n");
452                         /* Index contains no zero key? */
453                         result = -EIO;
454                 }
455                 iops->put(env, it);
456                 iops->fini(env, it);
457         } else {
458                 result = PTR_ERR(it);
459                 CERROR("Cannot clean pending (%d).\n", result);
460         }
461
462         RETURN(result);
463 }
464
465 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
466 {
467         struct lu_fid fid;
468         struct dt_object *d;
469         int rc = 0;
470         ENTRY;
471
472         d = dt_store_open(env, mdd->mdd_child, "", orph_index_name, &fid);
473         if (!IS_ERR(d)) {
474                 mdd->mdd_orphans = d;
475                 if (!dt_try_as_dir(env, d)) {
476                         rc = -ENOTDIR;
477                         CERROR("\"%s\" is not an index! : rc = %d\n",
478                                         orph_index_name, rc);
479                 }
480         } else {
481                 CERROR("cannot find \"%s\" obj %d\n",
482                        orph_index_name, (int)PTR_ERR(d));
483                 rc = PTR_ERR(d);
484         }
485
486         RETURN(rc);
487 }
488
489 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
490 {
491         ENTRY;
492         if (mdd->mdd_orphans != NULL) {
493                 lu_object_put(env, &mdd->mdd_orphans->do_lu);
494                 mdd->mdd_orphans = NULL;
495         }
496         EXIT;
497 }
498
499 /**
500  *  Iterate orphan index to cleanup orphan objects in case of recovery.
501  *  \param d   mdd device in recovery.
502  *
503  */
504
505 int __mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
506 {
507         return orph_index_iterate(env, d);
508 }
509
510 /**
511  *  delete an orphan \a obj from orphan index.
512  *  \param obj file or directory.
513  *  \param th  transaction for index insert.
514  *
515  *  \pre obj nlink == 0 && obj->mod_count != 0
516  *
517  *  \retval 0  success
518  *  \retval  -ve index operation error.
519  */
520
521 int __mdd_orphan_add(const struct lu_env *env,
522                      struct mdd_object *obj, struct thandle *th)
523 {
524         return orph_index_insert(env, obj, ORPH_OP_UNLINK, th);
525 }
526
527 /**
528  *  delete an orphan \a obj from orphan index.
529  *  \param obj file or directory.
530  *  \param th  transaction for index deletion and object destruction.
531  *
532  *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
533  *
534  *  \retval 0  success
535  *  \retval  -ve index operation error.
536  */
537
538 int __mdd_orphan_del(const struct lu_env *env,
539                      struct mdd_object *obj, struct thandle *th)
540 {
541         return orph_index_delete(env, obj, ORPH_OP_UNLINK, th);
542 }