Whamcloud - gitweb
LU-3963 libcfs: move mgs, osd-ldiskfs, osp, quota to linux list api
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_compat.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) 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, 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/osd/osd_compat.c
37  *
38  * on-disk structure for managing /O
39  *
40  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41  */
42
43 /* LUSTRE_VERSION_CODE */
44 #include <lustre_ver.h>
45 /* prerequisite for linux/xattr.h */
46 #include <linux/types.h>
47 /* prerequisite for linux/xattr.h */
48 #include <linux/fs.h>
49 /* XATTR_{REPLACE,CREATE} */
50 #include <linux/xattr.h>
51
52 /*
53  * struct OBD_{ALLOC,FREE}*()
54  * OBD_FAIL_CHECK
55  */
56 #include <obd_support.h>
57
58 #include "osd_internal.h"
59 #include "osd_oi.h"
60
61 static void osd_push_ctxt(const struct osd_device *dev,
62                           struct lvfs_run_ctxt *newctxt,
63                           struct lvfs_run_ctxt *save)
64 {
65         OBD_SET_CTXT_MAGIC(newctxt);
66         newctxt->pwdmnt = dev->od_mnt;
67         newctxt->pwd = dev->od_mnt->mnt_root;
68         newctxt->fs = get_ds();
69
70         push_ctxt(save, newctxt);
71 }
72
73 /* utility to make a directory */
74 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
75                                    const char *name, int mode, int fix)
76 {
77         struct dentry *dchild;
78         int err = 0;
79         ENTRY;
80
81         // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
82         CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
83         dchild = ll_lookup_one_len(name, dir, strlen(name));
84         if (IS_ERR(dchild))
85                 GOTO(out_up, dchild);
86
87         if (dchild->d_inode) {
88                 int old_mode = dchild->d_inode->i_mode;
89                 if (!S_ISDIR(old_mode)) {
90                         CERROR("found %s (%lu/%u) is mode %o\n", name,
91                                dchild->d_inode->i_ino,
92                                dchild->d_inode->i_generation, old_mode);
93                         GOTO(out_err, err = -ENOTDIR);
94                 }
95
96                 /* Fixup directory permissions if necessary */
97                 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
98                         CDEBUG(D_CONFIG,
99                                "fixing permissions on %s from %o to %o\n",
100                                name, old_mode, mode);
101                         dchild->d_inode->i_mode = (mode & S_IALLUGO) |
102                                                   (old_mode & ~S_IALLUGO);
103                         mark_inode_dirty(dchild->d_inode);
104                 }
105                 GOTO(out_up, dchild);
106         }
107
108         err = vfs_mkdir(dir->d_inode, dchild, mode);
109         if (err)
110                 GOTO(out_err, err);
111
112         RETURN(dchild);
113
114 out_err:
115         dput(dchild);
116         dchild = ERR_PTR(err);
117 out_up:
118         return dchild;
119 }
120
121 int osd_last_rcvd_subdir_count(struct osd_device *osd)
122 {
123         struct lr_server_data lsd;
124         struct dentry        *dlast;
125         loff_t                off;
126         int                   rc = 0;
127         int                   count = OBJ_SUBDIR_COUNT;
128
129         ENTRY;
130
131         dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
132                                   strlen(LAST_RCVD));
133         if (IS_ERR(dlast))
134                 return PTR_ERR(dlast);
135         else if (dlast->d_inode == NULL)
136                 goto out;
137
138         off = 0;
139         rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
140         if (rc == sizeof(lsd)) {
141                 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
142                        "subdir count = %d\n", lsd.lsd_uuid,
143                        lsd.lsd_subdir_count);
144                 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
145                         count = le16_to_cpu(lsd.lsd_subdir_count);
146         } else if (rc != 0) {
147                 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
148                 if (rc > 0)
149                         rc = -EFAULT;
150                 dput(dlast);
151                 return rc;
152         }
153 out:
154         dput(dlast);
155         LASSERT(count > 0);
156         return count;
157 }
158
159 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
160 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
161 {
162         struct lvfs_run_ctxt    new;
163         struct lvfs_run_ctxt    save;
164         struct dentry           *parent;
165         struct osd_mdobj_map    *omm;
166         struct dentry           *d;
167         struct osd_thread_info  *info = osd_oti_get(env);
168         struct lu_fid           *fid = &info->oti_fid3;
169         int                     rc = 0;
170         ENTRY;
171
172         OBD_ALLOC_PTR(dev->od_mdt_map);
173         if (dev->od_mdt_map == NULL)
174                 RETURN(-ENOMEM);
175
176         omm = dev->od_mdt_map;
177
178         parent = osd_sb(dev)->s_root;
179         osd_push_ctxt(dev, &new, &save);
180
181         d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
182                          0755, 1);
183         if (IS_ERR(d))
184                 GOTO(cleanup, rc = PTR_ERR(d));
185
186         omm->omm_remote_parent = d;
187
188         /* Set LMA for remote parent inode */
189         lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
190         rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
191
192         GOTO(cleanup, rc);
193
194 cleanup:
195         pop_ctxt(&save, &new);
196         if (rc) {
197                 if (omm->omm_remote_parent != NULL)
198                         dput(omm->omm_remote_parent);
199                 OBD_FREE_PTR(omm);
200                 dev->od_mdt_map = NULL;
201         }
202         return rc;
203 }
204
205 static void osd_mdt_fini(struct osd_device *osd)
206 {
207         struct osd_mdobj_map *omm = osd->od_mdt_map;
208
209         if (omm == NULL)
210                 return;
211
212         if (omm->omm_remote_parent)
213                 dput(omm->omm_remote_parent);
214
215         OBD_FREE_PTR(omm);
216         osd->od_ost_map = NULL;
217 }
218
219 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
220                              struct osd_object *obj, struct osd_thandle *oh)
221 {
222         struct osd_mdobj_map    *omm = osd->od_mdt_map;
223         struct osd_thread_info  *oti = osd_oti_get(env);
224         struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
225         char                    *name = oti->oti_name;
226         struct dentry           *dentry;
227         struct dentry           *parent;
228         int                     rc;
229
230         /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
231          * can identify this object quickly */
232         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
233         if (rc != 0)
234                 RETURN(rc);
235
236         lma->lma_incompat |= LMAI_REMOTE_PARENT;
237         lustre_lma_swab(lma);
238         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
239                              sizeof(*lma), XATTR_REPLACE);
240         if (rc != 0)
241                 RETURN(rc);
242
243         parent = omm->omm_remote_parent;
244         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
245         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
246                                            name, strlen(name));
247         mutex_lock(&parent->d_inode->i_mutex);
248         rc = osd_ldiskfs_add_entry(oh->ot_handle, dentry, obj->oo_inode,
249                                    NULL);
250         CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
251                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
252         ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
253         mark_inode_dirty(parent->d_inode);
254         mutex_unlock(&parent->d_inode->i_mutex);
255         RETURN(rc);
256 }
257
258 int osd_delete_from_remote_parent(const struct lu_env *env,
259                                   struct osd_device *osd,
260                                   struct osd_object *obj,
261                                   struct osd_thandle *oh)
262 {
263         struct osd_mdobj_map       *omm = osd->od_mdt_map;
264         struct osd_thread_info     *oti = osd_oti_get(env);
265         struct lustre_mdt_attrs    *lma = &oti->oti_mdt_attrs;
266         char                       *name = oti->oti_name;
267         struct dentry              *dentry;
268         struct dentry              *parent;
269         struct ldiskfs_dir_entry_2 *de;
270         struct buffer_head         *bh;
271         int                        rc;
272
273         /* Check lma to see whether it is remote object */
274         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
275         if (rc != 0)
276                 RETURN(rc);
277
278         if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
279                 RETURN(0);
280
281         parent = omm->omm_remote_parent;
282         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
283         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
284                                            name, strlen(name));
285         mutex_lock(&parent->d_inode->i_mutex);
286         bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
287                                     NULL, NULL);
288         if (bh == NULL) {
289                 mutex_unlock(&parent->d_inode->i_mutex);
290                 RETURN(-ENOENT);
291         }
292         CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
293                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
294         rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
295         ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
296         mark_inode_dirty(parent->d_inode);
297         mutex_unlock(&parent->d_inode->i_mutex);
298         brelse(bh);
299
300         /* Get rid of REMOTE_PARENT flag from incompat */
301         lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
302         lustre_lma_swab(lma);
303         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
304                              sizeof(*lma), XATTR_REPLACE);
305         RETURN(rc);
306 }
307
308 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
309                                 struct osd_device *osd,
310                                 const struct lu_fid *fid,
311                                 struct osd_inode_id *id)
312 {
313         struct osd_mdobj_map        *omm = osd->od_mdt_map;
314         char                        *name = oti->oti_name;
315         struct dentry               *parent;
316         struct dentry               *dentry;
317         struct ldiskfs_dir_entry_2 *de;
318         struct buffer_head         *bh;
319         int                         rc;
320         ENTRY;
321
322         parent = omm->omm_remote_parent;
323         sprintf(name, DFID_NOBRACE, PFID(fid));
324         dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
325                                            name, strlen(name));
326         mutex_lock(&parent->d_inode->i_mutex);
327         bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
328                                     NULL, NULL);
329         if (bh == NULL) {
330                 rc = -ENOENT;
331         } else {
332                 rc = 0;
333                 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
334                 brelse(bh);
335         }
336         mutex_unlock(&parent->d_inode->i_mutex);
337         if (rc == 0)
338                 osd_add_oi_cache(oti, osd, id, fid);
339         RETURN(rc);
340 }
341
342 /*
343  * directory structure on legacy OST:
344  *
345  * O/<seq>/d0-31/<objid>
346  * O/<seq>/LAST_ID
347  * last_rcvd
348  * LAST_GROUP
349  * CONFIGS
350  *
351  */
352 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
353 {
354         struct lvfs_run_ctxt     new;
355         struct lvfs_run_ctxt     save;
356         struct dentry           *rootd = osd_sb(dev)->s_root;
357         struct dentry           *d;
358         struct osd_thread_info  *info = osd_oti_get(env);
359         struct inode            *inode;
360         struct lu_fid           *fid = &info->oti_fid3;
361         int                      rc;
362         ENTRY;
363
364         OBD_ALLOC_PTR(dev->od_ost_map);
365         if (dev->od_ost_map == NULL)
366                 RETURN(-ENOMEM);
367
368         /* to get subdir count from last_rcvd */
369         rc = osd_last_rcvd_subdir_count(dev);
370         if (rc < 0) {
371                 OBD_FREE_PTR(dev->od_ost_map);
372                 RETURN(rc);
373         }
374
375         dev->od_ost_map->om_subdir_count = rc;
376         rc = 0;
377
378         INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
379         rwlock_init(&dev->od_ost_map->om_seq_list_lock);
380         mutex_init(&dev->od_ost_map->om_dir_init_mutex);
381
382         osd_push_ctxt(dev, &new, &save);
383
384         d = ll_lookup_one_len("O", rootd, strlen("O"));
385         if (IS_ERR(d))
386                 GOTO(cleanup, rc = PTR_ERR(d));
387         if (d->d_inode == NULL) {
388                 dput(d);
389                 /* The lookup() may be called again inside simple_mkdir().
390                  * Since the repeated lookup() only be called for "/O" at
391                  * mount time, it will not affect the whole performance. */
392                 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
393                 if (IS_ERR(d))
394                         GOTO(cleanup, rc = PTR_ERR(d));
395
396                 /* It is quite probably that the device is new formatted. */
397                 dev->od_maybe_new = 1;
398         }
399
400         inode = d->d_inode;
401         dev->od_ost_map->om_root = d;
402
403         /* 'What the @fid is' is not imporatant, because the object
404          * has no OI mapping, and only is visible inside the OSD.*/
405         lu_igif_build(fid, inode->i_ino, inode->i_generation);
406         rc = osd_ea_fid_set(info, inode, fid,
407                             LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
408
409         GOTO(cleanup, rc);
410
411 cleanup:
412         pop_ctxt(&save, &new);
413         if (IS_ERR(d)) {
414                 OBD_FREE_PTR(dev->od_ost_map);
415                 RETURN(PTR_ERR(d));
416         }
417         return rc;
418 }
419
420 static void osd_seq_free(struct osd_obj_map *map,
421                          struct osd_obj_seq *osd_seq)
422 {
423         int j;
424
425         list_del_init(&osd_seq->oos_seq_list);
426
427         if (osd_seq->oos_dirs) {
428                 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
429                         if (osd_seq->oos_dirs[j])
430                                 dput(osd_seq->oos_dirs[j]);
431                 }
432                 OBD_FREE(osd_seq->oos_dirs,
433                          sizeof(struct dentry *) * osd_seq->oos_subdir_count);
434         }
435
436         if (osd_seq->oos_root)
437                 dput(osd_seq->oos_root);
438
439         OBD_FREE_PTR(osd_seq);
440 }
441
442 static void osd_ost_fini(struct osd_device *osd)
443 {
444         struct osd_obj_seq    *osd_seq;
445         struct osd_obj_seq    *tmp;
446         struct osd_obj_map    *map = osd->od_ost_map;
447         ENTRY;
448
449         if (map == NULL)
450                 return;
451
452         write_lock(&map->om_seq_list_lock);
453         list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
454                                  oos_seq_list) {
455                 osd_seq_free(map, osd_seq);
456         }
457         write_unlock(&map->om_seq_list_lock);
458         if (map->om_root)
459                 dput(map->om_root);
460         OBD_FREE_PTR(map);
461         osd->od_ost_map = NULL;
462         EXIT;
463 }
464
465 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
466 {
467         int rc;
468         ENTRY;
469
470         /* prepare structures for OST */
471         rc = osd_ost_init(env, dev);
472         if (rc)
473                 RETURN(rc);
474
475         /* prepare structures for MDS */
476         rc = osd_mdt_init(env, dev);
477
478         RETURN(rc);
479 }
480
481 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
482 {
483         struct osd_obj_seq *osd_seq;
484
485         list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
486                 if (osd_seq->oos_seq == seq)
487                         return osd_seq;
488         }
489         return NULL;
490 }
491
492 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
493 {
494         struct osd_obj_seq *osd_seq;
495
496         read_lock(&map->om_seq_list_lock);
497         osd_seq = osd_seq_find_locked(map, seq);
498         read_unlock(&map->om_seq_list_lock);
499         return osd_seq;
500 }
501
502 void osd_obj_map_fini(struct osd_device *dev)
503 {
504         osd_ost_fini(dev);
505         osd_mdt_fini(dev);
506 }
507
508 /**
509  * Update the specified OI mapping.
510  *
511  * \retval   1, changed nothing
512  * \retval   0, changed successfully
513  * \retval -ve, on error
514  */
515 static int osd_obj_update_entry(struct osd_thread_info *info,
516                                 struct osd_device *osd,
517                                 struct dentry *dir, const char *name,
518                                 const struct lu_fid *fid,
519                                 const struct osd_inode_id *id,
520                                 handle_t *th)
521 {
522         struct inode               *parent = dir->d_inode;
523         struct dentry              *child;
524         struct ldiskfs_dir_entry_2 *de;
525         struct buffer_head         *bh;
526         struct inode               *inode;
527         struct dentry              *dentry = &info->oti_obj_dentry;
528         struct osd_inode_id        *oi_id  = &info->oti_id3;
529         struct lustre_mdt_attrs    *lma    = &info->oti_mdt_attrs;
530         struct lu_fid              *oi_fid = &lma->lma_self_fid;
531         int                         rc;
532         ENTRY;
533
534         LASSERT(th != NULL);
535         LASSERT(th->h_transaction != NULL);
536
537         child = &info->oti_child_dentry;
538         child->d_parent = dir;
539         child->d_name.hash = 0;
540         child->d_name.name = name;
541         child->d_name.len = strlen(name);
542
543         ll_vfs_dq_init(parent);
544         mutex_lock(&parent->i_mutex);
545         bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
546         if (bh == NULL)
547                 GOTO(out, rc = -ENOENT);
548
549         if (le32_to_cpu(de->inode) == id->oii_ino)
550                 GOTO(out, rc = 1);
551
552         osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
553         inode = osd_iget(info, osd, oi_id);
554         if (IS_ERR(inode)) {
555                 rc = PTR_ERR(inode);
556                 if (rc == -ENOENT || rc == -ESTALE)
557                         goto update;
558                 GOTO(out, rc);
559         }
560
561         rc = osd_get_lma(info, inode, dentry, lma);
562         if (rc == -ENODATA) {
563                 rc = osd_get_idif(info, inode, dentry, oi_fid);
564                 if (rc > 0) {
565                         oi_fid = NULL;
566                         rc = 0;
567                 }
568         }
569         iput(inode);
570
571         /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
572          * either a crashed object or a uninitialized one. Replace it. */
573         if (rc == -ENODATA || oi_fid == NULL)
574                 goto update;
575
576         if (rc != 0)
577                 GOTO(out, rc);
578
579         if (lu_fid_eq(fid, oi_fid)) {
580                 CERROR("%s: the FID "DFID" is used by two objects: "
581                        "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
582                        oi_id->oii_ino, oi_id->oii_gen,
583                        id->oii_ino, id->oii_gen);
584                 GOTO(out, rc = -EEXIST);
585         }
586
587 update:
588         /* There may be temporary inconsistency: On one hand, the new
589          * object may be referenced by multiple entries, which is out
590          * of our control unless we traverse the whole /O completely,
591          * which is non-flat order and inefficient, should be avoided;
592          * On the other hand, the old object may become orphan if it
593          * is still valid. Since it was referenced by an invalid entry,
594          * making it as invisible temporary may be not worse. OI scrub
595          * will process it later. */
596         rc = ldiskfs_journal_get_write_access(th, bh);
597         if (rc != 0)
598                 GOTO(out, rc);
599
600         de->inode = cpu_to_le32(id->oii_ino);
601         rc = ldiskfs_journal_dirty_metadata(th, bh);
602
603         GOTO(out, rc);
604
605 out:
606         brelse(bh);
607         mutex_unlock(&parent->i_mutex);
608         return rc;
609 }
610
611 static int osd_obj_del_entry(struct osd_thread_info *info,
612                              struct osd_device *osd,
613                              struct dentry *dird, char *name,
614                              handle_t *th)
615 {
616         struct ldiskfs_dir_entry_2 *de;
617         struct buffer_head         *bh;
618         struct dentry              *child;
619         struct inode               *dir = dird->d_inode;
620         int                         rc;
621         ENTRY;
622
623         LASSERT(th != NULL);
624         LASSERT(th->h_transaction != NULL);
625
626
627         child = &info->oti_child_dentry;
628         child->d_name.hash = 0;
629         child->d_name.name = name;
630         child->d_name.len = strlen(name);
631         child->d_parent = dird;
632         child->d_inode = NULL;
633
634         ll_vfs_dq_init(dir);
635         mutex_lock(&dir->i_mutex);
636         rc = -ENOENT;
637         bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
638         if (bh) {
639                 rc = ldiskfs_delete_entry(th, dir, de, bh);
640                 brelse(bh);
641         }
642         mutex_unlock(&dir->i_mutex);
643
644         RETURN(rc);
645 }
646
647 int osd_obj_add_entry(struct osd_thread_info *info,
648                       struct osd_device *osd,
649                       struct dentry *dir, char *name,
650                       const struct osd_inode_id *id,
651                       handle_t *th)
652 {
653         struct dentry *child;
654         struct inode *inode;
655         int rc;
656
657         ENTRY;
658
659         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
660                 RETURN(0);
661
662         LASSERT(th != NULL);
663         LASSERT(th->h_transaction != NULL);
664
665         inode = &info->oti_inode;
666         inode->i_sb = osd_sb(osd);
667         osd_id_to_inode(inode, id);
668         inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
669
670         child = &info->oti_child_dentry;
671         child->d_name.hash = 0;
672         child->d_name.name = name;
673         child->d_name.len = strlen(name);
674         child->d_parent = dir;
675         child->d_inode = inode;
676
677         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
678                 inode->i_ino++;
679
680         ll_vfs_dq_init(dir->d_inode);
681         mutex_lock(&dir->d_inode->i_mutex);
682         rc = osd_ldiskfs_add_entry(th, child, inode, NULL);
683         mutex_unlock(&dir->d_inode->i_mutex);
684
685         RETURN(rc);
686 }
687
688 /**
689  * Use LPU64 for legacy OST sequences, but use LPX64i for new
690  * sequences names, so that the O/{seq}/dN/{oid} more closely
691  * follows the DFID/PFID format. This makes it easier to map from
692  * debug messages to objects in the future, and the legacy space
693  * of FID_SEQ_OST_MDT0 will be unused in the future.
694  **/
695 static inline void osd_seq_name(char *seq_name, size_t name_size, obd_seq seq)
696 {
697         snprintf(seq_name, name_size,
698                  (fid_seq_is_rsvd(seq) ||
699                   fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
700                  fid_seq_is_idif(seq) ? 0 : seq);
701 }
702
703 static inline void osd_oid_name(char *name, size_t name_size,
704                                 const struct lu_fid *fid, obd_id id)
705 {
706         snprintf(name, name_size,
707                  (fid_seq_is_rsvd(fid_seq(fid)) ||
708                   fid_seq_is_mdt0(fid_seq(fid)) ||
709                   fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
710 }
711
712 /* external locking is required */
713 static int osd_seq_load_locked(struct osd_thread_info *info,
714                                struct osd_device *osd,
715                                struct osd_obj_seq *osd_seq)
716 {
717         struct osd_obj_map  *map = osd->od_ost_map;
718         struct dentry       *seq_dir;
719         struct inode        *inode;
720         struct lu_fid       *fid = &info->oti_fid3;
721         int                 rc = 0;
722         int                 i;
723         char                dir_name[32];
724         ENTRY;
725
726         if (osd_seq->oos_root != NULL)
727                 RETURN(0);
728
729         LASSERT(map);
730         LASSERT(map->om_root);
731
732         osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
733
734         seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
735         if (IS_ERR(seq_dir))
736                 GOTO(out_err, rc = PTR_ERR(seq_dir));
737         else if (seq_dir->d_inode == NULL)
738                 GOTO(out_put, rc = -EFAULT);
739
740         inode = seq_dir->d_inode;
741         osd_seq->oos_root = seq_dir;
742
743         /* 'What the @fid is' is not imporatant, because the object
744          * has no OI mapping, and only is visible inside the OSD.*/
745         lu_igif_build(fid, inode->i_ino, inode->i_generation);
746         rc = osd_ea_fid_set(info, inode, fid,
747                             LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
748         if (rc != 0)
749                 GOTO(out_put, rc);
750
751         LASSERT(osd_seq->oos_dirs == NULL);
752         OBD_ALLOC(osd_seq->oos_dirs,
753                   sizeof(seq_dir) * osd_seq->oos_subdir_count);
754         if (osd_seq->oos_dirs == NULL)
755                 GOTO(out_put, rc = -ENOMEM);
756
757         for (i = 0; i < osd_seq->oos_subdir_count; i++) {
758                 struct dentry   *dir;
759
760                 snprintf(dir_name, sizeof(dir_name), "d%u", i);
761                 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
762                                    0700, 1);
763                 if (IS_ERR(dir)) {
764                         GOTO(out_free, rc = PTR_ERR(dir));
765                 } else if (dir->d_inode == NULL) {
766                         dput(dir);
767                         GOTO(out_free, rc = -EFAULT);
768                 }
769
770                 inode = dir->d_inode;
771                 osd_seq->oos_dirs[i] = dir;
772
773                 /* 'What the @fid is' is not imporatant, because the object
774                  * has no OI mapping, and only is visible inside the OSD.*/
775                 lu_igif_build(fid, inode->i_ino, inode->i_generation);
776                 rc = osd_ea_fid_set(info, inode, fid,
777                                     LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
778                 if (rc != 0)
779                         GOTO(out_free, rc);
780         }
781
782         if (rc != 0) {
783 out_free:
784                 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
785                         if (osd_seq->oos_dirs[i] != NULL)
786                                 dput(osd_seq->oos_dirs[i]);
787                 }
788                 OBD_FREE(osd_seq->oos_dirs,
789                          sizeof(seq_dir) * osd_seq->oos_subdir_count);
790 out_put:
791                 dput(seq_dir);
792                 osd_seq->oos_root = NULL;
793         }
794 out_err:
795         RETURN(rc);
796 }
797
798 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
799                                         struct osd_device *osd, obd_seq seq)
800 {
801         struct osd_obj_map      *map;
802         struct osd_obj_seq      *osd_seq;
803         int                     rc = 0;
804         ENTRY;
805
806         map = osd->od_ost_map;
807         LASSERT(map);
808         LASSERT(map->om_root);
809
810         osd_seq = osd_seq_find(map, seq);
811         if (likely(osd_seq != NULL))
812                 RETURN(osd_seq);
813
814         /* Serializing init process */
815         mutex_lock(&map->om_dir_init_mutex);
816
817         /* Check whether the seq has been added */
818         read_lock(&map->om_seq_list_lock);
819         osd_seq = osd_seq_find_locked(map, seq);
820         if (osd_seq != NULL) {
821                 read_unlock(&map->om_seq_list_lock);
822                 GOTO(cleanup, rc = 0);
823         }
824         read_unlock(&map->om_seq_list_lock);
825
826         OBD_ALLOC_PTR(osd_seq);
827         if (osd_seq == NULL)
828                 GOTO(cleanup, rc = -ENOMEM);
829
830         INIT_LIST_HEAD(&osd_seq->oos_seq_list);
831         osd_seq->oos_seq = seq;
832         /* Init subdir count to be 32, but each seq can have
833          * different subdir count */
834         osd_seq->oos_subdir_count = map->om_subdir_count;
835         rc = osd_seq_load_locked(info, osd, osd_seq);
836         if (rc != 0)
837                 GOTO(cleanup, rc);
838
839         write_lock(&map->om_seq_list_lock);
840         list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
841         write_unlock(&map->om_seq_list_lock);
842
843 cleanup:
844         mutex_unlock(&map->om_dir_init_mutex);
845         if (rc != 0) {
846                 if (osd_seq != NULL)
847                         OBD_FREE_PTR(osd_seq);
848                 RETURN(ERR_PTR(rc));
849         }
850
851         RETURN(osd_seq);
852 }
853
854 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
855                        const struct lu_fid *fid, struct osd_inode_id *id)
856 {
857         struct osd_obj_map              *map;
858         struct osd_obj_seq              *osd_seq;
859         struct dentry                   *d_seq;
860         struct dentry                   *child;
861         struct ost_id                   *ostid = &info->oti_ostid;
862         int                             dirn;
863         char                            name[32];
864         struct ldiskfs_dir_entry_2      *de;
865         struct buffer_head              *bh;
866         struct inode                    *dir;
867         struct inode                    *inode;
868         ENTRY;
869
870         /* on the very first lookup we find and open directories */
871
872         map = dev->od_ost_map;
873         LASSERT(map);
874         LASSERT(map->om_root);
875
876         fid_to_ostid(fid, ostid);
877         osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
878         if (IS_ERR(osd_seq))
879                 RETURN(PTR_ERR(osd_seq));
880
881         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
882         d_seq = osd_seq->oos_dirs[dirn];
883         LASSERT(d_seq);
884
885         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
886
887         child = &info->oti_child_dentry;
888         child->d_parent = d_seq;
889         child->d_name.hash = 0;
890         child->d_name.name = name;
891         /* XXX: we can use rc from sprintf() instead of strlen() */
892         child->d_name.len = strlen(name);
893
894         dir = d_seq->d_inode;
895         mutex_lock(&dir->i_mutex);
896         bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
897         mutex_unlock(&dir->i_mutex);
898
899         if (bh == NULL)
900                 RETURN(-ENOENT);
901
902         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
903         brelse(bh);
904
905         inode = osd_iget(info, dev, id);
906         if (IS_ERR(inode))
907                 RETURN(PTR_ERR(inode));
908
909         iput(inode);
910         RETURN(0);
911 }
912
913 int osd_obj_map_insert(struct osd_thread_info *info,
914                        struct osd_device *osd,
915                        const struct lu_fid *fid,
916                        const struct osd_inode_id *id,
917                        handle_t *th)
918 {
919         struct osd_obj_map      *map;
920         struct osd_obj_seq      *osd_seq;
921         struct dentry           *d;
922         struct ost_id           *ostid = &info->oti_ostid;
923         obd_id                   oid;
924         int                     dirn, rc = 0;
925         char                    name[32];
926         ENTRY;
927
928         map = osd->od_ost_map;
929         LASSERT(map);
930
931         /* map fid to seq:objid */
932         fid_to_ostid(fid, ostid);
933
934         oid = ostid_id(ostid);
935         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
936         if (IS_ERR(osd_seq))
937                 RETURN(PTR_ERR(osd_seq));
938
939         dirn = oid & (osd_seq->oos_subdir_count - 1);
940         d = osd_seq->oos_dirs[dirn];
941         LASSERT(d);
942
943         osd_oid_name(name, sizeof(name), fid, oid);
944
945 again:
946         rc = osd_obj_add_entry(info, osd, d, name, id, th);
947         if (rc == -EEXIST) {
948                 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
949                 if (unlikely(rc == -ENOENT))
950                         goto again;
951
952                 if (unlikely(rc == 1))
953                         RETURN(0);
954         }
955
956         RETURN(rc);
957 }
958
959 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
960                        const struct lu_fid *fid, handle_t *th)
961 {
962         struct osd_obj_map      *map;
963         struct osd_obj_seq      *osd_seq;
964         struct dentry           *d;
965         struct ost_id           *ostid = &info->oti_ostid;
966         int                     dirn, rc = 0;
967         char                    name[32];
968         ENTRY;
969
970         map = osd->od_ost_map;
971         LASSERT(map);
972
973         /* map fid to seq:objid */
974         fid_to_ostid(fid, ostid);
975
976         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
977         if (IS_ERR(osd_seq))
978                 GOTO(cleanup, rc = PTR_ERR(osd_seq));
979
980         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
981         d = osd_seq->oos_dirs[dirn];
982         LASSERT(d);
983
984         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
985         rc = osd_obj_del_entry(info, osd, d, name, th);
986 cleanup:
987         RETURN(rc);
988 }
989
990 int osd_obj_map_update(struct osd_thread_info *info,
991                        struct osd_device *osd,
992                        const struct lu_fid *fid,
993                        const struct osd_inode_id *id,
994                        handle_t *th)
995 {
996         struct osd_obj_seq      *osd_seq;
997         struct dentry           *d;
998         struct ost_id           *ostid = &info->oti_ostid;
999         int                     dirn, rc = 0;
1000         char                    name[32];
1001         ENTRY;
1002
1003         fid_to_ostid(fid, ostid);
1004         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1005         if (IS_ERR(osd_seq))
1006                 RETURN(PTR_ERR(osd_seq));
1007
1008         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1009         d = osd_seq->oos_dirs[dirn];
1010         LASSERT(d);
1011
1012         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1013         rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1014
1015         RETURN(rc);
1016 }
1017
1018 int osd_obj_map_recover(struct osd_thread_info *info,
1019                         struct osd_device *osd,
1020                         struct inode *src_parent,
1021                         struct dentry *src_child,
1022                         const struct lu_fid *fid)
1023 {
1024         struct osd_obj_seq         *osd_seq;
1025         struct dentry              *tgt_parent;
1026         struct dentry              *tgt_child = &info->oti_child_dentry;
1027         struct inode               *dir;
1028         struct inode               *inode     = src_child->d_inode;
1029         struct ost_id              *ostid     = &info->oti_ostid;
1030         handle_t                   *jh;
1031         struct ldiskfs_dir_entry_2 *de;
1032         struct buffer_head         *bh;
1033         char                        name[32];
1034         int                         dirn;
1035         int                         rc        = 0;
1036         ENTRY;
1037
1038         if (fid_is_last_id(fid)) {
1039                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1040                 if (IS_ERR(osd_seq))
1041                         RETURN(PTR_ERR(osd_seq));
1042
1043                 tgt_parent = osd_seq->oos_root;
1044                 tgt_child->d_name.name = "LAST_ID";
1045                 tgt_child->d_name.len = strlen("LAST_ID");
1046         } else {
1047                 fid_to_ostid(fid, ostid);
1048                 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1049                 if (IS_ERR(osd_seq))
1050                         RETURN(PTR_ERR(osd_seq));
1051
1052                 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1053                 tgt_parent = osd_seq->oos_dirs[dirn];
1054                 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1055                 tgt_child->d_name.name = name;
1056                 tgt_child->d_name.len = strlen(name);
1057         }
1058         LASSERT(tgt_parent != NULL);
1059
1060         dir = tgt_parent->d_inode;
1061         tgt_child->d_name.hash = 0;
1062         tgt_child->d_parent = tgt_parent;
1063         tgt_child->d_inode = inode;
1064
1065         /* The non-initialized src_child may be destroyed. */
1066         jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1067                                 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1068                                 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1069                                 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1070         if (IS_ERR(jh))
1071                 RETURN(PTR_ERR(jh));
1072
1073         ll_vfs_dq_init(src_parent);
1074         ll_vfs_dq_init(dir);
1075
1076         mutex_lock(&src_parent->i_mutex);
1077         mutex_lock(&dir->i_mutex);
1078         bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1079         if (bh != NULL) {
1080                 /* XXX: If some other object occupied the same slot. And If such
1081                  *      inode is zero-sized and with SUID+SGID, then means it is
1082                  *      a new created one. Maybe we can remove it and insert the
1083                  *      original one back to the /O/<seq>/d<x>. But there are
1084                  *      something to be considered:
1085                  *
1086                  *      1) The OST-object under /lost+found has crashed LMA.
1087                  *         So it should not conflict with the current one.
1088                  *
1089                  *      2) There are race conditions that: someone may just want
1090                  *         to modify the current one. Even if the OI scrub takes
1091                  *         the object lock when remove the current one, it still
1092                  *         cause the modification to be lost becasue the target
1093                  *         has been removed when the RPC service thread waiting
1094                  *         for the lock.
1095                  *
1096                  *      So keep it there before we have suitable solution. */
1097                 brelse(bh);
1098                 mutex_unlock(&dir->i_mutex);
1099                 mutex_unlock(&src_parent->i_mutex);
1100                 ldiskfs_journal_stop(jh);
1101
1102                 rc = -EEXIST;
1103                 /* If the src object has never been modified, then remove it. */
1104                 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1105                     inode->i_mode & S_ISGID) {
1106                         rc = ll_vfs_unlink(src_parent, src_child);
1107                         if (unlikely(rc == -ENOENT))
1108                                 rc = 0;
1109                 }
1110                 RETURN(rc);
1111         }
1112
1113         bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1114                                     NULL, NULL);
1115         if (unlikely(bh == NULL))
1116                 GOTO(unlock, rc = -ENOENT);
1117
1118         rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1119         brelse(bh);
1120         if (rc != 0)
1121                 GOTO(unlock, rc);
1122
1123         rc = osd_ldiskfs_add_entry(jh, tgt_child, inode, NULL);
1124
1125         GOTO(unlock, rc);
1126
1127 unlock:
1128         mutex_unlock(&dir->i_mutex);
1129         mutex_unlock(&src_parent->i_mutex);
1130         ldiskfs_journal_stop(jh);
1131         return rc;
1132 }
1133
1134 static struct dentry *
1135 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1136                      const struct lu_fid *fid, char **name)
1137 {
1138         struct dentry *root = ERR_PTR(-ENOENT);
1139
1140         if (fid_is_last_id(fid)) {
1141                 struct osd_obj_seq *osd_seq;
1142
1143                 /* on creation of LAST_ID we create O/<seq> hierarchy */
1144                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1145                 if (IS_ERR(osd_seq))
1146                         RETURN((struct dentry *)osd_seq);
1147
1148                 *name = "LAST_ID";
1149                 root = osd_seq->oos_root;
1150         } else {
1151                 *name = osd_lf_fid2name(fid);
1152                 if (*name == NULL)
1153                         CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1154                 else if ((*name)[0])
1155                         root = osd_sb(osd)->s_root;
1156         }
1157
1158         return root;
1159 }
1160
1161 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1162                         const struct lu_fid *fid, const struct osd_inode_id *id,
1163                         handle_t *th)
1164 {
1165         struct dentry   *root;
1166         char            *name = NULL;
1167         int              rc;
1168         ENTRY;
1169
1170         root = osd_object_spec_find(info, osd, fid, &name);
1171         if (!IS_ERR(root)) {
1172                 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1173         } else {
1174                 rc = PTR_ERR(root);
1175                 if (rc == -ENOENT)
1176                         rc = 1;
1177         }
1178
1179         RETURN(rc);
1180 }
1181
1182 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1183                         const struct lu_fid *fid, const struct osd_inode_id *id,
1184                         handle_t *th)
1185 {
1186         struct dentry   *root;
1187         char            *name = NULL;
1188         int              rc;
1189         ENTRY;
1190
1191         root = osd_object_spec_find(info, osd, fid, &name);
1192         if (!IS_ERR(root)) {
1193                 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1194         } else {
1195                 rc = PTR_ERR(root);
1196                 if (rc == -ENOENT)
1197                         rc = 0;
1198         }
1199
1200         RETURN(rc);
1201 }
1202
1203 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1204                         const struct lu_fid *fid, struct osd_inode_id *id)
1205 {
1206         struct dentry   *root;
1207         struct dentry   *dentry;
1208         struct inode    *inode;
1209         char            *name = NULL;
1210         int             rc = -ENOENT;
1211         ENTRY;
1212
1213         if (fid_is_last_id(fid)) {
1214                 struct osd_obj_seq *osd_seq;
1215
1216                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1217                 if (IS_ERR(osd_seq))
1218                         RETURN(PTR_ERR(osd_seq));
1219                 root = osd_seq->oos_root;
1220                 name = "LAST_ID";
1221         } else {
1222                 root = osd_sb(osd)->s_root;
1223                 name = osd_lf_fid2name(fid);
1224                 if (name == NULL || strlen(name) == 0)
1225                         RETURN(-ENOENT);
1226         }
1227
1228         dentry = ll_lookup_one_len(name, root, strlen(name));
1229         if (!IS_ERR(dentry)) {
1230                 inode = dentry->d_inode;
1231                 if (inode) {
1232                         if (is_bad_inode(inode)) {
1233                                 rc = -EIO;
1234                         } else {
1235                                 osd_id_gen(id, inode->i_ino,
1236                                            inode->i_generation);
1237                                 rc = 0;
1238                         }
1239                 }
1240                 /* if dentry is accessible after osd_compat_spec_insert it
1241                  * will still contain NULL inode, so don't keep it in cache */
1242                 d_invalidate(dentry);
1243                 dput(dentry);
1244         }
1245
1246         RETURN(rc);
1247 }