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
203         if (S_ISDIR(inode->i_mode)) {
204                 rc = vfs_rmdir(pending_dir, dchild);
205         } else {
206                 rc = vfs_unlink(pending_dir, dchild);
207         }
208         if (rc) 
209                 CERROR("error %d unlinking orphan %*s from PENDING directory\n",
210                        rc, dchild->d_name.len, dchild->d_name.name);
211
212 #ifdef ENABLE_ORPHANS
213         if ((body->valid & OBD_MD_FLEASIZE)) {
214                 if (mds_log_op_unlink(obd, inode, req->rq_repmsg, 1) > 0)
215                         body->valid |= OBD_MD_FLCOOKIE;
216         }
217 #endif
218         if (handle) {
219                 int err = fsfilt_commit(obd, pending_dir, handle, 0);
220                 if (err) {
221                         CERROR("error committing orphan unlink: %d\n", err);
222                         rc = err;
223                         GOTO(out_free_msg, rc);
224                 }
225         }
226         rc = mds_osc_destroy_orphan(mds, req);
227 out_free_msg:
228         OBD_FREE(req->rq_repmsg, req->rq_replen);
229         req->rq_repmsg = NULL;
230 out_free_req:
231         OBD_FREE(req, sizeof(*req));
232 err_alloc_req:
233         RETURN(rc);
234 }
235
236 int mds_cleanup_orphans(struct obd_device *obd)
237 {
238         struct mds_obd *mds = &obd->u.mds;
239         struct obd_run_ctxt saved;
240         struct file *file;
241         struct dentry *dchild;
242         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
243         struct l_linux_dirent *dirent, *ptr;
244         unsigned int count = pending_dir->i_size;
245         int rc = 0, rc2 = 0, item = 0;
246         ENTRY;
247
248         push_ctxt(&saved, &obd->obd_ctxt, NULL);
249         dget(mds->mds_pending_dir);
250         mntget(mds->mds_vfsmnt);
251         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
252                            O_RDONLY | O_LARGEFILE);
253         if (IS_ERR(file))
254                 GOTO(err_open, rc2 = PTR_ERR(file));
255
256         OBD_ALLOC(dirent, count);
257         if (dirent == NULL)
258                 GOTO(err_alloc_dirent, rc2 = -ENOMEM);
259
260         rc = l_readdir(file, dirent, count);
261         filp_close(file, 0);
262         if (rc < 0)
263                 GOTO(err_out, rc2 = rc);
264
265         for (ptr = dirent; (char *)ptr < (char *)dirent + rc;
266                         (char *)ptr += ptr->d_reclen) {
267                 int namlen = strlen(ptr->d_name);
268
269                 if (((namlen == 1) && !strcmp(ptr->d_name, ".")) ||
270                     ((namlen == 2) && !strcmp(ptr->d_name, "..")))
271                         continue;
272
273                 down(&pending_dir->i_sem);
274                 dchild = lookup_one_len(ptr->d_name, mds->mds_pending_dir,
275                                         namlen);
276                 if (IS_ERR(dchild)) {
277                         up(&pending_dir->i_sem);
278                         GOTO(err_out, rc2 = PTR_ERR(dchild));
279                 }
280                 if (!dchild->d_inode) {
281                         CDEBUG(D_ERROR, "orphan %s has been removed\n",
282                                ptr->d_name);
283                         GOTO(next, rc2 = 0);
284                 }
285
286                 child_inode = dchild->d_inode;
287                 if (mds_inode_is_orphan(child_inode) &&
288                     mds_open_orphan_count(child_inode)) {
289                         CWARN("orphan %s was re-opened during recovery\n", 
290                               ptr->d_name);
291                         GOTO(next, rc2 = 0);
292                 }
293
294                 rc2 = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
295                 if (rc2 == 0) {
296                         item ++;
297                         CWARN("removed orphan %s from MDS and OST\n",
298                                ptr->d_name);
299                 } else {
300                         l_dput(dchild); 
301                         up(&pending_dir->i_sem);
302                         GOTO(err_out, rc2);
303                 }
304 next:
305                 l_dput(dchild);
306                 up(&pending_dir->i_sem);
307         }
308 err_out:
309         OBD_FREE(dirent, count);
310 err_pop:
311         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
312         if (rc2 == 0)
313                 rc2 = item;
314
315         RETURN(rc2);
316
317 err_open:
318         mntput(mds->mds_vfsmnt);
319         l_dput(mds->mds_pending_dir);
320         goto err_pop;
321 err_alloc_dirent:
322         filp_close(file, 0);
323         goto err_pop;
324 }