Whamcloud - gitweb
LU-11364 osd: remove unused 'ignore quota' parameters
[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[] = "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 *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 = mdo2fid(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, mdo2fid(obj));
109
110         rec->rec_fid = mdo2fid(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, MOR_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, mdo2fid(obj));
208
209         rc = dt_declare_delete(env, mdd->mdd_orphans, key, th);
210         if (rc)
211                 return rc;
212
213         rc = mdo_declare_ref_del(env, obj, th);
214         if (rc)
215                 return rc;
216
217         if (S_ISDIR(mdd_object_type(obj))) {
218                 rc = mdo_declare_ref_del(env, obj, th);
219                 if (rc)
220                         return rc;
221
222                 rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
223         }
224
225         return rc;
226 }
227
228 /**
229  *  delete an orphan \a obj from orphan index.
230  *  \param obj file or directory.
231  *  \param th  transaction for index deletion and object destruction.
232  *
233  *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
234  *
235  *  \retval 0  success
236  *  \retval  -ve index operation error.
237  */
238 int mdd_orphan_delete(const struct lu_env *env, struct mdd_object *obj,
239                       struct thandle *th)
240 {
241         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
242         struct dt_object *dor = mdd->mdd_orphans;
243         struct dt_key *key;
244         int rc;
245
246         ENTRY;
247
248         LASSERT(mdd_write_locked(env, obj) != 0);
249         LASSERT(obj->mod_flags & ORPHAN_OBJ);
250         LASSERT(obj->mod_count == 0);
251
252         LASSERT(dor);
253
254         key = mdd_orphan_key_fill(env, mdo2fid(obj));
255         dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
256
257         rc = dt_delete(env, mdd->mdd_orphans, key, th);
258         if (rc == -ENOENT) {
259                 key = mdd_orphan_key_fill_20(env, mdo2fid(obj));
260                 rc = dt_delete(env, mdd->mdd_orphans, key, th);
261         }
262
263         if (!rc) {
264                 /* lov objects will be destroyed by caller */
265                 mdo_ref_del(env, obj, th);
266                 if (S_ISDIR(mdd_object_type(obj))) {
267                         mdo_ref_del(env, obj, th);
268                         dt_ref_del(env, mdd->mdd_orphans, th);
269                 }
270                 obj->mod_flags &= ~ORPHAN_OBJ;
271         } else {
272                 CERROR("%s: could not delete orphan object "DFID": rc = %d\n",
273                        mdd2obd_dev(mdd)->obd_name, PFID(mdo2fid(obj)), rc);
274         }
275
276         dt_write_unlock(env, mdd->mdd_orphans);
277         RETURN(rc);
278 }
279
280
281 static int mdd_orphan_destroy(const struct lu_env *env, struct mdd_object *obj,
282                               struct dt_key *key)
283 {
284         struct thandle *th = NULL;
285         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
286         int rc = 0;
287         ENTRY;
288
289         th = mdd_trans_create(env, mdd);
290         if (IS_ERR(th)) {
291                 rc = PTR_ERR(th);
292                 if (rc != -EINPROGRESS)
293                         CERROR("%s: cannot get orphan thandle: rc = %d\n",
294                                mdd2obd_dev(mdd)->obd_name, rc);
295                 RETURN(rc);
296         }
297
298         rc = mdd_orphan_declare_delete(env, obj, th);
299         if (rc)
300                 GOTO(stop, rc);
301
302         rc = mdo_declare_destroy(env, obj, th);
303         if (rc)
304                 GOTO(stop, rc);
305
306         rc = mdd_trans_start(env, mdd, th);
307         if (rc)
308                 GOTO(stop, rc);
309
310         mdd_write_lock(env, obj, MOR_TGT_CHILD);
311         if (likely(obj->mod_count == 0)) {
312                 dt_write_lock(env, mdd->mdd_orphans, MOR_TGT_ORPHAN);
313                 rc = dt_delete(env, mdd->mdd_orphans, key, th);
314                 if (rc == 0) {
315                         mdo_ref_del(env, obj, th);
316                         if (S_ISDIR(mdd_object_type(obj))) {
317                                 mdo_ref_del(env, obj, th);
318                                 dt_ref_del(env, mdd->mdd_orphans, th);
319                         }
320                         rc = mdo_destroy(env, obj, th);
321                 } else {
322                         CERROR("%s: could not delete orphan "DFID": rc = %d\n",
323                                mdd2obd_dev(mdd)->obd_name, PFID(mdo2fid(obj)),
324                                rc);
325                 }
326                 dt_write_unlock(env, mdd->mdd_orphans);
327         }
328         mdd_write_unlock(env, obj);
329
330 stop:
331         rc = mdd_trans_stop(env, mdd, 0, th);
332
333         RETURN(rc);
334 }
335
336 /**
337  * Delete unused orphan with FID \a lf from PENDING directory
338  *
339  * \param mdd  MDD device finishing recovery
340  * \param lf   FID of file or directory to delete
341  * \param key  cookie for this entry in index iterator
342  *
343  * \retval 0   success
344  * \retval -ve error
345  */
346 static int mdd_orphan_key_test_and_delete(const struct lu_env *env,
347                                           struct mdd_device *mdd,
348                                           struct lu_fid *lf, struct dt_key *key)
349 {
350         struct mdd_object *mdo;
351         int rc;
352
353         mdo = mdd_object_find(env, mdd, lf);
354
355         if (IS_ERR(mdo))
356                 return PTR_ERR(mdo);
357
358         rc = -EBUSY;
359         if (mdo->mod_count == 0) {
360                 CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
361                 rc = mdd_orphan_destroy(env, mdo, key);
362                 if (rc) /* so replay-single.sh test_37 works */
363                         CERROR("%s: error unlinking orphan "DFID": rc = %d\n",
364                                mdd2obd_dev(mdd)->obd_name, PFID(lf), rc);
365         } else {
366                 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
367                 if (likely(mdo->mod_count > 0)) {
368                         CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
369                                PFID(lf), mdo->mod_count);
370                         mdo->mod_flags |= ORPHAN_OBJ;
371                 }
372                 mdd_write_unlock(env, mdo);
373         }
374
375         mdd_object_put(env, mdo);
376         return rc;
377 }
378
379 /**
380  * delete unreferenced files and directories in the PENDING directory
381  *
382  * Files that remain in PENDING after client->MDS recovery has completed
383  * have to be referenced (opened) by some client during recovery, or they
384  * will be deleted here (for clients that did not complete recovery).
385  *
386  * \param thread  info about orphan cleanup thread
387  *
388  * \retval 0   success
389  * \retval -ve error
390  */
391 static int mdd_orphan_index_iterate(const struct lu_env *env,
392                                     struct mdd_generic_thread *thread)
393 {
394         struct mdd_device *mdd = (struct mdd_device *)thread->mgt_data;
395         struct dt_object *dor = mdd->mdd_orphans;
396         struct lu_dirent *ent = &mdd_env_info(env)->mti_ent;
397         const struct dt_it_ops *iops;
398         struct dt_it *it;
399         struct lu_fid fid;
400         int key_sz = 0;
401         int rc;
402         __u64 cookie;
403         ENTRY;
404
405         iops = &dor->do_index_ops->dio_it;
406         it = iops->init(env, dor, LUDA_64BITHASH);
407         if (IS_ERR(it)) {
408                 rc = PTR_ERR(it);
409                 CERROR("%s: cannot clean '%s': rc = %d\n",
410                        mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name, rc);
411                 GOTO(out, rc);
412         }
413
414         rc = iops->load(env, it, 0);
415         if (rc < 0)
416                 GOTO(out_put, rc);
417         if (rc == 0) {
418                 CERROR("%s: error loading iterator to clean '%s'\n",
419                        mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name);
420                 /* Index contains no zero key? */
421                 GOTO(out_put, rc = -EIO);
422         }
423
424         do {
425                 if (thread->mgt_abort)
426                         break;
427
428                 key_sz = iops->key_size(env, it);
429                 /* filter out "." and ".." entries from PENDING dir. */
430                 if (key_sz < 8)
431                         goto next;
432
433                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
434                 if (rc != 0) {
435                         CERROR("%s: fail to get FID for orphan it: rc = %d\n",
436                                mdd2obd_dev(mdd)->obd_name, rc);
437                         goto next;
438                 }
439
440                 fid_le_to_cpu(&fid, &ent->lde_fid);
441                 if (!fid_is_sane(&fid)) {
442                         CERROR("%s: bad FID "DFID" cleaning '%s'\n",
443                                mdd2obd_dev(mdd)->obd_name, PFID(&fid),
444                                mdd_orphan_index_name);
445                         goto next;
446                 }
447
448                 /* kill orphan object */
449                 cookie = iops->store(env, it);
450                 iops->put(env, it);
451                 rc = mdd_orphan_key_test_and_delete(env, mdd, &fid,
452                                                 (struct dt_key *)ent->lde_name);
453
454                 /* after index delete reset iterator */
455                 if (rc == 0)
456                         rc = iops->get(env, it, (const void *)"");
457                 else
458                         rc = iops->load(env, it, cookie);
459 next:
460                 rc = iops->next(env, it);
461         } while (rc == 0);
462
463         GOTO(out_put, rc = 0);
464 out_put:
465         iops->put(env, it);
466         iops->fini(env, it);
467
468 out:
469         return rc;
470 }
471
472 /**
473  * open the PENDING directory for device \a mdd
474  *
475  * The PENDING directory persistently tracks files and directories that were
476  * unlinked from the namespace (nlink == 0) but are still held open by clients.
477  * Those inodes shouldn't be deleted if the MDS crashes, because the clients
478  * would not be able to recover and reopen those files.  Instead, these inodes
479  * are linked into the PENDING directory on disk, and only deleted if all
480  * clients close them, or the MDS finishes client recovery without any client
481  * reopening them (i.e. former clients didn't join recovery).
482  *  \param d   mdd device being started.
483  *
484  *  \retval 0  success
485  *  \retval  -ve index operation error.
486  *
487  */
488 int mdd_orphan_index_init(const struct lu_env *env, struct mdd_device *mdd)
489 {
490         struct lu_fid fid;
491         struct dt_object *d;
492         int rc = 0;
493
494         ENTRY;
495
496         /* create PENDING dir */
497         fid_zero(&fid);
498         rc = mdd_local_file_create(env, mdd, &mdd->mdd_local_root_fid,
499                                    mdd_orphan_index_name, S_IFDIR | S_IRUGO |
500                                    S_IWUSR | S_IXUGO, &fid);
501         if (rc < 0)
502                 RETURN(rc);
503
504         d = dt_locate(env, mdd->mdd_child, &fid);
505         if (IS_ERR(d))
506                 RETURN(PTR_ERR(d));
507         LASSERT(lu_object_exists(&d->do_lu));
508         if (!dt_try_as_dir(env, d)) {
509                 CERROR("%s: orphan dir '%s' is not an index: rc = %d\n",
510                        mdd2obd_dev(mdd)->obd_name, mdd_orphan_index_name, rc);
511                 dt_object_put(env, d);
512                 RETURN(-ENOTDIR);
513         }
514         mdd->mdd_orphans = d;
515         RETURN(0);
516 }
517
518 void mdd_orphan_index_fini(const struct lu_env *env, struct mdd_device *mdd)
519 {
520         ENTRY;
521         if (mdd->mdd_orphans != NULL) {
522                 dt_object_put(env, mdd->mdd_orphans);
523                 mdd->mdd_orphans = NULL;
524         }
525         EXIT;
526 }
527
528 static int mdd_orphan_cleanup_thread(void *args)
529 {
530         struct mdd_generic_thread *thread = (struct mdd_generic_thread *)args;
531         struct lu_env *env = NULL;
532         int rc;
533         ENTRY;
534
535         complete(&thread->mgt_started);
536
537         OBD_ALLOC_PTR(env);
538         if (env == NULL)
539                 GOTO(out, rc = -ENOMEM);
540
541         rc = lu_env_init(env, LCT_MD_THREAD);
542         if (rc)
543                 GOTO(out, rc);
544
545         rc = mdd_orphan_index_iterate(env, thread);
546
547         lu_env_fini(env);
548         GOTO(out, rc);
549 out:
550         if (env)
551                 OBD_FREE_PTR(env);
552         complete(&thread->mgt_finished);
553         return rc;
554 }
555
556 /**
557  *  Iterate orphan index to cleanup orphan objects after recovery is done.
558  *  \param d   mdd device in recovery.
559  */
560 int mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
561 {
562         int rc = -ENOMEM;
563         char *name = NULL;
564
565         OBD_ALLOC(name, MTI_NAME_MAXLEN);
566         if (name == NULL)
567                 goto out;
568
569         snprintf(name, MTI_NAME_MAXLEN, "orph_%s", mdd2obd_dev(d)->obd_name);
570
571         rc = mdd_generic_thread_start(&d->mdd_orphan_cleanup_thread,
572                                       mdd_orphan_cleanup_thread, d, name);
573 out:
574         if (rc)
575                 CERROR("%s: start orphan cleanup thread failed: rc = %d\n",
576                        mdd2obd_dev(d)->obd_name, rc);
577         if (name)
578                 OBD_FREE(name, MTI_NAME_MAXLEN);
579
580         return rc;
581 }