Whamcloud - gitweb
use special macro for print time_t, cleanup in includes.
[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 #ifndef AUTOCONF_INCLUDED
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         OBDO_ALLOC(oa);
80         if (oa == NULL)
81                 GOTO(out_free_memmd, rc = -ENOMEM);
82         oa->o_id = lsm->lsm_object_id;
83         oa->o_gr = lsm->lsm_object_gr;
84         oa->o_mode = mode & S_IFMT;
85         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
86
87         if (log_unlink && logcookies) {
88                 oa->o_valid |= OBD_MD_FLCOOKIE;
89                 oti.oti_logcookies = logcookies;
90         }
91         rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti, obd->obd_self_export);
92         OBDO_FREE(oa);
93         if (rc)
94                 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
95                        "%d\n", lsm->lsm_object_id, rc);
96 out_free_memmd:
97         obd_free_memmd(mds->mds_osc_exp, &lsm);
98         RETURN(rc);
99 }
100
101 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
102                              struct inode *inode, struct inode *pending_dir)
103 {
104         struct mds_obd *mds = &obd->u.mds;
105         struct lov_mds_md *lmm = NULL;
106         struct llog_cookie *logcookies = NULL;
107         int lmm_size, log_unlink = 0, cookie_size = 0;
108         void *handle = NULL;
109         umode_t mode;
110         int rc, err;
111         ENTRY;
112
113         LASSERT(mds->mds_osc_obd != NULL);
114         
115         /* We don't need to do any of these other things for orhpan dirs,
116          * especially not mds_get_md (may get a default LOV EA, bug 4554) */
117         mode = inode->i_mode;
118         if (S_ISDIR(mode)) {
119                 rc = vfs_rmdir(pending_dir, dchild);
120                 if (rc)
121                         CERROR("error %d unlinking dir %*s from PENDING\n",
122                                rc, dchild->d_name.len, dchild->d_name.name);
123                 RETURN(rc);
124         }
125
126         lmm_size = mds->mds_max_mdsize;
127         OBD_ALLOC(lmm, lmm_size);
128         if (lmm == NULL)
129                 RETURN(-ENOMEM);
130
131         rc = mds_get_md(obd, inode, lmm, &lmm_size, 1);
132         if (rc < 0)
133                 GOTO(out_free_lmm, rc);
134
135         handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
136                                   le32_to_cpu(lmm->lmm_stripe_count));
137         if (IS_ERR(handle)) {
138                 rc = PTR_ERR(handle);
139                 CERROR("error fsfilt_start: %d\n", rc);
140                 handle = NULL;
141                 GOTO(out_free_lmm, rc);
142         }
143
144         rc = vfs_unlink(pending_dir, dchild);
145         if (rc) {
146                 CERROR("error %d unlinking orphan %.*s from PENDING\n",
147                        rc, dchild->d_name.len, dchild->d_name.name);
148         } else if (lmm_size) {
149                 cookie_size = mds_get_cookie_size(obd, lmm); 
150                 OBD_ALLOC(logcookies, cookie_size);
151                 if (logcookies == NULL)
152                         rc = -ENOMEM;
153                 else if (mds_log_op_unlink(obd, lmm,lmm_size,logcookies,
154                                            cookie_size) > 0)
155                         log_unlink = 1;
156         }
157
158         err = fsfilt_commit(obd, pending_dir, handle, 0);
159         if (err) {
160                 CERROR("error committing orphan unlink: %d\n", err);
161                 if (!rc)
162                         rc = err;
163         } else if (!rc) {
164                 rc = mds_osc_destroy_orphan(obd, mode, lmm, lmm_size,
165                                             logcookies, log_unlink);
166         }
167
168         if (logcookies != NULL)
169                 OBD_FREE(logcookies, cookie_size);
170 out_free_lmm:
171         OBD_FREE(lmm, mds->mds_max_mdsize);
172         RETURN(rc);
173 }
174
175 /* Delete inodes which were previously open-unlinked but were not reopened
176  * during MDS recovery for whatever reason (e.g. client also failed, recovery
177  * aborted, etc). */
178 int mds_cleanup_pending(struct obd_device *obd)
179 {
180         struct mds_obd *mds = &obd->u.mds;
181         struct lvfs_run_ctxt saved;
182         struct file *file;
183         struct dentry *dchild, *dentry;
184         struct vfsmount *mnt;
185         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
186         struct l_linux_dirent *dirent, *n;
187         struct list_head dentry_list;
188         char d_name[LL_FID_NAMELEN];
189         unsigned long inum;
190         int i = 0, rc = 0, item = 0, namlen;
191         ENTRY;
192
193         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
194         /* dentry and mnt ref dropped in dentry_open() on error, or
195          * in filp_close() if dentry_open() succeeds */
196         dentry = dget(mds->mds_pending_dir);
197         if (IS_ERR(dentry))
198                 GOTO(err_pop, rc = PTR_ERR(dentry));
199         mnt = mntget(mds->mds_vfsmnt);
200         if (IS_ERR(mnt))
201                 GOTO(err_mntget, rc = PTR_ERR(mnt));
202
203         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
204                            O_RDONLY | O_LARGEFILE);
205         if (IS_ERR(file))
206                 GOTO(err_pop, rc = PTR_ERR(file));
207
208         CFS_INIT_LIST_HEAD(&dentry_list);
209         rc = l_readdir(file, &dentry_list);
210         filp_close(file, 0);
211         if (rc < 0)
212                 GOTO(err_out, rc);
213
214         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
215                 i++;
216                 list_del(&dirent->lld_list);
217
218                 namlen = strlen(dirent->lld_name);
219                 LASSERT(sizeof(d_name) >= namlen + 1);
220                 strcpy(d_name, dirent->lld_name);
221                 inum = dirent->lld_ino;
222                 OBD_FREE(dirent, sizeof(*dirent));
223
224                 CDEBUG(D_INODE, "entry %d of PENDING DIR: %s\n", i, d_name);
225
226                 if (((namlen == 1) && !strcmp(d_name, ".")) ||
227                     ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
228                         continue;
229
230                 LOCK_INODE_MUTEX(pending_dir);
231                 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
232                 if (IS_ERR(dchild)) {
233                         UNLOCK_INODE_MUTEX(pending_dir);
234                         GOTO(err_out, rc = PTR_ERR(dchild));
235                 }
236                 if (!dchild->d_inode) {
237                         CWARN("%s: orphan %s has already been removed\n",
238                               obd->obd_name, d_name);
239                         GOTO(next, rc = 0);
240                 }
241
242                 if (is_bad_inode(dchild->d_inode)) {
243                         CERROR("%s: bad orphan inode found %lu/%u\n",
244                                obd->obd_name, dchild->d_inode->i_ino,
245                                dchild->d_inode->i_generation);
246                         GOTO(next, rc = -ENOENT);
247                 }
248
249                 child_inode = dchild->d_inode;
250                 MDS_DOWN_READ_ORPHAN_SEM(child_inode);
251                 if (mds_inode_is_orphan(child_inode) &&
252                     mds_orphan_open_count(child_inode)) {
253                         MDS_UP_READ_ORPHAN_SEM(child_inode);
254                         CWARN("%s: orphan %s re-opened during recovery\n",
255                               obd->obd_name, d_name);
256                         GOTO(next, rc = 0);
257                 }
258                 MDS_UP_READ_ORPHAN_SEM(child_inode);
259
260                 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
261                 CDEBUG(D_INODE, "%s: removed orphan %s: rc %d\n",
262                        obd->obd_name, d_name, rc);
263                 if (rc == 0)
264                         item++;
265                 else
266                         rc = 0;
267 next:
268                 l_dput(dchild);
269                 UNLOCK_INODE_MUTEX(pending_dir);
270         }
271         rc = 0;
272 err_out:
273         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
274                 list_del(&dirent->lld_list);
275                 OBD_FREE(dirent, sizeof(*dirent));
276         }
277 err_pop:
278         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
279         if (item > 0)
280                 CWARN("%s: removed %d pending open-unlinked files\n",
281                       obd->obd_name, item);
282         RETURN(rc);
283
284 err_mntget:
285         l_dput(mds->mds_pending_dir);
286         goto err_pop;
287 }