Whamcloud - gitweb
- landing b_fid.
[fs/lustre-release.git] / lustre / mds / mds_unlink_open.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/mds/mds_orphan.c
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *   Author: Peter Braam <braam@clusterfs.com>
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *   Author: Phil Schwan <phil@clusterfs.com>
10  *
11  *   This file is part of Lustre, http://www.lustre.org.
12  *
13  *   Lustre is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   Lustre is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with Lustre; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 /* code for handling open unlinked files */
28
29 #define DEBUG_SUBSYSTEM S_MDS
30
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34
35 #include <portals/list.h>
36 #include <linux/obd_class.h>
37 #include <linux/lustre_fsfilt.h>
38 #include <linux/lustre_commit_confd.h>
39 #include <linux/lvfs.h>
40
41 #include "mds_internal.h"
42
43 static int mds_osc_destroy_orphan(struct mds_obd *mds,
44                                   struct inode *inode,
45                                   struct lov_mds_md *lmm,
46                                   int lmm_size,
47                                   struct llog_cookie *logcookies,
48                                   int log_unlink)
49 {
50         struct lov_stripe_md *lsm = NULL;
51         struct obd_trans_info oti = { 0 };
52         struct obdo *oa;
53         int rc;
54         ENTRY;
55
56         if (lmm_size == 0)
57                 RETURN(0);
58
59         rc = obd_unpackmd(mds->mds_lov_exp, &lsm, lmm, lmm_size);
60         if (rc < 0) {
61                 CERROR("Error unpack md %p\n", lmm);
62                 RETURN(rc);
63         } else {
64                 LASSERT(rc >= sizeof(*lsm));
65                 rc = 0;
66         }
67
68         oa = obdo_alloc();
69         if (oa == NULL)
70                 GOTO(out_free_memmd, rc = -ENOMEM);
71         oa->o_id = lsm->lsm_object_id;
72         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
73         oa->o_mode = inode->i_mode & S_IFMT;
74         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
75
76         if (log_unlink && logcookies) {
77                 oa->o_valid |= OBD_MD_FLCOOKIE;
78                 oti.oti_logcookies = logcookies;
79         }
80
81         rc = obd_destroy(mds->mds_lov_exp, oa, lsm, &oti);
82         obdo_free(oa);
83         if (rc)
84                 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
85                        "%d\n", lsm->lsm_object_id, rc);
86 out_free_memmd:
87         obd_free_memmd(mds->mds_lov_exp, &lsm);
88         RETURN(rc);
89 }
90
91 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
92                              struct inode *inode, struct inode *pending_dir)
93 {
94         struct mds_obd *mds = &obd->u.mds;
95         struct lov_mds_md *lmm = NULL;
96         struct llog_cookie *logcookies = NULL;
97         int lmm_size = 0, log_unlink = 0;
98         void *handle = NULL;
99         int rc, err;
100         ENTRY;
101
102         LASSERT(mds->mds_lov_obd != NULL);
103
104         OBD_ALLOC(lmm, mds->mds_max_mdsize);
105         if (lmm == NULL)
106                 RETURN(-ENOMEM);
107
108         down(&inode->i_sem);
109         rc = fsfilt_get_md(obd, inode, lmm, mds->mds_max_mdsize);
110         up(&inode->i_sem);
111
112         if (rc < 0) {
113                 CERROR("Error %d reading eadata for ino %lu\n",
114                        rc, inode->i_ino);
115                 GOTO(out_free_lmm, rc);
116         } else if (rc > 0) {
117                 lmm_size = rc;
118                 rc = mds_convert_lov_ea(obd, inode, lmm, lmm_size);
119                 if (rc > 0)
120                         lmm_size = rc;
121                 rc = 0;
122         }
123
124         handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
125                                   le32_to_cpu(lmm->lmm_stripe_count));
126         if (IS_ERR(handle)) {
127                 rc = PTR_ERR(handle);
128                 CERROR("error fsfilt_start: %d\n", rc);
129                 handle = NULL;
130                 GOTO(out_free_lmm, rc);
131         }
132
133         if (S_ISDIR(inode->i_mode))
134                 rc = vfs_rmdir(pending_dir, dchild);
135         else
136                 rc = vfs_unlink(pending_dir, dchild);
137
138         if (rc)
139                 CERROR("error %d unlinking orphan %*s from PENDING directory\n",
140                        rc, dchild->d_name.len, dchild->d_name.name);
141
142         if (!rc && lmm_size) {
143                 OBD_ALLOC(logcookies, mds->mds_max_cookiesize);
144                 if (logcookies == NULL)
145                         rc = -ENOMEM;
146                 else if (mds_log_op_unlink(obd, inode, lmm,lmm_size,logcookies,
147                                            mds->mds_max_cookiesize, NULL) > 0)
148                         log_unlink = 1;
149         }
150         err = fsfilt_commit(obd, mds->mds_sb, pending_dir, handle, 0);
151         if (err) {
152                 CERROR("error committing orphan unlink: %d\n", err);
153                 if (!rc)
154                         rc = err;
155         }
156         if (!rc) {
157                 rc = mds_osc_destroy_orphan(mds, inode, lmm, lmm_size,
158                                             logcookies, log_unlink);
159         }
160
161         if (logcookies != NULL)
162                 OBD_FREE(logcookies, mds->mds_max_cookiesize);
163 out_free_lmm:
164         OBD_FREE(lmm, mds->mds_max_mdsize);
165         RETURN(rc);
166 }
167
168 int mds_cleanup_orphans(struct obd_device *obd)
169 {
170         struct mds_obd *mds = &obd->u.mds;
171         struct lvfs_run_ctxt saved;
172         struct file *file;
173         struct dentry *dchild, *dentry;
174         struct vfsmount *mnt;
175         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
176         struct l_linux_dirent *dirent, *n;
177         struct list_head dentry_list;
178         char d_name[LL_ID_NAMELEN];
179         __u64 i = 0;
180         int rc = 0, item = 0, namlen;
181         ENTRY;
182
183         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
184         dentry = dget(mds->mds_pending_dir);
185         if (IS_ERR(dentry))
186                 GOTO(err_pop, rc = PTR_ERR(dentry));
187         mnt = mntget(mds->mds_vfsmnt);
188         if (IS_ERR(mnt))
189                 GOTO(err_mntget, rc = PTR_ERR(mnt));
190
191         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
192                            O_RDONLY | O_LARGEFILE);
193         if (IS_ERR(file))
194                 GOTO(err_pop, rc = PTR_ERR(file));
195
196         INIT_LIST_HEAD(&dentry_list);
197         rc = l_readdir(file, &dentry_list);
198         filp_close(file, 0);
199         if (rc < 0)
200                 GOTO(err_out, rc);
201
202         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
203                 i ++;
204                 list_del(&dirent->lld_list);
205
206                 namlen = strlen(dirent->lld_name);
207                 LASSERT(sizeof(d_name) >= namlen + 1);
208                 strcpy(d_name, dirent->lld_name);
209                 OBD_FREE(dirent, sizeof(*dirent));
210
211                 CDEBUG(D_INODE, "entry "LPU64" of PENDING DIR: %s\n",
212                        i, d_name);
213
214                 if (((namlen == 1) && !strcmp(d_name, ".")) ||
215                     ((namlen == 2) && !strcmp(d_name, ".."))) {
216                         continue;
217                 }
218
219                 down(&pending_dir->i_sem);
220                 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
221                 if (IS_ERR(dchild)) {
222                         up(&pending_dir->i_sem);
223                         GOTO(err_out, rc = PTR_ERR(dchild));
224                 }
225                 if (!dchild->d_inode) {
226                         CERROR("orphan %s has been removed\n", d_name);
227                         GOTO(next, rc = 0);
228                 }
229
230                 child_inode = dchild->d_inode;
231                 DOWN_READ_I_ALLOC_SEM(child_inode);
232                 if (mds_inode_is_orphan(child_inode) &&
233                     mds_orphan_open_count(child_inode)) {
234                         UP_READ_I_ALLOC_SEM(child_inode);
235                         CWARN("orphan %s re-opened during recovery\n", d_name);
236                         GOTO(next, rc = 0);
237                 }
238                 UP_READ_I_ALLOC_SEM(child_inode);
239                 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
240                 if (rc == 0) {
241                         item ++;
242                         CWARN("removed orphan %s from MDS and OST\n", d_name);
243                 } else {
244                         CDEBUG(D_INODE, "removed orphan %s from MDS/OST failed,"
245                                " rc = %d\n", d_name, rc);
246                         rc = 0;
247                 }
248 next:
249                 l_dput(dchild);
250                 up(&pending_dir->i_sem);
251         }
252 err_out:
253         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
254                 list_del(&dirent->lld_list);
255                 OBD_FREE(dirent, sizeof(*dirent));
256         }
257 err_pop:
258         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
259         if (rc == 0)
260                 rc = item;
261         RETURN(rc);
262
263 err_mntget:
264         l_dput(mds->mds_pending_dir);
265         goto err_pop;
266 }