Whamcloud - gitweb
LU-2240 mds: Assign special fid sequence to root.
[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, 2012, 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 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
99         struct dt_object        *dor    = mdd->mdd_orphans;
100         dor->do_ops->do_write_lock(env, dor, MOR_TGT_ORPHAN);
101 }
102
103 static inline void mdd_orphan_write_unlock(const struct lu_env *env,
104                                            struct mdd_device *mdd)
105 {
106
107         struct dt_object        *dor    = mdd->mdd_orphans;
108         dor->do_ops->do_write_unlock(env, dor);
109 }
110
111 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
112                                         struct mdd_device *mdd,
113                                         struct mdd_object *obj,
114                                         __u32 op,
115                                         struct thandle *th)
116 {
117         struct dt_object        *dor    = mdd->mdd_orphans;
118         const struct lu_fid     *lf     = mdo2fid(obj);
119         struct dt_key           *key    = orph_key_fill(env, lf, op);
120         ENTRY;
121
122         return  dor->do_index_ops->dio_insert(env, dor,
123                                               (struct dt_rec *)lf,
124                                               key, th,
125                                               BYPASS_CAPA, 1);
126 }
127
128 static inline int mdd_orphan_delete_obj(const struct lu_env *env,
129                                         struct mdd_device  *mdd ,
130                                         struct dt_key *key,
131                                         struct thandle *th)
132 {
133         struct dt_object        *dor    = mdd->mdd_orphans;
134
135         return  dor->do_index_ops->dio_delete(env, dor,
136                                               key, th,
137                                               BYPASS_CAPA);
138 }
139
140 static inline void mdd_orphan_ref_add(const struct lu_env *env,
141                                  struct mdd_device *mdd,
142                                  struct thandle *th)
143 {
144         struct dt_object        *dor    = mdd->mdd_orphans;
145         dor->do_ops->do_ref_add(env, dor, th);
146 }
147
148 static inline void mdd_orphan_ref_del(const struct lu_env *env,
149                                  struct mdd_device *mdd,
150                                  struct thandle *th)
151 {
152         struct dt_object        *dor    = mdd->mdd_orphans;
153         dor->do_ops->do_ref_del(env, dor, th);
154 }
155
156
157 int orph_declare_index_insert(const struct lu_env *env,
158                               struct mdd_object *obj,
159                               cfs_umode_t mode, struct thandle *th)
160 {
161         struct mdd_device       *mdd = mdo2mdd(&obj->mod_obj);
162         struct dt_key           *key;
163         int                     rc;
164
165         key = orph_key_fill(env, mdo2fid(obj), ORPH_OP_UNLINK);
166
167         rc = dt_declare_insert(env, mdd->mdd_orphans, NULL, key, th);
168         if (rc)
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, NULL, dotdot, th);
191
192         return rc;
193 }
194
195 static int orph_index_insert(const struct lu_env *env,
196                              struct mdd_object *obj,
197                              __u32 op,
198                              struct thandle *th)
199 {
200         struct mdd_device       *mdd    = mdo2mdd(&obj->mod_obj);
201         struct dt_object        *dor    = mdd->mdd_orphans;
202         const struct lu_fid     *lf_dor = lu_object_fid(&dor->do_lu);
203         struct dt_object        *next   = mdd_object_child(obj);
204         int rc;
205         ENTRY;
206
207         LASSERT(mdd_write_locked(env, obj) != 0);
208         LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
209
210         mdd_orphan_write_lock(env, mdd);
211
212         rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
213         if (rc)
214                 GOTO(out, rc);
215
216         mdo_ref_add(env, obj, th);
217         if (!S_ISDIR(mdd_object_type(obj)))
218                 goto out;
219
220         mdo_ref_add(env, obj, th);
221         mdd_orphan_ref_add(env, mdd, th);
222
223         /* try best to fixup directory, dont return errors
224          * from here */
225         if (!dt_try_as_dir(env, next))
226                 goto out;
227         next->do_index_ops->dio_delete(env, next,
228                                        (const struct dt_key *)dotdot,
229                                        th, BYPASS_CAPA);
230
231         next->do_index_ops->dio_insert(env, next,
232                                        (struct dt_rec *)lf_dor,
233                                        (const struct dt_key *)dotdot,
234                                        th, BYPASS_CAPA, 1);
235
236 out:
237         if (rc == 0)
238                 obj->mod_flags |= ORPHAN_OBJ;
239
240         mdd_orphan_write_unlock(env, mdd);
241
242         RETURN(rc);
243 }
244
245 int orph_declare_index_delete(const struct lu_env *env,
246                               struct mdd_object *obj,
247
248                               struct thandle *th)
249 {
250         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
251         struct dt_key     *key;
252         int                rc;
253
254         key = orph_key_fill(env, mdo2fid(obj), ORPH_OP_UNLINK);
255
256         rc = dt_declare_delete(env, mdd->mdd_orphans, key, th);
257         if (rc)
258                 return rc;
259
260         rc = mdo_declare_ref_del(env, obj, th);
261         if (rc)
262                 return rc;
263
264         if (S_ISDIR(mdd_object_type(obj))) {
265                 rc = mdo_declare_ref_del(env, obj, th);
266                 if (rc)
267                         return rc;
268
269                 rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
270         }
271
272         return rc;
273 }
274
275 static int orph_index_delete(const struct lu_env *env,
276                              struct mdd_object *obj,
277                              __u32 op,
278                              struct thandle *th)
279 {
280         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
281         struct dt_object *dor = mdd->mdd_orphans;
282         struct dt_key *key;
283         int rc;
284
285         ENTRY;
286
287         LASSERT(mdd_write_locked(env, obj) != 0);
288         LASSERT(obj->mod_flags & ORPHAN_OBJ);
289         LASSERT(obj->mod_count == 0);
290
291         LASSERT(dor);
292
293         key = orph_key_fill(env, mdo2fid(obj), op);
294         mdd_orphan_write_lock(env, mdd);
295
296         rc = mdd_orphan_delete_obj(env, mdd, key, th);
297
298         if (rc == -ENOENT) {
299                 key = orph_key_fill_18(env, mdo2fid(obj));
300                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
301         }
302
303         if (!rc) {
304                 /* lov objects will be destroyed by caller */
305                 mdo_ref_del(env, obj, th);
306                 if (S_ISDIR(mdd_object_type(obj))) {
307                         mdo_ref_del(env, obj, th);
308                         mdd_orphan_ref_del(env, mdd, th);
309                 }
310                 obj->mod_flags &= ~ORPHAN_OBJ;
311         } else {
312                 CERROR("could not delete object: rc = %d\n",rc);
313         }
314
315         mdd_orphan_write_unlock(env, mdd);
316         RETURN(rc);
317 }
318
319
320 static int orphan_object_destroy(const struct lu_env *env,
321                                  struct mdd_object *obj,
322                                  struct dt_key *key)
323 {
324         struct thandle *th = NULL;
325         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
326         int rc = 0;
327         ENTRY;
328
329         th = mdd_trans_create(env, mdd);
330         if (IS_ERR(th)) {
331                 CERROR("Cannot get thandle\n");
332                 RETURN(-ENOMEM);
333         }
334         rc = orph_declare_index_delete(env, obj, th);
335         if (rc)
336                 GOTO(stop, rc);
337
338         rc = mdo_declare_destroy(env, obj, th);
339         if (rc)
340                 GOTO(stop, rc);
341
342         rc = mdd_trans_start(env, mdd, th);
343         if (rc)
344                 GOTO(stop, rc);
345
346         mdd_write_lock(env, obj, MOR_TGT_CHILD);
347         if (likely(obj->mod_count == 0)) {
348                 mdd_orphan_write_lock(env, mdd);
349                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
350                 if (rc == 0) {
351                         mdo_ref_del(env, obj, th);
352                         if (S_ISDIR(mdd_object_type(obj))) {
353                                 mdo_ref_del(env, obj, th);
354                                 mdd_orphan_ref_del(env, mdd, th);
355                         }
356                         rc = mdo_destroy(env, obj, th);
357                 } else
358                         CERROR("could not delete object: rc = %d\n",rc);
359                 mdd_orphan_write_unlock(env, mdd);
360         }
361         mdd_write_unlock(env, obj);
362
363 stop:
364         mdd_trans_stop(env, mdd, 0, th);
365
366         RETURN(rc);
367 }
368
369 /**
370  * Delete unused orphan with FID \a lf from PENDING directory
371  *
372  * \param mdd  MDD device finishing recovery
373  * \param lf   FID of file or directory to delete
374  * \param key  cookie for this entry in index iterator
375  *
376  * \retval 0   success
377  * \retval -ve error
378  */
379 static int orph_key_test_and_del(const struct lu_env *env,
380                                  struct mdd_device *mdd,
381                                  struct lu_fid *lf,
382                                  struct dt_key *key)
383 {
384         struct mdd_object *mdo;
385         int rc;
386
387         mdo = mdd_object_find(env, mdd, lf);
388
389         if (IS_ERR(mdo))
390                 return PTR_ERR(mdo);
391
392         rc = -EBUSY;
393         if (mdo->mod_count == 0) {
394                 CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
395                 rc = orphan_object_destroy(env, mdo, key);
396                 if (rc) /* so replay-single.sh test_37 works */
397                         CERROR("%s: error unlinking orphan "DFID" from "
398                                "PENDING: rc = %d\n",
399                                mdd2obd_dev(mdd)->obd_name, PFID(lf), rc);
400         } else {
401                 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
402                 if (likely(mdo->mod_count > 0)) {
403                         CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
404                                PFID(lf), mdo->mod_count);
405                         mdo->mod_flags |= ORPHAN_OBJ;
406                 }
407                 mdd_write_unlock(env, mdo);
408         }
409
410         mdd_object_put(env, mdo);
411         return rc;
412 }
413
414 /**
415  * delete unreferenced files and directories in the PENDING directory
416  *
417  * Files that remain in PENDING after client->MDS recovery has completed
418  * have to be referenced (opened) by some client during recovery, or they
419  * will be deleted here (for clients that did not complete recovery).
420  *
421  * \param mdd  MDD device finishing recovery
422  *
423  * \retval 0   success
424  * \retval -ve error
425  */
426 static int orph_index_iterate(const struct lu_env *env,
427                               struct mdd_device *mdd)
428 {
429         struct dt_object *dor = mdd->mdd_orphans;
430         struct lu_dirent *ent = &mdd_env_info(env)->mti_ent;
431         const struct dt_it_ops *iops;
432         struct dt_it     *it;
433         struct lu_fid     fid;
434         int               key_sz = 0;
435         int               rc;
436         __u64             cookie;
437         ENTRY;
438
439         /* In recovery phase, do not need for any lock here */
440         iops = &dor->do_index_ops->dio_it;
441         it = iops->init(env, dor, LUDA_64BITHASH, BYPASS_CAPA);
442         if (IS_ERR(it)) {
443                 rc = PTR_ERR(it);
444                 CERROR("%s: cannot clean PENDING: rc = %d\n",
445                        mdd2obd_dev(mdd)->obd_name, rc);
446                 GOTO(out, rc);
447         }
448
449         rc = iops->load(env, it, 0);
450         if (rc < 0)
451                 GOTO(out_put, rc);
452         if (rc == 0) {
453                 CERROR("%s: error loading iterator to clean PENDING\n",
454                        mdd2obd_dev(mdd)->obd_name);
455                 /* Index contains no zero key? */
456                 GOTO(out_put, rc = -EIO);
457         }
458
459         do {
460                 key_sz = iops->key_size(env, it);
461                 /* filter out "." and ".." entries from PENDING dir. */
462                 if (key_sz < 8)
463                         goto next;
464
465                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
466                 if (rc != 0) {
467                         CERROR("%s: fail to get FID for orphan it: rc = %d\n",
468                                mdd2obd_dev(mdd)->obd_name, rc);
469                         goto next;
470                 }
471
472                 fid_le_to_cpu(&fid, &ent->lde_fid);
473                 if (!fid_is_sane(&fid)) {
474                         CERROR("%s: bad FID "DFID" cleaning PENDING\n",
475                                mdd2obd_dev(mdd)->obd_name, PFID(&fid));
476                         goto next;
477                 }
478
479                 /* kill orphan object */
480                 cookie = iops->store(env, it);
481                 iops->put(env, it);
482                 rc = orph_key_test_and_del(env, mdd, &fid,
483                                            (struct dt_key *)ent->lde_name);
484
485                 /* after index delete reset iterator */
486                 if (rc == 0)
487                         rc = iops->get(env, it, (const void *)"");
488                 else
489                         rc = iops->load(env, it, cookie);
490 next:
491                 rc = iops->next(env, it);
492         } while (rc == 0);
493
494         GOTO(out_put, rc = 0);
495 out_put:
496         iops->put(env, it);
497         iops->fini(env, it);
498
499 out:
500         return rc;
501 }
502
503 /**
504  * open the PENDING directory for device \a mdd
505  *
506  * The PENDING directory persistently tracks files and directories that were
507  * unlinked from the namespace (nlink == 0) but are still held open by clients.
508  * Those inodes shouldn't be deleted if the MDS crashes, because the clients
509  * would not be able to recover and reopen those files.  Instead, these inodes
510  * are linked into the PENDING directory on disk, and only deleted if all
511  * clients close them, or the MDS finishes client recovery without any client
512  * reopening them (i.e. former clients didn't join recovery).
513  *  \param d   mdd device being started.
514  *
515  *  \retval 0  success
516  *  \retval  -ve index operation error.
517  *
518  */
519 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
520 {
521         struct lu_fid fid;
522         struct dt_object *d;
523         int rc = 0;
524         ENTRY;
525
526         d = dt_store_open(env, mdd->mdd_child, "", orph_index_name, &fid);
527         if (!IS_ERR(d)) {
528                 mdd->mdd_orphans = d;
529                 if (!dt_try_as_dir(env, d)) {
530                         rc = -ENOTDIR;
531                         CERROR("\"%s\" is not an index! : rc = %d\n",
532                                         orph_index_name, rc);
533                 }
534         } else {
535                 CERROR("cannot find \"%s\" obj %d\n",
536                        orph_index_name, (int)PTR_ERR(d));
537                 rc = PTR_ERR(d);
538         }
539
540         RETURN(rc);
541 }
542
543 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
544 {
545         ENTRY;
546         if (mdd->mdd_orphans != NULL) {
547                 lu_object_put(env, &mdd->mdd_orphans->do_lu);
548                 mdd->mdd_orphans = NULL;
549         }
550         EXIT;
551 }
552
553 /**
554  *  Iterate orphan index to cleanup orphan objects after recovery is done.
555  *  \param d   mdd device in recovery.
556  */
557 int __mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
558 {
559         return orph_index_iterate(env, d);
560 }
561
562 /**
563  *  add an orphan \a obj to the orphan index.
564  *  \param obj file or directory.
565  *  \param th  transaction for index insert.
566  *
567  *  \pre obj nlink == 0 && obj->mod_count != 0
568  *
569  *  \retval 0  success
570  *  \retval  -ve index operation error.
571  */
572 int __mdd_orphan_add(const struct lu_env *env,
573                      struct mdd_object *obj, struct thandle *th)
574 {
575         return orph_index_insert(env, obj, ORPH_OP_UNLINK, th);
576 }
577
578 /**
579  *  delete an orphan \a obj from orphan index.
580  *  \param obj file or directory.
581  *  \param th  transaction for index deletion and object destruction.
582  *
583  *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
584  *
585  *  \retval 0  success
586  *  \retval  -ve index operation error.
587  */
588 int __mdd_orphan_del(const struct lu_env *env,
589                      struct mdd_object *obj, struct thandle *th)
590 {
591         return orph_index_delete(env, obj, ORPH_OP_UNLINK, th);
592 }