Whamcloud - gitweb
f6bc83c9c0785e514f77e0788d0b39b08fe2643c
[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 <libcfs/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_osc_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_mode = inode->i_mode & S_IFMT;
73         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
74
75         if (log_unlink && logcookies) {
76                 oa->o_valid |= OBD_MD_FLCOOKIE;
77                 oti.oti_logcookies = logcookies;
78         }
79
80         rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti);
81         obdo_free(oa);
82         if (rc)
83                 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
84                        "%d\n", lsm->lsm_object_id, rc);
85 out_free_memmd:
86         obd_free_memmd(mds->mds_osc_exp, &lsm);
87         RETURN(rc);
88 }
89
90 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
91                              struct inode *inode, struct inode *pending_dir)
92 {
93         struct mds_obd *mds = &obd->u.mds;
94         struct lov_mds_md *lmm = NULL;
95         struct llog_cookie *logcookies = NULL;
96         int lmm_size, log_unlink = 0;
97         void *handle = NULL;
98         int rc, err;
99         ENTRY;
100
101         LASSERT(mds->mds_osc_obd != NULL);
102
103         /* We don't need to do any of these other things for orhpan dirs,
104          * especially not mds_get_md (may get a default LOV EA, bug 4554) */
105         if (S_ISDIR(inode->i_mode)) {
106                 rc = vfs_rmdir(pending_dir, dchild);
107                 if (rc)
108                         CERROR("error %d unlinking dir %*s from PENDING\n",
109                                rc, dchild->d_name.len, dchild->d_name.name);
110                 RETURN(rc);
111         }
112
113         lmm_size = mds->mds_max_mdsize;
114         OBD_ALLOC(lmm, lmm_size);
115         if (lmm == NULL)
116                 RETURN(-ENOMEM);
117
118         rc = mds_get_md(obd, inode, lmm, &lmm_size, 1);
119         if (rc < 0)
120                 GOTO(out_free_lmm, rc);
121
122         handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
123                                   le32_to_cpu(lmm->lmm_stripe_count));
124         if (IS_ERR(handle)) {
125                 rc = PTR_ERR(handle);
126                 CERROR("error fsfilt_start: %d\n", rc);
127                 handle = NULL;
128                 GOTO(out_free_lmm, rc);
129         }
130
131         rc = vfs_unlink(pending_dir, dchild);
132         if (rc)
133                 CERROR("error %d unlinking orphan %.*s from PENDING\n",
134                        rc, dchild->d_name.len, dchild->d_name.name);
135         else if (lmm_size) {
136                 OBD_ALLOC(logcookies, mds->mds_max_cookiesize);
137                 if (logcookies == NULL)
138                         rc = -ENOMEM;
139                 else if (mds_log_op_unlink(obd, inode, lmm,lmm_size,logcookies,
140                                            mds->mds_max_cookiesize) > 0)
141                         log_unlink = 1;
142         }
143
144         err = fsfilt_commit(obd, pending_dir, handle, 0);
145         if (err) {
146                 CERROR("error committing orphan unlink: %d\n", err);
147                 if (!rc)
148                         rc = err;
149         } else if (!rc) {
150                 rc = mds_osc_destroy_orphan(mds, inode, lmm, lmm_size,
151                                             logcookies, log_unlink);
152         }
153
154         if (logcookies != NULL)
155                 OBD_FREE(logcookies, mds->mds_max_cookiesize);
156 out_free_lmm:
157         OBD_FREE(lmm, mds->mds_max_mdsize);
158         RETURN(rc);
159 }
160
161 int mds_cleanup_orphans(struct obd_device *obd)
162 {
163         struct mds_obd *mds = &obd->u.mds;
164         struct obd_run_ctxt saved;
165         struct file *file;
166         struct dentry *dchild, *dentry;
167         struct vfsmount *mnt;
168         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
169         struct l_linux_dirent *dirent, *n;
170         struct list_head dentry_list;
171         char d_name[LL_FID_NAMELEN];
172         unsigned long inum;
173         int i = 0, rc = 0, item = 0, namlen;
174         ENTRY;
175
176         push_ctxt(&saved, &obd->obd_ctxt, NULL);
177         dentry = dget(mds->mds_pending_dir);
178         if (IS_ERR(dentry))
179                 GOTO(err_pop, rc = PTR_ERR(dentry));
180         mnt = mntget(mds->mds_vfsmnt);
181         if (IS_ERR(mnt))
182                 GOTO(err_mntget, rc = PTR_ERR(mnt));
183
184         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
185                            O_RDONLY | O_LARGEFILE);
186         if (IS_ERR(file))
187                 GOTO(err_pop, rc = PTR_ERR(file));
188
189         INIT_LIST_HEAD(&dentry_list);
190         rc = l_readdir(file, &dentry_list);
191         filp_close(file, 0);
192         if (rc < 0)
193                 GOTO(err_out, rc);
194
195         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
196                 i++;
197                 list_del(&dirent->lld_list);
198
199                 namlen = strlen(dirent->lld_name);
200                 LASSERT(sizeof(d_name) >= namlen + 1);
201                 strcpy(d_name, dirent->lld_name);
202                 inum = dirent->lld_ino;
203                 OBD_FREE(dirent, sizeof(*dirent));
204
205                 CDEBUG(D_INODE, "entry %d of PENDING DIR: %s\n", i, d_name);
206
207                 if (((namlen == 1) && !strcmp(d_name, ".")) ||
208                     ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
209                         continue;
210
211                 down(&pending_dir->i_sem);
212                 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
213                 if (IS_ERR(dchild)) {
214                         up(&pending_dir->i_sem);
215                         GOTO(err_out, rc = PTR_ERR(dchild));
216                 }
217                 if (!dchild->d_inode) {
218                         CERROR("orphan %s has been removed\n", d_name);
219                         GOTO(next, rc = 0);
220                 }
221
222                 child_inode = dchild->d_inode;
223                 DOWN_READ_I_ALLOC_SEM(child_inode);
224                 if (mds_inode_is_orphan(child_inode) &&
225                     mds_orphan_open_count(child_inode)) {
226                         UP_READ_I_ALLOC_SEM(child_inode);
227                         CWARN("orphan %s re-opened during recovery\n", d_name);
228                         GOTO(next, rc = 0);
229                 }
230                 UP_READ_I_ALLOC_SEM(child_inode);
231
232                 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
233                 if (rc == 0) {
234                         item ++;
235                         CWARN("removed orphan %s from MDS and OST\n", d_name);
236                 } else {
237                         CDEBUG(D_INODE, "removed orphan %s from MDS/OST failed,"
238                                " rc = %d\n", d_name, rc);
239                         rc = 0;
240                 }
241 next:
242                 l_dput(dchild);
243                 up(&pending_dir->i_sem);
244         }
245 err_out:
246         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
247                 list_del(&dirent->lld_list);
248                 OBD_FREE(dirent, sizeof(*dirent));
249         }
250 err_pop:
251         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
252         if (rc == 0)
253                 rc = item;
254         RETURN(rc);
255
256 err_mntget:
257         l_dput(mds->mds_pending_dir);
258         goto err_pop;
259 }