Whamcloud - gitweb
- CROW (CReate On Write) (precreation is removed)
[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 /*
44  * used when destroying orphanes and from mds_reint_unlink() when MDS wants to
45  * destroy objects on OSS.
46  */
47 int
48 mds_unlink_object(struct mds_obd *mds, struct inode *inode,
49                   struct lov_mds_md *lmm, int lmm_size,
50                   struct llog_cookie *logcookies,
51                   int log_unlink, int async)
52 {
53         struct lov_stripe_md *lsm = NULL;
54         struct obd_trans_info oti = { 0 };
55         struct obdo *oa;
56         int rc;
57         ENTRY;
58
59         if (lmm_size == 0)
60                 RETURN(0);
61
62         rc = obd_unpackmd(mds->mds_dt_exp, &lsm, lmm, lmm_size);
63         if (rc < 0) {
64                 CERROR("Error unpack md %p\n", lmm);
65                 RETURN(rc);
66         } else {
67                 LASSERT(rc >= sizeof(*lsm));
68                 rc = 0;
69         }
70
71         oa = obdo_alloc();
72         if (oa == NULL)
73                 GOTO(out_free_memmd, rc = -ENOMEM);
74         oa->o_id = lsm->lsm_object_id;
75         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
76         oa->o_mode = inode->i_mode & S_IFMT;
77         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
78
79         if (log_unlink && logcookies) {
80                 oa->o_valid |= OBD_MD_FLCOOKIE;
81                 oti.oti_logcookies = logcookies;
82         }
83
84         CDEBUG(D_INODE, "destroy OSS object %d/%d\n",
85                (int)oa->o_id, (int)oa->o_gr);
86
87         if (async)
88                 oti.oti_flags |= OBD_MODE_ASYNC;
89         
90         rc = obd_destroy(mds->mds_dt_exp, oa, lsm, &oti);
91         obdo_free(oa);
92 out_free_memmd:
93         obd_free_memmd(mds->mds_dt_exp, &lsm);
94         RETURN(rc);
95 }
96
97 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
98                              struct inode *inode, struct inode *pending_dir)
99 {
100         struct mds_obd *mds = &obd->u.mds;
101         struct lov_mds_md *lmm = NULL;
102         struct llog_cookie *logcookies = NULL;
103         int lmm_size, log_unlink = 0;
104         void *handle = NULL;
105         int rc, err;
106         ENTRY;
107
108         LASSERT(mds->mds_dt_obd != NULL);
109         LASSERT(obd->obd_recovering == 0);
110
111         /* We don't need to do any of these other things for orhpan dirs,
112          * especially not mds_get_md (may get a default LOV EA, bug 4554) */
113         if (S_ISDIR(inode->i_mode)) {
114                 rc = vfs_rmdir(pending_dir, dchild);
115                 if (rc)
116                         CERROR("error %d unlinking dir %*s from PENDING\n",
117                                rc, dchild->d_name.len, dchild->d_name.name);
118                 RETURN(rc);
119         }
120
121         lmm_size = mds->mds_max_mdsize;
122         OBD_ALLOC(lmm, lmm_size);
123         if (lmm == NULL)
124                 RETURN(-ENOMEM);
125
126         rc = mds_get_md(obd, inode, lmm, &lmm_size, 1, 0);
127         if (rc < 0)
128                 GOTO(out_free_lmm, rc);
129
130         handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
131                                   le32_to_cpu(lmm->lmm_stripe_count));
132         if (IS_ERR(handle)) {
133                 rc = PTR_ERR(handle);
134                 CERROR("error fsfilt_start: %d\n", rc);
135                 handle = NULL;
136                 GOTO(out_free_lmm, rc);
137         }
138
139         rc = vfs_unlink(pending_dir, dchild);
140         if (rc) {
141                 CERROR("error %d unlinking orphan %.*s from PENDING\n",
142                        rc, dchild->d_name.len, dchild->d_name.name);
143         } else if (lmm_size) {
144                 OBD_ALLOC(logcookies, mds->mds_max_cookiesize);
145                 if (logcookies == NULL)
146                         rc = -ENOMEM;
147                 else if (mds_log_op_unlink(obd, inode, lmm,lmm_size,logcookies,
148                                            mds->mds_max_cookiesize, NULL) > 0)
149                         log_unlink = 1;
150         }
151         err = fsfilt_commit(obd, mds->mds_sb, pending_dir, handle, 0);
152         if (err) {
153                 CERROR("error committing orphan unlink: %d\n", err);
154                 if (!rc)
155                         rc = err;
156         } else if (!rc) {
157                 rc = mds_unlink_object(mds, inode, lmm, lmm_size,
158                                        logcookies, log_unlink, 0);
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         unsigned long inum;
180         __u64 i = 0;
181         int rc = 0, item = 0, namlen;
182         ENTRY;
183
184         LASSERT(obd->obd_recovering == 0);
185         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
186         dentry = dget(mds->mds_pending_dir);
187         if (IS_ERR(dentry))
188                 GOTO(err_pop, rc = PTR_ERR(dentry));
189         mnt = mntget(mds->mds_vfsmnt);
190         if (IS_ERR(mnt))
191                 GOTO(err_mntget, rc = PTR_ERR(mnt));
192
193         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
194                            O_RDONLY | O_LARGEFILE);
195         if (IS_ERR(file))
196                 GOTO(err_pop, rc = PTR_ERR(file));
197
198         INIT_LIST_HEAD(&dentry_list);
199         rc = l_readdir(file, &dentry_list);
200         filp_close(file, 0);
201         if (rc < 0)
202                 GOTO(err_out, rc);
203
204         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
205                 i++;
206                 list_del(&dirent->lld_list);
207
208                 namlen = strlen(dirent->lld_name);
209                 LASSERT(sizeof(d_name) >= namlen + 1);
210                 strcpy(d_name, dirent->lld_name);
211                 inum = dirent->lld_ino;
212                 OBD_FREE(dirent, sizeof(*dirent));
213
214                 CDEBUG(D_INODE, "entry "LPU64" of PENDING DIR: %s\n",
215                        i, d_name);
216
217                 if (((namlen == 1) && !strcmp(d_name, ".")) ||
218                     ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
219                         continue;
220
221                 down(&pending_dir->i_sem);
222                 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
223                 if (IS_ERR(dchild)) {
224                         up(&pending_dir->i_sem);
225                         GOTO(err_out, rc = PTR_ERR(dchild));
226                 }
227                 if (!dchild->d_inode) {
228                         CERROR("orphan %s has been removed\n", d_name);
229                         GOTO(next, rc = 0);
230                 }
231
232                 if (is_bad_inode(dchild->d_inode)) {
233                         CERROR("bad orphan inode found %lu/%u\n",
234                                dchild->d_inode->i_ino,
235                                dchild->d_inode->i_generation);
236                         GOTO(next, rc = -ENOENT);
237                 }
238
239                 child_inode = dchild->d_inode;
240                 DOWN_READ_I_ALLOC_SEM(child_inode);
241                 if (mds_orphan_open_count(child_inode)) {
242                         UP_READ_I_ALLOC_SEM(child_inode);
243                         CWARN("orphan %s re-opened during recovery\n", d_name);
244                         GOTO(next, rc = 0);
245                 }
246                 UP_READ_I_ALLOC_SEM(child_inode);
247                 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
248                 if (rc == 0) {
249                         item ++;
250                         CWARN("removed orphan %s from MDS and OST\n", d_name);
251                 } else {
252                         CDEBUG(D_INODE, "removed orphan %s from MDS/OST failed,"
253                                " rc = %d\n", d_name, rc);
254                         rc = 0;
255                 }
256 next:
257                 l_dput(dchild);
258                 up(&pending_dir->i_sem);
259         }
260 err_out:
261         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
262                 list_del(&dirent->lld_list);
263                 OBD_FREE(dirent, sizeof(*dirent));
264         }
265 err_pop:
266         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
267         if (rc == 0)
268                 rc = item;
269         RETURN(rc);
270
271 err_mntget:
272         l_dput(mds->mds_pending_dir);
273         goto err_pop;
274 }