Whamcloud - gitweb
LU-12846 mdd: return error while delete failed
[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, 2017, 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 static const char mdd_orphan_index_name[] = MDT_ORPHAN_DIR;
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 *mdd_orphan_key_fill(const struct lu_env *env,
59                                           const struct lu_fid *lf)
60 {
61         char *key = mdd_env_info(env)->mti_key;
62
63         LASSERT(key);
64         if (!(MTI_KEEP_KEY & mdd_env_info(env)->mti_flags))
65                 snprintf(key, sizeof(mdd_env_info(env)->mti_key),
66                          DFID_NOBRACE, PFID(lf));
67
68         return (struct dt_key *)key;
69 }
70
71 /* compatibility with orphan files created in versions before 2.11 */
72 static struct dt_key *mdd_orphan_key_fill_20(const struct lu_env *env,
73                                              const struct lu_fid *lf)
74 {
75         char *key = mdd_env_info(env)->mti_key;
76
77         LASSERT(key);
78         if (!(MTI_KEEP_KEY & mdd_env_info(env)->mti_flags))
79                 snprintf(key, sizeof(mdd_env_info(env)->mti_key),
80                          ORPHAN_FILE_NAME_FORMAT_20,
81                          fid_seq(lf), fid_oid(lf), fid_ver(lf),
82                          ORPH_OP_UNLINK);
83
84         return (struct dt_key *)key;
85 }
86
87 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
88                                         struct mdd_device *mdd,
89                                         struct mdd_object *obj,
90                                         struct thandle *th)
91 {
92         struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
93         struct dt_object *dor = mdd->mdd_orphans;
94         const struct lu_fid *lf = mdd_object_fid(obj);
95         struct dt_key *key = mdd_orphan_key_fill(env, lf);
96
97         rec->rec_fid = lf;
98         rec->rec_type = mdd_object_type(obj);
99
100         return dt_insert(env, dor, (const struct dt_rec *)rec, key, th);
101 }
102
103 int mdd_orphan_declare_insert(const struct lu_env *env, struct mdd_object *obj,
104                               umode_t mode, struct thandle *th)
105 {
106         struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
107         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
108         struct dt_key *key;
109         int rc;
110
111         key = mdd_orphan_key_fill(env, mdd_object_fid(obj));
112
113         rec->rec_fid = mdd_object_fid(obj);
114         rec->rec_type = mode;
115         rc = dt_declare_insert(env, mdd->mdd_orphans,
116                                (const struct dt_rec *)rec, key, th);
117         if (rc != 0)
118                 return rc;
119
120         rc = mdo_declare_ref_add(env, obj, th);
121         if (rc)
122                 return rc;
123
124         if (!S_ISDIR(mode))
125                 return 0;
126
127         rc = mdo_declare_ref_add(env, obj, th);
128         if (rc)
129                 return rc;
130
131         rc = dt_declare_ref_add(env, mdd->mdd_orphans, th);
132         if (rc)
133                 return rc;
134
135         rc = mdo_declare_index_delete(env, obj, dotdot, th);
136         if (rc)
137                 return rc;
138
139         rc = mdo_declare_index_insert(env, obj,
140                                       lu_object_fid(&mdd->mdd_orphans->do_lu),
141                                       S_IFDIR, dotdot, th);
142
143         return rc;
144 }
145
146 /**
147  *  add an orphan \a obj to the orphan index.
148  *  \param obj file or directory.
149  *  \param th  transaction for index insert.
150  *
151  *  \pre obj nlink == 0 && obj->mod_count != 0
152  *
153  *  \retval 0  success
154  *  \retval  -ve index operation error.
155  */
156 int mdd_orphan_insert(const struct lu_env *env, struct mdd_object *obj,
157                       struct thandle *th)
158 {
159         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
160         struct dt_object *dor = mdd->mdd_orphans;
161         const struct lu_fid *lf_dor = lu_object_fid(&dor->do_lu);
162         struct dt_object *next = mdd_object_child(obj);
163         struct dt_insert_rec *rec = &mdd_env_info(env)->mti_dt_rec;
164         int rc;
165         ENTRY;
166
167         LASSERT(mdd_write_locked(env, obj) != 0);
168         LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
169
170         dt_write_lock(env, mdd->mdd_orphans, DT_TGT_ORPHAN);
171
172         rc = mdd_orphan_insert_obj(env, mdd, obj, th);
173         if (rc)
174                 GOTO(out, rc);
175
176         mdo_ref_add(env, obj, th);
177         if (!S_ISDIR(mdd_object_type(obj)))
178                 GOTO(out, rc = 0);
179
180         mdo_ref_add(env, obj, th);
181         dt_ref_add(env, mdd->mdd_orphans, th);
182
183         /* try best to fixup directory, do not return errors from here */
184         if (!dt_try_as_dir(env, next))
185                 GOTO(out, rc = 0);
186
187         dt_delete(env, next, (const struct dt_key *)dotdot, th);
188
189         rec->rec_fid = lf_dor;
190         rec->rec_type = S_IFDIR;
191         dt_insert(env, next, (const struct dt_rec *)rec,
192                   (const struct dt_key *)dotdot, th);
193
194 out:
195         if (rc == 0)
196                 obj->mod_flags |= ORPHAN_OBJ;
197
198         dt_write_unlock(env, mdd->mdd_orphans);
199
200         RETURN(rc);
201 }
202
203 int mdd_orphan_declare_delete(const struct lu_env *env, struct mdd_object *obj,
204                               struct thandle *th)
205 {
206         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
207         struct dt_key *key;
208         int rc;
209
210         key = mdd_orphan_key_fill(env, mdd_object_fid(obj));
211
212         rc = dt_declare_delete(env, mdd->mdd_orphans, key, th);
213         if (rc)
214                 return rc;
215
216         if (!mdd_object_exists(obj))
217                 return -ENOENT;
218
219         rc = mdo_declare_ref_del(env, obj, th);
220         if (rc)
221                 return rc;
222
223         if (S_ISDIR(mdd_object_type(obj))) {
224                 rc = mdo_declare_ref_del(env, obj, th);
225                 if (rc)
226                         return rc;
227
228                 rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
229         }
230
231         return rc;
232 }
233
234 /**
235  *  delete an orphan \a obj from orphan index.
236  *  \param obj file or directory.
237  *  \param th  transaction for index deletion and object destruction.
238  *
239  *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
240  *
241  *  \retval 0  success
242  *  \retval  -ve index operation error.
243  */
244 int mdd_orphan_delete(const struct lu_env *env, struct mdd_object *obj,
245                       struct thandle *th)
246 {
247         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
248         struct dt_object *dor = mdd->mdd_orphans;
249         struct dt_key *key;
250         int rc = 0;
251
252         ENTRY;
253
254         LASSERT(mdd_write_locked(env, obj) != 0);
255         LASSERT(obj->mod_flags & ORPHAN_OBJ);
256         LASSERT(obj->mod_count == 0);
257
258         LASSERT(dor);
259
260         key = mdd_orphan_key_fill(env, mdd_object_fid(obj));
261         dt_write_lock(env, mdd->mdd_orphans, DT_TGT_ORPHAN);
262
263         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ORPHAN_DELETE))
264                 goto ref_del;
265
266         rc = dt_delete(env, mdd->mdd_orphans, key, th);
267         if (rc == -ENOENT) {
268                 key = mdd_orphan_key_fill_20(env, mdd_object_fid(obj));
269                 rc = dt_delete(env, mdd->mdd_orphans, key, th);
270         }
271
272 ref_del:
273         if (!rc) {
274                 /* lov objects will be destroyed by caller */
275                 mdo_ref_del(env, obj, th);
276                 if (S_ISDIR(mdd_object_type(obj))) {
277                         mdo_ref_del(env, obj, th);
278                         dt_ref_del(env, mdd->mdd_orphans, th);
279                 }
280                 obj->mod_flags &= ~ORPHAN_OBJ;
281         } else {
282                 CERROR("%s: could not delete orphan object "DFID": rc = %d\n",
283                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)), rc);
284         }
285
286         dt_write_unlock(env, mdd->mdd_orphans);
287         RETURN(rc);
288 }
289
290
291 static int mdd_orphan_destroy(const struct lu_env *env, struct mdd_object *obj,
292                               struct dt_key *key)
293 {
294         struct thandle *th = NULL;
295         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
296         bool orphan_exists = true;
297         int rc = 0, rc1 = 0;
298         ENTRY;
299
300         th = mdd_trans_create(env, mdd);
301         if (IS_ERR(th)) {
302                 rc = PTR_ERR(th);
303                 if (rc != -EINPROGRESS)
304                         CERROR("%s: cannot get orphan thandle: rc = %d\n",
305                                mdd2obd_dev(mdd)->obd_name, rc);
306                 RETURN(rc);
307         }
308
309         mdd_write_lock(env, obj, DT_TGT_CHILD);
310         rc = mdd_orphan_declare_delete(env, obj, th);
311         if (rc == -ENOENT)
312                 orphan_exists = false;
313         else if (rc)
314                 GOTO(unlock, rc);
315
316         if (orphan_exists) {
317                 rc = mdo_declare_destroy(env, obj, th);
318                 if (rc)
319                         GOTO(unlock, rc);
320         }
321
322         rc = mdd_trans_start(env, mdd, th);
323         if (rc)
324                 GOTO(unlock, rc);
325
326         if (likely(obj->mod_count == 0)) {
327                 dt_write_lock(env, mdd->mdd_orphans, DT_TGT_ORPHAN);
328                 rc = dt_delete(env, mdd->mdd_orphans, key, th);
329                 /* We should remove object even dt_delete failed */
330                 if (orphan_exists) {
331                         mdo_ref_del(env, obj, th);
332                         if (S_ISDIR(mdd_object_type(obj))) {
333                                 mdo_ref_del(env, obj, th);
334                                 dt_ref_del(env, mdd->mdd_orphans, th);
335                         }
336                         rc1 = mdo_destroy(env, obj, th);
337                 }
338                 dt_write_unlock(env, mdd->mdd_orphans);
339         }
340 unlock:
341         mdd_write_unlock(env, obj);
342         mdd_trans_stop(env, mdd, 0, th);
343
344         RETURN(rc ? rc : rc1);
345 }
346
347 /**
348  * Delete unused orphan with FID \a lf from PENDING directory
349  *
350  * \param mdd  MDD device finishing recovery
351  * \param lf   FID of file or directory to delete
352  * \param key  cookie for this entry in index iterator
353  *
354  * \retval 0   success
355  * \retval -ve error
356  */
357 static int mdd_orphan_key_test_and_delete(const struct lu_env *env,
358                                           struct mdd_device *mdd,
359                                           struct lu_fid *lf, 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                 CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
372                 rc = mdd_orphan_destroy(env, mdo, key);
373                 if (rc) /* below message checked in replay-single.sh test_37 */
374                         CERROR("%s: error unlinking orphan "DFID": rc = %d\n",
375                                mdd2obd_dev(mdd)->obd_name, PFID(lf), rc);
376         } else {
377                 mdd_write_lock(env, mdo, DT_TGT_CHILD);
378                 if (likely(mdo->mod_count > 0)) {
379                         CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
380                                PFID(lf), mdo->mod_count);
381                         mdo->mod_flags |= ORPHAN_OBJ;
382                 }
383                 mdd_write_unlock(env, mdo);
384         }
385
386         mdd_object_put(env, mdo);
387         return rc;
388 }
389
390 /**
391  * delete unreferenced files and directories in the PENDING directory
392  *
393  * Files that remain in PENDING after client->MDS recovery has completed
394  * have to be referenced (opened) by some client during recovery, or they
395  * will be deleted here (for clients that did not complete recovery).
396  *
397  * \param thread  info about orphan cleanup thread
398  *
399  * \retval 0   success
400  * \retval -ve error
401  */
402 static int mdd_orphan_index_iterate(const struct lu_env *env,
403                                     struct mdd_generic_thread *thread)
404 {
405         struct mdd_device *mdd = (struct mdd_device *)thread->mgt_data;
406         struct dt_object *dor = mdd->mdd_orphans;
407         struct lu_dirent *ent = &mdd_env_info(env)->mti_ent;
408         const struct dt_it_ops *iops;
409         struct dt_it *it;
410         struct lu_fid fid;
411         int key_sz = 0;
412         int rc;
413         ENTRY;
414
415         iops = &dor->do_index_ops->dio_it;
416         it = iops->init(env, dor, LUDA_64BITHASH);
417         if (IS_ERR(it)) {
418                 rc = PTR_ERR(it);
419                 CERROR("%s: cannot clean '%s': rc = %d\n",
420                        mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name, rc);
421                 GOTO(out, rc);
422         }
423
424         rc = iops->load(env, it, 0);
425         if (rc < 0)
426                 GOTO(out_put, rc);
427         if (rc == 0) {
428                 CERROR("%s: error loading iterator to clean '%s'\n",
429                        mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name);
430                 /* Index contains no zero key? */
431                 GOTO(out_put, rc = -EIO);
432         }
433
434         mdd_env_info(env)->mti_flags |= MTI_KEEP_KEY;
435         do {
436                 if (thread->mgt_abort)
437                         break;
438
439                 key_sz = iops->key_size(env, it);
440                 /* filter out "." and ".." entries from PENDING dir. */
441                 if (key_sz < 8)
442                         goto next;
443
444                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
445                 if (rc != 0) {
446                         CERROR("%s: fail to get FID for orphan it: rc = %d\n",
447                                mdd2obd_dev(mdd)->obd_name, rc);
448                         goto next;
449                 }
450
451                 fid_le_to_cpu(&fid, &ent->lde_fid);
452                 if (!fid_is_sane(&fid)) {
453                         CERROR("%s: bad FID "DFID" cleaning '%s'\n",
454                                mdd2obd_dev(mdd)->obd_name, PFID(&fid),
455                                mdd_orphan_index_name);
456                         goto next;
457                 }
458
459                 /* kill orphan object */
460                 iops->put(env, it);
461                 rc = mdd_orphan_key_test_and_delete(env, mdd, &fid,
462                                                 (struct dt_key *)ent->lde_name);
463                 /* after index delete reset iterator */
464                 if (rc == 0)
465                         rc = iops->get(env, it, (const void *)"");
466 next:
467                 rc = iops->next(env, it);
468         } while (rc == 0);
469
470         GOTO(out_put, rc = 0);
471 out_put:
472         iops->put(env, it);
473         iops->fini(env, it);
474
475 out:
476         return rc;
477 }
478
479 /**
480  * open the PENDING directory for device \a mdd
481  *
482  * The PENDING directory persistently tracks files and directories that were
483  * unlinked from the namespace (nlink == 0) but are still held open by clients.
484  * Those inodes shouldn't be deleted if the MDS crashes, because the clients
485  * would not be able to recover and reopen those files.  Instead, these inodes
486  * are linked into the PENDING directory on disk, and only deleted if all
487  * clients close them, or the MDS finishes client recovery without any client
488  * reopening them (i.e. former clients didn't join recovery).
489  *  \param d   mdd device being started.
490  *
491  *  \retval 0  success
492  *  \retval  -ve index operation error.
493  *
494  */
495 int mdd_orphan_index_init(const struct lu_env *env, struct mdd_device *mdd)
496 {
497         struct lu_fid fid;
498         struct dt_object *d;
499         int rc = 0;
500
501         ENTRY;
502
503         /* create PENDING dir */
504         fid_zero(&fid);
505         rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
506                                    mdd_orphan_index_name, S_IFDIR | S_IRUGO |
507                                    S_IWUSR | S_IXUGO, &fid);
508         if (rc < 0)
509                 RETURN(rc);
510
511         d = dt_locate(env, mdd->mdd_child, &fid);
512         if (IS_ERR(d))
513                 RETURN(PTR_ERR(d));
514         LASSERT(lu_object_exists(&d->do_lu));
515         if (!dt_try_as_dir(env, d)) {
516                 CERROR("%s: orphan dir '%s' is not an index: rc = %d\n",
517                        mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name, rc);
518                 dt_object_put(env, d);
519                 RETURN(-ENOTDIR);
520         }
521         mdd->mdd_orphans = d;
522         RETURN(0);
523 }
524
525 void mdd_orphan_index_fini(const struct lu_env *env, struct mdd_device *mdd)
526 {
527         ENTRY;
528         if (mdd->mdd_orphans != NULL) {
529                 dt_object_put(env, mdd->mdd_orphans);
530                 mdd->mdd_orphans = NULL;
531         }
532         EXIT;
533 }
534
535 static int mdd_orphan_cleanup_thread(void *args)
536 {
537         struct mdd_generic_thread *thread = (struct mdd_generic_thread *)args;
538         struct lu_env *env = NULL;
539         int rc;
540         ENTRY;
541
542         complete(&thread->mgt_started);
543
544         OBD_ALLOC_PTR(env);
545         if (env == NULL)
546                 GOTO(out, rc = -ENOMEM);
547
548         rc = lu_env_init(env, LCT_MD_THREAD);
549         if (rc)
550                 GOTO(out, rc);
551
552         rc = mdd_orphan_index_iterate(env, thread);
553
554         lu_env_fini(env);
555         GOTO(out, rc);
556 out:
557         if (env)
558                 OBD_FREE_PTR(env);
559         complete(&thread->mgt_finished);
560         return rc;
561 }
562
563 /**
564  *  Iterate orphan index to cleanup orphan objects after recovery is done.
565  *  \param d   mdd device in recovery.
566  */
567 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
568 {
569         int rc = -ENOMEM;
570         char *name = NULL;
571
572         OBD_ALLOC(name, MTI_NAME_MAXLEN);
573         if (name == NULL)
574                 goto out;
575
576         snprintf(name, MTI_NAME_MAXLEN, "orph_%s", mdd2obd_dev(d)->obd_name);
577
578         rc = mdd_generic_thread_start(&d->mdd_orphan_cleanup_thread,
579                                       mdd_orphan_cleanup_thread, d, name);
580 out:
581         if (rc)
582                 CERROR("%s: start orphan cleanup thread failed: rc = %d\n",
583                        mdd2obd_dev(d)->obd_name, rc);
584         if (name)
585                 OBD_FREE(name, MTI_NAME_MAXLEN);
586
587         return rc;
588 }