Whamcloud - gitweb
LU-601 mdd: cleanup error messages and code style
[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 const char *dotdot = "..";
61
62 enum {
63         ORPH_OP_UNLINK,
64         ORPH_OP_TRUNCATE
65 };
66
67 #define ORPHAN_FILE_NAME_FORMAT         "%016llx:%08x:%08x:%2x"
68 #define ORPHAN_FILE_NAME_FORMAT_18      "%llx:%08x"
69
70 static struct dt_key* orph_key_fill(const struct lu_env *env,
71                                     const struct lu_fid *lf, __u32 op)
72 {
73         char *key = mdd_env_info(env)->mti_orph_key;
74         int rc;
75
76         LASSERT(key);
77         rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT,
78                       (long long unsigned int)fid_seq(lf),
79                       fid_oid(lf), fid_ver(lf), op);
80         if (rc > 0)
81                 return (struct dt_key*) key;
82         else
83                 return ERR_PTR(rc);
84 }
85
86 static struct dt_key* orph_key_fill_18(const struct lu_env *env,
87                                        const struct lu_fid *lf)
88 {
89         char *key = mdd_env_info(env)->mti_orph_key;
90         int rc;
91
92         LASSERT(key);
93         rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT_18,
94                       (unsigned long long)fid_seq(lf), fid_oid(lf));
95         if (rc > 0)
96                 return (struct dt_key*) key;
97         else
98                 return ERR_PTR(rc);
99 }
100
101 static int orphan_key_to_fid(char *key, struct lu_fid *lf)
102 {
103         int rc = 0;
104         unsigned int op;
105
106         rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT,
107                     (long long unsigned int *)&lf->f_seq, &lf->f_oid,
108                     &lf->f_ver, &op);
109         if (rc == 4)
110                 return 0;
111
112         /* build igif */
113         rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT_18,
114                     (long long unsigned int *)&lf->f_seq, &lf->f_oid);
115         if (rc == 2) {
116                 lf->f_ver = 0;
117                 return 0;
118         }
119
120         CERROR("can not parse orphan file name %s\n", key);
121         return -EINVAL;
122 }
123
124 static inline void mdd_orphan_write_lock(const struct lu_env *env,
125                                     struct mdd_device *mdd)
126 {
127
128         struct dt_object        *dor    = mdd->mdd_orphans;
129         dor->do_ops->do_write_lock(env, dor, MOR_TGT_ORPHAN);
130 }
131
132 static inline void mdd_orphan_write_unlock(const struct lu_env *env,
133                                            struct mdd_device *mdd)
134 {
135
136         struct dt_object        *dor    = mdd->mdd_orphans;
137         dor->do_ops->do_write_unlock(env, dor);
138 }
139
140 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
141                                         struct mdd_device *mdd,
142                                         struct mdd_object *obj,
143                                         __u32 op,
144                                         struct thandle *th)
145 {
146         struct dt_object        *dor    = mdd->mdd_orphans;
147         const struct lu_fid     *lf     = mdo2fid(obj);
148         struct dt_key           *key    = orph_key_fill(env, lf, op);
149         ENTRY;
150
151         return  dor->do_index_ops->dio_insert(env, dor,
152                                               (struct dt_rec *)lf,
153                                               key, th,
154                                               BYPASS_CAPA, 1);
155 }
156
157 static inline int mdd_orphan_delete_obj(const struct lu_env *env,
158                                         struct mdd_device  *mdd ,
159                                         struct dt_key *key,
160                                         struct thandle *th)
161 {
162         struct dt_object        *dor    = mdd->mdd_orphans;
163
164         return  dor->do_index_ops->dio_delete(env, dor,
165                                               key, th,
166                                               BYPASS_CAPA);
167 }
168
169 static inline void mdd_orphan_ref_add(const struct lu_env *env,
170                                  struct mdd_device *mdd,
171                                  struct thandle *th)
172 {
173         struct dt_object        *dor    = mdd->mdd_orphans;
174         dor->do_ops->do_ref_add(env, dor, th);
175 }
176
177 static inline void mdd_orphan_ref_del(const struct lu_env *env,
178                                  struct mdd_device *mdd,
179                                  struct thandle *th)
180 {
181         struct dt_object        *dor    = mdd->mdd_orphans;
182         dor->do_ops->do_ref_del(env, dor, th);
183 }
184
185
186 int orph_declare_index_insert(const struct lu_env *env,
187                               struct mdd_object *obj,
188                               struct thandle *th)
189 {
190         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
191         int                rc;
192
193         rc = dt_declare_insert(env, mdd->mdd_orphans, NULL, NULL, th);
194         if (rc)
195                 return rc;
196
197         rc = mdo_declare_ref_add(env, obj, th);
198         if (rc)
199                 return rc;
200
201         if (!S_ISDIR(mdd_object_type(obj)))
202                 return 0;
203
204         rc = mdo_declare_ref_add(env, obj, th);
205         if (rc)
206                 return rc;
207
208         rc = dt_declare_ref_add(env, mdd->mdd_orphans, th);
209         if (rc)
210                 return rc;
211
212         rc = mdo_declare_index_delete(env, obj, dotdot, th);
213         if (rc)
214                 return rc;
215
216         rc = mdo_declare_index_insert(env, obj, NULL, dotdot, th);
217
218         return rc;
219 }
220
221 static int orph_index_insert(const struct lu_env *env,
222                              struct mdd_object *obj,
223                              __u32 op,
224                              struct thandle *th)
225 {
226         struct mdd_device       *mdd    = mdo2mdd(&obj->mod_obj);
227         struct dt_object        *dor    = mdd->mdd_orphans;
228         const struct lu_fid     *lf_dor = lu_object_fid(&dor->do_lu);
229         struct dt_object        *next   = mdd_object_child(obj);
230         int rc;
231         ENTRY;
232
233         LASSERT(mdd_write_locked(env, obj) != 0);
234         LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
235         LASSERT(obj->mod_count > 0);
236
237         mdd_orphan_write_lock(env, mdd);
238
239         rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
240         if (rc)
241                 GOTO(out, rc);
242
243         mdo_ref_add(env, obj, th);
244         if (!S_ISDIR(mdd_object_type(obj)))
245                 goto out;
246
247         mdo_ref_add(env, obj, th);
248         mdd_orphan_ref_add(env, mdd, th);
249
250         /* try best to fixup directory, dont return errors
251          * from here */
252         if (!dt_try_as_dir(env, next))
253                 goto out;
254         next->do_index_ops->dio_delete(env, next,
255                                        (const struct dt_key *)dotdot,
256                                        th, BYPASS_CAPA);
257
258         next->do_index_ops->dio_insert(env, next,
259                                        (struct dt_rec *)lf_dor,
260                                        (const struct dt_key *)dotdot,
261                                        th, BYPASS_CAPA, 1);
262
263 out:
264         if (rc == 0)
265                 obj->mod_flags |= ORPHAN_OBJ;
266
267         mdd_orphan_write_unlock(env, mdd);
268
269         RETURN(rc);
270 }
271
272 /**
273  * Destroy OSD object on MDD and associated OST objects.
274  *
275  * \param obj orphan object
276  * \param mdd used for sending llog msg to osts
277  *
278  * \retval  0   success
279  * \retval -ve  error
280  */
281 static int orphan_object_kill(const struct lu_env *env,
282                               struct mdd_object *obj,
283                               struct mdd_device *mdd,
284                               struct thandle *th)
285 {
286         struct lu_attr *la = &mdd_env_info(env)->mti_la;
287         int rc = 0;
288         ENTRY;
289
290         /* No need to lock this object as its recovery phase, and
291          * no other thread can access it. But we need to lock it
292          * as its precondition for osd api we using. */
293
294         mdo_ref_del(env, obj, th);
295         if (S_ISDIR(mdd_object_type(obj))) {
296                 mdo_ref_del(env, obj, th);
297                 mdd_orphan_ref_del(env, mdd, th);
298         } else {
299                 /* regular file , cleanup linked ost objects */
300                 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
301                 if (rc == 0)
302                         rc = mdd_lov_destroy(env, mdd, obj, la);
303         }
304         mdo_destroy(env, obj, th);
305         RETURN(rc);
306 }
307
308 int orph_declare_index_delete(const struct lu_env *env,
309                               struct mdd_object *obj,
310                               struct thandle *th)
311 {
312         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
313         int                rc;
314
315         rc = dt_declare_delete(env, mdd->mdd_orphans, NULL, th);
316         if (rc)
317                 return rc;
318
319         rc = mdo_declare_ref_del(env, obj, th);
320         if (rc)
321                 return rc;
322
323         if (S_ISDIR(mdd_object_type(obj))) {
324                 rc = mdo_declare_ref_del(env, obj, th);
325                 if (rc)
326                         return rc;
327
328                 rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
329         }
330
331         return rc;
332 }
333
334 static int orph_index_delete(const struct lu_env *env,
335                              struct mdd_object *obj,
336                              __u32 op,
337                              struct thandle *th)
338 {
339         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
340         struct dt_object *dor = mdd->mdd_orphans;
341         struct dt_key *key;
342         int rc;
343
344         ENTRY;
345
346         LASSERT(mdd_write_locked(env, obj) != 0);
347         LASSERT(obj->mod_flags & ORPHAN_OBJ);
348         LASSERT(obj->mod_count == 0);
349
350         LASSERT(dor);
351
352         key = orph_key_fill(env, mdo2fid(obj), op);
353         mdd_orphan_write_lock(env, mdd);
354
355         rc = mdd_orphan_delete_obj(env, mdd, key, th);
356
357         if (rc == -ENOENT) {
358                 key = orph_key_fill_18(env, mdo2fid(obj));
359                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
360         }
361
362         if (!rc) {
363                 /* lov objects will be destroyed by caller */
364                 mdo_ref_del(env, obj, th);
365                 if (S_ISDIR(mdd_object_type(obj))) {
366                         mdo_ref_del(env, obj, th);
367                         mdd_orphan_ref_del(env, mdd, th);
368                 }
369                 obj->mod_flags &= ~ORPHAN_OBJ;
370         } else {
371                 CERROR("could not delete object: rc = %d\n",rc);
372         }
373
374         mdd_orphan_write_unlock(env, mdd);
375         RETURN(rc);
376 }
377
378
379 static int orphan_object_destroy(const struct lu_env *env,
380                                  struct mdd_object *obj,
381                                  struct dt_key *key)
382 {
383         struct thandle *th = NULL;
384         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
385         struct md_attr *ma = &mdd_env_info(env)->mti_ma;
386         int rc = 0;
387         ENTRY;
388
389         /* init ma */
390         ma->ma_lmm_size = mdd_lov_mdsize(env, mdd);
391         ma->ma_lmm = mdd_max_lmm_get(env, mdd);
392         ma->ma_cookie_size = mdd_lov_cookiesize(env, mdd);
393         ma->ma_cookie = mdd_max_cookie_get(env, mdd);
394         ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
395         ma->ma_valid = 0;
396
397         th = mdd_trans_create(env, mdd);
398         if (IS_ERR(th)) {
399                 CERROR("Cannot get thandle\n");
400                 RETURN(-ENOMEM);
401         }
402         rc = orph_declare_index_delete(env, obj, th);
403         if (rc)
404                 GOTO(stop, rc);
405
406         rc = mdd_declare_object_kill(env, obj, ma, th);
407         if (rc)
408                 GOTO(stop, rc);
409
410         rc = mdd_trans_start(env, mdd, th);
411         if (rc)
412                 GOTO(stop, rc);
413
414         mdd_write_lock(env, obj, MOR_TGT_CHILD);
415         if (likely(obj->mod_count == 0)) {
416                 mdd_orphan_write_lock(env, mdd);
417                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
418                 if (rc == 0)
419                         orphan_object_kill(env, obj, mdd, th);
420                 else
421                         CERROR("could not delete object: rc = %d\n",rc);
422                 mdd_orphan_write_unlock(env, mdd);
423         }
424         mdd_write_unlock(env, obj);
425
426 stop:
427         mdd_trans_stop(env, mdd, 0, th);
428
429         RETURN(rc);
430 }
431
432 /**
433  * Delete unused orphan with FID \a lf from PENDING directory
434  *
435  * \param mdd  MDD device finishing recovery
436  * \param lf   FID of file or directory to delete
437  * \param key  cookie for this entry in index iterator
438  *
439  * \retval 0   success
440  * \retval -ve error
441  */
442 static int orph_key_test_and_del(const struct lu_env *env,
443                                  struct mdd_device *mdd,
444                                  struct lu_fid *lf,
445                                  struct dt_key *key)
446 {
447         struct mdd_object *mdo;
448         int rc;
449
450         mdo = mdd_object_find(env, mdd, lf);
451
452         if (IS_ERR(mdo))
453                 return PTR_ERR(mdo);
454
455         rc = -EBUSY;
456         if (mdo->mod_count == 0) {
457                 CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
458                 rc = orphan_object_destroy(env, mdo, key);
459                 if (rc) /* so replay-single.sh test_37 works */
460                         CERROR("%s: error unlinking orphan "DFID" from "
461                                "PENDING: rc = %d\n",
462                                mdd->mdd_obd_dev->obd_name, PFID(lf), rc);
463         } else {
464                 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
465                 if (likely(mdo->mod_count > 0)) {
466                         CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
467                                PFID(lf), mdo->mod_count);
468                         mdo->mod_flags |= ORPHAN_OBJ;
469                 }
470                 mdd_write_unlock(env, mdo);
471         }
472
473         mdd_object_put(env, mdo);
474         return rc;
475 }
476
477 /**
478  * delete unreferenced files and directories in the PENDING directory
479  *
480  * Files that remain in PENDING after client->MDS recovery has completed
481  * have to be referenced (opened) by some client during recovery, or they
482  * will be deleted here (for clients that did not complete recovery).
483  *
484  * \param mdd  MDD device finishing recovery
485  *
486  * \retval 0   success
487  * \retval -ve error
488  */
489 static int orph_index_iterate(const struct lu_env *env,
490                               struct mdd_device *mdd)
491 {
492         struct dt_object *dor = mdd->mdd_orphans;
493         char             *mti_key = mdd_env_info(env)->mti_orph_key;
494         const struct dt_it_ops *iops;
495         struct dt_it     *it;
496         char             *key;
497         struct lu_fid     fid;
498         int               result = 0;
499         int               key_sz = 0;
500         int               rc;
501         __u64             cookie;
502         ENTRY;
503
504         /* In recovery phase, do not need for any lock here */
505         iops = &dor->do_index_ops->dio_it;
506         it = iops->init(env, dor, LUDA_64BITHASH, BYPASS_CAPA);
507         if (IS_ERR(it)) {
508                 rc = PTR_ERR(it);
509                 CERROR("%s: cannot clean PENDING: rc = %d\n",
510                        mdd->mdd_obd_dev->obd_name, rc);
511                 GOTO(out, rc);
512         }
513
514         rc = iops->load(env, it, 0);
515         if (rc < 0)
516                 GOTO(out_put, rc);
517         if (rc == 0) {
518                 CERROR("%s: error loading iterator to clean PENDING\n",
519                        mdd->mdd_obd_dev->obd_name);
520                 /* Index contains no zero key? */
521                 GOTO(out_put, rc = -EIO);
522         }
523
524         do {
525                 key = (void *)iops->key(env, it);
526                 if (IS_ERR(key)) {
527                         CERROR("%s: key failed when clean PENDING: rc = %ld\n",
528                                mdd->mdd_obd_dev->obd_name, PTR_ERR(key));
529                         goto next;
530                 }
531                 key_sz = iops->key_size(env, it);
532
533                 /* filter out "." and ".." entries from PENDING dir. */
534                 if (key_sz < 8)
535                         goto next;
536
537                 memcpy(mti_key, key, key_sz);
538                 mti_key[key_sz] = 0;
539
540                 if (orphan_key_to_fid(mti_key, &fid))
541                         goto next;
542                 if (!fid_is_sane(&fid)) {
543                         CERROR("%s: bad FID "DFID" cleaning PENDING\n",
544                                mdd->mdd_obd_dev->obd_name, PFID(&fid));
545                         goto next;
546                 }
547
548                 /* kill orphan object */
549                 cookie = iops->store(env, it);
550                 iops->put(env, it);
551                 rc = orph_key_test_and_del(env, mdd, &fid,
552                                           (struct dt_key *)mti_key);
553
554                 /* after index delete reset iterator */
555                 if (rc == 0)
556                         result = iops->get(env, it, (const void *)"");
557                 else
558                         result = iops->load(env, it, cookie);
559 next:
560                 result = iops->next(env, it);
561         } while (result == 0);
562
563         GOTO(out_put, rc = 0);
564 out_put:
565         iops->put(env, it);
566         iops->fini(env, it);
567
568 out:
569         return rc;
570 }
571
572 /**
573  * open the PENDING directory for device \a mdd
574  *
575  * The PENDING directory persistently tracks files and directories that were
576  * unlinked from the namespace (nlink == 0) but are still held open by clients.
577  * Those inodes shouldn't be deleted if the MDS crashes, because the clients
578  * would not be able to recover and reopen those files.  Instead, these inodes
579  * are linked into the PENDING directory on disk, and only deleted if all
580  * clients close them, or the MDS finishes client recovery without any client
581  * reopening them (i.e. former clients didn't join recovery).
582  *  \param d   mdd device being started.
583  *
584  *  \retval 0  success
585  *  \retval  -ve index operation error.
586  *
587  */
588 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
589 {
590         struct lu_fid fid;
591         struct dt_object *d;
592         int rc = 0;
593         ENTRY;
594
595         d = dt_store_open(env, mdd->mdd_child, "", orph_index_name, &fid);
596         if (!IS_ERR(d)) {
597                 mdd->mdd_orphans = d;
598                 if (!dt_try_as_dir(env, d)) {
599                         rc = -ENOTDIR;
600                         CERROR("\"%s\" is not an index! : rc = %d\n",
601                                         orph_index_name, rc);
602                 }
603         } else {
604                 CERROR("cannot find \"%s\" obj %d\n",
605                        orph_index_name, (int)PTR_ERR(d));
606                 rc = PTR_ERR(d);
607         }
608
609         RETURN(rc);
610 }
611
612 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
613 {
614         ENTRY;
615         if (mdd->mdd_orphans != NULL) {
616                 lu_object_put(env, &mdd->mdd_orphans->do_lu);
617                 mdd->mdd_orphans = NULL;
618         }
619         EXIT;
620 }
621
622 /**
623  *  Iterate orphan index to cleanup orphan objects after recovery is done.
624  *  \param d   mdd device in recovery.
625  */
626 int __mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
627 {
628         return orph_index_iterate(env, d);
629 }
630
631 /**
632  *  add an orphan \a obj to the orphan index.
633  *  \param obj file or directory.
634  *  \param th  transaction for index insert.
635  *
636  *  \pre obj nlink == 0 && obj->mod_count != 0
637  *
638  *  \retval 0  success
639  *  \retval  -ve index operation error.
640  */
641 int __mdd_orphan_add(const struct lu_env *env,
642                      struct mdd_object *obj, struct thandle *th)
643 {
644         return orph_index_insert(env, obj, ORPH_OP_UNLINK, th);
645 }
646
647 /**
648  *  delete an orphan \a obj from orphan index.
649  *  \param obj file or directory.
650  *  \param th  transaction for index deletion and object destruction.
651  *
652  *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
653  *
654  *  \retval 0  success
655  *  \retval  -ve index operation error.
656  */
657 int __mdd_orphan_del(const struct lu_env *env,
658                      struct mdd_object *obj, struct thandle *th)
659 {
660         return orph_index_delete(env, obj, ORPH_OP_UNLINK, th);
661 }