Whamcloud - gitweb
LU-7787 mdd: clean up orphan object handling
[fs/lustre-release.git] / lustre / mdd / mdd_orphans.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/mdd/mdd_orphans.c
33  *
34  * Orphan handling code
35  *
36  * Author: Mike Pershin <tappro@clusterfs.com>
37  *         Pravin B Shelar <pravin.shelar@sun.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_MDS
41
42 #include <obd.h>
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_fid.h>
46 #include "mdd_internal.h"
47
48 const char orph_index_name[] = "PENDING";
49 static const char dotdot[] = "..";
50
51 enum {
52         ORPH_OP_UNLINK,
53 };
54
55 /* obsolete after 2.11, needed for upgrades from older 2.x versions */
56 #define ORPHAN_FILE_NAME_FORMAT_20      "%016llx:%08x:%08x:%2x"
57
58 static struct dt_key *orph_key_fill(const struct lu_env *env,
59                                     const struct lu_fid *lf)
60 {
61         char *key = mdd_env_info(env)->mti_key;
62         int rc;
63
64         LASSERT(key);
65         rc = snprintf(key, NAME_MAX + 1, DFID_NOBRACE, PFID(lf));
66         if (rc > 0)
67                 return (struct dt_key *)key;
68
69         return ERR_PTR(rc);
70 }
71
72 /* compatibility with orphan files created in versions before 2.11 */
73 static struct dt_key *orph_key_fill_20(const struct lu_env *env,
74                                        const struct lu_fid *lf)
75 {
76         char *key = mdd_env_info(env)->mti_key;
77         int rc;
78
79         LASSERT(key);
80         rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT_20,
81                       fid_seq(lf), fid_oid(lf), fid_ver(lf), ORPH_OP_UNLINK);
82         if (rc > 0)
83                 return (struct dt_key *)key;
84
85         return ERR_PTR(rc);
86 }
87
88 static inline void mdd_orphan_write_lock(const struct lu_env *env,
89                                          struct mdd_device *mdd)
90 {
91         struct dt_object *dor = mdd->mdd_orphans;
92         dt_write_lock(env, dor, MOR_TGT_ORPHAN);
93 }
94
95 static inline void mdd_orphan_write_unlock(const struct lu_env *env,
96                                            struct mdd_device *mdd)
97 {
98         struct dt_object *dor = mdd->mdd_orphans;
99         dt_write_unlock(env, dor);
100 }
101
102 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
103                                         struct mdd_device *mdd,
104                                         struct mdd_object *obj,
105                                         __u32 op, struct thandle *th)
106 {
107         struct dt_insert_rec    *rec    = &mdd_env_info(env)->mti_dt_rec;
108         struct dt_object        *dor    = mdd->mdd_orphans;
109         const struct lu_fid     *lf     = mdo2fid(obj);
110         struct dt_key           *key    = orph_key_fill(env, lf, op);
111
112         rec->rec_fid = lf;
113         rec->rec_type = mdd_object_type(obj);
114
115         return dt_insert(env, dor, (const struct dt_rec *)rec, key, th, 1);
116 }
117
118 static inline int mdd_orphan_delete_obj(const struct lu_env *env,
119                                         struct mdd_device  *mdd ,
120                                         struct dt_key *key,
121                                         struct thandle *th)
122 {
123         struct dt_object *dor = mdd->mdd_orphans;
124
125         return dt_delete(env, dor, key, th);
126 }
127
128 static inline int mdd_orphan_ref_add(const struct lu_env *env,
129                                      struct mdd_device *mdd,
130                                      struct thandle *th)
131 {
132         struct dt_object *dor = mdd->mdd_orphans;
133         return dt_ref_add(env, dor, th);
134 }
135
136 static inline int mdd_orphan_ref_del(const struct lu_env *env,
137                                      struct mdd_device *mdd,
138                                      struct thandle *th)
139 {
140         struct dt_object *dor = mdd->mdd_orphans;
141         return dt_ref_del(env, dor, th);
142 }
143
144
145 int orph_declare_index_insert(const struct lu_env *env, struct mdd_object *obj,
146                               umode_t mode, struct thandle *th)
147 {
148         struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
149         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
150         struct dt_key *key;
151         int rc;
152
153         key = orph_key_fill(env, mdo2fid(obj));
154
155         rec->rec_fid = mdo2fid(obj);
156         rec->rec_type = mode;
157         rc = dt_declare_insert(env, mdd->mdd_orphans,
158                                (const struct dt_rec *)rec, key, th);
159         if (rc != 0)
160                 return rc;
161
162         rc = mdo_declare_ref_add(env, obj, th);
163         if (rc)
164                 return rc;
165
166         if (!S_ISDIR(mode))
167                 return 0;
168
169         rc = mdo_declare_ref_add(env, obj, th);
170         if (rc)
171                 return rc;
172
173         rc = dt_declare_ref_add(env, mdd->mdd_orphans, th);
174         if (rc)
175                 return rc;
176
177         rc = mdo_declare_index_delete(env, obj, dotdot, th);
178         if (rc)
179                 return rc;
180
181         rc = mdo_declare_index_insert(env, obj,
182                                       lu_object_fid(&mdd->mdd_orphans->do_lu),
183                                       S_IFDIR, dotdot, th);
184
185         return rc;
186 }
187
188 static int orph_index_insert(const struct lu_env *env,
189                              struct mdd_object *obj,
190                              __u32 op, struct thandle *th)
191 {
192         struct mdd_device       *mdd    = mdo2mdd(&obj->mod_obj);
193         struct dt_object        *dor    = mdd->mdd_orphans;
194         const struct lu_fid     *lf_dor = lu_object_fid(&dor->do_lu);
195         struct dt_object        *next   = mdd_object_child(obj);
196         struct dt_insert_rec    *rec    = &mdd_env_info(env)->mti_dt_rec;
197         int                      rc;
198         ENTRY;
199
200         LASSERT(mdd_write_locked(env, obj) != 0);
201         LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
202
203         mdd_orphan_write_lock(env, mdd);
204
205         rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
206         if (rc)
207                 GOTO(out, rc);
208
209         mdo_ref_add(env, obj, th);
210         if (!S_ISDIR(mdd_object_type(obj)))
211                 GOTO(out, rc = 0);
212
213         mdo_ref_add(env, obj, th);
214         mdd_orphan_ref_add(env, mdd, th);
215
216         /* try best to fixup directory, dont return errors
217          * from here */
218         if (!dt_try_as_dir(env, next))
219                 GOTO(out, rc = 0);
220
221         dt_delete(env, next, (const struct dt_key *)dotdot, th);
222
223         rec->rec_fid = lf_dor;
224         rec->rec_type = S_IFDIR;
225         dt_insert(env, next, (const struct dt_rec *)rec,
226                   (const struct dt_key *)dotdot, th, 1);
227
228 out:
229         if (rc == 0)
230                 obj->mod_flags |= ORPHAN_OBJ;
231
232         mdd_orphan_write_unlock(env, mdd);
233
234         RETURN(rc);
235 }
236
237 int orph_declare_index_delete(const struct lu_env *env, struct mdd_object *obj,
238                               struct thandle *th)
239 {
240         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
241         struct dt_key *key;
242         int rc;
243
244         key = orph_key_fill(env, mdo2fid(obj));
245
246         rc = dt_declare_delete(env, mdd->mdd_orphans, key, th);
247         if (rc)
248                 return rc;
249
250         rc = mdo_declare_ref_del(env, obj, th);
251         if (rc)
252                 return rc;
253
254         if (S_ISDIR(mdd_object_type(obj))) {
255                 rc = mdo_declare_ref_del(env, obj, th);
256                 if (rc)
257                         return rc;
258
259                 rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
260         }
261
262         return rc;
263 }
264
265 static int orph_index_delete(const struct lu_env *env, struct mdd_object *obj,
266                              __u32 op, struct thandle *th)
267 {
268         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
269         struct dt_object *dor = mdd->mdd_orphans;
270         struct dt_key *key;
271         int rc;
272
273         ENTRY;
274
275         LASSERT(mdd_write_locked(env, obj) != 0);
276         LASSERT(obj->mod_flags & ORPHAN_OBJ);
277         LASSERT(obj->mod_count == 0);
278
279         LASSERT(dor);
280
281         key = orph_key_fill(env, mdo2fid(obj), op);
282         mdd_orphan_write_lock(env, mdd);
283
284         rc = mdd_orphan_delete_obj(env, mdd, key, th);
285
286         if (rc == -ENOENT) {
287                 key = orph_key_fill_20(env, mdo2fid(obj));
288                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
289         }
290
291         if (!rc) {
292                 /* lov objects will be destroyed by caller */
293                 mdo_ref_del(env, obj, th);
294                 if (S_ISDIR(mdd_object_type(obj))) {
295                         mdo_ref_del(env, obj, th);
296                         mdd_orphan_ref_del(env, mdd, th);
297                 }
298                 obj->mod_flags &= ~ORPHAN_OBJ;
299         } else {
300                 CERROR("%s: could not delete orphan object "DFID": rc = %d\n",
301                        mdd2obd_dev(mdd)->obd_name, PFID(mdo2fid(obj)), rc);
302         }
303
304         mdd_orphan_write_unlock(env, mdd);
305         RETURN(rc);
306 }
307
308
309 static int orphan_destroy(const struct lu_env *env, struct mdd_object *obj,
310                           struct dt_key *key)
311 {
312         struct thandle *th = NULL;
313         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
314         int rc = 0;
315         ENTRY;
316
317         th = mdd_trans_create(env, mdd);
318         if (IS_ERR(th)) {
319                 CERROR("Cannot get thandle\n");
320                 RETURN(PTR_ERR(th));
321         }
322
323         rc = orph_declare_index_delete(env, obj, th);
324         if (rc)
325                 GOTO(stop, rc);
326
327         rc = mdo_declare_destroy(env, obj, th);
328         if (rc)
329                 GOTO(stop, rc);
330
331         rc = mdd_trans_start(env, mdd, th);
332         if (rc)
333                 GOTO(stop, rc);
334
335         mdd_write_lock(env, obj, MOR_TGT_CHILD);
336         if (likely(obj->mod_count == 0)) {
337                 mdd_orphan_write_lock(env, mdd);
338                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
339                 if (rc == 0) {
340                         mdo_ref_del(env, obj, th);
341                         if (S_ISDIR(mdd_object_type(obj))) {
342                                 mdo_ref_del(env, obj, th);
343                                 mdd_orphan_ref_del(env, mdd, th);
344                         }
345                         rc = mdo_destroy(env, obj, 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
352 stop:
353         rc = mdd_trans_stop(env, mdd, 0, th);
354
355         RETURN(rc);
356 }
357
358 /**
359  * Delete unused orphan with FID \a lf from PENDING directory
360  *
361  * \param mdd  MDD device finishing recovery
362  * \param lf   FID of file or directory to delete
363  * \param key  cookie for this entry in index iterator
364  *
365  * \retval 0   success
366  * \retval -ve error
367  */
368 static int orph_key_test_and_del(const struct lu_env *env,
369                                  struct mdd_device *mdd, struct lu_fid *lf,
370                                  struct dt_key *key)
371 {
372         struct mdd_object *mdo;
373         int rc;
374
375         mdo = mdd_object_find(env, mdd, lf);
376
377         if (IS_ERR(mdo))
378                 return PTR_ERR(mdo);
379
380         rc = -EBUSY;
381         if (mdo->mod_count == 0) {
382                 CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
383                 rc = orphan_destroy(env, mdo, key);
384                 if (rc) /* so replay-single.sh test_37 works */
385                         CERROR("%s: error unlinking orphan "DFID" from "
386                                "PENDING: rc = %d\n",
387                                mdd2obd_dev(mdd)->obd_name, PFID(lf), rc);
388         } else {
389                 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
390                 if (likely(mdo->mod_count > 0)) {
391                         CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
392                                PFID(lf), mdo->mod_count);
393                         mdo->mod_flags |= ORPHAN_OBJ;
394                 }
395                 mdd_write_unlock(env, mdo);
396         }
397
398         mdd_object_put(env, mdo);
399         return rc;
400 }
401
402 /**
403  * delete unreferenced files and directories in the PENDING directory
404  *
405  * Files that remain in PENDING after client->MDS recovery has completed
406  * have to be referenced (opened) by some client during recovery, or they
407  * will be deleted here (for clients that did not complete recovery).
408  *
409  * \param thread  info about orphan cleanup thread
410  *
411  * \retval 0   success
412  * \retval -ve error
413  */
414 static int orph_index_iterate(const struct lu_env *env,
415                               struct mdd_generic_thread *thread)
416 {
417         struct mdd_device *mdd = (struct mdd_device *)thread->mgt_data;
418         struct dt_object *dor = mdd->mdd_orphans;
419         struct lu_dirent *ent = &mdd_env_info(env)->mti_ent;
420         const struct dt_it_ops *iops;
421         struct dt_it     *it;
422         struct lu_fid     fid;
423         int               key_sz = 0;
424         int               rc;
425         __u64             cookie;
426         ENTRY;
427
428         iops = &dor->do_index_ops->dio_it;
429         it = iops->init(env, dor, LUDA_64BITHASH);
430         if (IS_ERR(it)) {
431                 rc = PTR_ERR(it);
432                 CERROR("%s: cannot clean PENDING: rc = %d\n",
433                        mdd2obd_dev(mdd)->obd_name, rc);
434                 GOTO(out, rc);
435         }
436
437         rc = iops->load(env, it, 0);
438         if (rc < 0)
439                 GOTO(out_put, rc);
440         if (rc == 0) {
441                 CERROR("%s: error loading iterator to clean PENDING\n",
442                        mdd2obd_dev(mdd)->obd_name);
443                 /* Index contains no zero key? */
444                 GOTO(out_put, rc = -EIO);
445         }
446
447         do {
448                 if (thread->mgt_abort)
449                         break;
450
451                 key_sz = iops->key_size(env, it);
452                 /* filter out "." and ".." entries from PENDING dir. */
453                 if (key_sz < 8)
454                         goto next;
455
456                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
457                 if (rc != 0) {
458                         CERROR("%s: fail to get FID for orphan it: rc = %d\n",
459                                mdd2obd_dev(mdd)->obd_name, rc);
460                         goto next;
461                 }
462
463                 fid_le_to_cpu(&fid, &ent->lde_fid);
464                 if (!fid_is_sane(&fid)) {
465                         CERROR("%s: bad FID "DFID" cleaning PENDING\n",
466                                mdd2obd_dev(mdd)->obd_name, PFID(&fid));
467                         goto next;
468                 }
469
470                 /* kill orphan object */
471                 cookie = iops->store(env, it);
472                 iops->put(env, it);
473                 rc = orph_key_test_and_del(env, mdd, &fid,
474                                            (struct dt_key *)ent->lde_name);
475
476                 /* after index delete reset iterator */
477                 if (rc == 0)
478                         rc = iops->get(env, it, (const void *)"");
479                 else
480                         rc = iops->load(env, it, cookie);
481 next:
482                 rc = iops->next(env, it);
483         } while (rc == 0);
484
485         GOTO(out_put, rc = 0);
486 out_put:
487         iops->put(env, it);
488         iops->fini(env, it);
489
490 out:
491         return rc;
492 }
493
494 /**
495  * open the PENDING directory for device \a mdd
496  *
497  * The PENDING directory persistently tracks files and directories that were
498  * unlinked from the namespace (nlink == 0) but are still held open by clients.
499  * Those inodes shouldn't be deleted if the MDS crashes, because the clients
500  * would not be able to recover and reopen those files.  Instead, these inodes
501  * are linked into the PENDING directory on disk, and only deleted if all
502  * clients close them, or the MDS finishes client recovery without any client
503  * reopening them (i.e. former clients didn't join recovery).
504  *  \param d   mdd device being started.
505  *
506  *  \retval 0  success
507  *  \retval  -ve index operation error.
508  *
509  */
510 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
511 {
512         struct lu_fid            fid;
513         struct dt_object        *d;
514         int                      rc = 0;
515
516         ENTRY;
517
518         /* create PENDING dir */
519         fid_zero(&fid);
520         rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
521                                    orph_index_name, S_IFDIR | S_IRUGO |
522                                    S_IWUSR | S_IXUGO, &fid);
523         if (rc < 0)
524                 RETURN(rc);
525
526         d = dt_locate(env, mdd->mdd_child, &fid);
527         if (IS_ERR(d))
528                 RETURN(PTR_ERR(d));
529         LASSERT(lu_object_exists(&d->do_lu));
530         if (!dt_try_as_dir(env, d)) {
531                 CERROR("%s: \"%s\" is not an index: rc = %d\n",
532                        mdd2obd_dev(mdd)->obd_name, orph_index_name, rc);
533                 dt_object_put(env, d);
534                 RETURN(-ENOTDIR);
535         }
536         mdd->mdd_orphans = d;
537         RETURN(0);
538 }
539
540 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
541 {
542         ENTRY;
543         if (mdd->mdd_orphans != NULL) {
544                 dt_object_put(env, mdd->mdd_orphans);
545                 mdd->mdd_orphans = NULL;
546         }
547         EXIT;
548 }
549
550 static int __mdd_orphan_cleanup(void *args)
551 {
552         struct mdd_generic_thread *thread = (struct mdd_generic_thread *)args;
553         struct lu_env             *env = NULL;
554         int                        rc;
555         ENTRY;
556
557         complete(&thread->mgt_started);
558
559         OBD_ALLOC_PTR(env);
560         if (env == NULL)
561                 GOTO(out, rc = -ENOMEM);
562
563         rc = lu_env_init(env, LCT_MD_THREAD);
564         if (rc)
565                 GOTO(out, rc);
566
567         rc = orph_index_iterate(env, thread);
568
569         lu_env_fini(env);
570         GOTO(out, rc);
571 out:
572         if (env)
573                 OBD_FREE_PTR(env);
574         complete(&thread->mgt_finished);
575         return rc;
576 }
577
578 /**
579  *  Iterate orphan index to cleanup orphan objects after recovery is done.
580  *  \param d   mdd device in recovery.
581  */
582 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
583 {
584         int rc = -ENOMEM;
585         char *name = NULL;
586
587         OBD_ALLOC(name, MTI_NAME_MAXLEN);
588         if (name == NULL)
589                 goto out;
590
591         snprintf(name, MTI_NAME_MAXLEN, "orph_cleanup_%s",
592                  mdd2obd_dev(d)->obd_name);
593
594         rc = mdd_generic_thread_start(&d->mdd_orph_cleanup_thread,
595                                       __mdd_orphan_cleanup, (void *)d, name);
596 out:
597         if (rc)
598                 CERROR("%s: start orphan cleanup thread failed:%d\n",
599                        mdd2obd_dev(d)->obd_name, rc);
600         if (name)
601                 OBD_FREE(name, MTI_NAME_MAXLEN);
602         return rc;
603 }
604
605 /**
606  *  add an orphan \a obj to the orphan index.
607  *  \param obj file or directory.
608  *  \param th  transaction for index insert.
609  *
610  *  \pre obj nlink == 0 && obj->mod_count != 0
611  *
612  *  \retval 0  success
613  *  \retval  -ve index operation error.
614  */
615 int __mdd_orphan_add(const struct lu_env *env, struct mdd_object *obj,
616                      struct thandle *th)
617 {
618         return orph_index_insert(env, obj, th);
619 }
620
621 /**
622  *  delete an orphan \a obj from orphan index.
623  *  \param obj file or directory.
624  *  \param th  transaction for index deletion and object destruction.
625  *
626  *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
627  *
628  *  \retval 0  success
629  *  \retval  -ve index operation error.
630  */
631 int __mdd_orphan_del(const struct lu_env *env, struct mdd_object *obj,
632                      struct thandle *th)
633 {
634         return orph_index_delete(env, obj, th);
635 }