Whamcloud - gitweb
land b1_5 onto HEAD
[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 the Lustre file system, http://www.lustre.org
12  *   Lustre is a trademark of Cluster File Systems, Inc.
13  *
14  *   You may have signed or agreed to another license before downloading
15  *   this software.  If so, you are bound by the terms and conditions
16  *   of that agreement, and the following does not apply to you.  See the
17  *   LICENSE file included with this distribution for more information.
18  *
19  *   If you did not agree to a different license, then this copy of Lustre
20  *   is open source software; you can redistribute it and/or modify it
21  *   under the terms of version 2 of the GNU General Public License as
22  *   published by the Free Software Foundation.
23  *
24  *   In either case, Lustre is distributed in the hope that it will be
25  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *   license text for more details.
28  */
29
30 /* code for handling open unlinked files */
31
32 #define DEBUG_SUBSYSTEM S_MDS
33
34 #ifdef HAVE_KERNEL_CONFIG_H
35 #include <linux/config.h>
36 #endif
37 #include <linux/module.h>
38 #include <linux/version.h>
39
40 #include <libcfs/list.h>
41 #include <obd_class.h>
42 #include <lustre_fsfilt.h>
43 #include <lustre_mds.h>
44 #include <lustre_commit_confd.h>
45 #include <lvfs.h>
46
47 #include "mds_internal.h"
48
49 int mds_osc_destroy_orphan(struct obd_device *obd,
50                            umode_t mode,
51                            struct lov_mds_md *lmm,
52                            int lmm_size,
53                            struct llog_cookie *logcookies,
54                            int log_unlink)
55 {
56         struct mds_obd *mds = &obd->u.mds;
57         struct lov_stripe_md *lsm = NULL;
58         struct obd_trans_info oti = { 0 };
59         struct obdo *oa;
60         int rc;
61         ENTRY;
62
63         if (lmm_size == 0)
64                 RETURN(0);
65
66         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
67         if (rc < 0) {
68                 CERROR("Error unpack md %p\n", lmm);
69                 RETURN(rc);
70         } else {
71                 LASSERT(rc >= sizeof(*lsm));
72                 rc = 0;
73         }
74
75         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
76         if (rc)
77                 GOTO(out_free_memmd, rc);
78
79         oa = obdo_alloc();
80         if (oa == NULL)
81                 GOTO(out_free_memmd, rc = -ENOMEM);
82         oa->o_id = lsm->lsm_object_id;
83         oa->o_mode = mode & S_IFMT;
84         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
85
86         if (log_unlink && logcookies) {
87                 oa->o_valid |= OBD_MD_FLCOOKIE;
88                 oti.oti_logcookies = logcookies;
89         }
90         rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti, obd->obd_self_export);
91         obdo_free(oa);
92         if (rc)
93                 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
94                        "%d\n", lsm->lsm_object_id, rc);
95 out_free_memmd:
96         obd_free_memmd(mds->mds_osc_exp, &lsm);
97         RETURN(rc);
98 }
99
100 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
101                              struct inode *inode, struct inode *pending_dir)
102 {
103         struct mds_obd *mds = &obd->u.mds;
104         struct lov_mds_md *lmm = NULL;
105         struct llog_cookie *logcookies = NULL;
106         int lmm_size, log_unlink = 0, cookie_size = 0;
107         void *handle = NULL;
108         umode_t mode;
109         int rc, err;
110         ENTRY;
111
112         LASSERT(mds->mds_osc_obd != NULL);
113         
114         /* We don't need to do any of these other things for orhpan dirs,
115          * especially not mds_get_md (may get a default LOV EA, bug 4554) */
116         mode = inode->i_mode;
117         if (S_ISDIR(mode)) {
118                 rc = vfs_rmdir(pending_dir, dchild);
119                 if (rc)
120                         CERROR("error %d unlinking dir %*s from PENDING\n",
121                                rc, dchild->d_name.len, dchild->d_name.name);
122                 RETURN(rc);
123         }
124
125         lmm_size = mds->mds_max_mdsize;
126         OBD_ALLOC(lmm, lmm_size);
127         if (lmm == NULL)
128                 RETURN(-ENOMEM);
129
130         rc = mds_get_md(obd, inode, lmm, &lmm_size, 1);
131         if (rc < 0)
132                 GOTO(out_free_lmm, rc);
133
134         handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
135                                   le32_to_cpu(lmm->lmm_stripe_count));
136         if (IS_ERR(handle)) {
137                 rc = PTR_ERR(handle);
138                 CERROR("error fsfilt_start: %d\n", rc);
139                 handle = NULL;
140                 GOTO(out_free_lmm, rc);
141         }
142
143         rc = vfs_unlink(pending_dir, dchild);
144         if (rc) {
145                 CERROR("error %d unlinking orphan %.*s from PENDING\n",
146                        rc, dchild->d_name.len, dchild->d_name.name);
147         } else if (lmm_size) {
148                 cookie_size = mds_get_cookie_size(obd, lmm); 
149                 OBD_ALLOC(logcookies, cookie_size);
150                 if (logcookies == NULL)
151                         rc = -ENOMEM;
152                 else if (mds_log_op_unlink(obd, lmm,lmm_size,logcookies,
153                                            cookie_size) > 0)
154                         log_unlink = 1;
155         }
156
157         err = fsfilt_commit(obd, pending_dir, handle, 0);
158         if (err) {
159                 CERROR("error committing orphan unlink: %d\n", err);
160                 if (!rc)
161                         rc = err;
162         } else if (!rc) {
163                 rc = mds_osc_destroy_orphan(obd, mode, lmm, lmm_size,
164                                             logcookies, log_unlink);
165         }
166
167         if (logcookies != NULL)
168                 OBD_FREE(logcookies, cookie_size);
169 out_free_lmm:
170         OBD_FREE(lmm, mds->mds_max_mdsize);
171         RETURN(rc);
172 }
173
174 /* Delete inodes which were previously open-unlinked but were not reopened
175  * during MDS recovery for whatever reason (e.g. client also failed, recovery
176  * aborted, etc). */
177 int mds_cleanup_pending(struct obd_device *obd)
178 {
179         struct mds_obd *mds = &obd->u.mds;
180         struct lvfs_run_ctxt saved;
181         struct file *file;
182         struct dentry *dchild, *dentry;
183         struct vfsmount *mnt;
184         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
185         struct l_linux_dirent *dirent, *n;
186         struct list_head dentry_list;
187         char d_name[LL_FID_NAMELEN];
188         unsigned long inum;
189         int i = 0, rc = 0, item = 0, namlen;
190         ENTRY;
191
192         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
193         /* dentry and mnt ref dropped in dentry_open() on error, or
194          * in filp_close() if dentry_open() succeeds */
195         dentry = dget(mds->mds_pending_dir);
196         if (IS_ERR(dentry))
197                 GOTO(err_pop, rc = PTR_ERR(dentry));
198         mnt = mntget(mds->mds_vfsmnt);
199         if (IS_ERR(mnt))
200                 GOTO(err_mntget, rc = PTR_ERR(mnt));
201
202         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
203                            O_RDONLY | O_LARGEFILE);
204         if (IS_ERR(file))
205                 GOTO(err_pop, rc = PTR_ERR(file));
206
207         INIT_LIST_HEAD(&dentry_list);
208         rc = l_readdir(file, &dentry_list);
209         filp_close(file, 0);
210         if (rc < 0)
211                 GOTO(err_out, rc);
212
213         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
214                 i++;
215                 list_del(&dirent->lld_list);
216
217                 namlen = strlen(dirent->lld_name);
218                 LASSERT(sizeof(d_name) >= namlen + 1);
219                 strcpy(d_name, dirent->lld_name);
220                 inum = dirent->lld_ino;
221                 OBD_FREE(dirent, sizeof(*dirent));
222
223                 CDEBUG(D_INODE, "entry %d of PENDING DIR: %s\n", i, d_name);
224
225                 if (((namlen == 1) && !strcmp(d_name, ".")) ||
226                     ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
227                         continue;
228
229                 LOCK_INODE_MUTEX(pending_dir);
230                 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
231                 if (IS_ERR(dchild)) {
232                         UNLOCK_INODE_MUTEX(pending_dir);
233                         GOTO(err_out, rc = PTR_ERR(dchild));
234                 }
235                 if (!dchild->d_inode) {
236                         CWARN("%s: orphan %s has already been removed\n",
237                               obd->obd_name, d_name);
238                         GOTO(next, rc = 0);
239                 }
240
241                 if (is_bad_inode(dchild->d_inode)) {
242                         CERROR("%s: bad orphan inode found %lu/%u\n",
243                                obd->obd_name, dchild->d_inode->i_ino,
244                                dchild->d_inode->i_generation);
245                         GOTO(next, rc = -ENOENT);
246                 }
247
248                 child_inode = dchild->d_inode;
249                 MDS_DOWN_READ_ORPHAN_SEM(child_inode);
250                 if (mds_inode_is_orphan(child_inode) &&
251                     mds_orphan_open_count(child_inode)) {
252                         MDS_UP_READ_ORPHAN_SEM(child_inode);
253                         CWARN("%s: orphan %s re-opened during recovery\n",
254                               obd->obd_name, d_name);
255                         GOTO(next, rc = 0);
256                 }
257                 MDS_UP_READ_ORPHAN_SEM(child_inode);
258
259                 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
260                 if (rc == 0) {
261                         item ++;
262                         CDEBUG(D_HA, "%s: removed orphan %s\n",
263                                obd->obd_name, d_name);
264                 } else {
265                         CDEBUG(D_INODE, "%s: removed orphan %s failed,"
266                                " rc = %d\n", obd->obd_name, d_name, rc);
267                         rc = 0;
268                 }
269 next:
270                 l_dput(dchild);
271                 UNLOCK_INODE_MUTEX(pending_dir);
272         }
273         rc = 0;
274 err_out:
275         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
276                 list_del(&dirent->lld_list);
277                 OBD_FREE(dirent, sizeof(*dirent));
278         }
279 err_pop:
280         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
281         if (item > 0)
282                 CWARN("%s: removed %d pending open-unlinked files\n",
283                       obd->obd_name, item);
284         RETURN(rc);
285
286 err_mntget:
287         l_dput(mds->mds_pending_dir);
288         goto err_pop;
289 }