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