Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mds/mds_unlink_open.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  * Author: Phil Schwan <phil@clusterfs.com>
41  */
42
43 /* code for handling open unlinked files */
44
45 #define DEBUG_SUBSYSTEM S_MDS
46
47 #ifndef AUTOCONF_INCLUDED
48 #include <linux/config.h>
49 #endif
50 #include <linux/module.h>
51 #include <linux/version.h>
52
53 #include <libcfs/list.h>
54 #include <obd_class.h>
55 #include <lustre_fsfilt.h>
56 #include <lustre_mds.h>
57 #include <lvfs.h>
58
59 #include "mds_internal.h"
60
61 int mds_osc_destroy_orphan(struct obd_device *obd,
62                            umode_t mode,
63                            struct lov_mds_md *lmm,
64                            int lmm_size,
65                            struct llog_cookie *logcookies,
66                            int log_unlink)
67 {
68         struct mds_obd *mds = &obd->u.mds;
69         struct lov_stripe_md *lsm = NULL;
70         struct obd_trans_info oti = { 0 };
71         struct obdo *oa;
72         int rc;
73         ENTRY;
74
75         if (lmm_size == 0)
76                 RETURN(0);
77
78         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
79         if (rc < 0) {
80                 CERROR("Error unpack md %p\n", lmm);
81                 RETURN(rc);
82         } else {
83                 LASSERT(rc >= sizeof(*lsm));
84                 rc = 0;
85         }
86
87         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
88         if (rc)
89                 GOTO(out_free_memmd, rc);
90
91         OBDO_ALLOC(oa);
92         if (oa == NULL)
93                 GOTO(out_free_memmd, rc = -ENOMEM);
94         oa->o_id = lsm->lsm_object_id;
95         oa->o_mode = mode & S_IFMT;
96         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
97
98         if (log_unlink && logcookies) {
99                 oa->o_valid |= OBD_MD_FLCOOKIE;
100                 oti.oti_logcookies = logcookies;
101         }
102         rc = obd_destroy(mds->mds_osc_exp, oa, lsm, &oti, obd->obd_self_export);
103         OBDO_FREE(oa);
104         if (rc)
105                 CDEBUG(D_INODE, "destroy orphan objid 0x"LPX64" on ost error "
106                        "%d\n", lsm->lsm_object_id, rc);
107 out_free_memmd:
108         obd_free_memmd(mds->mds_osc_exp, &lsm);
109         RETURN(rc);
110 }
111
112 static int mds_unlink_orphan(struct obd_device *obd, struct dentry *dchild,
113                              struct inode *inode, struct inode *pending_dir)
114 {
115         struct mds_obd *mds = &obd->u.mds;
116         struct lov_mds_md *lmm = NULL;
117         struct llog_cookie *logcookies = NULL;
118         int lmm_size, log_unlink = 0, cookie_size = 0;
119         void *handle = NULL;
120         umode_t mode;
121         int rc, err;
122         ENTRY;
123
124         LASSERT(mds->mds_osc_obd != NULL);
125
126         /* We don't need to do any of these other things for orhpan dirs,
127          * especially not mds_get_md (may get a default LOV EA, bug 4554) */
128         mode = inode->i_mode;
129         if (S_ISDIR(mode)) {
130                 rc = ll_vfs_rmdir(pending_dir, dchild, mds->mds_vfsmnt);
131                 if (rc)
132                         CERROR("error %d unlinking dir %*s from PENDING\n",
133                                rc, dchild->d_name.len, dchild->d_name.name);
134                 RETURN(rc);
135         }
136
137         lmm_size = mds->mds_max_mdsize;
138         OBD_ALLOC(lmm, lmm_size);
139         if (lmm == NULL)
140                 RETURN(-ENOMEM);
141
142         rc = mds_get_md(obd, inode, lmm, &lmm_size, 1, 0, 0);
143         if (rc < 0)
144                 GOTO(out_free_lmm, rc);
145
146         handle = fsfilt_start_log(obd, pending_dir, FSFILT_OP_UNLINK, NULL,
147                                   le32_to_cpu(lmm->lmm_stripe_count));
148         if (IS_ERR(handle)) {
149                 rc = PTR_ERR(handle);
150                 CERROR("error fsfilt_start: %d\n", rc);
151                 handle = NULL;
152                 GOTO(out_free_lmm, rc);
153         }
154
155         rc = ll_vfs_unlink(pending_dir, dchild, mds->mds_vfsmnt);
156         if (rc) {
157                 CERROR("error %d unlinking orphan %.*s from PENDING\n",
158                        rc, dchild->d_name.len, dchild->d_name.name);
159         } else if (lmm_size) {
160                 cookie_size = mds_get_cookie_size(obd, lmm); 
161                 OBD_ALLOC(logcookies, cookie_size);
162                 if (logcookies == NULL)
163                         rc = -ENOMEM;
164                 else if (mds_log_op_unlink(obd, lmm,lmm_size,logcookies,
165                                            cookie_size) > 0)
166                         log_unlink = 1;
167         }
168
169         err = fsfilt_commit(obd, pending_dir, handle, 0);
170         if (err) {
171                 CERROR("error committing orphan unlink: %d\n", err);
172                 if (!rc)
173                         rc = err;
174         } else if (!rc) {
175                 rc = mds_osc_destroy_orphan(obd, mode, lmm, lmm_size,
176                                             logcookies, log_unlink);
177         }
178
179         if (logcookies != NULL)
180                 OBD_FREE(logcookies, cookie_size);
181 out_free_lmm:
182         OBD_FREE(lmm, mds->mds_max_mdsize);
183         RETURN(rc);
184 }
185
186 static __u64 mds_orphans_max_version(struct obd_device *obd)
187 {
188         struct obd_export *exp;
189         __u32 epoch = lr_epoch(obd->u.mds.mds_last_transno);
190         spin_lock(&obd->obd_dev_lock);
191         list_for_each_entry(exp, &obd->obd_delayed_exports, exp_obd_chain) {
192                 struct lu_export_data *led = &exp->exp_target_data;
193                 epoch = min(epoch, le32_to_cpu(led->led_lcd->lcd_first_epoch));
194         }
195         spin_unlock(&obd->obd_dev_lock);
196         return (__u64)epoch << LR_EPOCH_BITS;
197 }
198
199 /* Delete inodes which were previously open-unlinked but were not reopened
200  * during MDS recovery for whatever reason (e.g. client also failed, recovery
201  * aborted, etc). */
202 int mds_cleanup_pending(struct obd_device *obd)
203 {
204         struct mds_obd *mds = &obd->u.mds;
205         struct lvfs_run_ctxt saved;
206         struct file *file;
207         struct dentry *dchild, *dentry;
208         struct vfsmount *mnt;
209         struct inode *child_inode, *pending_dir = mds->mds_pending_dir->d_inode;
210         struct l_linux_dirent *dirent, *n;
211         struct list_head dentry_list;
212         char d_name[LL_FID_NAMELEN];
213         unsigned long inum;
214         __u64 max_version;
215         int i = 0, rc = 0, item = 0, namlen;
216         ENTRY;
217
218         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
219         /* dentry and mnt ref dropped in dentry_open() on error, or
220          * in filp_close() if dentry_open() succeeds */
221         dentry = dget(mds->mds_pending_dir);
222         if (IS_ERR(dentry))
223                 GOTO(err_pop, rc = PTR_ERR(dentry));
224         mnt = mntget(mds->mds_vfsmnt);
225         if (IS_ERR(mnt))
226                 GOTO(err_mntget, rc = PTR_ERR(mnt));
227
228         file = dentry_open(mds->mds_pending_dir, mds->mds_vfsmnt,
229                            O_RDONLY | O_LARGEFILE);
230         if (IS_ERR(file))
231                 GOTO(err_pop, rc = PTR_ERR(file));
232
233         CFS_INIT_LIST_HEAD(&dentry_list);
234         rc = l_readdir(file, &dentry_list);
235         filp_close(file, 0);
236         if (rc < 0)
237                 GOTO(err_out, rc);
238
239         /** Get maximum version for orphans to delete. All other orphans may be
240          *  needed for delayed clients */
241         max_version = mds_orphans_max_version(obd);
242
243         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
244                 __u64 version;
245
246                 i++;
247                 list_del(&dirent->lld_list);
248
249                 namlen = strlen(dirent->lld_name);
250                 LASSERT(sizeof(d_name) >= namlen + 1);
251                 strcpy(d_name, dirent->lld_name);
252                 inum = dirent->lld_ino;
253                 OBD_FREE_PTR(dirent);
254
255                 CDEBUG(D_INODE, "entry %d of PENDING DIR: %s\n", i, d_name);
256
257                 if (((namlen == 1) && !strcmp(d_name, ".")) ||
258                     ((namlen == 2) && !strcmp(d_name, "..")) || inum == 0)
259                         continue;
260
261                 LOCK_INODE_MUTEX(pending_dir);
262                 dchild = lookup_one_len(d_name, mds->mds_pending_dir, namlen);
263                 if (IS_ERR(dchild)) {
264                         UNLOCK_INODE_MUTEX(pending_dir);
265                         GOTO(err_out, rc = PTR_ERR(dchild));
266                 }
267                 if (!dchild->d_inode) {
268                         CWARN("%s: orphan %s has already been removed\n",
269                               obd->obd_name, d_name);
270                         GOTO(next, rc = 0);
271                 }
272
273                 if (is_bad_inode(dchild->d_inode)) {
274                         CERROR("%s: bad orphan inode found %lu/%u\n",
275                                obd->obd_name, dchild->d_inode->i_ino,
276                                dchild->d_inode->i_generation);
277                         GOTO(next, rc = -ENOENT);
278                 }
279
280                 child_inode = dchild->d_inode;
281                 MDS_DOWN_READ_ORPHAN_SEM(child_inode);
282                 if (mds_inode_is_orphan(child_inode) &&
283                     mds_orphan_open_count(child_inode)) {
284                         MDS_UP_READ_ORPHAN_SEM(child_inode);
285                         CWARN("%s: orphan %s re-opened during recovery\n",
286                               obd->obd_name, d_name);
287                         GOTO(next, rc = 0);
288                 }
289                 /** Keep orphans for possible use by delayed exports. Remove
290                  * orphans with version lower than minimal one of all exports */
291                 version = fsfilt_get_version(obd, child_inode);
292                 if ((__s64)version != -EOPNOTSUPP &&
293                     version >= max_version) {
294                         MDS_UP_READ_ORPHAN_SEM(child_inode);
295                         CDEBUG(D_INFO,
296                                "%s: orphan %s is needed for delayed exports\n",
297                                obd->obd_name, d_name);
298                         GOTO(next, rc = 0);
299                 }
300                 MDS_UP_READ_ORPHAN_SEM(child_inode);
301
302                 rc = mds_unlink_orphan(obd, dchild, child_inode, pending_dir);
303                 CDEBUG(D_INODE, "%s: removed orphan %s: rc %d\n",
304                        obd->obd_name, d_name, rc);
305                 if (rc == 0)
306                         item++;
307                 else
308                         rc = 0;
309 next:
310                 l_dput(dchild);
311                 UNLOCK_INODE_MUTEX(pending_dir);
312         }
313         rc = 0;
314 err_out:
315         list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
316                 list_del(&dirent->lld_list);
317                 OBD_FREE(dirent, sizeof(*dirent));
318         }
319 err_pop:
320         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
321         if (item > 0)
322                 CWARN("%s: removed %d pending open-unlinked files\n",
323                       obd->obd_name, item);
324         RETURN(rc);
325
326 err_mntget:
327         l_dput(mds->mds_pending_dir);
328         goto err_pop;
329 }
330
331 /**
332  * Determine there is no orphan with the same inode number. That may happens
333  * since unlink replay don't delete inode but keep orphan for delayed clients.
334  * Therefore replays like 'create, unlink, create' will fail due to inode can't
335  * be reused.
336  */
337 int mds_check_stale_orphan(struct obd_device *obd, struct ll_fid *fid)
338 {
339         struct mds_obd *mds = &obd->u.mds;
340         char fidname[32];
341         struct dentry *result;
342         struct inode *inode, *pending_dir = mds->mds_pending_dir->d_inode;
343         int fidlen = 0, rc = 0;
344
345         /* no need in checks*/
346         if (fid->id == 0 || obd->obd_recovering == 0)
347                 RETURN(0);
348
349         /** open by fid like mds_fid2dentry does */
350         snprintf(fidname, sizeof(fidname), "0x%lx", (unsigned long)(fid->id));
351         fidlen = strlen(fidname);
352         result = mds_lookup(obd, fidname, mds->mds_fid_de, fidlen);
353         if (IS_ERR(result))
354                 RETURN(0);
355         inode = result->d_inode;
356         if (!inode)
357                 GOTO(out, rc = 0);
358
359         LOCK_INODE_MUTEX(pending_dir);
360         MDS_DOWN_READ_ORPHAN_SEM(inode);
361         if (mds_inode_is_orphan(inode)) {
362                 struct dentry *orphan;
363
364                 /* The exactly same inode can't be orphan */
365                 LASSERT(inode->i_generation != fid->generation);
366
367                 if (mds_orphan_open_count(inode) > 0) {
368                         CERROR("Orphan "LPU64"/%u is in use!\n",
369                                fid->id, fid->generation);
370                         GOTO(unlock_child, rc = -EFAULT);
371                 }
372
373                 /** Found orphan in pending dir and delete it */
374                 fidlen = ll_fid2str(fidname, fid->id, inode->i_generation);
375                 orphan = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
376                 if (IS_ERR(orphan)) {
377                         rc = PTR_ERR(orphan);
378                         CERROR("error looking up %s in PENDING: rc = %d\n",
379                                 fidname, rc);
380                         GOTO(unlock_child, rc);
381                 }
382                 if (orphan->d_inode != inode) {
383                         l_dput(orphan);
384                         CWARN("%s: Found wrong orphan %s %p/%p\n",
385                               obd->obd_name, fidname, orphan->d_inode, inode);
386                         GOTO(unlock_child, rc = -EFAULT);
387                 }
388                 MDS_UP_READ_ORPHAN_SEM(inode);
389
390                 rc = mds_unlink_orphan(obd, orphan, inode, pending_dir);
391                 CDEBUG(D_INODE, "%s: removed orphan %s: rc %d\n",
392                        obd->obd_name, fidname, rc);
393                 l_dput(orphan);
394                 GOTO(unlock, rc);
395         }
396 unlock_child:
397         MDS_UP_READ_ORPHAN_SEM(inode);
398 unlock:
399         UNLOCK_INODE_MUTEX(pending_dir);
400 out:
401         l_dput(result);
402         RETURN(0);
403 }