Whamcloud - gitweb
b=2368
[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         CWARN("pending destroy of %dx open file %s = %s\n",
72               mds_open_orphan_count(dchild->d_inode),
73               rec->ur_name, fidname);
74
75         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
76         if (IS_ERR(pending_child))
77                 GOTO(out_lock, rc = PTR_ERR(pending_child));
78
79         if (pending_child->d_inode != NULL) {
80                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
81                 LASSERT(pending_child->d_inode == dchild->d_inode);
82                 GOTO(out_dput, rc = 0);
83         }
84
85         *handle = fsfilt_start(obd, pending_dir, FSFILT_OP_RENAME, NULL);
86         if (IS_ERR(*handle))
87                 GOTO(out_dput, rc = PTR_ERR(*handle));
88
89         lock_kernel();
90         rc = vfs_rename(dparent->d_inode, dchild, pending_dir, pending_child);
91         unlock_kernel();
92         if (rc)
93                 CERROR("error renaming orphan %lu/%s to PENDING: rc = %d\n",
94                        dparent->d_inode->i_ino, rec->ur_name, rc);
95         else
96                 mds_inode_set_orphan(dchild->d_inode);
97 out_dput:
98         dput(pending_child);
99 out_lock:
100         up(&pending_dir->i_sem);
101         RETURN(rc);
102 }
103
104 static int mds_osc_destroy_orphan(struct mds_obd *mds, 
105                                   struct ptlrpc_request *request)
106 {
107         struct mds_body *body;
108         struct lov_mds_md *lmm = NULL;
109         struct lov_stripe_md *lsm = NULL;
110         struct obd_trans_info oti = { 0 };
111         struct obdo *oa;
112         int rc;
113         ENTRY;
114
115         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
116         if (!(body->valid & OBD_MD_FLEASIZE))
117                 RETURN(0);
118         if (body->eadatasize == 0) {
119                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
120                 RETURN(rc = -EPROTO); 
121         }
122
123         lmm = lustre_msg_buf(request->rq_repmsg, 1, body->eadatasize);
124         LASSERT(lmm != NULL);
125
126         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, body->eadatasize);
127         if (rc < 0) {
128                 CERROR("Error unpack md %p\n", lmm);
129                 RETURN(rc);
130         } else {
131                 LASSERT(rc >= sizeof(*lsm));
132                 rc = 0;
133         }
134
135         oa = obdo_alloc();
136         if (oa == NULL)
137                 GOTO(out_free_memmd, rc = -ENOMEM);
138         oa->o_id = lsm->lsm_object_id;
139         oa->o_mode = body->mode & S_IFMT;
140         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
141
142 #ifdef ENABLE_ORPHANS
143         if (body->valid & OBD_MD_FLCOOKIE) {
144                 oa->o_valid |= OBD_MD_FLCOOKIE;
145                 oti.oti_logcookies = 
146                         lustre_msg_buf(request->rq_repmsg, 2,
147                                        sizeof(struct llog_cookie) *
148                                        lsm->lsm_stripe_count);
149                 if (oti.oti_logcookies == NULL)
150                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
151                         body->valid &= ~OBD_MD_FLCOOKIE;
152         }
153 #endif
154
155         rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti);
156         obdo_free(oa);
157         if (rc) 
158                 CERROR("destroy orphan objid 0x"LPX64" on ost error "
159                        "%d\n", lsm->lsm_object_id, rc);
160 out_free_memmd:
161         obd_free_memmd(mds->mds_osc_exp, &lsm);
162         RETURN(rc);
163 }
164
165 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
166                              struct inode *inode, struct inode *pending_dir)
167 {
168         struct mds_obd *mds = &obd->u.mds;
169         struct mds_body *body;
170         void *handle = NULL;
171         struct ptlrpc_request *req;
172         int lengths[3] = {sizeof(struct mds_body),
173                           mds->mds_max_mdsize,
174                           mds->mds_max_cookiesize};
175         int rc;
176         ENTRY;
177
178         LASSERT(mds->mds_osc_obd != NULL);
179         OBD_ALLOC(req, sizeof(*req));
180         if (!req) {
181                 CERROR("request allocation out of memory\n");
182                 GOTO(err_alloc_req, rc = -ENOMEM);
183         }
184         rc = lustre_pack_reply(req, 3, lengths, NULL);
185         if (rc) {
186                 CERROR("cannot pack request %d\n", rc);
187                 GOTO(out_free_req, rc);
188         }
189         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
190         LASSERT(body != NULL);
191
192         mds_pack_inode2body(body, inode);
193         mds_pack_md(obd, req->rq_repmsg, 1, body, inode, 1);
194
195         handle = fsfilt_start(obd, pending_dir, FSFILT_OP_UNLINK_LOG, NULL);
196         if (IS_ERR(handle)) {
197                 rc = PTR_ERR(handle);
198                 CERROR("error fsfilt_start: %d\n", rc);
199                 handle = NULL;
200                 GOTO(out_free_msg, rc);
201         }
202         rc = vfs_unlink(pending_dir, dchild);
203         if (rc) 
204                 CERROR("error %d unlinking orphan %*s from PENDING directory\n",
205                        rc, dchild->d_name.len, dchild->d_name.name);
206
207 #ifdef ENABLE_ORPHANS
208         if ((body->valid & OBD_MD_FLEASIZE)) {
209                 if (mds_log_op_unlink(obd, inode, req->rq_repmsg, 1) > 0)
210                         body->valid |= OBD_MD_FLCOOKIE;
211         }
212 #endif
213         if (handle) {
214                 int err = fsfilt_commit(obd, pending_dir, handle, 0);
215                 if (err) {
216                         CERROR("error committing orphan unlink: %d\n", err);
217                         rc = err;
218                         GOTO(out_free_msg, rc);
219                 }
220         }
221         rc = mds_osc_destroy_orphan(mds, req);
222 out_free_msg:
223         OBD_FREE(req->rq_repmsg, req->rq_replen);
224         req->rq_repmsg = NULL;
225 out_free_req:
226         OBD_FREE(req, sizeof(*req));
227 err_alloc_req:
228         RETURN(rc);
229 }
230
231 int mds_cleanup_orphans(struct obd_device *obd)
232 {
233         struct mds_obd *mds = &obd->u.mds;
234         struct obd_run_ctxt saved;
235         struct file *file;
236         struct dentry *dchild;
237         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
238         struct l_linux_dirent *dirent, *ptr;
239         unsigned int count = pending_dir->i_size;
240         int rc = 0, rc2 = 0, item = 0;
241         ENTRY;
242
243         push_ctxt(&saved, &obd->obd_ctxt, NULL);
244         dget(mds->mds_pending_dir);
245         mntget(mds->mds_vfsmnt);
246         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
247                            O_RDONLY | O_LARGEFILE);
248         if (IS_ERR(file))
249                 GOTO(err_open, rc2 = PTR_ERR(file));
250
251         OBD_ALLOC(dirent, count);
252         if (dirent == NULL)
253                 GOTO(err_alloc_dirent, rc2 = -ENOMEM);
254
255         rc = l_readdir(file, dirent, count);
256         filp_close(file, 0);
257         if (rc < 0)
258                 GOTO(err_out, rc2 = rc);
259
260         for (ptr = dirent; (char *)ptr < (char *)dirent + rc;
261                         (char *)ptr += ptr->d_reclen) {
262                 int namlen = strlen(ptr->d_name);
263
264                 if (((namlen == 1) && !strcmp(ptr->d_name, ".")) ||
265                     ((namlen == 2) && !strcmp(ptr->d_name, "..")))
266                         continue;
267
268                 down(&pending_dir->i_sem);
269                 dchild = lookup_one_len(ptr->d_name, mds->mds_pending_dir,
270                                         namlen);
271                 if (IS_ERR(dchild)) {
272                         up(&pending_dir->i_sem);
273                         GOTO(err_out, rc2 = PTR_ERR(dchild));
274                 }
275                 if (!dchild->d_inode) {
276                         CDEBUG(D_ERROR, "orphan %s has been removed\n",
277                                ptr->d_name);
278                         GOTO(next, rc2 = 0);
279                 }
280
281                 child_inode = dchild->d_inode;
282                 if (mds_inode_is_orphan(child_inode) &&
283                     mds_open_orphan_count(child_inode)) {
284                         CWARN("orphan %s was re-opened during recovery\n", 
285                               ptr->d_name);
286                         GOTO(next, rc2 = 0);
287                 }
288
289                 rc2 = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
290                 if (rc2 == 0) {
291                         item ++;
292                         CWARN("removed orphan %s from MDS and OST\n",
293                                ptr->d_name);
294                 } else {
295                         l_dput(dchild); 
296                         up(&pending_dir->i_sem);
297                         GOTO(err_out, rc2);
298                 }
299 next:
300                 l_dput(dchild);
301                 up(&pending_dir->i_sem);
302         }
303 err_out:
304         OBD_FREE(dirent, count);
305 err_pop:
306         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
307         if (rc2 == 0)
308                 rc2 = item;
309
310         RETURN(rc2);
311
312 err_open:
313         mntput(mds->mds_vfsmnt);
314         l_dput(mds->mds_pending_dir);
315         goto err_pop;
316 err_alloc_dirent:
317         filp_close(file, 0);
318         goto err_pop;
319 }