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