Whamcloud - gitweb
Branch: b_new_cmd
[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 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/version.h>
37
38 #include <libcfs/list.h>
39 #include <obd_class.h>
40 #include <lustre_fsfilt.h>
41 #include <lustre_mds.h>
42 #include <lustre_commit_confd.h>
43 #include <lvfs.h>
44
45 #include "mds_internal.h"
46
47 static int mds_osc_destroy_orphan(struct obd_device *obd,
48                                   struct inode *inode,
49                                   struct lov_mds_md *lmm,
50                                   int lmm_size,
51                                   struct llog_cookie *logcookies,
52                                   int log_unlink)
53 {
54         struct mds_obd *mds = &obd->u.mds;
55         struct lov_stripe_md *lsm = NULL;
56         struct obd_trans_info oti = { 0 };
57         struct obdo *oa;
58         int rc;
59         ENTRY;
60
61         if (lmm_size == 0)
62                 RETURN(0);
63
64         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
65         if (rc < 0) {
66                 CERROR("Error unpack md %p\n", lmm);
67                 RETURN(rc);
68         } else {
69                 LASSERT(rc >= sizeof(*lsm));
70                 rc = 0;
71         }
72
73         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
74         if (rc)
75                 GOTO(out_free_memmd, rc);
76
77         oa = obdo_alloc();
78         if (oa == NULL)
79                 GOTO(out_free_memmd, rc = -ENOMEM);
80         oa->o_id = lsm->lsm_object_id;
81         oa->o_mode = inode->i_mode & S_IFMT;
82         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
83
84         if (log_unlink && logcookies) {
85                 oa->o_valid |= OBD_MD_FLCOOKIE;
86                 oti.oti_logcookies = logcookies;
87         }
88         rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti, obd->obd_self_export);
89         obdo_free(oa);
90         if (rc)
91                 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
92                        "%d\n", lsm->lsm_object_id, rc);
93 out_free_memmd:
94         obd_free_memmd(mds->mds_osc_exp, &lsm);
95         RETURN(rc);
96 }
97
98 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
99                              struct inode *inode, struct inode *pending_dir)
100 {
101         struct mds_obd *mds = &obd->u.mds;
102         struct lov_mds_md *lmm = NULL;
103         struct llog_cookie *logcookies = NULL;
104         int lmm_size, log_unlink = 0, cookie_size = 0;
105         void *handle = NULL;
106         int rc, err;
107         ENTRY;
108
109         LASSERT(mds->mds_osc_obd != NULL);
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);
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                 cookie_size = mds_get_cookie_size(obd, lmm); 
145                 OBD_ALLOC(logcookies, cookie_size);
146                 if (logcookies == NULL)
147                         rc = -ENOMEM;
148                 else if (mds_log_op_unlink(obd, inode, lmm,lmm_size,logcookies,
149                                            cookie_size) > 0)
150                         log_unlink = 1;
151         }
152
153         err = fsfilt_commit(obd, pending_dir, handle, 0);
154         if (err) {
155                 CERROR("error committing orphan unlink: %d\n", err);
156                 if (!rc)
157                         rc = err;
158         } else if (!rc) {
159                 rc = mds_osc_destroy_orphan(obd, inode, lmm, lmm_size,
160                                             logcookies, log_unlink);
161         }
162
163         if (logcookies != NULL)
164                 OBD_FREE(logcookies, cookie_size);
165 out_free_lmm:
166         OBD_FREE(lmm, mds->mds_max_mdsize);
167         RETURN(rc);
168 }
169
170 /* Delete inodes which were previously open-unlinked but were not reopened
171  * during MDS recovery for whatever reason (e.g. client also failed, recovery
172  * aborted, etc). */
173 int mds_cleanup_pending(struct obd_device *obd)
174 {
175         struct mds_obd *mds = &obd->u.mds;
176         struct lvfs_run_ctxt saved;
177         struct file *file;
178         struct dentry *dchild, *dentry;
179         struct vfsmount *mnt;
180         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
181         struct l_linux_dirent *dirent, *n;
182         struct list_head dentry_list;
183         char d_name[LL_FID_NAMELEN];
184         unsigned long inum;
185         int i = 0, rc = 0, item = 0, namlen;
186         ENTRY;
187
188         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
189         /* dentry and mnt ref dropped in dentry_open() on error, or
190          * in filp_close() if dentry_open() succeeds */
191         dentry = dget(mds->mds_pending_dir);
192         if (IS_ERR(dentry))
193                 GOTO(err_pop, rc = PTR_ERR(dentry));
194         mnt = mntget(mds->mds_vfsmnt);
195         if (IS_ERR(mnt))
196                 GOTO(err_mntget, rc = PTR_ERR(mnt));
197
198         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
199                            O_RDONLY | O_LARGEFILE);
200         if (IS_ERR(file))
201                 GOTO(err_pop, rc = PTR_ERR(file));
202
203         INIT_LIST_HEAD(&dentry_list);
204         rc = l_readdir(file, &dentry_list);
205         filp_close(file, 0);
206         if (rc < 0)
207                 GOTO(err_out, rc);
208
209         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
210                 i++;
211                 list_del(&dirent->lld_list);
212
213                 namlen = strlen(dirent->lld_name);
214                 LASSERT(sizeof(d_name) >= namlen + 1);
215                 strcpy(d_name, dirent->lld_name);
216                 inum = dirent->lld_ino;
217                 OBD_FREE(dirent, sizeof(*dirent));
218
219                 CDEBUG(D_INODE, "entry %d of PENDING DIR: %s\n", i, d_name);
220
221                 if (((namlen == 1) && !strcmp(d_name, ".")) ||
222                     ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
223                         continue;
224
225                 LOCK_INODE_MUTEX(pending_dir);
226                 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
227                 if (IS_ERR(dchild)) {
228                         UNLOCK_INODE_MUTEX(pending_dir);
229                         GOTO(err_out, rc = PTR_ERR(dchild));
230                 }
231                 if (!dchild->d_inode) {
232                         CWARN("%s: orphan %s has already been removed\n",
233                               obd->obd_name, d_name);
234                         GOTO(next, rc = 0);
235                 }
236
237                 if (is_bad_inode(dchild->d_inode)) {
238                         CERROR("%s: bad orphan inode found %lu/%u\n",
239                                obd->obd_name, dchild->d_inode->i_ino,
240                                dchild->d_inode->i_generation);
241                         GOTO(next, rc = -ENOENT);
242                 }
243
244                 child_inode = dchild->d_inode;
245                 MDS_DOWN_READ_ORPHAN_SEM(child_inode);
246                 if (mds_inode_is_orphan(child_inode) &&
247                     mds_orphan_open_count(child_inode)) {
248                         MDS_UP_READ_ORPHAN_SEM(child_inode);
249                         CWARN("%s: orphan %s re-opened during recovery\n",
250                               obd->obd_name, d_name);
251                         GOTO(next, rc = 0);
252                 }
253                 MDS_UP_READ_ORPHAN_SEM(child_inode);
254
255                 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
256                 if (rc == 0) {
257                         item ++;
258                         CDEBUG(D_HA, "%s: removed orphan %s\n",
259                                obd->obd_name, d_name);
260                 } else {
261                         CDEBUG(D_INODE, "%s: removed orphan %s failed,"
262                                " rc = %d\n", obd->obd_name, d_name, rc);
263                         rc = 0;
264                 }
265 next:
266                 l_dput(dchild);
267                 UNLOCK_INODE_MUTEX(pending_dir);
268         }
269         rc = 0;
270 err_out:
271         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
272                 list_del(&dirent->lld_list);
273                 OBD_FREE(dirent, sizeof(*dirent));
274         }
275 err_pop:
276         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
277         if (item > 0)
278                 CWARN("%s: removed %d pending open-unlinked files\n",
279                       obd->obd_name, item);
280         RETURN(rc);
281
282 err_mntget:
283         l_dput(mds->mds_pending_dir);
284         goto err_pop;
285 }