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