Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / mdd / mdd_orphans.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/mdd/mdd_orphans.c
39  *
40  * Orphan handling code
41  *
42  * Author: Mike Pershin <tappro@clusterfs.com>
43  *         Pravin B Shelar <pravin.shelar@sun.com>
44  */
45
46 #ifndef EXPORT_SYMTAB
47 # define EXPORT_SYMTAB
48 #endif
49 #define DEBUG_SUBSYSTEM S_MDS
50
51 #include <obd.h>
52 #include <obd_class.h>
53 #include <lustre_ver.h>
54 #include <obd_support.h>
55 #include <lustre_fid.h>
56 #include "mdd_internal.h"
57
58 const char orph_index_name[] = "PENDING";
59 const char *dotdot = "..";
60
61 enum {
62         ORPH_OP_UNLINK,
63         ORPH_OP_TRUNCATE
64 };
65
66 #define ORPHAN_FILE_NAME_FORMAT         "%016llx:%08x:%08x:%2x"
67 #define ORPHAN_FILE_NAME_FORMAT_18      "%llx:%08x"
68
69 static struct dt_key* orph_key_fill(const struct lu_env *env,
70                                     const struct lu_fid *lf, __u32 op)
71 {
72         char *key = mdd_env_info(env)->mti_orph_key;
73         int rc;
74
75         LASSERT(key);
76         rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT,
77                       (long long unsigned int)fid_seq(lf),
78                       fid_oid(lf), fid_ver(lf), op);
79         if (rc > 0)
80                 return (struct dt_key*) key;
81         else
82                 return ERR_PTR(rc);
83 }
84
85 static struct dt_key* orph_key_fill_18(const struct lu_env *env,
86                                        const struct lu_fid *lf)
87 {
88         char *key = mdd_env_info(env)->mti_orph_key;
89         int rc;
90
91         LASSERT(key);
92         rc = snprintf(key, NAME_MAX + 1, ORPHAN_FILE_NAME_FORMAT_18,
93                       (unsigned long long)fid_seq(lf), fid_oid(lf));
94         if (rc > 0)
95                 return (struct dt_key*) key;
96         else
97                 return ERR_PTR(rc);
98 }
99
100 static int orphan_key_to_fid(char *key, struct lu_fid *lf)
101 {
102         int rc = 0;
103         unsigned int op;
104
105         rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT,
106                     (long long unsigned int *)&lf->f_seq, &lf->f_oid,
107                     &lf->f_ver, &op);
108         if (rc == 4)
109                 return 0;
110
111         /* build igif */
112         rc = sscanf(key, ORPHAN_FILE_NAME_FORMAT_18,
113                     (long long unsigned int *)&lf->f_seq, &lf->f_oid);
114         if (rc == 2) {
115                 lf->f_ver = 0;
116                 return 0;
117         }
118
119         CERROR("can not parse orphan file name %s\n", key);
120         return -EINVAL;
121 }
122
123 static inline void mdd_orphan_write_lock(const struct lu_env *env,
124                                     struct mdd_device *mdd)
125 {
126
127         struct dt_object        *dor    = mdd->mdd_orphans;
128         dor->do_ops->do_write_lock(env, dor, MOR_TGT_ORPHAN);
129 }
130
131 static inline void mdd_orphan_write_unlock(const struct lu_env *env,
132                                            struct mdd_device *mdd)
133 {
134
135         struct dt_object        *dor    = mdd->mdd_orphans;
136         dor->do_ops->do_write_unlock(env, dor);
137 }
138
139 static inline int mdd_orphan_insert_obj(const struct lu_env *env,
140                                         struct mdd_device *mdd,
141                                         struct mdd_object *obj,
142                                         __u32 op,
143                                         struct thandle *th)
144 {
145         struct dt_object        *dor    = mdd->mdd_orphans;
146         const struct lu_fid     *lf     = mdo2fid(obj);
147         struct dt_key           *key    = orph_key_fill(env, lf, op);
148         ENTRY;
149
150         return  dor->do_index_ops->dio_insert(env, dor,
151                                               (struct dt_rec *)lf,
152                                               key, th,
153                                               BYPASS_CAPA, 1);
154 }
155
156 static inline int mdd_orphan_delete_obj(const struct lu_env *env,
157                                         struct mdd_device  *mdd ,
158                                         struct dt_key *key,
159                                         struct thandle *th)
160 {
161         struct dt_object        *dor    = mdd->mdd_orphans;
162
163         return  dor->do_index_ops->dio_delete(env, dor,
164                                               key, th,
165                                               BYPASS_CAPA);
166 }
167
168 static inline void mdd_orphan_ref_add(const struct lu_env *env,
169                                  struct mdd_device *mdd,
170                                  struct thandle *th)
171 {
172         struct dt_object        *dor    = mdd->mdd_orphans;
173         dor->do_ops->do_ref_add(env, dor, th);
174 }
175
176 static inline void mdd_orphan_ref_del(const struct lu_env *env,
177                                  struct mdd_device *mdd,
178                                  struct thandle *th)
179 {
180         struct dt_object        *dor    = mdd->mdd_orphans;
181         dor->do_ops->do_ref_del(env, dor, th);
182 }
183
184
185 int orph_declare_index_insert(const struct lu_env *env,
186                               struct mdd_object *obj,
187                               struct thandle *th)
188 {
189         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
190         int                rc;
191
192         rc = dt_declare_insert(env, mdd->mdd_orphans, NULL, NULL, th);
193         if (rc)
194                 return rc;
195
196         rc = mdo_declare_ref_add(env, obj, th);
197         if (rc)
198                 return rc;
199
200         if (!S_ISDIR(mdd_object_type(obj)))
201                 return 0;
202
203         rc = mdo_declare_ref_add(env, obj, th);
204         if (rc)
205                 return rc;
206
207         rc = dt_declare_ref_add(env, mdd->mdd_orphans, th);
208         if (rc)
209                 return rc;
210
211         rc = mdo_declare_index_delete(env, obj, dotdot, th);
212         if (rc)
213                 return rc;
214
215         rc = mdo_declare_index_insert(env, obj, NULL, dotdot, th);
216
217         return rc;
218 }
219
220 static int orph_index_insert(const struct lu_env *env,
221                              struct mdd_object *obj,
222                              __u32 op,
223                              struct thandle *th)
224 {
225         struct mdd_device       *mdd    = mdo2mdd(&obj->mod_obj);
226         struct dt_object        *dor    = mdd->mdd_orphans;
227         const struct lu_fid     *lf_dor = lu_object_fid(&dor->do_lu);
228         struct dt_object        *next   = mdd_object_child(obj);
229         int rc;
230         ENTRY;
231
232         LASSERT(mdd_write_locked(env, obj) != 0);
233         LASSERT(!(obj->mod_flags & ORPHAN_OBJ));
234         LASSERT(obj->mod_count > 0);
235
236         mdd_orphan_write_lock(env, mdd);
237
238         rc = mdd_orphan_insert_obj(env, mdd, obj, op, th);
239         if (rc)
240                 GOTO(out, rc);
241
242         mdo_ref_add(env, obj, th);
243         if (!S_ISDIR(mdd_object_type(obj)))
244                 goto out;
245
246         mdo_ref_add(env, obj, th);
247         mdd_orphan_ref_add(env, mdd, th);
248
249         /* try best to fixup directory, dont return errors
250          * from here */
251         if (!dt_try_as_dir(env, next))
252                 goto out;
253         next->do_index_ops->dio_delete(env, next,
254                                        (const struct dt_key *)dotdot,
255                                        th, BYPASS_CAPA);
256
257         next->do_index_ops->dio_insert(env, next,
258                                        (struct dt_rec *)lf_dor,
259                                        (const struct dt_key *)dotdot,
260                                        th, BYPASS_CAPA, 1);
261
262 out:
263         if (rc == 0)
264                 obj->mod_flags |= ORPHAN_OBJ;
265
266         mdd_orphan_write_unlock(env, mdd);
267
268         RETURN(rc);
269 }
270
271 /**
272  * Destroy OSD object on MDD and associated OST objects.
273  *
274  * \param obj orphan object
275  * \param mdd used for sending llog msg to osts
276  *
277  * \retval  0   success
278  * \retval -ve  error
279  */
280 static int orphan_object_kill(const struct lu_env *env,
281                               struct mdd_object *obj,
282                               struct mdd_device *mdd,
283                               struct thandle *th)
284 {
285         struct lu_attr *la = &mdd_env_info(env)->mti_la;
286         int rc = 0;
287         ENTRY;
288
289         /* No need to lock this object as its recovery phase, and
290          * no other thread can access it. But we need to lock it
291          * as its precondition for osd api we using. */
292
293         mdo_ref_del(env, obj, th);
294         if (S_ISDIR(mdd_object_type(obj))) {
295                 mdo_ref_del(env, obj, th);
296                 mdd_orphan_ref_del(env, mdd, th);
297         } else {
298                 /* regular file , cleanup linked ost objects */
299                 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
300                 if (rc == 0)
301                         rc = mdd_lov_destroy(env, mdd, obj, la);
302         }
303         mdo_destroy(env, obj, th);
304         RETURN(rc);
305 }
306
307 int orph_declare_index_delete(const struct lu_env *env,
308                               struct mdd_object *obj,
309                               struct thandle *th)
310 {
311         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
312         int                rc;
313
314         rc = dt_declare_delete(env, mdd->mdd_orphans, NULL, th);
315         if (rc)
316                 return rc;
317
318         rc = mdo_declare_ref_del(env, obj, th);
319         if (rc)
320                 return rc;
321
322         if (S_ISDIR(mdd_object_type(obj))) {
323                 rc = mdo_declare_ref_del(env, obj, th);
324                 if (rc)
325                         return rc;
326
327                 rc = dt_declare_ref_del(env, mdd->mdd_orphans, th);
328         }
329
330         return rc;
331 }
332
333 static int orph_index_delete(const struct lu_env *env,
334                              struct mdd_object *obj,
335                              __u32 op,
336                              struct thandle *th)
337 {
338         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
339         struct dt_object *dor = mdd->mdd_orphans;
340         struct dt_key *key;
341         int rc;
342
343         ENTRY;
344
345         LASSERT(mdd_write_locked(env, obj) != 0);
346         LASSERT(obj->mod_flags & ORPHAN_OBJ);
347         LASSERT(obj->mod_count == 0);
348
349         LASSERT(dor);
350
351         key = orph_key_fill(env, mdo2fid(obj), op);
352         mdd_orphan_write_lock(env, mdd);
353
354         rc = mdd_orphan_delete_obj(env, mdd, key, th);
355
356         if (rc == -ENOENT) {
357                 key = orph_key_fill_18(env, mdo2fid(obj));
358                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
359         }
360
361         if (!rc) {
362                 /* lov objects will be destroyed by caller */
363                 mdo_ref_del(env, obj, th);
364                 if (S_ISDIR(mdd_object_type(obj))) {
365                         mdo_ref_del(env, obj, th);
366                         mdd_orphan_ref_del(env, mdd, th);
367                 }
368                 obj->mod_flags &= ~ORPHAN_OBJ;
369         } else {
370                 CERROR("could not delete object: rc = %d\n",rc);
371         }
372
373         mdd_orphan_write_unlock(env, mdd);
374         RETURN(rc);
375 }
376
377
378 static int orphan_object_destroy(const struct lu_env *env,
379                                  struct mdd_object *obj,
380                                  struct dt_key *key)
381 {
382         struct thandle *th = NULL;
383         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
384         struct md_attr *ma = &mdd_env_info(env)->mti_ma;
385         int rc = 0;
386         ENTRY;
387
388         /* init ma */
389         ma->ma_lmm_size = mdd_lov_mdsize(env, mdd);
390         ma->ma_lmm = mdd_max_lmm_get(env, mdd);
391         ma->ma_cookie_size = mdd_lov_cookiesize(env, mdd);
392         ma->ma_cookie = mdd_max_cookie_get(env, mdd);
393         ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
394         ma->ma_valid = 0;
395
396         th = mdd_trans_create(env, mdd);
397         if (IS_ERR(th)) {
398                 CERROR("Cannot get thandle\n");
399                 RETURN(-ENOMEM);
400         }
401         rc = orph_declare_index_delete(env, obj, th);
402         if (rc)
403                 GOTO(stop, rc);
404
405         rc = mdd_declare_object_kill(env, obj, ma, th);
406         if (rc)
407                 GOTO(stop, rc);
408
409         rc = mdd_trans_start(env, mdd, th);
410         if (rc)
411                 GOTO(stop, rc);
412
413         mdd_write_lock(env, obj, MOR_TGT_CHILD);
414         if (likely(obj->mod_count == 0)) {
415                 mdd_orphan_write_lock(env, mdd);
416                 rc = mdd_orphan_delete_obj(env, mdd, key, th);
417                 if (rc == 0)
418                         orphan_object_kill(env, obj, mdd, th);
419                 else
420                         CERROR("could not delete object: rc = %d\n",rc);
421                 mdd_orphan_write_unlock(env, mdd);
422         }
423         mdd_write_unlock(env, obj);
424
425 stop:
426         mdd_trans_stop(env, mdd, 0, th);
427
428         RETURN(rc);
429 }
430
431 /**
432  * Delete unused orphan with FID \a lf from PENDING directory
433  *
434  * \param mdd  MDD device finishing recovery
435  * \param lf   FID of file or directory to delete
436  * \param key  cookie for this entry in index iterator
437  *
438  * \retval 0   success
439  * \retval -ve error
440  */
441 static int orph_key_test_and_del(const struct lu_env *env,
442                                  struct mdd_device *mdd,
443                                  struct lu_fid *lf,
444                                  struct dt_key *key)
445 {
446         struct mdd_object *mdo;
447         int rc;
448
449         mdo = mdd_object_find(env, mdd, lf);
450
451         if (IS_ERR(mdo))
452                 return PTR_ERR(mdo);
453
454         rc = -EBUSY;
455         if (mdo->mod_count == 0) {
456                 CDEBUG(D_HA, "Found orphan "DFID", delete it\n", PFID(lf));
457                 rc = orphan_object_destroy(env, mdo, key);
458                 if (rc) /* so replay-single.sh test_37 works */
459                         CERROR("%s: error unlinking orphan "DFID" from "
460                                "PENDING: rc = %d\n",
461                                mdd->mdd_obd_dev->obd_name, PFID(lf), rc);
462         } else {
463                 mdd_write_lock(env, mdo, MOR_TGT_CHILD);
464                 if (likely(mdo->mod_count > 0)) {
465                         CDEBUG(D_HA, "Found orphan "DFID" count %d, skip it\n",
466                                PFID(lf), mdo->mod_count);
467                         mdo->mod_flags |= ORPHAN_OBJ;
468                 }
469                 mdd_write_unlock(env, mdo);
470         }
471
472         mdd_object_put(env, mdo);
473         return rc;
474 }
475
476 /**
477  * delete unreferenced files and directories in the PENDING directory
478  *
479  * Files that remain in PENDING after client->MDS recovery has completed
480  * have to be referenced (opened) by some client during recovery, or they
481  * will be deleted here (for clients that did not complete recovery).
482  *
483  * \param mdd  MDD device finishing recovery
484  *
485  * \retval 0   success
486  * \retval -ve error
487  */
488 static int orph_index_iterate(const struct lu_env *env,
489                               struct mdd_device *mdd)
490 {
491         struct dt_object *dor = mdd->mdd_orphans;
492         char             *mti_key = mdd_env_info(env)->mti_orph_key;
493         const struct dt_it_ops *iops;
494         struct dt_it     *it;
495         char             *key;
496         struct lu_fid     fid;
497         int               result = 0;
498         int               key_sz = 0;
499         int               rc;
500         __u64             cookie;
501         ENTRY;
502
503         /* In recovery phase, do not need for any lock here */
504         iops = &dor->do_index_ops->dio_it;
505         it = iops->init(env, dor, LUDA_64BITHASH, BYPASS_CAPA);
506         if (IS_ERR(it)) {
507                 rc = PTR_ERR(it);
508                 CERROR("%s: cannot clean PENDING: rc = %d\n",
509                        mdd->mdd_obd_dev->obd_name, rc);
510                 GOTO(out, rc);
511         }
512
513         rc = iops->load(env, it, 0);
514         if (rc < 0)
515                 GOTO(out_put, rc);
516         if (rc == 0) {
517                 CERROR("%s: error loading iterator to clean PENDING\n",
518                        mdd->mdd_obd_dev->obd_name);
519                 /* Index contains no zero key? */
520                 GOTO(out_put, rc = -EIO);
521         }
522
523         do {
524                 key = (void *)iops->key(env, it);
525                 if (IS_ERR(key)) {
526                         CERROR("%s: key failed when clean PENDING: rc = %ld\n",
527                                mdd->mdd_obd_dev->obd_name, PTR_ERR(key));
528                         goto next;
529                 }
530                 key_sz = iops->key_size(env, it);
531
532                 /* filter out "." and ".." entries from PENDING dir. */
533                 if (key_sz < 8)
534                         goto next;
535
536                 memcpy(mti_key, key, key_sz);
537                 mti_key[key_sz] = 0;
538
539                 if (orphan_key_to_fid(mti_key, &fid))
540                         goto next;
541                 if (!fid_is_sane(&fid)) {
542                         CERROR("%s: bad FID "DFID" cleaning PENDING\n",
543                                mdd->mdd_obd_dev->obd_name, PFID(&fid));
544                         goto next;
545                 }
546
547                 /* kill orphan object */
548                 cookie = iops->store(env, it);
549                 iops->put(env, it);
550                 rc = orph_key_test_and_del(env, mdd, &fid,
551                                           (struct dt_key *)mti_key);
552
553                 /* after index delete reset iterator */
554                 if (rc == 0)
555                         result = iops->get(env, it, (const void *)"");
556                 else
557                         result = iops->load(env, it, cookie);
558 next:
559                 result = iops->next(env, it);
560         } while (result == 0);
561
562         GOTO(out_put, rc = 0);
563 out_put:
564         iops->put(env, it);
565         iops->fini(env, it);
566
567 out:
568         return rc;
569 }
570
571 /**
572  * open the PENDING directory for device \a mdd
573  *
574  * The PENDING directory persistently tracks files and directories that were
575  * unlinked from the namespace (nlink == 0) but are still held open by clients.
576  * Those inodes shouldn't be deleted if the MDS crashes, because the clients
577  * would not be able to recover and reopen those files.  Instead, these inodes
578  * are linked into the PENDING directory on disk, and only deleted if all
579  * clients close them, or the MDS finishes client recovery without any client
580  * reopening them (i.e. former clients didn't join recovery).
581  *  \param d   mdd device being started.
582  *
583  *  \retval 0  success
584  *  \retval  -ve index operation error.
585  *
586  */
587 int orph_index_init(const struct lu_env *env, struct mdd_device *mdd)
588 {
589         struct lu_fid fid;
590         struct dt_object *d;
591         int rc = 0;
592         ENTRY;
593
594         d = dt_store_open(env, mdd->mdd_child, "", orph_index_name, &fid);
595         if (!IS_ERR(d)) {
596                 mdd->mdd_orphans = d;
597                 if (!dt_try_as_dir(env, d)) {
598                         rc = -ENOTDIR;
599                         CERROR("\"%s\" is not an index! : rc = %d\n",
600                                         orph_index_name, rc);
601                 }
602         } else {
603                 CERROR("cannot find \"%s\" obj %d\n",
604                        orph_index_name, (int)PTR_ERR(d));
605                 rc = PTR_ERR(d);
606         }
607
608         RETURN(rc);
609 }
610
611 void orph_index_fini(const struct lu_env *env, struct mdd_device *mdd)
612 {
613         ENTRY;
614         if (mdd->mdd_orphans != NULL) {
615                 lu_object_put(env, &mdd->mdd_orphans->do_lu);
616                 mdd->mdd_orphans = NULL;
617         }
618         EXIT;
619 }
620
621 /**
622  *  Iterate orphan index to cleanup orphan objects after recovery is done.
623  *  \param d   mdd device in recovery.
624  */
625 int __mdd_orphan_cleanup(const struct lu_env *env, struct mdd_device *d)
626 {
627         return orph_index_iterate(env, d);
628 }
629
630 /**
631  *  add an orphan \a obj to the orphan index.
632  *  \param obj file or directory.
633  *  \param th  transaction for index insert.
634  *
635  *  \pre obj nlink == 0 && obj->mod_count != 0
636  *
637  *  \retval 0  success
638  *  \retval  -ve index operation error.
639  */
640 int __mdd_orphan_add(const struct lu_env *env,
641                      struct mdd_object *obj, struct thandle *th)
642 {
643         return orph_index_insert(env, obj, ORPH_OP_UNLINK, th);
644 }
645
646 /**
647  *  delete an orphan \a obj from orphan index.
648  *  \param obj file or directory.
649  *  \param th  transaction for index deletion and object destruction.
650  *
651  *  \pre obj->mod_count == 0 && ORPHAN_OBJ is set for obj.
652  *
653  *  \retval 0  success
654  *  \retval  -ve index operation error.
655  */
656 int __mdd_orphan_del(const struct lu_env *env,
657                      struct mdd_object *obj, struct thandle *th)
658 {
659         return orph_index_delete(env, obj, ORPH_OP_UNLINK, th);
660 }