Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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 <portals/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 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
45  * we instead link the inode into the PENDING directory until it is
46  * finally released.  We can't simply call mds_reint_rename() or some
47  * part thereof, because we don't have the inode to check for link
48  * count/open status until after it is locked.
49  *
50  * For lock ordering, we always get the PENDING, then pending_child lock
51  * last to avoid deadlocks.
52  */
53
54 int mds_open_unlink_rename(struct mds_update_record *rec,
55                            struct obd_device *obd, struct dentry *dparent,
56                            struct dentry *dchild, void **handle)
57 {
58         struct mds_obd *mds = &obd->u.mds;
59         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
60         struct dentry *pending_child;
61         char fidname[LL_FID_NAMELEN];
62         int fidlen = 0, rc;
63         ENTRY;
64
65         LASSERT(!mds_inode_is_orphan(dchild->d_inode));
66
67         down(&pending_dir->i_sem);
68         fidlen = ll_fid2str(fidname, dchild->d_inode->i_ino,
69                             dchild->d_inode->i_generation);
70
71         CDEBUG(D_HA, "pending destroy of %dx open file %s = %s\n",
72                mds_open_orphan_count(dchild->d_inode), rec->ur_name, fidname);
73
74         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
75         if (IS_ERR(pending_child))
76                 GOTO(out_lock, rc = PTR_ERR(pending_child));
77
78         if (pending_child->d_inode != NULL) {
79                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
80                 LASSERT(pending_child->d_inode == dchild->d_inode);
81                 GOTO(out_dput, rc = 0);
82         }
83
84         *handle = fsfilt_start(obd, pending_dir, FSFILT_OP_RENAME, NULL);
85         if (IS_ERR(*handle))
86                 GOTO(out_dput, rc = PTR_ERR(*handle));
87
88         lock_kernel();
89         rc = vfs_rename(dparent->d_inode, dchild, pending_dir, pending_child);
90         unlock_kernel();
91         if (rc)
92                 CERROR("error renaming orphan %lu/%s to PENDING: rc = %d\n",
93                        dparent->d_inode->i_ino, rec->ur_name, rc);
94         else
95                 mds_inode_set_orphan(dchild->d_inode);
96 out_dput:
97         dput(pending_child);
98 out_lock:
99         up(&pending_dir->i_sem);
100         RETURN(rc);
101 }
102
103 static int mds_osc_destroy_orphan(struct mds_obd *mds,
104                                   struct inode *inode,
105                                   struct lov_mds_md *lmm,
106                                   int lmm_size,
107                                   struct llog_cookie *logcookies,
108                                   int log_unlink)
109 {
110         struct lov_stripe_md *lsm = NULL;
111         struct obd_trans_info oti = { 0 };
112         struct obdo *oa;
113         int rc;
114         ENTRY;
115
116         if (lmm_size == 0)
117                 RETURN(0);
118
119         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
120         if (rc < 0) {
121                 CERROR("Error unpack md %p\n", lmm);
122                 RETURN(rc);
123         } else {
124                 LASSERT(rc >= sizeof(*lsm));
125                 rc = 0;
126         }
127
128         oa = obdo_alloc();
129         if (oa == NULL)
130                 GOTO(out_free_memmd, rc = -ENOMEM);
131         oa->o_id = lsm->lsm_object_id;
132         oa->o_mode = inode->i_mode & S_IFMT;
133         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
134
135         if (log_unlink && logcookies) {
136                 oa->o_valid |= OBD_MD_FLCOOKIE;
137                 oti.oti_logcookies = logcookies;
138         }
139
140         rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti);
141         obdo_free(oa);
142         if (rc)
143                 CERROR("destroy orphan objid 0x"LPX64" on ost error "
144                        "%d\n", lsm->lsm_object_id, rc);
145 out_free_memmd:
146         obd_free_memmd(mds->mds_osc_exp, &lsm);
147         RETURN(rc);
148 }
149
150 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
151                              struct inode *inode, struct inode *pending_dir)
152 {
153         struct mds_obd *mds = &obd->u.mds;
154         struct lov_mds_md *lmm = NULL;
155         struct llog_cookie *logcookies = NULL;
156         int lmm_size = 0, log_unlink = 0;
157         void *handle = NULL;
158         int rc, err;
159         ENTRY;
160
161         LASSERT(mds->mds_osc_obd != NULL);
162
163         OBD_ALLOC(lmm, mds->mds_max_mdsize);
164         if (lmm == NULL)
165                 RETURN(-ENOMEM);
166
167         down(&inode->i_sem);
168         rc = fsfilt_get_md(obd, inode, lmm, mds->mds_max_mdsize);
169         up(&inode->i_sem);
170
171         if (rc < 0) {
172                 CERROR("Error %d reading eadata for ino %lu\n",
173                        rc, inode->i_ino);
174                 GOTO(out_free_lmm, rc);
175         } else if (rc > 0) {
176                 lmm_size = rc;
177                 rc = mds_convert_lov_ea(obd, inode, lmm, lmm_size);
178                 if (rc > 0)
179                         lmm_size = rc;
180                 rc = 0;
181         }
182
183         handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
184                                   le32_to_cpu(lmm->lmm_stripe_count));
185         if (IS_ERR(handle)) {
186                 rc = PTR_ERR(handle);
187                 CERROR("error fsfilt_start: %d\n", rc);
188                 handle = NULL;
189                 GOTO(out_free_lmm, rc);
190         }
191
192         down(&inode->i_sem);
193         rc = fsfilt_get_md(obd, inode, lmm, mds->mds_max_mdsize);
194         up(&inode->i_sem);
195
196         if (rc < 0) {
197                 CERROR("Error %d reading eadata for ino %lu\n",
198                        rc, inode->i_ino);
199                 GOTO(out_free_lmm, rc);
200         } else if (rc > 0) {
201                 lmm_size = rc;
202                 rc = 0;
203         }
204
205         if (S_ISDIR(inode->i_mode))
206                 rc = vfs_rmdir(pending_dir, dchild);
207         else
208                 rc = vfs_unlink(pending_dir, dchild);
209
210         if (rc)
211                 CERROR("error %d unlinking orphan %*s from PENDING directory\n",
212                        rc, dchild->d_name.len, dchild->d_name.name);
213
214         if (!rc && lmm_size) {
215                 OBD_ALLOC(logcookies, mds->mds_max_cookiesize);
216                 if (logcookies == NULL)
217                         rc = -ENOMEM;
218                 else if (mds_log_op_unlink(obd, inode, lmm,lmm_size,logcookies,
219                                            mds->mds_max_cookiesize) > 0)
220                         log_unlink = 1;
221         }
222         err = fsfilt_commit(obd, pending_dir, handle, 0);
223         if (err) {
224                 CERROR("error committing orphan unlink: %d\n", err);
225                 if (!rc)
226                         rc = err;
227         }
228         if (!rc) {
229                 rc = mds_osc_destroy_orphan(mds, inode, lmm, lmm_size,
230                                             logcookies, log_unlink);
231         }
232
233         if (logcookies != NULL)
234                 OBD_FREE(logcookies, mds->mds_max_cookiesize);
235 out_free_lmm:
236         OBD_FREE(lmm, mds->mds_max_mdsize);
237         RETURN(rc);
238 }
239
240 int mds_cleanup_orphans(struct obd_device *obd)
241 {
242         struct mds_obd *mds = &obd->u.mds;
243         struct obd_run_ctxt saved;
244         struct file *file;
245         struct dentry *dchild, *dentry;
246         struct vfsmount *mnt;
247         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
248         struct l_linux_dirent *dirent, *n;
249         struct list_head dentry_list;
250         char d_name[LL_FID_NAMELEN];
251         __u64 i = 0;
252         int rc = 0, item = 0, namlen;
253         ENTRY;
254
255         push_ctxt(&saved, &obd->obd_ctxt, NULL);
256         dentry = dget(mds->mds_pending_dir);
257         if (IS_ERR(dentry))
258                 GOTO(err_pop, rc = PTR_ERR(dentry));
259         mnt = mntget(mds->mds_vfsmnt);
260         if (IS_ERR(mnt))
261                 GOTO(err_mntget, rc = PTR_ERR(mnt));
262
263         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
264                            O_RDONLY | O_LARGEFILE);
265         if (IS_ERR(file))
266                 GOTO(err_pop, rc = PTR_ERR(file));
267
268         INIT_LIST_HEAD(&dentry_list);
269         rc = l_readdir(file, &dentry_list);
270         filp_close(file, 0);
271         if (rc < 0)
272                 GOTO(err_out, rc);
273
274         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
275                 i ++;
276                 list_del(&dirent->lld_list);
277
278                 namlen = strlen(dirent->lld_name);
279                 LASSERT(sizeof(d_name) >= namlen + 1);
280                 strcpy(d_name, dirent->lld_name);
281                 OBD_FREE(dirent, sizeof(*dirent));
282
283                 CDEBUG(D_INODE, "entry "LPU64" of PENDING DIR: %s\n",
284                        i, d_name);
285
286                 if (((namlen == 1) && !strcmp(d_name, ".")) ||
287                     ((namlen == 2) && !strcmp(d_name, ".."))) {
288                         continue;
289                 }
290
291                 down(&pending_dir->i_sem);
292                 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
293                 if (IS_ERR(dchild)) {
294                         up(&pending_dir->i_sem);
295                         GOTO(err_out, rc = PTR_ERR(dchild));
296                 }
297                 if (!dchild->d_inode) {
298                         CERROR("orphan %s has been removed\n", d_name);
299                         GOTO(next, rc = 0);
300                 }
301
302                 child_inode = dchild->d_inode;
303                 if (mds_inode_is_orphan(child_inode) &&
304                     mds_open_orphan_count(child_inode)) {
305                         CWARN("orphan %s was re-opened during recovery\n", d_name);
306                         GOTO(next, rc = 0);
307                 }
308
309                 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
310                 if (rc == 0) {
311                         item ++;
312                         CWARN("removed orphan %s from MDS and OST\n", d_name);
313                 } else {
314                         CERROR("removed orphan %s from MDS and OST failed,"
315                                " rc = %d\n", d_name, rc);
316                         rc = 0;
317                 }
318 next:
319                 l_dput(dchild);
320                 up(&pending_dir->i_sem);
321         }
322 err_out:
323         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
324                 list_del(&dirent->lld_list);
325                 OBD_FREE(dirent, sizeof(*dirent));
326         }
327 err_pop:
328         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
329         if (rc == 0)
330                 rc = item;
331         RETURN(rc);
332
333 err_mntget:
334         l_dput(mds->mds_pending_dir);
335         goto err_pop;
336 }